mirror of https://github.com/axmolengine/axmol.git
Merge pull request #13186 from jianglong0156/addEmotionTest
fix getStringUTFChars bug
This commit is contained in:
commit
8cdcade07a
|
@ -155,6 +155,17 @@ bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8)
|
|||
return llvm::convertUTF16ToUTF8String(utf16, outUtf8);
|
||||
}
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||
CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8)
|
||||
{
|
||||
const unsigned short * unicodeChar = ( const unsigned short *)env->GetStringChars(srcjStr, nullptr);
|
||||
const std::u16string unicodeStr((const char16_t *)unicodeChar);
|
||||
bool flag = UTF16ToUTF8(unicodeStr, outUtf8);
|
||||
env->ReleaseStringChars(srcjStr, unicodeChar);
|
||||
return flag;
|
||||
}
|
||||
#endif
|
||||
|
||||
std::vector<char16_t> getChar16VectorFromUTF16String(const std::u16string& utf16)
|
||||
{
|
||||
std::vector<char16_t> ret;
|
||||
|
|
|
@ -30,6 +30,10 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||
#include "platform/android/jni/JniHelper.h"
|
||||
#endif
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace StringUtils {
|
||||
|
@ -68,6 +72,10 @@ CC_DLL bool UTF8ToUTF16(const std::string& utf8, std::u16string& outUtf16);
|
|||
*/
|
||||
CC_DLL bool UTF16ToUTF8(const std::u16string& utf16, std::string& outUtf8);
|
||||
|
||||
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
||||
CC_DLL bool getUTFCharsFromJavaEnv(JNIEnv* env, jstring srcjStr, std::string& outUtf8);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Trims the unicode spaces at the end of char16_t vector.
|
||||
*/
|
||||
|
|
|
@ -35,6 +35,8 @@
|
|||
#include "platform/CCFileUtils.h"
|
||||
#include "platform/android/jni/JniHelper.h"
|
||||
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
namespace network {
|
||||
|
@ -579,17 +581,13 @@ private:
|
|||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const char* str = nullptr;
|
||||
char* ret = nullptr;
|
||||
str = env->GetStringUTFChars(jstr, nullptr);
|
||||
if (nullptr != str)
|
||||
char *ret = nullptr;
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue))
|
||||
{
|
||||
ret = strdup(str);
|
||||
strValue = "";
|
||||
}
|
||||
|
||||
env->ReleaseStringUTFChars(jstr, str);
|
||||
|
||||
ret = strdup(strValue.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -715,7 +713,10 @@ void HttpClient::processResponse(HttpResponse* response, char* responseMessage)
|
|||
}
|
||||
free(contentInfo);
|
||||
|
||||
strcpy(responseMessage, urlConnection.getResponseMessage());
|
||||
char *messageInfo = urlConnection.getResponseMessage();
|
||||
strcpy(responseMessage, messageInfo);
|
||||
free(messageInfo);
|
||||
|
||||
urlConnection.disconnect();
|
||||
|
||||
// write data to HttpResponse
|
||||
|
|
|
@ -29,6 +29,7 @@ THE SOFTWARE.
|
|||
#include "../CCApplication.h"
|
||||
#include "platform/CCFileUtils.h"
|
||||
#include <jni.h>
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
|
@ -59,10 +60,11 @@ std::string getStringWithEllipsisJni(const char* text, float width, float fontSi
|
|||
}
|
||||
|
||||
jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize);
|
||||
const char* str = t.env->GetStringUTFChars(retFromJava, 0);
|
||||
ret = str;
|
||||
|
||||
t.env->ReleaseStringUTFChars(retFromJava, str);
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(t.env, retFromJava, ret))
|
||||
{
|
||||
ret = "";
|
||||
}
|
||||
|
||||
t.env->DeleteLocalRef(stringArg1);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include "JniHelper.h"
|
||||
#include <jni.h>
|
||||
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
using namespace cocos2d;
|
||||
|
||||
extern "C" {
|
||||
|
@ -33,9 +35,13 @@ extern "C" {
|
|||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) {
|
||||
const char* pszText = env->GetStringUTFChars(text, NULL);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, text, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
const char* pszText = strValue.c_str();
|
||||
cocos2d::IMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText));
|
||||
env->ReleaseStringUTFChars(text, pszText);
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) {
|
||||
|
|
|
@ -27,6 +27,8 @@ THE SOFTWARE.
|
|||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
#define LOG_TAG "JniHelper"
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
|
||||
|
@ -265,14 +267,16 @@ namespace cocos2d {
|
|||
|
||||
JNIEnv *env = JniHelper::getEnv();
|
||||
if (!env) {
|
||||
return nullptr;
|
||||
return "";
|
||||
}
|
||||
|
||||
const char* chars = env->GetStringUTFChars(jstr, nullptr);
|
||||
std::string ret(chars);
|
||||
env->ReleaseStringUTFChars(jstr, chars);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, jstr, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
|
||||
return ret;
|
||||
return strValue;
|
||||
}
|
||||
|
||||
} //namespace cocos2d
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "spidermonkey_specifics.h"
|
||||
#include "ScriptingCore.h"
|
||||
#include "js_manual_conversions.h"
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
#define LOG_TAG "CCJavascriptJavaBridge"
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
||||
|
@ -38,10 +39,14 @@ extern "C" {
|
|||
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxJavascriptJavaBridge_evalString
|
||||
(JNIEnv *env, jclass cls, jstring value)
|
||||
{
|
||||
const char *_value = env->GetStringUTFChars(value, NULL);
|
||||
ScriptingCore::getInstance()->evalString(_value,NULL);
|
||||
env->ReleaseStringUTFChars(value, _value);
|
||||
|
||||
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue))
|
||||
{
|
||||
CCLOG("Cocos2dxJavascriptJavaBridge_evalString error, invalid string code");
|
||||
return 0;
|
||||
}
|
||||
ScriptingCore::getInstance()->evalString(strValue.c_str(), nullptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -79,9 +84,13 @@ bool JavascriptJavaBridge::CallInfo::execute(void)
|
|||
|
||||
case TypeString:
|
||||
m_retjstring = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID);
|
||||
const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0);
|
||||
m_ret.stringValue = new string(stringBuff);
|
||||
m_env->ReleaseStringUTFChars(m_retjstring, stringBuff);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
|
||||
m_ret.stringValue = new string(strValue);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -119,10 +128,13 @@ bool JavascriptJavaBridge::CallInfo::executeWithArgs(jvalue *args)
|
|||
|
||||
case TypeString:
|
||||
m_retjstring = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args);
|
||||
const char *stringBuff = m_env->GetStringUTFChars(m_retjstring, 0);
|
||||
m_ret.stringValue = new string(stringBuff);
|
||||
m_env->ReleaseStringUTFChars(m_retjstring, stringBuff);
|
||||
break;
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjstring, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
m_ret.stringValue = new string(strValue);
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_env->ExceptionCheck() == JNI_TRUE)
|
||||
|
@ -429,4 +441,4 @@ void JavascriptJavaBridge::_js_register(JSContext *cx, JS::HandleObject global)
|
|||
|
||||
JavascriptJavaBridge::js_parent = NULL;
|
||||
JavascriptJavaBridge::js_proto = JS_InitClass(cx, global, JS::NullPtr(), &JavascriptJavaBridge::js_class , JavascriptJavaBridge::_js_constructor, 0, props, funcs, NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "CCLuaJavaBridge.h"
|
||||
#include "platform/android/jni/JniHelper.h"
|
||||
#include <android/log.h>
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
#define LOG_TAG "luajc"
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
||||
|
@ -40,9 +41,12 @@ bool LuaJavaBridge::CallInfo::execute(void)
|
|||
|
||||
case TypeString:
|
||||
m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID);
|
||||
const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0);
|
||||
m_ret.stringValue = new string(stringBuff);
|
||||
m_env->ReleaseStringUTFChars(m_retjs, stringBuff);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
m_ret.stringValue = new string(strValue);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -80,9 +84,12 @@ bool LuaJavaBridge::CallInfo::executeWithArgs(jvalue *args)
|
|||
|
||||
case TypeString:
|
||||
m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args);
|
||||
const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0);
|
||||
m_ret.stringValue = new string(stringBuff);
|
||||
m_env->ReleaseStringUTFChars(m_retjs, stringBuff);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(m_env, m_retjs, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
m_ret.stringValue = new string(strValue);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
#include "CCLuaJavaBridge.h"
|
||||
|
||||
#include "base/ccUTF8.h"
|
||||
|
||||
#define LOG_TAG "Cocos2dxLuaJavaBridge_java"
|
||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
||||
|
||||
|
@ -13,20 +15,23 @@ extern "C" {
|
|||
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString
|
||||
(JNIEnv *env, jclass cls, jint functionId, jstring value)
|
||||
{
|
||||
const char *value_ = env->GetStringUTFChars(value, 0);
|
||||
int ret = LuaJavaBridge::callLuaFunctionById(functionId, value_);
|
||||
env->ReleaseStringUTFChars(value, value_);
|
||||
std::string strValue = "";
|
||||
if (!cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, strValue))
|
||||
{
|
||||
strValue = "";
|
||||
}
|
||||
int ret = LuaJavaBridge::callLuaFunctionById(functionId, strValue.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString
|
||||
(JNIEnv *env, jclass cls, jstring luaFunctionName, jstring value)
|
||||
{
|
||||
const char *luaFunctionName_ = env->GetStringUTFChars(luaFunctionName, 0);
|
||||
const char *value_ = env->GetStringUTFChars(value, 0);
|
||||
int ret = LuaJavaBridge::callLuaGlobalFunction(luaFunctionName_, value_);
|
||||
env->ReleaseStringUTFChars(luaFunctionName, luaFunctionName_);
|
||||
env->ReleaseStringUTFChars(value, value_);
|
||||
std::string functionNameStr = "";
|
||||
std::string valueStr = "";
|
||||
cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, luaFunctionName, functionNameStr);
|
||||
cocos2d::StringUtils::getUTFCharsFromJavaEnv(env, value, valueStr);
|
||||
int ret = LuaJavaBridge::callLuaGlobalFunction(functionNameStr.c_str(), valueStr.c_str());
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -108,6 +108,13 @@ public class AppActivity extends Cocos2dxActivity {
|
|||
|
||||
public static String getUtfStr() {
|
||||
final String utf8Str = "you will see emotion:💝";
|
||||
app.runOnGLThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
Cocos2dxJavascriptJavaBridge.evalString("cc.log(\"" + utf8Str + "\")");
|
||||
}
|
||||
});
|
||||
return utf8Str;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,41 +28,45 @@
|
|||
var ReflectionTestLayer = BaseTestLayer.extend({
|
||||
_title:"jsb.reflection",
|
||||
_subtitle:"call java/objective-c methods in js",
|
||||
|
||||
onRestartCallback:function (sender) {
|
||||
new ReflectionTestScene().runThisTest();
|
||||
},
|
||||
ctor:function() {
|
||||
this._super(cc.color(0,0,0,255), cc.color(98,99,117,255));
|
||||
|
||||
var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 35);
|
||||
var resultLabel = new cc.LabelTTF("Show emotion result", "Arial", 20);
|
||||
resultLabel.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height - 130));
|
||||
this.addChild(resultLabel);
|
||||
|
||||
var label = new cc.LabelTTF("Show Alert Dialog", "Arial", 30);
|
||||
var menuItem = new cc.MenuItemLabel(label, function(){
|
||||
if(cc.sys.os == cc.sys.OS_ANDROID){
|
||||
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;)V", "How are you ?", "I'm great !");
|
||||
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
|
||||
var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","cocos2d-js","Yes! you call a Native UI from Reflection");
|
||||
cc.log("ret val is "+ret);
|
||||
resultLabel.setString("ret val is "+ret);
|
||||
}
|
||||
}, this);
|
||||
|
||||
var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 35);
|
||||
var label2 = new cc.LabelTTF("CallReflectionWithBool", "Arial", 30);
|
||||
var menuItem2 = new cc.MenuItemLabel(label2, function(){
|
||||
if(cc.sys.os == cc.sys.OS_ANDROID){
|
||||
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog", "(Ljava/lang/String;Ljava/lang/String;Z)V", "How are you ?", "I'm great !", true);
|
||||
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
|
||||
var ret = jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:addBool:","cocos2d-js","Yes! you call a Native UI from Reflection", true);
|
||||
cc.log("ret val is "+ret);
|
||||
resultLabel.setString("ret val is "+ret);
|
||||
}
|
||||
}, this);
|
||||
|
||||
var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 35);
|
||||
var label3 = new cc.LabelTTF("CallReflectionWithUTF8Str", "Arial", 30);
|
||||
var menuItem3 = new cc.MenuItemLabel(label3, function(){
|
||||
if(cc.sys.os == cc.sys.OS_ANDROID){
|
||||
var ret = jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "getUtfStr", "()Ljava/lang/String;");
|
||||
jsb.reflection.callStaticMethod("org/cocos2dx/js_tests/AppActivity", "showAlertDialog",
|
||||
"(Ljava/lang/String;Ljava/lang/String;)V", "Show Emotion", ret);
|
||||
cc.log("result:" + ret);
|
||||
resultLabel.setString("result:" + ret);
|
||||
}else if(cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX){
|
||||
var ret = "you will see emotion:💝";
|
||||
jsb.reflection.callStaticMethod("NativeOcClass","callNativeUIWithTitle:andContent:","Show Emotion", ret);
|
||||
cc.log("result:" + ret);
|
||||
resultLabel.setString("result:" + ret);
|
||||
}
|
||||
}, this);
|
||||
var menu = new cc.Menu(menuItem, menuItem2, menuItem3);
|
||||
|
|
Loading…
Reference in New Issue