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; }
/* * Indexer for global variables from the LuaInterpreter * Supports navigation of tables by using . operator */ public object this[string fullPath] { get { object returnValue = null; int oldTop = LuaDLL.lua_gettop(L); string[] path = fullPath.Split(new char[] { '.' }); LuaDLL.lua_getglobal(L, path[0]); returnValue = translator.getObject(L, -1); if (path.Length > 1) { string[] remainingPath = new string[path.Length - 1]; Array.Copy(path, 1, remainingPath, 0, path.Length - 1); returnValue = getObject(remainingPath); } LuaDLL.lua_settop(L, oldTop); return(returnValue); } set { int oldTop = LuaDLL.lua_gettop(L); string[] path = fullPath.Split(new char[] { '.' }); if (path.Length == 1) { translator.push(L, value); LuaDLL.lua_setglobal(L, fullPath); } else { //LuaDLL.lua_getglobal(L, path[0]); LuaDLL.lua_rawglobal(L, path[0]); LuaTypes type = LuaDLL.lua_type(L, -1); if (type == LuaTypes.LUA_TNIL) { Debugger.LogError("Table {0} not exists", path[0]); LuaDLL.lua_settop(L, oldTop); return; } string[] remainingPath = new string[path.Length - 1]; Array.Copy(path, 1, remainingPath, 0, path.Length - 1); setObject(remainingPath, value); } LuaDLL.lua_settop(L, oldTop); // Globals auto-complete // comment by topameng, too many time cost, you shound register you type in other position /*if (value == null) * { * // Remove now obsolete entries * globals.Remove(fullPath); * } * else * { * // Add new entries * if (!globals.Contains(fullPath)) * registerGlobal(fullPath, value.GetType(), 0); * }*/ } }
public static void lua_pop(IntPtr luaState, int amount) { LuaDLL.lua_settop(luaState, -(amount) - 1); }
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 (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(LuaUserData)) { if (luatype == LuaTypes.LUA_TUSERDATA) { 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 (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 { return(null); } } else { object obj = translator.getNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }
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); //用luanet替换_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, "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); }
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)) { Debug.Log("checkType::::paramType::::" + paramType); ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()]; } else if (cls == "Vector2" && paramType == typeof(Vector2)) { Debug.Log("checkType::::paramType::::" + paramType); 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)) { Debug.Log("checkType::::paramType::::" + paramType); 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 void LuaSetTop(int newTop) { LuaDLL.lua_settop(L, newTop); }
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)); }
public void EndPCall(int oldTop) { LuaDLL.lua_settop(L, oldTop - 1); }
static int Print_Warning(IntPtr L) { try { int n = LuaDLL.lua_gettop(L); StringBuilder sb = StringBuilderCache.Acquire(); #if UNITY_EDITOR2 int line = LuaDLL.tolua_where(L, 1); string filename = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, n); if (!filename.Contains(".")) { sb.AppendFormat("[{0}.lua:{1}]:", filename, line); } else { sb.AppendFormat("[{0}:{1}]:", filename, line); } #endif sb.Append("["); sb.Append(DateTime.Now.ToString("HH:mm:ss")); sb.Append("] "); for (int i = 1; i <= n; i++) { if (i > 1) { sb.Append(" "); } if (LuaDLL.lua_isstring(L, i) == 1) { sb.Append(LuaDLL.lua_tostring(L, i)); } else if (LuaDLL.lua_isnil(L, i)) { sb.Append("nil"); } else if (LuaDLL.lua_isboolean(L, i)) { sb.Append(LuaDLL.lua_toboolean(L, i) ? "true" : "false"); } else { IntPtr p = LuaDLL.lua_topointer(L, i); if (p == IntPtr.Zero) { sb.Append("nil"); } else { sb.AppendFormat("{0}:0x{1}", LuaDLL.luaL_typename(L, i), p.ToString("X")); } } } GameBase.Debugger.LogWarning_Fixed(StringBuilderCache.GetStringAndRelease(sb)); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }