Skip to content

Commit 4e1006d

Browse files
committed
Auto Updater done!
1 parent 4a8596b commit 4e1006d

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

Server/AutoUpdater/Update.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
1-
namespace Server.AutoUpdater;
1+
using System.Diagnostics;
2+
using System.IO;
3+
using System.Linq;
4+
using UnityEngine;
5+
6+
namespace Server.AutoUpdater;
27

38
public static class Update
49
{
10+
public static string GetUpdateScript(string url, string path)
11+
{
12+
var commands = new string[] {
13+
"Wait-Process -Name 'INVAXION' -ErrorAction SilentlyContinue",
14+
$"Invoke-WebRequest -Uri '{url}' -OutFile '{path}'",
15+
"Remove-Item -Path $MyInvocation.MyCommand.Source"
16+
};
17+
return string.Join("\n", commands);
18+
}
19+
20+
public static void Procceed(GithubReleases.Release release)
21+
{
22+
var assetLink = release.assets.Where(asset => asset.name == "ServerEmulator.dll").FirstOrDefault().browser_download_url;
23+
var outFile = Path.Combine(Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "BepInEx"), "plugins"), "ServerEmulator.dll");
24+
var script = GetUpdateScript(assetLink, outFile);
25+
26+
var scriptName = $"ServerEmulator_{new Tag(release.tag_name)}_update.ps1";
27+
var scriptPath = Path.Combine(Path.GetTempPath(), scriptName);
28+
File.WriteAllText(scriptPath, script);
529

30+
var updateProcess = new Process()
31+
{
32+
StartInfo =
33+
{
34+
FileName = "powershell.exe",
35+
Arguments = $"-ExecutionPolicy ByPass -File {scriptName}",
36+
WorkingDirectory = Path.GetTempPath(),
37+
}
38+
};
39+
updateProcess.Start();
40+
Application.Quit();
41+
}
642
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Aquatrax;
2+
using HarmonyLib;
3+
using UnityEngine;
4+
5+
namespace Server.GeneralPatches;
6+
7+
8+
public static class TipHelperInputPatches
9+
{
10+
private static readonly Harmony _harmony = new Harmony("tiphelper-patches");
11+
12+
13+
public static bool PatchedUpdate(ref TipHelper __instance)
14+
{
15+
// The original function used Input.GetKeyDown() for some reason, so I changed it to Input.GetKeyUp() instead.
16+
17+
if (InputManager.currentRef.getCurentGroup() == "TipHelper")
18+
{
19+
if (Input.GetKeyUp(KeyCode.Escape) || InputDevice.isButtonChecked_Cancel)
20+
{
21+
__instance.ButtonCancel();
22+
}
23+
else if (Input.GetKeyUp(KeyCode.Return) || InputDevice.isButtonChecked_Submit)
24+
{
25+
__instance.ButtonCommit();
26+
}
27+
}
28+
return false;
29+
}
30+
31+
public static void Hook()
32+
{
33+
var TipHelper__Update = AccessTools.Method(typeof(TipHelper), "Update");
34+
var TipHelperInputPatches__PatchedUpdate = AccessTools.Method(typeof(TipHelperInputPatches), "PatchedUpdate");
35+
_harmony.Patch(TipHelper__Update, prefix: new HarmonyMethod(TipHelperInputPatches__PatchedUpdate));
36+
}
37+
}

Server/Server.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Server : BaseUnityPlugin
2020
public static bool Debug = true;
2121
private static Process _process;
2222
private static bool ShuttingDown = false;
23+
private static bool ManiaLoaderDisabled = true;
2324

2425
private void Awake()
2526
{
@@ -29,6 +30,7 @@ private void Awake()
2930
PlaceholderServerData = new();
3031
Logger.LogInfo($"Plugin {PluginInfo.PLUGIN_GUID} is loaded!");
3132

33+
GeneralPatches.TipHelperInputPatches.Hook();
3234
HookManager.Instance.Create();
3335
var currentVersion = new AutoUpdater.Tag(PluginInfo.PLUGIN_VERSION);
3436

@@ -63,12 +65,7 @@ void ShowUpdateDialog()
6365
Aquatrax.TipHelper.Instance.InitText($"An update for ServerEmulator has been found ({currentVersion} -> {releaseVersion}), update now?");
6466
Aquatrax.TipHelper.Instance.Commit = delegate ()
6567
{
66-
// TODO: Download the update and install it.
67-
Logger.LogInfo("Clicked yes!");
68-
};
69-
Aquatrax.TipHelper.Instance.Cancel = delegate ()
70-
{
71-
Logger.LogInfo("Clicked no!");
68+
AutoUpdater.Update.Procceed(NewVersion);
7269
};
7370
});
7471
DiscordRichPresence.GameEvents.switchScene -= ShowUpdateDialog;
@@ -79,8 +76,11 @@ void ShowUpdateDialog()
7976
}
8077
})));
8178

82-
ManiaBeatmapsLoader = new OsuManiaLoader.Loader();
83-
if (ManiaBeatmapsLoader.BeatmapPacks.Count > 0) Logger.LogInfo($"Loaded {ManiaBeatmapsLoader.BeatmapPacks.Count} osu!mania packs!");
79+
if (!ManiaLoaderDisabled)
80+
{
81+
ManiaBeatmapsLoader = new OsuManiaLoader.Loader();
82+
if (ManiaBeatmapsLoader.BeatmapPacks.Count > 0) Logger.LogInfo($"Loaded {ManiaBeatmapsLoader.BeatmapPacks.Count} osu!mania packs!");
83+
}
8484

8585
if (File.Exists(Path.Combine(Path.Combine("INVAXION_Data", "Plugins"), "discord_game_sdk.dll")))
8686
{
@@ -149,6 +149,8 @@ private void OnApplicationQuit()
149149
_process = null;
150150
}
151151

152+
if (!Debug) return;
153+
152154
var commands = new List<string>();
153155

154156
foreach (var command in MustImplement)

Server/Server.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>net35</TargetFramework>
44
<AssemblyName>ServerEmulator</AssemblyName>
55
<Description>INVAXION Server Emulator</Description>
6-
<Version>1.0.2</Version>
6+
<Version>1.0.3</Version>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<LangVersion>latest</LangVersion>
99
</PropertyGroup>

0 commit comments

Comments
 (0)