Skip to content

Commit 47e85da

Browse files
Refactor autoupdate cmds, error handling and improve version comparison logic
1 parent c61a53f commit 47e85da

2 files changed

Lines changed: 45 additions & 45 deletions

File tree

src/Managers/AutoUpdate.cs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private static void StartScreen_Start()
1515
if (!Plugin.config.autoUpdate) return;
1616
if (Environment.GetEnvironmentVariable("WINEPREFIX") != null)
1717
{
18-
Plugin.logger.LogWarning("Wine/Proton is not supported!");
18+
Plugin.logger.LogError("Autoupdate is not supported on Wine!");
1919
return;
2020
}
2121
HttpClient client = new();
@@ -34,20 +34,24 @@ private static void StartScreen_Start()
3434
latest = release;
3535
break;
3636
}
37-
if (
38-
new Version(latest?.GetProperty("tag_name").GetString()!.TrimStart('v')!.Split('-')[0]!)
39-
<=
40-
new Version(Plugin.VERSION.Split('-')[0])
41-
) return;
37+
string newVersion = latest?.GetProperty("tag_name").GetString()!.TrimStart('v')!;
38+
if (newVersion.IsVersionOlderOrEqual(Plugin.VERSION)) return;
4239
string os = Application.platform switch
4340
{
4441
RuntimePlatform.WindowsPlayer => "win",
4542
RuntimePlatform.LinuxPlayer => "linux",
4643
RuntimePlatform.OSXPlayer => "macos",
4744
_ => "unknown",
4845
};
49-
if (os == "unknown") return;
50-
string bepinex_url = client.GetAsync("https://polymod.dev/data/bepinex.txt").UnwrapAsync().Content.ReadAsStringAsync().UnwrapAsync().Replace("{os}", os);
46+
if (os == "unknown")
47+
{
48+
Plugin.logger.LogError("Unsupported platform for autoupdate!");
49+
return;
50+
}
51+
string bepinex_url = client
52+
.GetAsync("https://polymod.dev/data/bepinex.txt").UnwrapAsync()
53+
.Content.ReadAsStringAsync().UnwrapAsync()
54+
.Replace("{os}", os);
5155
void Update()
5256
{
5357
Time.timeScale = 0;
@@ -63,46 +67,28 @@ void Update()
6367
WorkingDirectory = Path.Combine(Plugin.BASE_PATH),
6468
CreateNoWindow = true,
6569
};
66-
Console.Write(5);
6770
if (Application.platform == RuntimePlatform.WindowsPlayer)
6871
{
69-
string batchPath = Path.Combine(Plugin.BASE_PATH, "update.bat");
70-
File.WriteAllText(batchPath, $@"
71-
@echo off
72-
echo Waiting for Polytopia.exe to exit...
73-
:waitloop
74-
tasklist | findstr /I ""Polytopia.exe"" >nul
75-
if not errorlevel 1 (
76-
timeout /T 1 >nul
77-
goto waitloop
78-
)
79-
80-
echo Updating...
81-
robocopy ""New"" . /E /MOVE /NFL /NDL /NJH /NJS /NP >nul
82-
rmdir /S /Q ""New""
83-
del /F /Q ""BepInEx\plugins\PolyMod.dll""
84-
move /Y ""PolyMod.new.dll"" ""BepInEx\plugins\PolyMod.dll""
85-
86-
echo Launching game...
87-
start steam://rungameid/874390
88-
timeout /T 3 /NOBREAK >nul
89-
exit
90-
");
9172
info.FileName = "cmd.exe";
92-
info.Arguments = $"/C start \"\" \"{batchPath}\"";
93-
info.WorkingDirectory = Plugin.BASE_PATH;
94-
info.CreateNoWindow = true;
95-
info.UseShellExecute = false;
73+
info.Arguments = "/C " +
74+
":waitloop & " +
75+
"tasklist | findstr /I \"Polytopia.exe\" >nul && (timeout /T 1 >nul & goto waitloop) & " +
76+
"robocopy \"New\" . /E /MOVE /NFL /NDL /NJH /NJS /NP >nul & " +
77+
"rmdir /S /Q \"New\" & " +
78+
"del /F /Q \"BepInEx\\plugins\\PolyMod.dll\" & " +
79+
"move /Y \"PolyMod.new.dll\" \"BepInEx\\plugins\\PolyMod.dll\" & " +
80+
"start steam://rungameid/874390";
9681
}
97-
else
82+
if (Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.OSXPlayer)
9883
{
9984
info.FileName = "/bin/bash";
100-
info.Arguments =
101-
"-c 'sleep 3" +
102-
" && mv -f New/* . && mv -f New/.* . 2>/dev/null || true && rm -rf New" +
103-
" && rm -f BepInEx/plugins/PolyMod.dll" +
104-
" && mv -f PolyMod.new.dll BepInEx/plugins/PolyMod.dll" +
105-
" && xdg-open steam://rungameid/874390'";
85+
info.Arguments = "-c \"" +
86+
"while pgrep -x Polytopia > /dev/null; do sleep 1; done && " +
87+
"mv New/* . && rm -rf New && " +
88+
"rm -f BepInEx/plugins/PolyMod.dll && " +
89+
"mv PolyMod.new.dll BepInEx/plugins/PolyMod.dll && " +
90+
"steam steam://rungameid/874390" +
91+
"\"";
10692
}
10793
Process.Start(info);
10894
Application.Quit();

src/Util.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Polytopia.Data;
77

88
namespace PolyMod;
9-
109
internal static class Util
1110
{
1211
internal static Il2CppSystem.Type WrapType<T>() where T : class
@@ -41,6 +40,23 @@ internal static Version CutRevision(this Version self)
4140
return new(self.Major, self.Minor, self.Build);
4241
}
4342

43+
internal static bool IsVersionOlderOrEqual(this string version1, string version2)
44+
{
45+
Version version1_ = new(version1.Split('-')[0]);
46+
Version version2_ = new(version2.Split('-')[0]);
47+
48+
if (version1_ < version2_) return true;
49+
if (version1_ > version2_) return false;
50+
51+
string pre1 = version1.Contains('-') ? version1.Split('-')[1] : "";
52+
string pre2 = version2.Contains('-') ? version2.Split('-')[1] : "";
53+
54+
if (string.IsNullOrEmpty(pre1) && !string.IsNullOrEmpty(pre2)) return false;
55+
if (!string.IsNullOrEmpty(pre1) && string.IsNullOrEmpty(pre2)) return true;
56+
57+
return string.Compare(pre1, pre2, StringComparison.Ordinal) <= 0;
58+
}
59+
4460
internal static string GetStyle(TribeData.Type tribe, SkinType skin)
4561
{
4662
return skin != SkinType.Default ? EnumCache<SkinType>.GetName(skin) : EnumCache<TribeData.Type>.GetName(tribe);
@@ -91,8 +107,6 @@ internal static string GetStyle(TribeData.Type tribe, SkinType skin)
91107
baseName = baseName.Replace(SpriteData.TILE_UNKNOWN, EnumCache<TerrainData.Type>.GetName(TerrainData.Type.Field));
92108
baseName = baseName.Replace(SpriteData.TILE_WATER, EnumCache<TerrainData.Type>.GetName(TerrainData.Type.Water));
93109
baseName = baseName.Replace(SpriteData.TILE_WETLAND, EnumCache<TerrainData.Type>.GetName(TerrainData.Type.Field) + "_flooded");
94-
95-
baseName = baseName.Replace("UI_", "");
96110
return baseName;
97111
}
98112
}

0 commit comments

Comments
 (0)