Skip to content

Commit

Permalink
Implement updater
Browse files Browse the repository at this point in the history
  • Loading branch information
evilfactory committed Aug 29, 2024
1 parent 6c7f985 commit 14c1932
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Luatrauma.AutoUpdater/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
namespace Luatrauma.AutoUpdater
using System.Diagnostics;

namespace Luatrauma.AutoUpdater
{
internal class Program
{
public static string[] Args;

Check warning on line 7 in Luatrauma.AutoUpdater/Program.cs

View workflow job for this annotation

GitHub Actions / publish-release / build / build

Non-nullable field 'Args' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 7 in Luatrauma.AutoUpdater/Program.cs

View workflow job for this annotation

GitHub Actions / publish-release / build / build

Non-nullable field 'Args' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 7 in Luatrauma.AutoUpdater/Program.cs

View workflow job for this annotation

GitHub Actions / publish-release / build / build

Non-nullable field 'Args' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

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());
}
}
}
}
62 changes: 62 additions & 0 deletions Luatrauma.AutoUpdater/Updater.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 14c1932

Please sign in to comment.