public static void PushLuaObject(this IntPtr l, object val, Type type) { if (l != IntPtr.Zero) { if (type == null) { PushLuaObject(l, val); } else { if (val != null && val.GetType() == type) { PushLuaObject(l, val); } else { ILuaTypeHub sub = LuaTypeHub.GetTypeHub(type); if (sub != null) { sub.PushLua(l, val); } else { PushLuaRawObject(l, val); l.PushCommonMetaTable(); l.setmetatable(-2); } } } } }
public static int ClrFuncConvert(IntPtr l) { var stype = l.GetType(1); Type dtype; l.GetLua(2, out dtype); if (dtype == null) { return(ClrFuncWrap(l)); } else { ILuaTypeHub sub = LuaTypeHub.GetTypeHub(stype); ILuaConvert nsub = sub as ILuaConvert; if (nsub != null) { var meta = nsub.GetConverter(dtype); if (meta != null) { return(meta(l, 1)); } } } var val = l.GetLua(1); l.PushLuaObject(val.ConvertTypeEx(dtype), dtype); return(1); }
public static void PushLuaObject(this IntPtr l, object val) { if (l != IntPtr.Zero) { if (val == null) { l.checkstack(6); LuaObjCache.PushOrCreateObjCacheReg(l); // reg l.pushlightuserdata(IntPtr.Zero); // reg 0 l.gettable(-2); // reg ud if (!l.isnoneornil(-1)) { l.remove(-2); // ud return; } l.pop(1); // reg l.getmetatable(-1); // reg meta l.GetField(-1, LuaConst.LS_META_KEY_INDEX); // reg meta index l.pushlightuserdata(IntPtr.Zero); // reg meta index 0 LuaTypeHub.EmptyTypeHub.PushLua(l, null); // reg meta index 0 ud l.pushvalue(-1); // reg meta index 0 ud ud l.insert(-6); // ud reg meta index 0 ud l.settable(-3); // ud reg meta index l.pop(3); // ud } else { if (LuaObjCache.PushObjFromCache(l, val)) { return; } var type = val.GetType(); ILuaTypeHub sub = LuaTypeHub.GetTypeHub(type); if (sub != null) { var h = sub.PushLua(l, val); if (sub.ShouldCache && h != IntPtr.Zero) { LuaObjCache.RegObj(l, val, -1, h); } } else { PushLuaRawObject(l, val); l.PushCommonMetaTable(); l.setmetatable(-2); } } } }
public static void UpdateData(this IntPtr l, int index, object val) { if (val == null) { if (l.istable(index)) { Capstones.LuaLib.LuaCommonMeta.LuaTransCommon.Instance.SetData(l, index, val); } } else { var hub = LuaTypeHub.GetTypeHub(val.GetType()); hub.SetData(l, index, val); } }
public static int ClrFuncWrap(IntPtr l) { var type = l.GetType(1); ILuaTypeHub sub = LuaTypeHub.GetTypeHub(type); ILuaNative nsub = sub as ILuaNative; if (nsub == null) { l.PushLuaObject(l.GetLua(1)); return(1); } else { nsub.Wrap(l, 1); return(1); } }
public static void PushLuaType(this IntPtr l, Type t) { if (LuaObjCache.PushObjFromCache(l, t)) { return; } ILuaTypeHub hub = LuaTypeHub.GetTypeHub(t); if (hub != null) { PushLuaTypeRaw(l, hub); } else { PushLuaObject(l, t); } }
public static void PushLuaExplicit <T>(this IntPtr l, T val) { var type = typeof(T); ILuaTypeHub sub = LuaTypeHub.GetTypeHub(type); if (sub != null) { if (sub.ShouldCache) { if (sub.PushFromCache(l, val)) { return; } } var sub2 = sub as ILuaPush <T>; IntPtr h; if (sub2 != null) { h = sub2.PushLua(l, val); } else { #if ENABLE_PROFILER && ENABLE_PROFILER_LUA_DEEP && !DISABLE_PROFILER_LUA_GC_ALLOC using (var pcon = new Capstones.UnityFramework.ProfilerContext("box val of " + typeof(T).ToString())) #endif h = sub.PushLua(l, val); } if (sub.ShouldCache && h != IntPtr.Zero) { LuaObjCache.RegObj(l, val, -1, h); } } else { PushLuaRawObject(l, val); l.PushCommonMetaTable(); l.setmetatable(-2); } }
public static void GetLua <T>(this IntPtr l, int index, out T val) { if (l.istable(index)) { l.checkstack(2); l.pushvalue(index); // ud l.pushlightuserdata(LuaConst.LRKEY_TYPE_TRANS); // ud #trans l.gettable(-2); // ud trans ILuaTrans trans = null; if (l.isuserdata(-1)) { trans = l.GetLuaLightObject(-1) as ILuaTrans; } l.pop(2); if (trans != null) { var ttrans = trans as ILuaTrans <T>; if (ttrans != null) { val = ttrans.GetLua(l, index); return; } var mtrans = trans as ILuaTransMulti; if (mtrans != null) { val = mtrans.GetLua <T>(l, index); return; } var raw = trans.GetLua(l, index); if (raw is T) { val = (T)raw; } else { val = default(T); } return; } } { object func; if (typeof(T).IsValueType || typeof(T).IsSealed) { if (typeof(T).IsEnum()) { var ltype = l.type(index); if (ltype == lua.LUA_TNUMBER || ltype == lua.LUA_TSTRING) { var hub = LuaTypeHub.GetTypeHub(typeof(T)) as LuaTypeHub.TypeHubEnumPrecompiled <T>; if (hub != null) { val = hub.GetLuaChecked(l, index); return; } if (ltype == lua.LUA_TNUMBER) { var num = l.tonumber(index); val = (T)Enum.ToObject(typeof(T), (ulong)num); return; } else { var str = l.GetString(index); val = (T)Enum.Parse(typeof(T), str); return; } } } else { LuaPushNative._NativePushLuaFuncs.TryGetValue(typeof(T), out func); ILuaTrans <T> gfunc = func as ILuaTrans <T>; if (gfunc != null) { val = gfunc.GetLua(l, index); return; } } } var raw = GetLua(l, index); if (raw == null) { val = default(T); return; } else { if (raw is T) { val = (T)raw; return; } else if (raw is double) { if (IsNumeric(typeof(T))) { val = (T)Convert.ChangeType(raw, typeof(T)); return; } } else if (raw is LuaWrap.BaseLua) { if (typeof(T).IsSubclassOf(typeof(Delegate))) { val = (T)(object)CapsLuaDelegateGenerator.CreateDelegate(typeof(T), raw as LuaWrap.BaseLua); return; } } } val = default(T); return; } }
public static int ClrMetaIndex(IntPtr l) { // ... = tab key var oldtop = l.gettop(); switch (0) { default: if (oldtop < 2) { break; } if (!l.istable(1)) { break; } string key = l.GetString(2); if (key == null) { key = ""; } l.PushString("___path"); // ... "___path" l.rawget(1); // ... path string path = l.GetString(-1); if (path == null) { path = ""; } string full = path + key; l.pop(1); // ... Type ftype = null; List <Type> gtypes = new List <Type>(2); #if NETFX_CORE foreach (var asm in _SearchAssemblies) #else foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) #endif { if (ftype == null) { try { #if NETFX_CORE ftype = asm.GetType(full); #else ftype = asm.GetType(full, false); #endif } catch { } } for (int i = 0; i < _MaxGenericParamCount; ++i) { try { #if NETFX_CORE var gtype = asm.GetType(full + "`" + i.ToString()); #else var gtype = asm.GetType(full + "`" + i.ToString(), false); #endif if (gtype != null) { gtypes.Add(gtype); } } catch { } } } if (ftype == null && gtypes.Count <= 0) { l.newtable(); // ... ntab l.pushvalue(2); // ... ntab key l.pushvalue(-2); // ... ntab key ntab l.settable(1); // ... ntab l.PushString(full + "."); // ... ntab npath l.SetField(-2, "___path"); // ... ntab l.PushClrHierarchyMetatable(); // ... ntab meta l.setmetatable(-2); // ... ntab } else { var typehub = LuaTypeHub.GetTypeHub(ftype); foreach (var gtype in gtypes) { typehub.RegGenericTypeDefinition(gtype); } l.PushLuaType(typehub); // ... type if (l.IsExtended()) { LuaExtend.MakeLuaExtend(l, -1); } l.pushvalue(2); // ... type key l.pushvalue(-2); // ... type key type l.settable(1); // ... type } break; } return(l.gettop() - oldtop); }