private lua_pushvalue ( |
||
luaState | ||
index | int | |
return | void |
public static void RefString(IntPtr strPoint, int index, string s, IntPtr L) { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_pushvalue(L, index); //把字符串ref了之后就不GC了 LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_settop(L, oldTop); stringDict[(long)strPoint] = s; }
//必须检测类型 public static object[] CheckObjectArray(IntPtr L, int stackPos) { LuaTypes luatype = LuaDLL.lua_type(L, stackPos); if (luatype == LuaTypes.LUA_TTABLE) { int index = 1; object val = null; List <object> list = new List <object>(); LuaDLL.lua_pushvalue(L, stackPos); while (true) { LuaDLL.lua_rawgeti(L, -1, index); luatype = LuaDLL.lua_type(L, -1); if (luatype == LuaTypes.LUA_TNIL) { LuaDLL.lua_pop(L, 1); return(list.ToArray());; } val = ToVarObject(L, -1); list.Add(val); LuaDLL.lua_pop(L, 1); ++index; } } else if (luatype == LuaTypes.LUA_TUSERDATA) { return((object[])CheckObject(L, stackPos, typeof(object[]))); } else if (luatype == LuaTypes.LUA_TNIL) { return(null); } LuaDLL.luaL_typerror(L, stackPos, "object[] or table"); return(null); }
public static void Register(IntPtr L) { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "Resources"); if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "Resources"); LuaDLL.lua_getglobal(L, "Resources"); } LuaDLL.lua_pushstdcallcfunction(L, LuaResources.New, "New"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.FindObjectsOfTypeAll, "FindObjectsOfTypeAll"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.Load, "Load"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.LoadAsync, "LoadAsync"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.LoadAll, "LoadAll"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.GetBuiltinResource, "GetBuiltinResource"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.UnloadAsset, "UnloadAsset"); LuaDLL.lua_pushstdcallcfunction(L, LuaResources.UnloadUnusedAssets, "UnloadUnusedAssets"); LuaDLL.lua_getglobal(L, "readIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__index"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getglobal(L, "writeIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__newindex"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_pushstdcallcfunction(L, new LuaCSFunction(LuaStatic.GameObjectGC)); LuaDLL.lua_setfield(L, -2, "__gc"); LuaDLL.lua_settop(L, oldTop); LuaStatic.AddTypeDict(typeof(UnityEngine.Resources)); }
public static object ToVarTable(IntPtr L, int stackPos) { stackPos = LuaDLL.abs_index(L, stackPos); LuaValueType ret = LuaDLL.tolua_getvaluetype(L, stackPos); switch (ret) { case LuaValueType.Vector3: return(ToVector3(L, stackPos)); case LuaValueType.Quaternion: return(ToQuaternion(L, stackPos)); case LuaValueType.Vector4: return(ToVector4(L, stackPos)); case LuaValueType.Color: return(ToColor(L, stackPos)); case LuaValueType.Ray: return(ToRay(L, stackPos)); case LuaValueType.Bounds: return(ToBounds(L, stackPos)); case LuaValueType.Vector2: return(ToVector2(L, stackPos)); case LuaValueType.LayerMask: return(ToLayerMask(L, stackPos)); default: LuaState state = LuaState.Get(L); LuaDLL.lua_pushvalue(L, stackPos); int reference = LuaDLL.toluaL_ref(L); return(state.GetTable(reference)); } }
public Lua() { luaState = LuaDLL.luaL_newstate(); translator = new ObjectTranslator(this, luaState); LuaDLL.luaL_openlibs(luaState); LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.luaL_dostring(luaState, Lua.init_luanet); panicCallback = new LuaCSFunction(PanicCallback); LuaDLL.lua_atpanic(luaState, panicCallback); }
public static int errorFunc_traceback(IntPtr L) { if (!LuaDLL.lua_isstring(L, 1)) /* 'message' not a string? */ { return(1); /* keep it intact */ } LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "debug"); if (LuaDLL.lua_istable(L, -1)) { LuaDLL.lua_pop(L, 1); return(1); } LuaDLL.lua_getfield(L, -1, "traceback"); if (LuaDLL.lua_isfunction(L, -1)) { LuaDLL.lua_pop(L, 2); return(1); } LuaDLL.lua_pushvalue(L, 1); /* pass error message */ LuaDLL.lua_pushinteger(L, 2); /* skip this function and traceback */ LuaDLL.lua_call(L, 2, 1); /* call debug.traceback */ return(1); }
public static int loader(IntPtr L) { try { int top = LuaDLL.lua_gettop(L); string fileName = String.Empty; fileName = LuaDLL.lua_tostring(L, 1); fileName = fileName.Replace('.', '/'); fileName += ".lua"; string fullPath = Path.Combine(luaPath, fileName); if (File.Exists(fullPath)) { byte[] fileData = StreamingAssetsHelper.ReadAllBytes(fullPath); if (LuaDLL.luaL_loadbuffer(L, fileData, fileData.Length, "@" + fileName) != LuaDLL.LUA_OK) { UnityEngine.Debug.LogWarning("lua2:" + LuaDLL.lua_tostring(L, -1)); throw new LuaException(L, LuaDLL.lua_tostring(L, -1)); } LuaDLL.lua_pushvalue(L, 1); if (!LuaExtend.PCall(L, 1, LuaDLL.LUA_MULTRET)) { throw new LuaException(L, "require failed:" + fileName); //avoid redundancy error msg log, because PCall has log error msg } return(LuaDLL.lua_gettop(L) - top); } else { throw new LuaException(L, "file is not found:" + fileName); } } catch (Exception e) { return(LuaDLL.wluaL_error(L, e)); } }
public static int print(IntPtr L) { // For each argument we'll 'tostring' it int n = LuaDLL.lua_gettop(L); string s = String.Empty; //LuaDLL.lua_getglobal(L, "debug"); //LuaDLL.lua_getfield(L, -1, "traceback"); //LuaDLL.lua_pushvalue(L, 1); //LuaDLL.lua_pushnumber(L, 2); //LuaDLL.lua_call(L, 2, 1); //n = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "tostring"); for (int i = 1; i <= n; i++) { LuaDLL.lua_pushvalue(L, -1); /* function to be called */ LuaDLL.lua_pushvalue(L, i); /* value to print */ LuaDLL.lua_call(L, 1, 1); if (i > 1) { s += "\t"; } s += LuaDLL.lua_tostring(L, -1); LuaDLL.lua_pop(L, 1); /* pop result */ //LuaDLL.PrintCmd(s); } Debug.Log("LUA: " + s); return(0); }
protected int Resume(IntPtr L, int nArgs) { int ret = LuaDLL.lua_resume(L, nArgs); if (ret > (int)LuaThreadStatus.LUA_YIELD) { string error = null; int top = LuaDLL.lua_gettop(L); LuaDLL.tolua_pushtraceback(L); LuaDLL.lua_pushthread(L); LuaDLL.lua_pushvalue(L, top); if (LuaDLL.lua_pcall(L, 2, -1, 0) != 0) { LuaDLL.lua_settop(L, top); } error = LuaDLL.lua_tostring(L, -1); luaState.LuaSetTop(0); throw new LuaException(error); } return(ret); }
public Lua() { luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) //LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(luaState, true); LuaDLL.lua_settable(luaState, LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, "luanet"); LuaDLL.lua_pushvalue(luaState, LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(luaState, "luanet"); LuaDLL.lua_pushstring(luaState, "getmetatable"); LuaDLL.lua_getglobal(luaState, "getmetatable"); LuaDLL.lua_settable(luaState, -3); LuaDLL.lua_replace(luaState, LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, luaState); LuaDLL.lua_replace(luaState, LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaFunctionCallback(PanicCallback); LuaDLL.lua_atpanic(luaState, panicCallback); }
//需要额外推一份函数值到栈上。如:..., f, f, param1, param2 => ..., f, result1, result2 //如果不出错,则保留返回值在堆栈上,返回 true //如果出错,压入含函数调试信息的调用堆栈信息,返回 false public bool PCallWithFunctionInfo(int nArgs, int nResults) { bool bRet = PCall(nArgs, nResults); if (bRet) { //-> f, results return(true); } else { //-> f, err string errorInfo = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_pop(L, 1); //-> f LuaDLL.lua_pushvalue(L, -1); //-> f, f //输出 callback 信息 string callbackInfo = GetFunctionInfo(); //->f errorInfo = HobaText.Format("{0}\ncallback: {1}", errorInfo, callbackInfo); LuaDLL.lua_pushstring(L, errorInfo); //-> f, err return(false); } }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(IntPtr luaState, object o, int index, string metatable) { CreateMetaTable(luaState, o, metatable); // Stores the object index in the Lua list and pushes the index into the Lua stack //if (!o.GetType().IsValueType) //{ LuaDLL.luaL_getmetatable(luaState, "luaNet_objects"); LuaDLL.luanet_newudata(luaState, index); LuaDLL.lua_pushvalue(luaState, -3); LuaDLL.lua_remove(luaState, -4); LuaDLL.lua_setmetatable(luaState, -2); LuaDLL.lua_pushvalue(luaState, -1); LuaDLL.lua_rawseti(luaState, -3, index); LuaDLL.lua_remove(luaState, -2); //} //else //{ // LuaDLL.luanet_newudata(luaState, index); // LuaDLL.lua_pushvalue(luaState, -2); // LuaDLL.lua_remove(luaState, -3); // LuaDLL.lua_setmetatable(luaState, -2); //} }
public LuaState() { // Create State L = LuaDLL.luaL_newstate(); // Create LuaInterface library LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushstring(L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(L, true); LuaDLL.lua_settable(L, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "luanet"); LuaDLL.lua_pushvalue(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鍘嬪叆浜哶G琛? LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawget"); LuaDLL.lua_getglobal(L, "rawget"); LuaDLL.lua_settable(L, -3); LuaDLL.lua_pushstring(L, "rawset"); LuaDLL.lua_getglobal(L, "rawset"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鐢╨uanet鏇挎崲_G琛? translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); //鎭㈠_G琛? translator.PushTranslator(L); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "lprint"); warnFunction = new LuaCSFunction(LuaStatic.warn); LuaDLL.lua_pushstdcallcfunction(L, warnFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "lwarn"); breakFunction = new LuaCSFunction(LuaStatic.breakFunc); LuaDLL.lua_pushstdcallcfunction(L, breakFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "breakpoint"); loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); pcallFunction = new LuaCSFunction(LuaStatic.pcall); LuaDLL.lua_pushstdcallcfunction(L, pcallFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "pcall"); LuaDLL.lua_pushstdcallcfunction(L, LuaStatic.errorFunc_traceback); errorFuncRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX); // Insert our loader FIRST loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(L, loaderFunction); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.luaL_getn(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); DoString(LuaStatic.init_luanet); tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; // Silently convert nullable types to their non null requics } long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(extractValues[runtimeHandleValue]); } //CP: Added support for generic parameters if (paramType.IsGenericParameter) { if (luatype == LuaTypes.LUA_TBOOLEAN) { return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TSTRING) { return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TNUMBER) { return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]); } //else // suppress CS0642 ; //an unsupported type was encountered } if (paramType.IsValueType && luatype == LuaTypes.LUA_TTABLE) { int oldTop = LuaDLL.lua_gettop(luaState); ExtractValue ret = null; LuaDLL.lua_pushvalue(luaState, stackPos); LuaDLL.lua_pushstring(luaState, "class"); LuaDLL.lua_gettable(luaState, -2); if (!LuaDLL.lua_isnil(luaState, -1)) { string cls = LuaDLL.lua_tostring(luaState, -1); if (cls == "Vector3" && paramType == typeof(Vector3)) { ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()]; } else if (cls == "Vector2" && paramType == typeof(Vector2)) { ret = extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()]; } else if (cls == "Quaternion" && paramType == typeof(Quaternion)) { ret = extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()]; } else if (cls == "Color" && paramType == typeof(Color)) { ret = extractValues[typeof(Color).TypeHandle.Value.ToInt64()]; } else if (cls == "Vector4" && paramType == typeof(Vector4)) { ret = extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()]; } else if (cls == "Ray" && paramType == typeof(Ray)) { ret = extractValues[typeof(Ray).TypeHandle.Value.ToInt64()]; } else { ret = null; } } LuaDLL.lua_settop(luaState, oldTop); if (ret != null) { return(ret); } } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } else if (luatype == LuaTypes.LUA_TNIL) { return(extractNetObject); // kevinh - silently convert nil to a null string pointer } } else if (paramType == typeof(LuaTable)) { if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaFunction)) { if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[runtimeHandleValue]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) { #if __NOGEN__ translator.throwError(luaState, "Delegates not implemnented"); #else return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated)); #endif } else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) { #if __NOGEN__ translator.throwError(luaState, "Interfaces not implemnented"); #else return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated)); #endif } else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) { // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found return(extractNetObject); } else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaTypes.LUA_TNIL != LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { object obj = translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } } else { //object obj = translator.getNetObject(luaState, stackPos); //topameng 修改这里使支持注册到c#的lua类 object obj = translator.getRawNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }
public static int searcher(IntPtr L) { LuaDLL.lua_pushvalue(L, LuaDLL.lua_upvalueindex(1)); LuaDLL.lua_pushvalue(L, 1); return(2); }
//L: [namespace Table] public bool AddObject(IntPtr L, System.Object obj, string metatable /* nullable */) { IntPtr userdata = IntPtr.Zero; if (luaObjs.TryGetValue(obj, out userdata)) { LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,reftable,userdataKey LuaDLL.lua_rawget(L, -2); //namespace,reftable,userdata LuaDLL.lua_remove(L, -2); //namespace,userdata if (LuaDLL.lua_isuserdata(L, -1)) { return(true); } else { LuaDLL.lua_pop(L, 1); //namespace } } userdata = LuaDLL.lua_newuserdata(L, 1); //namespace,obj if (metatable == null) { Type type = obj.GetType(); while (type != null) { LuaDLL.wlua_getfield(L, -2, type.Name); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); //namespace,obj type = type.BaseType; continue; } if (LuaDLL.lua_istable(L, -1)) { metatable = type.Name; break; } else { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "metatable must be a table:" + type.Name); } } } else { LuaDLL.wlua_getfield(L, -2, metatable); //namespace,obj,typet if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 2); //namespace throw new LuaException(L, "failed to find metatable:" + metatable); } } //namespace,obj,typet LuaDLL.lua_setmetatable(L, -2); //namespace,obj objs[userdata.ToInt64()] = obj; luaObjs[obj] = userdata; LuaDLL.lua_rawgeti(L, LuaDLL.LUA_REGISTRYINDEX, weakRefForUserData); //namespace,obj,reftable LuaDLL.lua_pushlightuserdata(L, userdata); //namespace,obj,reftable,userdatakey LuaDLL.lua_pushvalue(L, -3); //namespace,obj,reftable,userdatakey,obj LuaDLL.lua_rawset(L, -3); //namespace,obj,reftable LuaDLL.lua_pop(L, 1); //namespace,obj return(true); }
public void LuaPushValue(int idx) { LuaDLL.lua_pushvalue(L, idx); }
public static void Register(IntPtr L) { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "GameObject"); if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "GameObject"); LuaDLL.lua_getglobal(L, "GameObject"); } LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.New, "New"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.CreatePrimitive, "CreatePrimitive"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponent, "GetComponent"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponentInChildren, "GetComponentInChildren"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponentInParent, "GetComponentInParent"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponents, "GetComponents"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponentsInChildren, "GetComponentsInChildren"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.GetComponentsInParent, "GetComponentsInParent"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.SetActive, "SetActive"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.CompareTag, "CompareTag"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.FindGameObjectWithTag, "FindGameObjectWithTag"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.FindWithTag, "FindWithTag"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.FindGameObjectsWithTag, "FindGameObjectsWithTag"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.SendMessageUpwards, "SendMessageUpwards"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.SendMessage, "SendMessage"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.BroadcastMessage, "BroadcastMessage"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.AddComponent, "AddComponent"); LuaDLL.lua_pushstdcallcfunction(L, LuaGameObject.Find, "Find"); LuaDLL.lua_pushcsharpproperty(L, "transform", LuaGameObject.get_transform, null); LuaDLL.lua_pushcsharpproperty(L, "layer", LuaGameObject.get_layer, LuaGameObject.set_layer); LuaDLL.lua_pushcsharpproperty(L, "activeSelf", LuaGameObject.get_activeSelf, null); LuaDLL.lua_pushcsharpproperty(L, "activeInHierarchy", LuaGameObject.get_activeInHierarchy, null); LuaDLL.lua_pushcsharpproperty(L, "isStatic", LuaGameObject.get_isStatic, LuaGameObject.set_isStatic); LuaDLL.lua_pushcsharpproperty(L, "tag", LuaGameObject.get_tag, LuaGameObject.set_tag); LuaDLL.lua_pushcsharpproperty(L, "gameObject", LuaGameObject.get_gameObject, null); LuaDLL.lua_getglobal(L, "readIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__index"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getglobal(L, "writeIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__newindex"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_pushstdcallcfunction(L, new LuaCSFunction(LuaStatic.GameObjectGC)); LuaDLL.lua_setfield(L, -2, "__gc"); LuaDLL.lua_getglobal(L, "Object"); if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "Object"); LuaDLL.lua_getglobal(L, "Object"); LuaDLL.lua_setmetatable(L, -2); } else { LuaDLL.lua_setmetatable(L, -2); } LuaDLL.lua_settop(L, oldTop); LuaStatic.AddTypeDict(typeof(UnityEngine.GameObject)); }
/* * Gets the function in the index positon of the Lua stack. */ internal LuaFunction getFunction(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return(new LuaFunction(LuaDLL.lua_ref(luaState, 1), interpreter)); }
/* * Gets the userdata in the index positon of the Lua stack. */ internal LuaUserData getUserData(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return(new LuaUserData(LuaDLL.lua_ref(luaState, 1), interpreter)); }
/* * Gets the table in the index positon of the Lua stack. */ internal LuaTable getTable(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return(new LuaTable(LuaDLL.lua_ref(luaState, 1), interpreter)); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luaTypes = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; } long key = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(this.extractValues[key]); } if (paramType.IsGenericParameter) { if (luaTypes == LuaTypes.LUA_TBOOLEAN) { return(this.extractValues[typeof(bool).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TSTRING) { return(this.extractValues[typeof(string).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TTABLE) { return(this.extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TUSERDATA) { return(this.extractValues[typeof(object).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TFUNCTION) { return(this.extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TNUMBER) { return(this.extractValues[typeof(double).TypeHandle.Value.ToInt64()]); } } if (paramType.IsValueType && luaTypes == LuaTypes.LUA_TTABLE) { int newTop = LuaDLL.lua_gettop(luaState); ExtractValue extractValue = null; LuaDLL.lua_pushvalue(luaState, stackPos); LuaDLL.lua_pushstring(luaState, "class"); LuaDLL.lua_gettable(luaState, -2); if (!LuaDLL.lua_isnil(luaState, -1)) { string a = LuaDLL.lua_tostring(luaState, -1); if (a == "Vector3" && paramType == typeof(Vector3)) { extractValue = this.extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()]; } else if (a == "Vector2" && paramType == typeof(Vector2)) { extractValue = this.extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()]; } else if (a == "Quaternion" && paramType == typeof(Quaternion)) { extractValue = this.extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()]; } else if (a == "Color" && paramType == typeof(Color)) { extractValue = this.extractValues[typeof(Color).TypeHandle.Value.ToInt64()]; } else if (a == "Vector4" && paramType == typeof(Vector4)) { extractValue = this.extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()]; } else if (a == "Ray" && paramType == typeof(Ray)) { extractValue = this.extractValues[typeof(Ray).TypeHandle.Value.ToInt64()]; } else { extractValue = null; } } LuaDLL.lua_settop(luaState, newTop); if (extractValue != null) { return(extractValue); } } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(this.extractValues[key]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(this.extractValues[key]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(this.extractValues[key]); } if (luaTypes == LuaTypes.LUA_TNIL) { return(this.extractNetObject); } } else if (paramType == typeof(LuaTable)) { if (luaTypes == LuaTypes.LUA_TTABLE) { return(this.extractValues[key]); } } else if (paramType == typeof(LuaFunction)) { if (luaTypes == LuaTypes.LUA_TFUNCTION) { return(this.extractValues[key]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luaTypes == LuaTypes.LUA_TFUNCTION) { this.translator.throwError(luaState, "Delegates not implemnented"); } else if (paramType.IsInterface && luaTypes == LuaTypes.LUA_TTABLE) { this.translator.throwError(luaState, "Interfaces not implemnented"); } else { if ((paramType.IsInterface || paramType.IsClass) && luaTypes == LuaTypes.LUA_TNIL) { return(this.extractNetObject); } if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL) { object netObject = this.translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (netObject != null && paramType.IsAssignableFrom(netObject.GetType())) { return(this.extractNetObject); } } } else { object rawNetObject = this.translator.getRawNetObject(luaState, stackPos); if (rawNetObject != null && paramType.IsAssignableFrom(rawNetObject.GetType())) { return(this.extractNetObject); } } } return(null); }
/* * Gets the table in the index positon of the Lua stack. */ internal LuaTable getTable(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return(new LuaTable(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX), interpreter)); }
public static void lua_getregistry(IntPtr L) { LuaDLL.lua_pushvalue(L, LuaIndexes.LUA_REGISTRYINDEX); }
public static void lua_pushglobaltable(IntPtr l) { LuaDLL.lua_pushvalue(l, LuaIndexes.LUA_GLOBALSINDEX); }
public LuaState() { // Create State L = LuaDLL.luaL_newstate(); // Create LuaInterface library LuaDLL.luaL_openlibs(L); LuaDLL.lua_pushstring(L, "LUAINTERFACE LOADED"); LuaDLL.lua_pushboolean(L, true); LuaDLL.lua_settable(L, (int)LuaIndexes.LUA_REGISTRYINDEX); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "luanet"); LuaDLL.lua_pushvalue(L, (int)LuaIndexes.LUA_GLOBALSINDEX); LuaDLL.lua_getglobal(L, "luanet"); LuaDLL.lua_pushstring(L, "getmetatable"); LuaDLL.lua_getglobal(L, "getmetatable"); LuaDLL.lua_settable(L, -3); // Set luanet as global for object translator LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); translator = new ObjectTranslator(this, L); LuaDLL.lua_replace(L, (int)LuaIndexes.LUA_GLOBALSINDEX); GCHandle handle = GCHandle.Alloc(translator, GCHandleType.Pinned); IntPtr thisptr = GCHandle.ToIntPtr(handle); LuaDLL.lua_pushlightuserdata(L, thisptr); LuaDLL.lua_setglobal(L, "_translator"); // We need to keep this in a managed reference so the delegate doesn't get garbage collected panicCallback = new LuaCSFunction(LuaStatic.panic); LuaDLL.lua_atpanic(L, panicCallback); printFunction = new LuaCSFunction(LuaStatic.print); LuaDLL.lua_pushstdcallcfunction(L, printFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "print"); loadfileFunction = new LuaCSFunction(LuaStatic.loadfile); LuaDLL.lua_pushstdcallcfunction(L, loadfileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "loadfile"); dofileFunction = new LuaCSFunction(LuaStatic.dofile); LuaDLL.lua_pushstdcallcfunction(L, dofileFunction); LuaDLL.lua_setfield(L, LuaIndexes.LUA_GLOBALSINDEX, "dofile"); // Insert our loader FIRST loaderFunction = new LuaCSFunction(LuaStatic.loader); LuaDLL.lua_pushstdcallcfunction(L, loaderFunction); int loaderFunc = LuaDLL.lua_gettop(L); LuaDLL.lua_getfield(L, LuaIndexes.LUA_GLOBALSINDEX, "package"); LuaDLL.lua_getfield(L, -1, "loaders"); int loaderTable = LuaDLL.lua_gettop(L); // Shift table elements right for (int e = LuaDLL.luaL_getn(L, loaderTable) + 1; e > 1; e--) { LuaDLL.lua_rawgeti(L, loaderTable, e - 1); LuaDLL.lua_rawseti(L, loaderTable, e); } LuaDLL.lua_pushvalue(L, loaderFunc); LuaDLL.lua_rawseti(L, loaderTable, 1); LuaDLL.lua_settop(L, 0); DoString(LuaStatic.init_luanet); tracebackFunction = new LuaCSFunction(LuaStatic.traceback); }
/* * Gets the function in the index positon of the Lua stack. */ internal LuaFunction getFunction(IntPtr luaState, int index) { LuaDLL.lua_pushvalue(luaState, index); return new LuaFunction(LuaDLL.luaL_ref(luaState, LuaIndexes.LUA_REGISTRYINDEX), interpreter); }
public static void Register(IntPtr L) { int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "Transform"); if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "Transform"); LuaDLL.lua_getglobal(L, "Transform"); } LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.SetParent, "SetParent"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.Translate, "Translate"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.Rotate, "Rotate"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.RotateAround, "RotateAround"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.LookAt, "LookAt"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.TransformDirection, "TransformDirection"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.InverseTransformDirection, "InverseTransformDirection"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.TransformVector, "TransformVector"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.InverseTransformVector, "InverseTransformVector"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.TransformPoint, "TransformPoint"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.InverseTransformPoint, "InverseTransformPoint"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.DetachChildren, "DetachChildren"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.SetAsFirstSibling, "SetAsFirstSibling"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.SetAsLastSibling, "SetAsLastSibling"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.SetSiblingIndex, "SetSiblingIndex"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.GetSiblingIndex, "GetSiblingIndex"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.Find, "Find"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.IsChildOf, "IsChildOf"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.FindChild, "FindChild"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.GetEnumerator, "GetEnumerator"); LuaDLL.lua_pushstdcallcfunction(L, LuaTransform.GetChild, "GetChild"); LuaDLL.lua_pushcsharpproperty(L, "position", LuaTransform.get_position, LuaTransform.set_position); LuaDLL.lua_pushcsharpproperty(L, "localPosition", LuaTransform.get_localPosition, LuaTransform.set_localPosition); LuaDLL.lua_pushcsharpproperty(L, "eulerAngles", LuaTransform.get_eulerAngles, LuaTransform.set_eulerAngles); LuaDLL.lua_pushcsharpproperty(L, "localEulerAngles", LuaTransform.get_localEulerAngles, LuaTransform.set_localEulerAngles); LuaDLL.lua_pushcsharpproperty(L, "right", LuaTransform.get_right, LuaTransform.set_right); LuaDLL.lua_pushcsharpproperty(L, "up", LuaTransform.get_up, LuaTransform.set_up); LuaDLL.lua_pushcsharpproperty(L, "forward", LuaTransform.get_forward, LuaTransform.set_forward); LuaDLL.lua_pushcsharpproperty(L, "rotation", LuaTransform.get_rotation, LuaTransform.set_rotation); LuaDLL.lua_pushcsharpproperty(L, "localRotation", LuaTransform.get_localRotation, LuaTransform.set_localRotation); LuaDLL.lua_pushcsharpproperty(L, "localScale", LuaTransform.get_localScale, LuaTransform.set_localScale); LuaDLL.lua_pushcsharpproperty(L, "parent", LuaTransform.get_parent, LuaTransform.set_parent); LuaDLL.lua_pushcsharpproperty(L, "worldToLocalMatrix", LuaTransform.get_worldToLocalMatrix, null); LuaDLL.lua_pushcsharpproperty(L, "localToWorldMatrix", LuaTransform.get_localToWorldMatrix, null); LuaDLL.lua_pushcsharpproperty(L, "root", LuaTransform.get_root, null); LuaDLL.lua_pushcsharpproperty(L, "childCount", LuaTransform.get_childCount, null); LuaDLL.lua_pushcsharpproperty(L, "lossyScale", LuaTransform.get_lossyScale, null); LuaDLL.lua_pushcsharpproperty(L, "hasChanged", LuaTransform.get_hasChanged, LuaTransform.set_hasChanged); LuaDLL.lua_getglobal(L, "readIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__index"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_getglobal(L, "writeIndex"); LuaDLL.lua_pushvalue(L, -1); LuaDLL.lua_setfield(L, -3, "__newindex"); LuaDLL.lua_pop(L, 1); LuaDLL.lua_pushstdcallcfunction(L, new LuaCSFunction(LuaStatic.GameObjectGC)); LuaDLL.lua_setfield(L, -2, "__gc"); LuaDLL.lua_getglobal(L, "Component"); if (LuaDLL.lua_isnil(L, -1)) { LuaDLL.lua_pop(L, 1); LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, "Component"); LuaDLL.lua_getglobal(L, "Component"); LuaDLL.lua_setmetatable(L, -2); } else { LuaDLL.lua_setmetatable(L, -2); } LuaDLL.lua_settop(L, oldTop); LuaStatic.AddTypeDict(typeof(UnityEngine.Transform)); }