11using System . Diagnostics ;
2+ using System . IO . Compression ;
23using System . Text . Json ;
34using HarmonyLib ;
45using UnityEngine ;
56
67namespace PolyMod . Managers ;
8+
79internal static class AutoUpdate
810{
911 [ HarmonyPostfix ]
1012 [ HarmonyPatch ( typeof ( StartScreen ) , nameof ( StartScreen . Start ) ) ]
1113 private static void StartScreen_Start ( )
1214 {
1315 if ( ! Plugin . config . autoUpdate ) return ;
16+ if ( Environment . GetEnvironmentVariable ( "WINEPREFIX" ) != null )
17+ {
18+ Plugin . logger . LogWarning ( "Wine/Proton is not supported!" ) ;
19+ return ;
20+ }
1421 HttpClient client = new ( ) ;
1522 client . DefaultRequestHeaders . Add ( "User-Agent" , "PolyMod" ) ;
1623 try
@@ -32,29 +39,53 @@ private static void StartScreen_Start()
3239 <=
3340 new Version ( Plugin . VERSION )
3441 ) return ;
42+ Console . WriteLine ( latest ? . GetProperty ( "assets" ) [ 0 ] . GetProperty ( "browser_download_url" ) . GetString ( ) ! ) ;
43+ string bepinex_version = client . GetAsync ( "https://polymod.dev/data/bepinex.txt" ) . UnwrapAsync ( ) . Content . ReadAsStringAsync ( ) . UnwrapAsync ( ) ;
44+ string os = Application . platform switch
45+ {
46+ RuntimePlatform . WindowsPlayer => "win" ,
47+ RuntimePlatform . LinuxPlayer => "linux" ,
48+ RuntimePlatform . OSXPlayer => "macos" ,
49+ _ => "unknown" ,
50+ } ;
51+ if ( os == "unknown" ) return ;
52+ bepinex_version = bepinex_version . Replace ( "{os}" , os ) ;
53+ Console . WriteLine ( bepinex_version ) ;
3554 void Update ( )
3655 {
56+ Time . timeScale = 0 ;
3757 File . WriteAllBytes (
38- Path . Combine ( Plugin . BASE_PATH , "BepInEx" , "plugins" , " PolyMod.new.dll") ,
58+ Path . Combine ( Plugin . BASE_PATH , "PolyMod.new.dll" ) ,
3959 client . GetAsync ( latest ? . GetProperty ( "assets" ) [ 0 ] . GetProperty ( "browser_download_url" ) . GetString ( ) ! ) . UnwrapAsync ( )
4060 . Content . ReadAsByteArrayAsync ( ) . UnwrapAsync ( )
4161 ) ;
62+ using ZipArchive bepinex = new ( client . GetAsync ( bepinex_version ) . UnwrapAsync ( ) . Content . ReadAsStream ( ) ) ;
63+ bepinex . ExtractToDirectory ( Path . Combine ( Plugin . BASE_PATH , "New" ) ) ;
4264 ProcessStartInfo info = new ( )
4365 {
44- WorkingDirectory = Path . Combine ( Plugin . BASE_PATH , "BepInEx" , "plugins" ) ,
66+ WorkingDirectory = Path . Combine ( Plugin . BASE_PATH ) ,
4567 CreateNoWindow = true ,
4668 } ;
4769 if ( Application . platform == RuntimePlatform . WindowsPlayer )
4870 {
4971 info . FileName = "cmd.exe" ;
50- info . Arguments
51- = "/C timeout 3 && del /F /Q PolyMod.dll && move /Y PolyMod.new.dll PolyMod.dll && start steam://rungameid/874390" ;
72+ info . Arguments =
73+ "/C timeout 3" +
74+ " && robocopy \" New\" . /E /MOVE /NFL /NDL /NJH /NJS /NP" +
75+ " && rmdir /S /Q \" New\" " +
76+ " && del /F /Q \" BepInEx\\ plugins\\ PolyMod.dll\" " +
77+ " && move /Y \" PolyMod.new.dll\" \" BepInEx\\ plugins\\ PolyMod.dll\" " +
78+ " && start steam://rungameid/874390" ;
5279 }
5380 else
5481 {
5582 info . FileName = "/bin/bash" ;
56- info . Arguments
57- = "-c 'sleep 3 && rm -f PolyMod.dll && mv PolyMod.new.dll PolyMod.dll && xdg-open steam://rungameid/874390'" ;
83+ info . Arguments =
84+ "-c 'sleep 3" +
85+ " && mv -f New/* . && mv -f New/.* . 2>/dev/null || true && rm -rf New" +
86+ " && rm -f BepInEx/plugins/PolyMod.dll" +
87+ " && mv -f PolyMod.new.dll BepInEx/plugins/PolyMod.dll" +
88+ " && xdg-open steam://rungameid/874390'" ;
5889 }
5990 Process . Start ( info ) ;
6091 Application . Quit ( ) ;
@@ -66,8 +97,7 @@ void Update()
6697 new (
6798 "polymod.autoupdate.update" ,
6899 PopupBase . PopupButtonData . States . None ,
69- ( Il2CppSystem . Action ) Update ,
70- closesPopup : false
100+ ( Il2CppSystem . Action ) Update
71101 )
72102 } ) )
73103 ) . Show ( ) ;
0 commit comments