static void LuaPCall(IntPtr L, LuaCSFunction func) { LuaDLL.tolua_pushcfunction(L, func); if (LuaDLL.lua_pcall(L, 0, -1, 0) != 0) { string error = LuaDLL.lua_tostring(L, -1); Exception last = LuaException.luaStack; LuaException.luaStack = null; throw new LuaException(error, last); } }
public bool LuaDoString(string chunk, string chunkName = "@LuaStatePtr.cs") { byte[] buffer = Encoding.UTF8.GetBytes(chunk); int status = LuaDLL.luaL_loadbuffer(L, buffer, buffer.Length, chunkName); if (status != 0) { return(false); } return(LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, 0) == 0); //return LuaDLL.luaL_dostring(L, chunk); }
/* * Excutes a Lua file and returns all the chunk's return * values in an array */ public object[] DoFile(string fileName, LuaTable env) { LuaDLL.lua_pushstdcallcfunction(L, tracebackFunction); int oldTop = LuaDLL.lua_gettop(L); // Load with Unity3D resources byte[] text = LuaStatic.Load(fileName); // Debugger.LogWarning("---------------" + fileName); if (text == null) { if (!fileName.Contains("mobdebug")) { Debugger.LogError("Loader lua file failed: {0}", fileName); } LuaDLL.lua_pop(L, 1); return(null); } // string luafile = Util.LuaPath(fileName); //Encoding.UTF8.GetByteCount(text) int len = LuaStatic.getBytesLength(text); if (LuaDLL.luaL_loadbuffer(L, text, len, fileName) == 0) { if (env != null) { env.push(L); //LuaDLL.lua_setfenv(L, -1); LuaDLL.lua_setfenv(L, -2); } if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0) { object[] results = translator.popValues(L, oldTop); LuaDLL.lua_pop(L, 1); return(results); } else { ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(L, 1); } } else { ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(L, 1); } return(null); // Never reached - keeps compiler happy }
public bool PCall(int oldTop, int args) { if (LuaDLL.lua_pcall(this.L, args, -1, -args - 2) != 0) { string text = LuaDLL.lua_tostring(this.L, -1); LuaDLL.lua_settop(this.L, oldTop - 1); if (text == null) { text = "Unknown Lua Error"; } throw new LuaScriptException(text, string.Empty); } return(true); }
public string LuaToString(int index) { string s = LuaDLL.lua_tostring(L, index); if (s == null) { // 可能抛出一个table作为error参数,调用tostring获取其字符串 LuaDLL.lua_getglobal(L, "tostring"); LuaDLL.lua_pushvalue(L, index - 1); LuaDLL.lua_pcall(L, 1, 1, 0); s = LuaDLL.lua_tostring(L, -1); } return(s); }
public static void Push(IntPtr L, RaycastHit hit, int flag) { LuaStatic.GetPackRaycastHit(L); if ((flag & 1) != 0) { ToLua.Push(L, hit.get_collider()); } else { LuaDLL.lua_pushnil(L); } LuaDLL.lua_pushnumber(L, (double)hit.get_distance()); if ((flag & 2) != 0) { ToLua.Push(L, hit.get_normal()); } else { LuaDLL.lua_pushnil(L); } if ((flag & 4) != 0) { ToLua.Push(L, hit.get_point()); } else { LuaDLL.lua_pushnil(L); } if ((flag & 8) != 0) { ToLua.Push(L, hit.get_rigidbody()); } else { LuaDLL.lua_pushnil(L); } if ((flag & 16) != 0) { ToLua.Push(L, hit.get_transform()); } else { LuaDLL.lua_pushnil(L); } if (LuaDLL.lua_pcall(L, 6, 1, 0) != 0) { string msg = LuaDLL.lua_tostring(L, -1); throw new LuaException(msg, null, 1); } }
public int Resume(params object[] args) { this.objs = null; this.luaState.Push(this); IntPtr intPtr = this.luaState.LuaToThread(-1); this.luaState.LuaPop(1); int narg = 0; if (args != null) { narg = args.Length; for (int i = 0; i < args.Length; i++) { ToLua.Push(intPtr, args[i]); } } int num = LuaDLL.lua_resume(intPtr, narg); int num2; if (num > 1) { num2 = LuaDLL.lua_gettop(intPtr); LuaDLL.tolua_pushtraceback(intPtr); LuaDLL.lua_pushthread(intPtr); LuaDLL.lua_pushvalue(intPtr, -3); if (LuaDLL.lua_pcall(intPtr, 2, -1, 0) != 0) { LuaDLL.lua_settop(intPtr, num2); } string msg = LuaDLL.lua_tostring(intPtr, -1); LuaDLL.lua_settop(intPtr, 0); throw new LuaException(msg, null, 1); } num2 = LuaDLL.lua_gettop(intPtr); if (num2 > 0) { this.objs = new object[num2]; for (int j = 0; j < num2; j++) { this.objs[j] = ToLua.ToVarObject(intPtr, j + 1); } } if (num == 0) { this.Dispose(); } return(num); }
public static int luaL_throw(IntPtr L, string message) { LuaDLL.tolua_pushtraceback(L); LuaDLL.lua_pushstring(L, message); LuaDLL.lua_pushnumber(L, 1.0); if (LuaDLL.lua_pcall(L, 2, -1, 0) == 0) { message = LuaDLL.lua_tostring(L, -1); } else { LuaDLL.lua_pop(L, 1); } throw new LuaException(message, null, 2); }
public static string TracebackError(IntPtr L) { int top = LuaDLL.lua_gettop(L); LuaDLL.tolua_pushtraceback(L); LuaDLL.lua_pushthread(L); LuaDLL.lua_pushvalue(L, -3); if (LuaDLL.lua_pcall(L, 2, -1, 0) != 0) { LuaDLL.lua_settop(L, top); } return(LuaDLL.lua_tostring(L, -1)); }
public bool PCall(int oldTop, int args) { if (LuaDLL.lua_pcall(L, args, -1, -args - 2) != 0) { string err = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, oldTop - 1); if (err == null) { err = "Unknown Lua Error"; } throw new LuaScriptException(err, ""); } return(true); }
//如果不出错,则保留返回值在堆栈上 //如果出错,压入调用堆栈信息,抛出异常 public void Call(int nArgs) { int oldTop = LuaDLL.lua_gettop(L) - nArgs - 1; if (LuaDLL.lua_pcall(L, nArgs, 0, 0) == 0) { return; } else { //ThrowExceptionFromError(oldTop); Debug.LogError(LuaDLL.lua_tostring(L, -1)); LuaDLL.lua_pop(L, 1); } }
public static int DoFile(IntPtr L) { try { string fileName = LuaDLL.lua_tostring(L, 1); if (!Path.HasExtension(fileName)) { var ext = ""; #if !UNITY_EDITOR ext = ".txt"; #else if (Logic.Game.GameConfig.instance.loadLuaRemote) { ext = ".txt"; } else { ext = ".lua"; } #endif fileName += ext; } int n = LuaDLL.lua_gettop(L); byte[] text = LuaFileUtils.Instance.ReadFile(fileName); if (text == null) { return(0); } if (LuaDLL.luaL_loadbuffer(L, text, text.Length, fileName) == 0) { if (LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, 0) != 0) { string error = LuaDLL.lua_tostring(L, -1); throw new LuaException(error); } } return(LuaDLL.lua_gettop(L) - n); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public bool PCall(int oldTop, int args) { if (LuaDLL.lua_pcall(L, args, -1, -args - 2) != 0) { string err = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, oldTop - 1); if (err == null) { err = "Unknown Lua Error"; } Debugger.LogError(err); return(false); } return(true); }
public static int luaL_callmeta(IntPtr L, int stackPos, string field) { stackPos = LuaDLL.abs_index(L, stackPos); if (LuaDLL.luaL_getmetafield(L, stackPos, field) == 0) { return(0); } LuaDLL.lua_pushvalue(L, stackPos); if (LuaDLL.lua_pcall(L, 1, 1, 0) != 0) { string msg = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_pop(L, 1); throw new LuaException(msg, null, 1); } return(1); }
public static Bounds ToBounds(IntPtr L, int stackPos) { int num = LuaDLL.lua_gettop(L); LuaStatic.GetUnpackBounds(L); LuaDLL.lua_pushvalue(L, stackPos); if (LuaDLL.lua_pcall(L, 1, 2, 0) == 0) { Vector3 vector = ToLua.ToVector3(L, num + 1); Vector3 vector2 = ToLua.ToVector3(L, num + 2); return(new Bounds(vector, vector2)); } string msg = LuaDLL.lua_tostring(L, -1); throw new LuaException(msg, null, 1); }
/// <summary> /// 我添加的两个相应的函数 /// </summary> /// <param name="fileName"></param> /// <param name="chunk"></param> /// <returns></returns> public object[] DoStringFile(string fileName, string chunk) { LuaDLL.lua_pushstdcallcfunction(L, tracebackFunction); int oldTop = LuaDLL.lua_gettop(L); LuaTable env = null; // Load with Unity3D resources byte[] text = Encoding.UTF8.GetBytes(chunk); if (text == null) { Debugger.LogError("Loader lua file failed: {0}", fileName); LuaDLL.lua_pop(L, 1); return(null); } //Encoding.UTF8.GetByteCount(text) if (LuaDLL.luaL_loadbuffer(L, text, text.Length, fileName) == 0) { if (env != null) { env.push(L); //LuaDLL.lua_setfenv(L, -1); LuaDLL.lua_setfenv(L, -2); } if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0) { object[] results = translator.popValues(L, oldTop); LuaDLL.lua_pop(L, 1); return(results); } else { ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(L, 1); } } else { ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(L, 1); } return(null); // Never reached - keeps compiler happy }
public static bool PCall(IntPtr L, int nargs, int nresult) { int msgHandlerIndex = LuaDLL.lua_gettop(L) - nargs; LuaDLL.lua_pushcfunction(L, LuaFuncs.msgHandler); LuaDLL.lua_insert(L, -nargs - 2); //msghandler,f,nargs int ret = LuaDLL.lua_pcall(L, nargs, nresult, -nargs - 2); if (ret != LuaDLL.LUA_OK) //msghandler,errmsg { LuaDLL.lua_remove(L, -2); Debug.LogError(LuaDLL.lua_tostring(L, -1)); return(false); } LuaDLL.lua_remove(L, msgHandlerIndex); return(true); }
/* * Excutes a Lua file and returns all the chunk's return * values in an array */ public object[] DoFile(string fileName, LuaTable env) { LuaDLL.lua_pushstdcallcfunction(L, tracebackFunction); int oldTop = LuaDLL.lua_gettop(L); // Load with Unity3D resources byte[] text = LuaStatic.LoadLua(fileName); if (text == null) { ThrowExceptionFromError(oldTop); } //Encoding.UTF8.GetByteCount(text) if (LuaDLL.luaL_loadbuffer(L, text, text.Length, fileName) == 0) { if (env != null) { env.push(L); //LuaDLL.lua_setfenv(L, -1); LuaDLL.lua_setfenv(L, -2); } // lxc Test添加文件名参数作为模块名字 string moduleName = Path.GetFileNameWithoutExtension(fileName); translator.push(L, moduleName); //if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0) if (LuaDLL.lua_pcall(L, 1, -1, -2) == 0) { object[] results = translator.popValues(L, oldTop); LuaDLL.lua_pop(L, 1); return(results); } else { ThrowExceptionFromError(oldTop); } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }
public static int toluaL_exception(IntPtr L, Exception e, string msg = null) { msg = msg == null ? e.Message : msg; LuaException.luaStack = new LuaException(msg, e, 2); //log lua stack int top = LuaDLL.lua_gettop(L); LuaDLL.lua_getglobal(L, "debug"); LuaDLL.lua_getfield(L, -1, "traceback"); LuaDLL.lua_pcall(L, 0, 1, 0); string luastack = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, top); msg = string.Format("{0}\n{1}", msg, luastack); return(LuaDLL.tolua_error(L, msg)); }
/* * Calls the function casting return values to the types * in returnTypes */ internal object[] call(object[] args, Type[] returnTypes) { int nArgs = 0; LuaScriptMgr.PushTraceBack(L); int oldTop = LuaDLL.lua_gettop(L); if (!LuaDLL.lua_checkstack(L, args.Length + 6)) { LuaDLL.lua_pop(L, 1); throw new LuaException("Lua stack overflow"); } push(L); if (args != null) { nArgs = args.Length; for (int i = 0; i < args.Length; i++) { PushArgs(L, args[i]); } } int error = LuaDLL.lua_pcall(L, nArgs, -1, -nArgs - 2); if (error != 0) { string err = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_settop(L, oldTop - 1); if (err == null) { err = "Unknown Lua Error"; } Debugger.LogError(err); LuaDLL.lua_settop(L, oldTop - 1); return(null); } object[] ret = returnTypes != null?translator.popValues(L, oldTop, returnTypes) : translator.popValues(L, oldTop); LuaDLL.lua_settop(L, oldTop - 1); return(ret); }
public static void luaL_setfuncs(IntPtr luaState, LuaL_Reg[] funcs, int nup) { LuaDLL.wlua_getglobal(luaState, "cswrapfunc"); //nup,wrap for (int i = 0; i < funcs.Length; ++i) { LuaDLL.lua_pushvalue(luaState, -1); //nup,wrap,wrap LuaL_Reg reg = funcs[i]; for (int j = 0; j < nup; ++j) { lua_pushvalue(luaState, -nup); } //nup,wrap,wrap,nup lua_pushcclosure(luaState, reg.func, nup); //nup,wrap,wrap,f LuaDLL.lua_pcall(luaState, 1, 1, 0); //nup,wrap,f0 wlua_setfield(luaState, -(nup + 3), reg.name); //nup,wrap } lua_pop(luaState, nup + 1); }
public static int print(IntPtr L) { // For each argument we'll 'tostring' it int n = LuaDLL.lua_gettop(L); string s = String.Empty; int ret = LuaDLL.wlua_getglobal(L, "tostring"); if (ret == LuaDLL.LUA_ERROR) { return(0); } for (int i = 1; i <= n; i++) { LuaDLL.lua_pushvalue(L, -1); /* function to be called */ LuaDLL.lua_pushvalue(L, i); /* value to print */ ret = LuaDLL.lua_pcall(L, 1, 1, 0); if (ret != LuaDLL.LUA_OK) { LuaDLL.lua_pop(L, 1); continue; } string str = LuaDLL.lua_tostring(L, -1); if (str == null) { Debug.LogError("lua print return null...."); } else { s += str; } if (i < n) { s += "\t"; } LuaDLL.lua_pop(L, 1); /* pop result */ } Debug.Log("LUA: " + s); return(0); }
public static Ray ToRay(IntPtr L, int stackPos) { int top = LuaDLL.lua_gettop(L); LuaStatic.GetUnpackRayRef(L); LuaDLL.lua_pushvalue(L, stackPos); if (LuaDLL.lua_pcall(L, 1, 2, 0) == 0) { Vector3 origin = ToVector3(L, top + 1); Vector3 dir = ToVector3(L, top + 2); return(new Ray(origin, dir)); } else { string error = LuaDLL.lua_tostring(L, -1); throw new LuaException(error); } }
public static Bounds ToBounds(IntPtr L, int stackPos) { int top = LuaDLL.lua_gettop(L); LuaStatic.GetUnpackBounds(L); LuaDLL.lua_pushvalue(L, stackPos); if (LuaDLL.lua_pcall(L, 1, 2, 0) == 0) { Vector3 center = ToVector3(L, top + 1); Vector3 size = ToVector3(L, top + 2); return(new Bounds(center, size)); } else { string error = LuaDLL.lua_tostring(L, -1); throw new LuaException(error); } }
public object[] DoFile(string fileName, LuaTable env) { LuaDLL.lua_pushstdcallcfunction(this.L, this.tracebackFunction, 0); int oldTop = LuaDLL.lua_gettop(this.L); byte[] array = LuaStatic.Load(fileName); if (array == null) { if (!fileName.Contains("mobdebug")) { Debugger.LogError("Loader lua file failed: {0}", new object[] { fileName }); } LuaDLL.lua_pop(this.L, 1); return(null); } string name = Util.LuaPath(fileName); if (LuaDLL.luaL_loadbuffer(this.L, array, array.Length, name) == 0) { if (env != null) { env.push(this.L); LuaDLL.lua_setfenv(this.L, -2); } if (LuaDLL.lua_pcall(this.L, 0, -1, -2) == 0) { object[] result = this.translator.popValues(this.L, oldTop); LuaDLL.lua_pop(this.L, 1); return(result); } this.ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(this.L, 1); } else { this.ThrowExceptionFromError(oldTop); LuaDLL.lua_pop(this.L, 1); } return(null); }
public static void Push(IntPtr L, Touch t, int flag) { LuaStatic.GetPackTouch(L); LuaDLL.lua_pushinteger(L, t.fingerId); if ((flag & TouchBits.Position) != 0) { Push(L, t.position); } else { LuaDLL.lua_pushnil(L); } if ((flag & TouchBits.RawPosition) != 0) { Push(L, t.rawPosition); } else { LuaDLL.lua_pushnil(L); } if ((flag & TouchBits.DeltaPosition) != 0) { Push(L, t.deltaPosition); } else { LuaDLL.lua_pushnil(L); } LuaDLL.lua_pushnumber(L, t.deltaTime); LuaDLL.lua_pushinteger(L, t.tapCount); LuaDLL.lua_pushinteger(L, (int)t.phase); if (LuaDLL.lua_pcall(L, 7, -1, 0) != 0) { string error = LuaDLL.lua_tostring(L, -1); throw new LuaException(error); } }
/* * Excutes a Lua file and returns all the chunk's return * values in an array */ public object[] DoFile(string fileName, LuaTable env) { LuaDLL.lua_pushstdcallcfunction(L, tracebackFunction); int oldTop = LuaDLL.lua_gettop(L); // Load with Unity3D resources TextAsset file = (TextAsset)Resources.Load(fileName); if (file == null) { ThrowExceptionFromError(oldTop); } if (LuaDLL.luaL_loadbuffer(L, file.bytes, file.bytes.Length, fileName) == 0) { if (env != null) { env.push(L); //LuaDLL.lua_setfenv(L, -1); LuaDLL.lua_setfenv(L, -2); } if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0) { object[] results = translator.popValues(L, oldTop); LuaDLL.lua_pop(L, 1); return(results); } else { ThrowExceptionFromError(oldTop); } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }
public static void OpenLuaLibs(IntPtr L) { LuaDLL.lua_pushstdcallcfunction(L, LuaDLL.tolua_openlualibs); if (LuaDLL.lua_pcall(L, 0, -1, 0) != 0) { string error = LuaDLL.lua_tostring(L, -1); LuaDLL.lua_pop(L, 1); throw new LuaException(error); } SetOutMethods(L, "Vector3", GetOutVector3); SetOutMethods(L, "Vector2", GetOutVector2); SetOutMethods(L, "Vector4", GetOutVector4); SetOutMethods(L, "Color", GetOutColor); SetOutMethods(L, "Quaternion", GetOutQuaternion); SetOutMethods(L, "Ray", GetOutRay); SetOutMethods(L, "Bounds", GetOutBounds); SetOutMethods(L, "Touch", GetOutTouch); SetOutMethods(L, "RaycastHit", GetOutRaycastHit); SetOutMethods(L, "LayerMask", GetOutLayerMask); }
/* * Excutes a Lua file and returns all the chunk's return * values in an array */ public object[] DoFile(string fileName) { int oldTop = LuaDLL.lua_gettop(luaState); if (LuaDLL.luaL_loadfile(luaState, fileName) == 0) { if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0) { return(translator.popValues(luaState, oldTop)); } else { ThrowExceptionFromError(oldTop); } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }
public object[] DoString(string chunk, string chunkName) { int oldTop = LuaDLL.lua_gettop(L); byte[] data = Encoding.UTF8.GetBytes(chunk); if (LuaDLL.luaL_loadbuffer(L, data, data.Length, chunkName) == 0) { if (LuaDLL.lua_pcall(L, 0, -1, 0) == 0) { return(LuaStatic.PopValues(L, oldTop)); } else { ThrowExceptionFromError(oldTop); } } else { ThrowExceptionFromError(oldTop); } return(null); // Never reached - keeps compiler happy }