public T LoadString <T>(string chunk, string chunkName = "chunk", LuaTable env = null) { #if THREAD_SAFT || HOTFIX_ENABLE lock (luaEnvLock) { #endif if (typeof(T) != typeof(LuaFunction) && !typeof(T).IsSubclassOf(typeof(Delegate))) { throw new InvalidOperationException(typeof(T).Name + " is not a delegate type nor LuaFunction"); } var _L = L; int oldTop = LuaAPI.lua_gettop(_L); if (LuaAPI.luaL_loadbuffer(_L, chunk, chunkName) != 0) { ThrowExceptionFromError(oldTop); } if (env != null) { env.push(_L); LuaAPI.lua_setfenv(_L, -2); } T result = (T)translator.GetObject(_L, -1, typeof(T)); LuaAPI.lua_settop(_L, oldTop); return(result); #if THREAD_SAFT || HOTFIX_ENABLE } #endif }
internal static int LoadFromResource(RealStatePtr L) { try { string filename = LuaAPI.lua_tostring(L, 1).Replace('.', '/') + ".lua"; // Load with Unity3D resources UnityEngine.TextAsset file = (UnityEngine.TextAsset)UnityEngine.Resources.Load(filename); if (file == null) { LuaAPI.lua_pushstring(L, string.Format( "\n\tno such resource '{0}'", filename)); } else { if (LuaAPI.luaL_loadbuffer(L, file.text, "@" + filename) != 0) { return(LuaAPI.luaL_error(L, String.Format("error loading module {0} from resource, {1}", LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)))); } } return(1); } catch (System.Exception e) { return(LuaAPI.luaL_error(L, "c# exception in LoadFromResource:" + e)); } }
internal static int LoadFromCustomDataPath(RealStatePtr L) { try { string filename = LuaAPI.lua_tostring(L, 1).Replace('.', '/') + ".lua"; var filepath = XLuaFramework.Util.GetRelativePath() + filename; #if UNITY_ANDROID && !UNITY_EDITOR UnityEngine.WWW www = new UnityEngine.WWW(filepath); while (true) { if (www.isDone || !string.IsNullOrEmpty(www.error)) { System.Threading.Thread.Sleep(50); //比较hacker的做法 if (!string.IsNullOrEmpty(www.error)) { LuaAPI.lua_pushstring(L, string.Format( "\n\tno such file '{0}' in streamingAssetsPath!", filename)); } else { UnityEngine.Debug.Log("load lua file from StreamingAssets is obsolete, filename:" + filename); if (LuaAPI.luaL_loadbuffer(L, www.text, "@" + filename) != 0) { return(LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}", LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)))); } } break; } } #else if (File.Exists(filepath)) { Stream stream = File.Open(filepath, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(stream); string text = reader.ReadToEnd(); stream.Close(); UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename); if (LuaAPI.luaL_loadbuffer(L, text, "@" + filename) != 0) { return(LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}", LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)))); } } else { LuaAPI.lua_pushstring(L, string.Format( "\n\tno such file '{0}' in streamingAssetsPath!", filename)); } #endif return(1); } catch (System.Exception e) { return(LuaAPI.luaL_error(L, "c# exception in LoadFromStreamingAssetsPath:" + e)); } }
internal static int LoadFromStreamingAssetsPath(RealStatePtr L) { try { string filename = LuaAPI.lua_tostring(L, 1).Replace('.', '/') + ".lua"; var filepath = UnityEngine.Application.streamingAssetsPath + "/" + filename; #if UNITY_ANDROID && !UNITY_EDITOR UnityEngine.WWW www = new UnityEngine.WWW(filepath); while (true) { if (www.isDone || !string.IsNullOrEmpty(www.error)) { System.Threading.Thread.Sleep(50); //比较hacker的做法 if (!string.IsNullOrEmpty(www.error)) { LuaAPI.lua_pushstring(L, string.Format( "\n\tno such file '{0}' in streamingAssetsPath!", filename)); } else { UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename); if (LuaAPI.luaL_loadbuffer(L, www.text, "@" + filename) != 0) { return(LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}", LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)))); } } break; } } #else if (File.Exists(filepath)) { string text = File.ReadAllText(filepath); UnityEngine.Debug.LogWarning("load lua file from StreamingAssets is obsolete, filename:" + filename); if (LuaAPI.luaL_loadbuffer(L, text, "@" + filename) != 0) { return(LuaAPI.luaL_error(L, String.Format("error loading module {0} from streamingAssetsPath, {1}", LuaAPI.lua_tostring(L, 1), LuaAPI.lua_tostring(L, -1)))); } } else { LuaAPI.lua_pushstring(L, string.Format( "\n\tno such file '{0}' in streamingAssetsPath!", filename)); } #endif return(1); } catch (System.Exception e) { return(LuaAPI.luaL_error(L, "c# exception in LoadFromStreamingAssetsPath:" + e)); } }
internal static int DoString(RealStatePtr L) { var str = LuaAPI.lua_tostring(L, 1); var _L = L; int oldTop = LuaAPI.lua_gettop(_L); int errFunc = LuaAPI.load_error_func(_L, LuaAPI.get_error_func_ref(_L)); if (LuaAPI.luaL_loadbuffer(_L, str, LuaAPI.lua_tostring(L, 2)) != 0) { var err = LuaAPI.lua_tostring(L, -1); UnityEngine.Debug.LogError(err); } else if (LuaAPI.lua_pcall(_L, 0, -1, errFunc) != 0) { var err = LuaAPI.lua_tostring(L, -1); UnityEngine.Debug.LogError(err); } LuaAPI.lua_settop(L, oldTop); return(0); }
public static int Compile(RealStatePtr L) { string snippet = LuaAPI.lua_tostring(L, 1); string code; try { code = ComposeCode(Parser.Parse(snippet)); } catch (Exception e) { return(LuaAPI.luaL_error(L, String.Format("template compile error:{0}\r\n", e.Message))); } //UnityEngine.Debug.Log("code=" + code); if (LuaAPI.luaL_loadbuffer(L, code, "luatemplate") != 0) { return(LuaAPI.lua_error(L)); } return(1); }
public object[] DoString(string chunk, string chunkName = "chunk", LuaTable env = null) { #if THREAD_SAFT || HOTFIX_ENABLE lock (luaEnvLock) { #endif var _L = L; int oldTop = LuaAPI.lua_gettop(_L); int errFunc = LuaAPI.load_error_func(_L, errorFuncRef); if (LuaAPI.luaL_loadbuffer(_L, chunk, chunkName) == 0) { if (env != null) { env.push(_L); LuaAPI.lua_setfenv(_L, -2); } //if (LuaAPI.lua_pcall(_L, 0, -1, errFunc) == 0) if (LuaAPI.lua_pcall(_L, 0, -1, 0) == 0) { LuaAPI.lua_remove(_L, errFunc); return(translator.popValues(_L, oldTop)); } else { ThrowExceptionFromError(oldTop); } } else { ThrowExceptionFromError(oldTop); } return(null); #if THREAD_SAFT || HOTFIX_ENABLE } #endif }