diff --git a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs index 3595e0036..b24141c0f 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GameMain.cs @@ -563,6 +563,11 @@ namespace Barotrauma { DebugConsole.NewMessage("LOADING COROUTINE FINISHED", Color.Lime); } + +#if CLIENT + LuaCsUpdateChecker.Check(); +#endif + yield return CoroutineStatus.Success; } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Lua/LuaCsUpdateChecker.cs b/Barotrauma/BarotraumaClient/ClientSource/Lua/LuaCsUpdateChecker.cs new file mode 100644 index 000000000..0fa185285 --- /dev/null +++ b/Barotrauma/BarotraumaClient/ClientSource/Lua/LuaCsUpdateChecker.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; + +namespace Barotrauma +{ + static class LuaCsUpdateChecker + { + public static void Check() + { + if (!File.Exists(LuaSetup.VERSION_FILE)) { return; } + + ContentPackage luaCsPackage = LuaSetup.GetPackage(); + string luaCsPath = Path.GetDirectoryName(luaCsPackage.Path); + + if (luaCsPackage == null) { return; } + + string clientVersion = File.ReadAllText(LuaSetup.VERSION_FILE); + string serverVersion = luaCsPackage.ModVersion; + + if (clientVersion == serverVersion) { return; } + + var msg = new GUIMessageBox("LuaCs Update", $"Your LuaCs client version is different from the version found in the LuaCsForBarotrauma workshop files. Do you want to update?\n\n Client Version: {clientVersion}\n Server Version: {serverVersion}", + new LocalizedString[2] { TextManager.Get("Yes"), TextManager.Get("Cancel") }); + + msg.Buttons[0].OnClicked = (GUIButton button, object obj) => + { + string[] filesToUpdate = new string[] + { + "Barotrauma.dll", "Barotrauma.deps.json", + "0harmony.dll", "MoonSharp.Interpreter.dll", + }; + + + try + { + foreach (string file in filesToUpdate) + { + File.Move(file, file + ".todelete", true); + File.Copy(Path.Combine(luaCsPath, "Binary", file), file, true); + } + + File.WriteAllText(LuaSetup.VERSION_FILE, serverVersion); + } + catch (Exception e) + { + new GUIMessageBox("Failed", $"Failed to update, error: {e}"); + + msg.Close(); + return true; + } + + new GUIMessageBox("Restart", "LuaCs updated! Restart your game to apply the changes."); + + msg.Close(); + return true; + }; + + msg.Buttons[1].OnClicked = (GUIButton button, object obj) => + { + msg.Close(); + return true; + }; + + } + } +} \ No newline at end of file diff --git a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs index 5230a8c93..5b06d6307 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/DebugConsole.cs @@ -1253,7 +1253,7 @@ namespace Barotrauma commands.Add(new Command("install_cl_lua", "Installs client-Side Lua into your client.", (string[] args) => { - ContentPackage luaPackage = LuaSetup.GetLuaPackage(); + ContentPackage luaPackage = LuaSetup.GetPackage(); if (luaPackage == null) { @@ -1265,7 +1265,7 @@ namespace Barotrauma { string path = Path.GetDirectoryName(luaPackage.Path); - string[] filesToMove = new string[] + string[] filesToCopy = new string[] { "Barotrauma.dll", "Barotrauma.deps.json", "0harmony.dll", "Mono.Cecil.dll", @@ -1278,15 +1278,21 @@ namespace Barotrauma File.Move("Barotrauma.dll", "Barotrauma.dll.old", true); File.Move("Barotrauma.deps.json", "Barotrauma.deps.json.old", true); - foreach (string file in filesToMove) + foreach (string file in filesToCopy) { File.Copy(Path.Combine(path, "Binary", file), file, true); } + + File.WriteAllText(LuaSetup.VERSION_FILE, luaPackage.ModVersion); + } + catch (UnauthorizedAccessException e) + { + GameMain.Lua.PrintError("You seem to already have Client Side Lua installed, if you are trying to reinstall, make sure uninstall it first (mainmenu button located top left)."); + + return; } catch (Exception e) { - // GameMain.Lua.PrintError("You seem to already have Client Side Lua installed, if you are trying to reinstall, make sure uninstall it first (mainmenu button located top left)."); - GameMain.Lua.HandleLuaException(e); return; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs index 6d3e1d4bd..ee3f8a685 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs @@ -13,7 +13,8 @@ namespace Barotrauma { partial class LuaSetup { - public const string LUA_PATH = "Lua/LuaSetup.lua"; + public const string LUASETUP_FILE = "Lua/LuaSetup.lua"; + public const string VERSION_FILE = "luacsversion.txt"; public Script lua; @@ -24,7 +25,7 @@ namespace Barotrauma public LuaScriptLoader luaScriptLoader; - public static ContentPackage GetLuaPackage() + public static ContentPackage GetPackage() { foreach (ContentPackage package in ContentPackageManager.LocalPackages) { @@ -313,13 +314,13 @@ namespace Barotrauma // LuaDocs.GenerateDocsAll(); - ContentPackage luaPackage = GetLuaPackage(); + ContentPackage luaPackage = GetPackage(); - if (File.Exists(LUA_PATH)) + if (File.Exists(LUASETUP_FILE)) { try { - lua.Call(lua.LoadFile(LUA_PATH), Path.GetDirectoryName(Path.GetFullPath(LUA_PATH))); + lua.Call(lua.LoadFile(LUASETUP_FILE), Path.GetDirectoryName(Path.GetFullPath(LUASETUP_FILE))); } catch (Exception e) {