public void WriteCookieAll(string ext_path) { List <string> table_list = new List <string>(); LuaAPI.xlua_getglobal(m_lua_state, DefineConstantsCookieHandler.COOKIE_VAR); int nIndex = LuaAPI.lua_gettop(m_lua_state); LuaAPI.lua_pushnil(m_lua_state); // nil入栈作为初始key while (0 != LuaAPI.lua_next(m_lua_state, nIndex)) { LuaAPI.lua_pushvalue(m_lua_state, -2); if (LuaAPI.lua_isstring(m_lua_state, -1)) { table_list.Add(LuaAPI.lua_tostring(m_lua_state, -1)); } LuaAPI.lua_pop(m_lua_state, 2); } LuaAPI.lua_pop(m_lua_state, 1); List <string> .Enumerator itr = table_list.GetEnumerator(); while (itr.MoveNext()) { Debug.LogWarning("WriteCookieAll :" + itr.ToString()); WriteCookie(itr.ToString(), itr.ToString(), ext_path); } }
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); }
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); }
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); }