static int ConnectToResourceDB_Lua(IntPtr L) { var path = Api.lua_tostring(L, 1); var overwrite = false; if (Api.lua_gettop(L) == 2) { overwrite = Api.lua_toboolean(L, 2); } var dbPath = Application.persistentDataPath + "/" + path; if (overwrite || !File.Exists(dbPath)) { var dbDir = Path.GetDirectoryName(dbPath); if (!Directory.Exists(dbDir)) { Directory.CreateDirectory(dbDir); } var allBytes = ResMgr.LoadBytes(path); File.WriteAllBytes(dbPath, allBytes); } var conn = new SqliteConnection("URI=file:" + dbPath); conn.Open(); lua.Lua.PushObjectInternal(L, conn); return(1); }
static int LoadBytes_Lua(IntPtr L) { var encrypted = false; if (Api.lua_gettop(L) == 2) { if (Api.lua_type(L, 2) == Api.LUA_TBOOLEAN) { encrypted = Api.lua_toboolean(L, 2); } } var bytes = ResMgr.LoadBytes(Api.lua_tostring(L, 1), encrypted); if (bytes == null) { return(0); } Api.lua_pushbytes(L, bytes); return(1); }
static byte[] LoadLuaScriptRunTime(string scriptName, out string scriptPath) { if (System.IntPtr.Size > 4) { scriptName = scriptName + "_64"; } else { scriptName = scriptName + "_32"; } scriptName = scriptName.ToLower(); var s = ResMgr.LoadBytes("_LuaRoot/" + scriptName); if (s != null) { scriptPath = scriptName; return(s); } scriptPath = string.Empty; return(null); }
static int UntarFromResources_Lua(IntPtr L) { var uri = Api.lua_tostring(L, 1); var path = Api.lua_tostring(L, 2); var unpackPath = Path.Combine(Application.persistentDataPath, path); TarArchive archive = null; try { if (!Directory.Exists(unpackPath)) { Directory.CreateDirectory(unpackPath); } var forceUnpack = false; if (Api.lua_gettop(L) == 3) { forceUnpack = Api.lua_toboolean(L, 3); } var bytes = ResMgr.LoadBytes(uri); var ms = new MemoryStream(bytes); archive = TarArchive.CreateInputTarArchive(ms); List <string> names = new List <string>(); archive.ProgressMessageEvent += (ar, entry, msg) => { names.Add(entry.Name); }; bool shouldUnpack = false; if (forceUnpack) { shouldUnpack = true; } else { archive.ListContents(); for (int i = 0; i < names.Count; ++i) { var p = Path.Combine(unpackPath, names[i]); if (!File.Exists(p)) { shouldUnpack = true; break; } } if (shouldUnpack) { archive.Dispose(); archive = TarArchive.CreateInputTarArchive(new MemoryStream(bytes)); } } if (shouldUnpack) { archive.ExtractContents(unpackPath); } archive.Dispose(); Api.lua_createtable(L, names.Count, 0); for (int i = 0; i < names.Count; ++i) { Api.lua_pushstring(L, names[i]); Api.lua_seti(L, -2, i); } return(1); } catch (Exception e) { if (archive != null) { archive.Dispose(); } Api.lua_pushnil(L); Api.lua_pushstring(L, e.Message); return(2); } }