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+ }
0 commit comments