public static void LoadCSTable(RealStatePtr L, Type type) { int oldTop = LuaAPI.lua_gettop(L); if (0 != LuaAPI.xlua_getglobal(L, "CS")) { throw new Exception("call xlua_getglobal fail!"); } List <string> path = getPathOfType(type); for (int i = 0; i < path.Count; ++i) { LuaAPI.xlua_pushasciistring(L, path[i]); if (0 != LuaAPI.xlua_pgettable(L, -2)) { LuaAPI.lua_settop(L, oldTop); LuaAPI.lua_pushnil(L); return; } if (!LuaAPI.lua_istable(L, -1) && i < path.Count - 1) { LuaAPI.lua_settop(L, oldTop); LuaAPI.lua_pushnil(L); return; } LuaAPI.lua_remove(L, -2); } }
private void AddSearcher(LuaCSFunction searcher, int index) { #if THREAD_SAFE || HOTFIX_ENABLE lock (luaEnvLock) { #endif var _L = L; //insert the loader LuaAPI.xlua_getloaders(_L); if (!LuaAPI.lua_istable(_L, -1)) { throw new Exception("Can not set searcher!"); } uint len = LuaAPI.xlua_objlen(_L, -1); index = index < 0 ? (int)(len + index + 2) : index; for (int e = (int)len + 1; e > index; e--) { LuaAPI.xlua_rawgeti(_L, -1, e - 1); LuaAPI.xlua_rawseti(_L, -2, e); } LuaAPI.lua_pushstdcallcfunction(_L, searcher); LuaAPI.xlua_rawseti(_L, -2, index); LuaAPI.lua_pop(_L, 1); #if THREAD_SAFE || HOTFIX_ENABLE } #endif }
public bool WriteCookie(string file_name, string table_list, string ext_path) { ByteBuffer write_file = new ByteBuffer(); // Debug.Log("WriteCookie file_name : "+file_name +" table_list :"+table_list+" ext_path:"+ext_path); string table_list_str = (table_list != "") ? table_list : DefineConstantsCookieHandler.COOKIE_COMMON; string parent_str; string[] r_table_list = table_list_str.Split('.'); // 写入空表 parent_str = DefineConstantsCookieHandler.COOKIE_VAR; // out_buff_len = (uint)sprintf(out_buff, "%s = %s or {}\n", parent_str.c_str(), parent_str.c_str()); string out_buff = string.Format("{0} = {1} or {{}}\n", parent_str, parent_str); write_file.WriteText(out_buff); LuaAPI.xlua_getglobal(m_lua_state, parent_str); int deep_size = 1; foreach (var value in r_table_list) { //string encryptValue = DESHelper.DESEncrypt(value); out_buff = string.Format("{0}[\"{1}\"]", parent_str, value); parent_str = out_buff; //out_buff_len = (uint)sprintf(out_buff, "%s = %s or {}\n", parent_str.c_str(), parent_str.c_str()); out_buff = string.Format("{0} = {1} or {{}}\n", parent_str, parent_str); write_file.WriteText(out_buff); if (!LuaAPI.lua_istable(m_lua_state, -1)) { LuaAPI.lua_pop(m_lua_state, deep_size); return(false); } LuaAPI.lua_pushstring(m_lua_state, value); LuaAPI.xlua_pgettable(m_lua_state, -2); deep_size++; } // 写入表中的数据 bool rlt = SerializeTableType(ref write_file, parent_str); LuaAPI.lua_pop(m_lua_state, 1); // Debug.Log("write rlt:"+rlt.ToString()); if (rlt) { SaveToFile(file_name, write_file, ext_path); return(true); } else { return(false); } }
internal static bool CheckCircleAndSetMeta(IntPtr L, ref int cycle_time) { int now_top = LuaAPI.lua_gettop(L); // 堆栈操作保护 int nIndex = LuaAPI.lua_gettop(L); // 取table索引值 if (!LuaAPI.lua_istable(L, nIndex)) { LuaAPI.lua_settop(L, now_top); Debug.Log("CheckCircleAndSetMeta Need Table!"); return(false); } LuaAPI.lua_pushnil(L); // nil入栈作为初始key while (0 != LuaAPI.lua_next(L, nIndex)) { // 现在栈顶(-1)是value,-2位置是对应的key // 这里可以判断key是什么并且对value进行各种处理 // key值只能是数字或者字符串 if (LuaAPI.lua_isnumber(L, -2) && LuaAPI.lua_isstring(L, -2)) { LuaAPI.lua_settop(L, now_top); Debug.Log("Need Number or String Here!"); return(false); } if (LuaAPI.lua_isnumber(L, -1) && LuaAPI.lua_isstring(L, -1) && !LuaAPI.lua_istable(L, -1) && !LuaAPI.lua_isboolean(L, -1)) { LuaAPI.lua_settop(L, now_top); Debug.Log("Table Contains Unsupported Value! Expected [Number, String, Table]."); return(false); } if (LuaAPI.lua_istable(L, -1)) { cycle_time += 1; if (cycle_time > 50) { Debug.Log("Table Has Cycle!"); LuaAPI.lua_settop(L, now_top); return(false); } if (!CheckCircleAndSetMeta(L, ref cycle_time) || !SetMetaTable(L, nIndex)) { LuaAPI.lua_settop(L, now_top); return(false); } } LuaAPI.lua_pop(L, 1); } LuaAPI.lua_settop(L, now_top); return(true); }
public static int Execute(RealStatePtr L) { if (!LuaAPI.lua_isfunction(L, 1)) { return(LuaAPI.luaL_error(L, "invalid compiled template, function needed!\r\n")); } if (LuaAPI.lua_istable(L, 2)) { LuaAPI.lua_setfenv(L, 1); } LuaAPI.lua_pcall(L, 0, 1, 0); return(1); }
private object getLuaTable(RealStatePtr L, int idx, object target) { if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA) { object obj = translator.SafeGetCSObj(L, idx); return((obj != null && obj is LuaTable) ? obj : null); } if (!LuaAPI.lua_istable(L, idx)) { return(null); } LuaAPI.lua_pushvalue(L, idx); return(new LuaTable(LuaAPI.luaL_ref(L), translator.luaEnv)); }
public static void SetCSTable(RealStatePtr L, Type type, int cls_table) { int oldTop = LuaAPI.lua_gettop(L); cls_table = abs_idx(oldTop, cls_table); if (0 != LuaAPI.xlua_getglobal(L, "CS")) { throw new Exception("call xlua_getglobal fail!"); } List <string> path = getPathOfType(type); for (int i = 0; i < path.Count - 1; ++i) { LuaAPI.xlua_pushasciistring(L, path[i]); if (0 != LuaAPI.xlua_pgettable(L, -2)) { LuaAPI.lua_settop(L, oldTop); throw new Exception("SetCSTable for [" + type + "] error: " + LuaAPI.lua_tostring(L, -1)); } if (LuaAPI.lua_isnil(L, -1)) { LuaAPI.lua_pop(L, 1); LuaAPI.lua_createtable(L, 0, 0); LuaAPI.xlua_pushasciistring(L, path[i]); LuaAPI.lua_pushvalue(L, -2); LuaAPI.lua_rawset(L, -4); } else if (!LuaAPI.lua_istable(L, -1)) { LuaAPI.lua_settop(L, oldTop); throw new Exception("SetCSTable for [" + type + "] error: ancestors is not a table!"); } LuaAPI.lua_remove(L, -2); } LuaAPI.xlua_pushasciistring(L, path[path.Count - 1]); LuaAPI.lua_pushvalue(L, cls_table); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); }
internal static int CheckNewType(IntPtr L) { if (!LuaAPI.lua_istable(L, 1)) { Debug.Log("Need Table Here!"); return(0); } // key值只能是数字或者字符串 if (LuaAPI.lua_isnumber(L, 2) && LuaAPI.lua_isstring(L, 2)) { Debug.Log("Need Number or String Here!"); return(0); } // value值只能是数字、字符串、非循环table if (LuaAPI.lua_isnumber(L, 3) && LuaAPI.lua_isstring(L, 3) && !LuaAPI.lua_istable(L, 3) && !LuaAPI.lua_isboolean(L, 3)) { Debug.Log("This Type Cannot Add to Cookies!Need[Table, Number, String]!"); return(0); } // 如果是table,检查是否有循环结构或者是其他类型 if (LuaAPI.lua_istable(L, 3)) { int cycle_time = 0; if (!CheckCircleAndSetMeta(L, ref cycle_time) || !SetMetaTable(L, 3)) { Debug.Log("This Type Cannot Add to Cookies!Need[Table, Number, String]!"); LuaAPI.lua_pop(L, 1); return(0); } } LuaAPI.lua_rawset(L, 1); return(0); }
private bool luaTableCheck(RealStatePtr L, int idx) { return(LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA && translator.SafeGetCSObj(L, idx) is LuaTable)); }
private ObjectCast genCaster(Type type) { ObjectCast fixTypeGetter = (RealStatePtr L, int idx, object target) => { if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA) { object obj = translator.SafeGetCSObj(L, idx); return((obj != null && type.IsAssignableFrom(obj.GetType())) ? obj : null); } return(null); }; if (typeof(Delegate).IsAssignableFrom(type)) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_isfunction(L, idx)) { return null; } return translator.CreateDelegateBridge(L, type, idx); }); } else if (type.IsInterface) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } return translator.CreateInterfaceBridge(L, type, idx); }); } else if (type.IsEnum) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } LuaTypes lua_type = LuaAPI.lua_type(L, idx); if (lua_type == LuaTypes.LUA_TSTRING) { return Enum.Parse(type, LuaAPI.lua_tostring(L, idx)); } else if (lua_type == LuaTypes.LUA_TNUMBER) { return Enum.ToObject(type, LuaAPI.xlua_tointeger(L, idx)); } throw new InvalidCastException("invalid value for enum " + type); }); } else if (type.IsArray) { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } uint len = LuaAPI.xlua_objlen(L, idx); int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index Type et = type.GetElementType(); ObjectCast elementCaster = GetCaster(et); Array ary = target == null?Array.CreateInstance(et, len) : target as Array; if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to Array"); } for (int i = 0; i < len; ++i) { LuaAPI.lua_pushnumber(L, i + 1); LuaAPI.lua_rawget(L, idx); if (et.IsPrimitive) { if (!StaticLuaCallbacks.TryPrimitiveArraySet(type, L, ary, i, n + 1)) { ary.SetValue(elementCaster(L, n + 1, null), i); } } else { if (StaticLuaCallbacks.GenTryArraySetPtr == null || !StaticLuaCallbacks.GenTryArraySetPtr(type, L, translator, ary, i, n + 1)) { ary.SetValue(elementCaster(L, n + 1, null), i); } } LuaAPI.lua_pop(L, 1); } return ary; }); } else if (typeof(IList).IsAssignableFrom(type) && type.IsGenericType) { ObjectCast elementCaster = GetCaster(type.GetGenericArguments()[0]); return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } obj = target == null?Activator.CreateInstance(type) : target; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index IList list = obj as IList; uint len = LuaAPI.xlua_objlen(L, n); if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to IList"); } for (int i = 0; i < len; ++i) { LuaAPI.lua_pushnumber(L, i + 1); LuaAPI.lua_rawget(L, idx); if (i < list.Count && target != null) { var item = elementCaster(L, n + 1, list[i]); if (item != null) { list[i] = item; } } else { var item = elementCaster(L, n + 1, null); if (item != null) { list.Add(item); } } LuaAPI.lua_pop(L, 1); } return obj; }); } else if (typeof(IDictionary).IsAssignableFrom(type) && type.IsGenericType) { ObjectCast keyCaster = GetCaster(type.GetGenericArguments()[0]); ObjectCast valueCaster = GetCaster(type.GetGenericArguments()[1]); return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } IDictionary dic = (target == null ? Activator.CreateInstance(type) : target) as IDictionary; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index LuaAPI.lua_pushnil(L); if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to IDictionary"); } while (LuaAPI.lua_next(L, idx) != 0) { object k = keyCaster(L, n + 1, null); // -2:key if (k != null) { object v = valueCaster(L, n + 2, !dic.Contains(k) ? null : dic[k]); if (v != null) { dic[k] = v; // -1:value } } LuaAPI.lua_pop(L, 1); // removes value, keeps key for next iteration } return dic; }); } else if ((type.IsClass && type.GetConstructor(System.Type.EmptyTypes) != null) || (type.IsValueType && !type.IsEnum)) //class has default construtor { return((RealStatePtr L, int idx, object target) => { object obj = fixTypeGetter(L, idx, target); if (obj != null) { return obj; } if (!LuaAPI.lua_istable(L, idx)) { return null; } obj = target == null?Activator.CreateInstance(type) : target; int n = LuaAPI.lua_gettop(L); idx = idx > 0 ? idx : LuaAPI.lua_gettop(L) + idx + 1;// abs of index if (!LuaAPI.lua_checkstack(L, 1)) { throw new Exception("stack overflow while cast to " + type); } /*foreach (PropertyInfo prop in type.GetProperties()) * { * LuaAPI.xlua_pushasciistring(L, prop.Name); * LuaAPI.lua_rawget(L, idx); * if (!LuaAPI.lua_isnil(L, -1)) * { * try * { * prop.SetValue(obj, GetCaster(prop.PropertyType)(L, n + 1, * target == null || prop.PropertyType.IsPrimitive || prop.PropertyType == typeof(string) ? null : prop.GetValue(obj, null)), null); * } * catch (Exception e) * { * throw new Exception("exception in tran " + prop.Name + ", msg=" + e.Message); * } * } * LuaAPI.lua_pop(L, 1); * }*/ foreach (FieldInfo field in type.GetFields()) { LuaAPI.xlua_pushasciistring(L, field.Name); LuaAPI.lua_rawget(L, idx); if (!LuaAPI.lua_isnil(L, -1)) { try { field.SetValue(obj, GetCaster(field.FieldType)(L, n + 1, target == null || field.FieldType.IsPrimitive || field.FieldType == typeof(string) ? null : field.GetValue(obj))); } catch (Exception e) { throw new Exception("exception in tran " + field.Name + ", msg=" + e.Message); } } LuaAPI.lua_pop(L, 1); } return obj; }); } else { return(fixTypeGetter); } }
private ObjectCheck genChecker(Type type) { ObjectCheck fixTypeCheck = (RealStatePtr L, int idx) => { if (LuaAPI.lua_type(L, idx) == LuaTypes.LUA_TUSERDATA) { object obj = translator.SafeGetCSObj(L, idx); if (obj != null) { return(type.IsAssignableFrom(obj.GetType())); } else { Type type_of_obj = translator.GetTypeOf(L, idx); if (type_of_obj != null) { return(type.IsAssignableFrom(type_of_obj)); } } } return(false); }; if (!type.IsAbstract && typeof(Delegate).IsAssignableFrom(type)) { return((RealStatePtr L, int idx) => { return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_isfunction(L, idx) || fixTypeCheck(L, idx); }); } else if (type.IsEnum) { return(fixTypeCheck); } else if (type.IsInterface) { return((RealStatePtr L, int idx) => { return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx); }); } else { if ((type.IsClass && type.GetConstructor(System.Type.EmptyTypes) != null)) //class has default construtor { return((RealStatePtr L, int idx) => { return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx); }); } else if (type.IsValueType) { return((RealStatePtr L, int idx) => { return LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx); }); } else if (type.IsArray) { return((RealStatePtr L, int idx) => { return LuaAPI.lua_isnil(L, idx) || LuaAPI.lua_istable(L, idx) || fixTypeCheck(L, idx); }); } else { return((RealStatePtr L, int idx) => { return LuaAPI.lua_isnil(L, idx) || fixTypeCheck(L, idx); }); } } }
protected bool SerializeTableType(ref ByteBuffer write_file, string parent_str) { int now_top = LuaAPI.lua_gettop(m_lua_state); // 堆栈操作保护 string field_str_buff = new string(new char[1024]); string write_buff = new string(new char[1024]); int nIndex = LuaAPI.lua_gettop(m_lua_state); // 取table索引值 if (!LuaAPI.lua_istable(m_lua_state, nIndex)) { LuaAPI.lua_settop(m_lua_state, now_top); Debug.Log("Need Table Here!"); return(false); } string my_name_str; LuaAPI.lua_pushnil(m_lua_state); // nil入栈作为初始key while (0 != LuaAPI.lua_next(m_lua_state, nIndex)) { // 现在栈顶(-1)是value,-2位置是对应的key // 这里可以判断key是什么并且对value进行各种处理 // 写key值 LuaAPI.lua_pushvalue(m_lua_state, -2); if (SerializeBasicType(ref field_str_buff) != 0) { write_buff = string.Format("{0}[{1}]", parent_str, field_str_buff); my_name_str = write_buff; } else { LuaAPI.lua_settop(m_lua_state, now_top); return(false); } LuaAPI.lua_pop(m_lua_state, 1); if (LuaAPI.lua_isnumber(m_lua_state, -1) || LuaAPI.lua_isstring(m_lua_state, -1) || LuaAPI.lua_isboolean(m_lua_state, -1)) { if (SerializeBasicType(ref field_str_buff) != 0) { write_buff = string.Format("{0} = {1}\n", my_name_str, field_str_buff); write_file.WriteText(write_buff); } else { LuaAPI.lua_settop(m_lua_state, now_top); return(false); } } else if (LuaAPI.lua_istable(m_lua_state, -1)) { write_buff = string.Format("{0} = {1} or {{}}\n", my_name_str, my_name_str); write_file.WriteText(write_buff); if (!SerializeTableType(ref write_file, my_name_str)) { LuaAPI.lua_settop(m_lua_state, now_top); return(false); } } else { Debug.Log("This Type Cannot Write to Cookies!Need[Table, Number, String]!"); } LuaAPI.lua_pop(m_lua_state, 1); // 弹出value,让key留在栈顶 } LuaAPI.lua_settop(m_lua_state, now_top); return(true); }