Skip to content

Commit d44d839

Browse files
Implement auto-update feature and add UnwrapAsync extension method
1 parent 25dcc1b commit d44d839

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/Managers/AutoUpdate.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Diagnostics;
2+
using System.Text.Json;
3+
using HarmonyLib;
4+
using UnityEngine;
5+
6+
namespace PolyMod.Managers;
7+
internal static class AutoUpdate
8+
{
9+
[HarmonyPostfix]
10+
[HarmonyPatch(typeof(StartScreen), nameof(StartScreen.Start))]
11+
private static void StartScreen_Start()
12+
{
13+
HttpClient client = new();
14+
client.DefaultRequestHeaders.Add("User-Agent", "PolyMod");
15+
try
16+
{
17+
var json = JsonDocument.Parse(
18+
client.GetAsync("https://api.github.com/repos/PolyModdingTeam/PolyMod/releases/latest").UnwrapAsync()
19+
.Content.ReadAsStringAsync().UnwrapAsync()
20+
);
21+
if (
22+
new Version(json.RootElement.GetProperty("tag_name").GetString()!.TrimStart('v'))
23+
<=
24+
new Version(Plugin.VERSION)
25+
) return;
26+
void Update()
27+
{
28+
File.WriteAllBytes(
29+
Path.Combine(Plugin.BASE_PATH, "BepInEx", "plugins", "PolyMod.new.dll"),
30+
client.GetAsync(json.RootElement.GetProperty("assets")[0].GetProperty("browser_download_url").GetString()!).UnwrapAsync()
31+
.Content.ReadAsByteArrayAsync().UnwrapAsync()
32+
);
33+
ProcessStartInfo info = new()
34+
{
35+
WorkingDirectory = Path.Combine(Plugin.BASE_PATH, "BepInEx", "plugins"),
36+
CreateNoWindow = true,
37+
};
38+
if (Application.platform == RuntimePlatform.WindowsPlayer)
39+
{
40+
info.FileName = "cmd.exe";
41+
info.Arguments
42+
= "/C timeout 3 && del /F /Q PolyMod.dll && move /Y PolyMod.new.dll PolyMod.dll && start steam://rungameid/874390";
43+
}
44+
else
45+
{
46+
info.FileName = "/bin/bash";
47+
info.Arguments
48+
= "-c 'sleep 3 && rm -f PolyMod.dll && mv PolyMod.new.dll PolyMod.dll && xdg-open steam://rungameid/874390'";
49+
}
50+
Process.Start(info);
51+
Application.Quit();
52+
}
53+
PopupManager.GetBasicPopup(new(
54+
Localization.Get("polymod.version.autoupdate"),
55+
Localization.Get("polymod.version.autoupdate.description"),
56+
new(new PopupBase.PopupButtonData[] {
57+
new(
58+
"buttons.ok",
59+
PopupBase.PopupButtonData.States.None,
60+
(Il2CppSystem.Action)Update,
61+
closesPopup: false
62+
)
63+
}))
64+
).Show();
65+
}
66+
catch (Exception e)
67+
{
68+
Plugin.logger.LogError($"Failed to check updates: {e.Message}");
69+
}
70+
}
71+
72+
internal static void Init()
73+
{
74+
Harmony.CreateAndPatchAll(typeof(AutoUpdate));
75+
}
76+
}

src/Plugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public override void Load()
5454
Compatibility.Init();
5555

5656
Audio.Init();
57+
AutoUpdate.Init();
5758
Loc.Init();
5859
Visual.Init();
5960
Hub.Init();

src/Util.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ internal static Version Cast(this Il2CppSystem.Version self)
3030
return new(self.ToString());
3131
}
3232

33+
internal static T UnwrapAsync<T>(this Task<T> self)
34+
{
35+
return self.GetAwaiter().GetResult();
36+
}
37+
3338
internal static Version CutRevision(this Version self)
3439
{
3540
return new(self.Major, self.Minor, self.Build);

0 commit comments

Comments
 (0)