From 14c19327274bf62bb25b11a3e69d744daf270f63 Mon Sep 17 00:00:00 2001 From: EvilFactory Date: Thu, 29 Aug 2024 20:12:20 -0300 Subject: [PATCH] Implement updater --- Luatrauma.AutoUpdater/Program.cs | 22 +++++++++++- Luatrauma.AutoUpdater/Updater.cs | 62 ++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Luatrauma.AutoUpdater/Updater.cs diff --git a/Luatrauma.AutoUpdater/Program.cs b/Luatrauma.AutoUpdater/Program.cs index c6f4aa7..5107151 100644 --- a/Luatrauma.AutoUpdater/Program.cs +++ b/Luatrauma.AutoUpdater/Program.cs @@ -1,10 +1,30 @@ -namespace Luatrauma.AutoUpdater +using System.Diagnostics; + +namespace Luatrauma.AutoUpdater { internal class Program { + public static string[] Args; + static void Main(string[] args) { + Args = args; + + Task task = Start(); + task.Wait(); + } + + public async static Task Start() + { + await Updater.Update(); + + Console.WriteLine("Update completed."); + if (Args.Length > 1) + { + Console.WriteLine("Starting " + string.Join(" ", Args)); + Process.Start(Args[0], Args.Skip(1).ToArray()); + } } } } diff --git a/Luatrauma.AutoUpdater/Updater.cs b/Luatrauma.AutoUpdater/Updater.cs new file mode 100644 index 0000000..ef8e673 --- /dev/null +++ b/Luatrauma.AutoUpdater/Updater.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.IO.Compression; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace Luatrauma.AutoUpdater +{ + static class Updater + { + public const string TempFolder = "Luatrauma.AutoUpdater.Temp"; + + public async static Task Update() + { + string patchUrl; + +#if WINDOWS + patchUrl = "https://github.com/evilfactory/LuaCsForBarotrauma/releases/download/latest/luacsforbarotrauma_patch_windows_client.zip"; +#elif LINUX + patchUrl = "https://github.com/evilfactory/LuaCsForBarotrauma/releases/download/latest/luacsforbarotrauma_patch_linux_client.zip"; +#elif MACOS + patchUrl = "https://github.com/evilfactory/LuaCsForBarotrauma/releases/download/latest/luacsforbarotrauma_patch_mac_client.zip; +#endif + + string tempFolder = Path.Combine(Directory.GetCurrentDirectory(), TempFolder); + string patchZip = Path.Combine(tempFolder, "patch.zip"); + + Directory.CreateDirectory(tempFolder); + + Console.WriteLine("Downloading patch zip from {0}", patchUrl); + + try + { + using var client = new HttpClient(); + using var s = await client.GetStreamAsync(patchUrl); + using var fs = new FileStream(patchZip, FileMode.OpenOrCreate); + await s.CopyToAsync(fs); + } + catch (Exception e) + { + Console.WriteLine("Failed to download patch zip: {0}", e.Message); + return; + } + + Console.WriteLine("Downloaded patch zip to {0}", patchZip); + + try + { + ZipFile.ExtractToDirectory(patchZip, Directory.GetCurrentDirectory(), true); + } + catch (Exception e) + { + Console.WriteLine("Failed to extract patch zip: {0}", e.Message); + return; + } + + Console.WriteLine("Extracted patch zip to {0}", Directory.GetCurrentDirectory()); + } + } +}