public void registerObject() {
try {
AndroidLua androidLua = new AndroidLua();
luaState.pushObjectValue("androidLua");
luaState.setGlobal(name);
} catch (LuaException e) {
e.printStackTrace();
}
}
* lua 传递数据给 android
* @param luaObject
*/
public void getDataFromLua(LuaObject luaObject){
LuaState luaState=luaObject.getLuaState();
Map<String, String> stringStringMap = (Map<String, String>) LuaUtil.parseLuaTable(luaState);
for (Map.Entry<String, String> entry :stringStringMap.entrySet()){
Log.d(TAG,entry.getKey());
Log.d(TAG,entry.getValue());
}
}
public static Object parseLuaTable(LuaState luaState){
Object luaData=null;
int top = luaState.getTop();
if (luaState.isTable(top)) {
luaState.pushNil();
while (luaState.next(top) != 0) {
int keyType = luaState.type(-2);
String keyName=luaState.typeName(keyType);
int valueType = luaState.type(-1);
String valueName = luaState.typeName(valueType);
if (keyName.equals("number")) {
if(luaData==null){
luaData=new ArrayList<>();
}
if(luaData instanceof List){
parseTableValueList(luaState, valueName, (List<Object>) luaData);
}else {
parseTableValueMap(luaState, valueName, (Map<String, Object>) luaData);
}
}
else if (keyName.equals("string")) {
if(luaData==null){
luaData = new HashMap<>();
}
if(luaData instanceof Map){
parseTableValueMap(luaState, valueName, (Map<String, Object>) luaData);
}else {
parseTableValueList(luaState, valueName, (List<Object>) luaData);
}
}
if(!"function".equals(valueName)){
luaState.pop(1);
}
}
}
return luaData;
}
private static void parseTableValueMap(LuaState mLuaState, String valueName, Map<String, Object> objectMap) {
switch (valueName) {
case "null":
break;
case "number":
objectMap.put(mLuaState.toString(-2), mLuaState.toNumber(-1));
break;
case "string":
objectMap.put(mLuaState.toString(-2), mLuaState.toString(-1));
break;
case "boolean":
objectMap.put(mLuaState.toString(-2), mLuaState.toBoolean(-1));
break;
case "int":
case "integer":
objectMap.put(mLuaState.toString(-2), mLuaState.toInteger(-1));
break;
case "function" :
String key=mLuaState.toString(-2);
int value=mLuaState.Lref(LuaState.LUA_REGISTRYINDEX);
objectMap.put(key,value);
break;
case "table":
objectMap.put(mLuaState.toString(-2), parseLuaTable(mLuaState));
break;
default:
break;
}
}
private static void parseTableValueList(LuaState mLuaState, String valueName, List<Object> objectMap) {
switch (valueName) {
case "null":
break;
case "number":
objectMap.add( mLuaState.toNumber(-1));
break;
case "string":
objectMap.add(mLuaState.toString(-1));
break;
case "boolean":
objectMap.add( mLuaState.toBoolean(-1));
break;
case "int":
case "integer":
objectMap.add(mLuaState.toInteger(-1));
break;
case "function" :
int value=mLuaState.Lref(LuaState.LUA_REGISTRYINDEX);
objectMap.add(value);
break;
case "table":
objectMap.add(parseLuaTable(mLuaState)) ;
break;
default:
break;
}
}
private void getDataFromLua() {
luaState.getGlobal("pushDataToAndroid");
luaState.call(0, 0);
}