public void PushResourcePrioritizedCacheLoadPriority(RealStatePtr L, ResourcePrioritizedCache.LoadPriority val) { if (ResourcePrioritizedCacheLoadPriority_TypeID == -1) { bool is_first; ResourcePrioritizedCacheLoadPriority_TypeID = getTypeId(L, typeof(ResourcePrioritizedCache.LoadPriority), out is_first); if (ResourcePrioritizedCacheLoadPriority_EnumRef == -1) { Utils.LoadCSTable(L, typeof(ResourcePrioritizedCache.LoadPriority)); ResourcePrioritizedCacheLoadPriority_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, ResourcePrioritizedCacheLoadPriority_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, ResourcePrioritizedCacheLoadPriority_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for ResourcePrioritizedCache.LoadPriority ,value=" + val); } LuaAPI.lua_getref(L, ResourcePrioritizedCacheLoadPriority_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushDotEngineUIUILayerLevel(RealStatePtr L, DotEngine.UI.UILayerLevel val) { if (DotEngineUIUILayerLevel_TypeID == -1) { bool is_first; DotEngineUIUILayerLevel_TypeID = getTypeId(L, typeof(DotEngine.UI.UILayerLevel), out is_first); if (DotEngineUIUILayerLevel_EnumRef == -1) { Utils.LoadCSTable(L, typeof(DotEngine.UI.UILayerLevel)); DotEngineUIUILayerLevel_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, DotEngineUIUILayerLevel_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, DotEngineUIUILayerLevel_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for DotEngine.UI.UILayerLevel ,value=" + val); } LuaAPI.lua_getref(L, DotEngineUIUILayerLevel_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushTutorialDrivenClassTestEnumInner(RealStatePtr L, Tutorial.DrivenClass.TestEnumInner val) { if (TutorialDrivenClassTestEnumInner_TypeID == -1) { bool is_first; TutorialDrivenClassTestEnumInner_TypeID = getTypeId(L, typeof(Tutorial.DrivenClass.TestEnumInner), out is_first); if (TutorialDrivenClassTestEnumInner_EnumRef == -1) { Utils.LoadCSTable(L, typeof(Tutorial.DrivenClass.TestEnumInner)); TutorialDrivenClassTestEnumInner_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, TutorialDrivenClassTestEnumInner_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, TutorialDrivenClassTestEnumInner_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for Tutorial.DrivenClass.TestEnumInner ,value=" + val); } LuaAPI.lua_getref(L, TutorialDrivenClassTestEnumInner_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushXLuaTestMyEnum(RealStatePtr L, XLuaTest.MyEnum val) { if (XLuaTestMyEnum_TypeID == -1) { bool is_first; XLuaTestMyEnum_TypeID = getTypeId(L, typeof(XLuaTest.MyEnum), out is_first); if (XLuaTestMyEnum_EnumRef == -1) { Utils.LoadCSTable(L, typeof(XLuaTest.MyEnum)); XLuaTestMyEnum_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, XLuaTestMyEnum_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, XLuaTestMyEnum_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for XLuaTest.MyEnum ,value=" + val); } LuaAPI.lua_getref(L, XLuaTestMyEnum_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushVVMUIScriptXLuaXLuaCommandType(RealStatePtr L, VVMUI.Script.XLua.XLuaCommandType val) { if (VVMUIScriptXLuaXLuaCommandType_TypeID == -1) { bool is_first; VVMUIScriptXLuaXLuaCommandType_TypeID = getTypeId(L, typeof(VVMUI.Script.XLua.XLuaCommandType), out is_first); if (VVMUIScriptXLuaXLuaCommandType_EnumRef == -1) { Utils.LoadCSTable(L, typeof(VVMUI.Script.XLua.XLuaCommandType)); VVMUIScriptXLuaXLuaCommandType_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, VVMUIScriptXLuaXLuaCommandType_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, VVMUIScriptXLuaXLuaCommandType_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for VVMUI.Script.XLua.XLuaCommandType ,value=" + val); } LuaAPI.lua_getref(L, VVMUIScriptXLuaXLuaCommandType_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public ObjectTranslator(LuaEnv luaenv, RealStatePtr L) { assemblies = new List <Assembly>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { assemblies.Add(assembly); } this.luaEnv = luaenv; objectCasters = new ObjectCasters(this); objectCheckers = new ObjectCheckers(this); methodWrapsCache = new MethodWrapsCache(this, objectCheckers, objectCasters); metaFunctions = new StaticLuaCallbacks(); importTypeFunction = new LuaCSFunction(StaticLuaCallbacks.ImportType); loadAssemblyFunction = new LuaCSFunction(StaticLuaCallbacks.LoadAssembly); castFunction = new LuaCSFunction(StaticLuaCallbacks.Cast); LuaAPI.lua_newtable(L); LuaAPI.lua_newtable(L); LuaAPI.xlua_pushasciistring(L, "__mode"); LuaAPI.xlua_pushasciistring(L, "v"); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_setmetatable(L, -2); cacheRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); initCSharpCallLua(); }
public void OpenLib(RealStatePtr L) { if (0 != LuaAPI.xlua_getglobal(L, "xlua")) { throw new Exception("call xlua_getglobal fail!" + LuaAPI.lua_tostring(L, -1)); } LuaAPI.xlua_pushasciistring(L, "import_type"); LuaAPI.lua_pushstdcallcfunction(L, importTypeFunction); LuaAPI.lua_rawset(L, -3); LuaAPI.xlua_pushasciistring(L, "cast"); LuaAPI.lua_pushstdcallcfunction(L, castFunction); LuaAPI.lua_rawset(L, -3); LuaAPI.xlua_pushasciistring(L, "load_assembly"); LuaAPI.lua_pushstdcallcfunction(L, loadAssemblyFunction); LuaAPI.lua_rawset(L, -3); LuaAPI.xlua_pushasciistring(L, "access"); LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaAccess); LuaAPI.lua_rawset(L, -3); LuaAPI.xlua_pushasciistring(L, "private_accessible"); LuaAPI.lua_pushstdcallcfunction(L, StaticLuaCallbacks.XLuaPrivateAccessible); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pop(L, 1); LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex common_array_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_createtable(L, 1, 4); // 4 for __gc, __tostring, __index, __newindex common_delegate_meta = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); }
public void PushUnityEngineSystemLanguage(RealStatePtr L, UnityEngine.SystemLanguage val) { if (UnityEngineSystemLanguage_TypeID == -1) { bool is_first; UnityEngineSystemLanguage_TypeID = getTypeId(L, typeof(UnityEngine.SystemLanguage), out is_first); if (UnityEngineSystemLanguage_EnumRef == -1) { Utils.LoadCSTable(L, typeof(UnityEngine.SystemLanguage)); UnityEngineSystemLanguage_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, UnityEngineSystemLanguage_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, UnityEngineSystemLanguage_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for UnityEngine.SystemLanguage ,value=" + val); } LuaAPI.lua_getref(L, UnityEngineSystemLanguage_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushSystemNetSocketsProtocolType(RealStatePtr L, System.Net.Sockets.ProtocolType val) { if (SystemNetSocketsProtocolType_TypeID == -1) { bool is_first; SystemNetSocketsProtocolType_TypeID = getTypeId(L, typeof(System.Net.Sockets.ProtocolType), out is_first); if (SystemNetSocketsProtocolType_EnumRef == -1) { Utils.LoadCSTable(L, typeof(System.Net.Sockets.ProtocolType)); SystemNetSocketsProtocolType_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, SystemNetSocketsProtocolType_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, SystemNetSocketsProtocolType_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for System.Net.Sockets.ProtocolType ,value=" + val); } LuaAPI.lua_getref(L, SystemNetSocketsProtocolType_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public static void PushLogicModelEntityFlag(this ObjectTranslator t, RealStatePtr L, Logic.Model.EntityFlag val) { if (LogicModelEntityFlag_TypeID == -1) { bool is_first; LogicModelEntityFlag_TypeID = t.getTypeId(L, typeof(Logic.Model.EntityFlag), out is_first); if (LogicModelEntityFlag_EnumRef == -1) { Utils.LoadCSTable(L, typeof(Logic.Model.EntityFlag)); LogicModelEntityFlag_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, LogicModelEntityFlag_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, LogicModelEntityFlag_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for Logic.Model.EntityFlag ,value=" + val); } LuaAPI.lua_getref(L, LogicModelEntityFlag_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
public void PushMinecraftBlocksDataBlockVertexType(RealStatePtr L, Minecraft.BlocksData.BlockVertexType val) { if (MinecraftBlocksDataBlockVertexType_TypeID == -1) { bool is_first; MinecraftBlocksDataBlockVertexType_TypeID = getTypeId(L, typeof(Minecraft.BlocksData.BlockVertexType), out is_first); if (MinecraftBlocksDataBlockVertexType_EnumRef == -1) { Utils.LoadCSTable(L, typeof(Minecraft.BlocksData.BlockVertexType)); MinecraftBlocksDataBlockVertexType_EnumRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); } } if (LuaAPI.xlua_tryget_cachedud(L, (int)val, MinecraftBlocksDataBlockVertexType_EnumRef) == 1) { return; } IntPtr buff = LuaAPI.xlua_pushstruct(L, 4, MinecraftBlocksDataBlockVertexType_TypeID); if (!CopyByValue.Pack(buff, 0, (int)val)) { throw new Exception("pack fail fail for Minecraft.BlocksData.BlockVertexType ,value=" + val); } LuaAPI.lua_getref(L, MinecraftBlocksDataBlockVertexType_EnumRef); LuaAPI.lua_pushvalue(L, -2); LuaAPI.xlua_rawseti(L, -2, (int)val); LuaAPI.lua_pop(L, 1); }
int getTypeId(RealStatePtr L, Type type, out bool is_first, LOGLEVEL log_level = LOGLEVEL.WARN) { int type_id; is_first = false; if (!typeIdMap.TryGetValue(type, out type_id)) // no reference { if (type.IsArray) { if (common_array_meta == -1) throw new Exception("Fatal Exception! Array Metatable not inited!"); return common_array_meta; } if (typeof(MulticastDelegate).IsAssignableFrom(type)) { if (common_delegate_meta == -1) throw new Exception("Fatal Exception! Delegate Metatable not inited!"); return common_delegate_meta; } is_first = true; Type alias_type = null; aliasCfg.TryGetValue(type, out alias_type); LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName); if (LuaAPI.lua_isnil(L, -1)) //no meta yet, try to use reflection meta { LuaAPI.lua_pop(L, 1); if (TryDelayWrapLoader(L, alias_type == null ? type : alias_type)) { LuaAPI.luaL_getmetatable(L, alias_type == null ? type.FullName : alias_type.FullName); } else { throw new Exception("Fatal: can not load metatable of type:" + type); } } //循环依赖,自身依赖自己的class,比如有个自身类型的静态readonly对象。 if (typeIdMap.TryGetValue(type, out type_id)) { LuaAPI.lua_pop(L, 1); } else { LuaAPI.lua_pushvalue(L, -1); type_id = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnumber(L, type_id); LuaAPI.xlua_rawseti(L, -2, 1); LuaAPI.lua_pop(L, 1); if (type.IsValueType) { typeMap.Add(type_id, type); } typeIdMap.Add(type, type_id); } } return type_id; }
// 初始化缓存系统 static void BuildCacheSystem() { int orginTop = LuaAPI.lua_gettop(Utils.Lua.L); LuaAPI.lua_newtable(Utils.Lua.L); int reference = LuaAPI.luaL_ref(Utils.Lua.L); caches = new LuaTable(reference, Utils.Lua); LuaAPI.lua_settop(Utils.Lua.L, orginTop); }
private object getLuaFunction(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 LuaFunction) ? obj : null); } if (!LuaAPI.lua_isfunction(L, idx)) { return(null); } LuaAPI.lua_pushvalue(L, idx); return(new LuaFunction(LuaAPI.luaL_ref(L), translator.luaEnv)); }
internal void createFunctionMetatable(RealStatePtr L) { LuaAPI.lua_newtable(L); LuaAPI.xlua_pushasciistring(L,"__gc"); LuaAPI.lua_pushstdcallcfunction(L,metaFunctions.GcMeta); LuaAPI.lua_rawset(L,-3); LuaAPI.lua_pushlightuserdata(L, LuaAPI.xlua_tag()); LuaAPI.lua_pushnumber(L, 1); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_pushvalue(L, -1); int type_id = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnumber(L, type_id); LuaAPI.xlua_rawseti(L, -2, 1); LuaAPI.lua_pop(L, 1); typeIdMap.Add(typeof(LuaCSFunction), type_id); }
public object CreateInterfaceBridge(RealStatePtr L, Type interfaceType, int idx) { Func <int, LuaEnv, LuaBase> creator; if (!interfaceBridgeCreators.TryGetValue(interfaceType, out creator)) { #if UNITY_EDITOR var bridgeType = ce.EmitInterfaceImpl(interfaceType); creator = (int reference, LuaEnv luaenv) => { return(Activator.CreateInstance(bridgeType, new object[] { reference, luaEnv }) as LuaBase); }; interfaceBridgeCreators.Add(interfaceType, creator); #else throw new InvalidCastException("This interface must add to CSharpCallLua: " + interfaceType); #endif } LuaAPI.lua_pushvalue(L, idx); return(creator(LuaAPI.luaL_ref(L), luaEnv)); }
public ObjectTranslator(LuaEnv luaenv, RealStatePtr L) { #if XLUA_GENERAL var dumb_field = typeof(ObjectTranslator).GetField("s_gen_reg_dumb_obj", BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.NonPublic); if (dumb_field != null) { dumb_field.GetValue(null); } #endif assemblies = new List <Assembly>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { assemblies.Add(assembly); } this.luaEnv = luaenv; objectCasters = new ObjectCasters(this); objectCheckers = new ObjectCheckers(this); methodWrapsCache = new MethodWrapsCache(this, objectCheckers, objectCasters); metaFunctions = new StaticLuaCallbacks(); importTypeFunction = new LuaCSFunction(StaticLuaCallbacks.ImportType); loadAssemblyFunction = new LuaCSFunction(StaticLuaCallbacks.LoadAssembly); castFunction = new LuaCSFunction(StaticLuaCallbacks.Cast); cTypeFunction = new LuaCSFunction(StaticLuaCallbacks.Ctype); LuaAPI.lua_newtable(L); LuaAPI.lua_newtable(L); LuaAPI.xlua_pushasciistring(L, "__mode"); LuaAPI.xlua_pushasciistring(L, "v"); LuaAPI.lua_rawset(L, -3); LuaAPI.lua_setmetatable(L, -2); cacheRef = LuaAPI.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); initCSharpCallLua(); }
public Delegate CreateDelegateBridge(RealStatePtr L, Type delegateType, int idx) { LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_rawget(L, LuaIndexes.LUA_REGISTRYINDEX); if (!LuaAPI.lua_isnil(L, -1)) { int referenced = LuaAPI.xlua_tointeger(L, -1); LuaAPI.lua_pop(L, 1); if (delegate_bridges[referenced].IsAlive) { DelegateBridgeBase exist_bridge = delegate_bridges[referenced].Target as DelegateBridgeBase; Delegate exist_delegate; if (exist_bridge.TryGetDelegate(delegateType, out exist_delegate)) { return(exist_delegate); } else { exist_delegate = exist_bridge.GetDelegateByType(delegateType); exist_bridge.AddDelegate(delegateType, exist_delegate); return(exist_delegate); } } } else { LuaAPI.lua_pop(L, 1); } LuaAPI.lua_pushvalue(L, idx); int reference = LuaAPI.luaL_ref(L); LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_pushnumber(L, reference); LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX); DelegateBridgeBase bridge; try { #if UNITY_EDITOR if (!DelegateBridge.Gen_Flag) { bridge = Activator.CreateInstance(delegate_birdge_type, new object[] { reference, luaEnv }) as DelegateBridgeBase; } else #endif { bridge = new DelegateBridge(reference, luaEnv); } } catch (Exception e) { LuaAPI.lua_pushvalue(L, idx); LuaAPI.lua_pushnil(L); LuaAPI.lua_rawset(L, LuaIndexes.LUA_REGISTRYINDEX); LuaAPI.lua_pushnil(L); LuaAPI.xlua_rawseti(L, LuaIndexes.LUA_REGISTRYINDEX, reference); throw e; } try { var ret = bridge.GetDelegateByType(delegateType); bridge.AddDelegate(delegateType, ret); delegate_bridges[reference] = new WeakReference(bridge); return(ret); } catch (Exception e) { bridge.Dispose(); throw e; } }