forked from tyzeron/LethalCompanyMinimap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionChecker.cs
101 lines (92 loc) · 3.5 KB
/
VersionChecker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// ----------------------------------------------------------------------
// Copyright (c) Tyzeron. All Rights Reserved.
// Licensed under the GNU Affero General Public License, Version 3
// ----------------------------------------------------------------------
using System.Net.Http;
using System.Threading.Tasks;
using System;
using System.Runtime.InteropServices;
namespace LethalCompanyMinimap
{
public class VersionChecker
{
public static string latestVersion = null;
private static async Task<bool> GetVersionFromUrlAsync(string url)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", BuildUserAgentString());
try
{
string response = await client.GetStringAsync(url);
latestVersion = ParseTagNameFromJson(response);
return true;
}
catch (Exception e)
{
MinimapMod.mls.LogError($"Failed to get the latest version from {url} for {MinimapMod.modName} Mod. Error: {e.Message}");
return false;
}
}
}
public static async Task GetLatestVersionAsync()
{
string mainUrl = "http://lethalminimap.tyzeron.com";
string fallbackUrl = $"https://api.github.com/repos/{MinimapMod.modRepository}/releases/latest";
if (!await GetVersionFromUrlAsync(mainUrl))
{
await GetVersionFromUrlAsync(fallbackUrl);
}
}
private static string ParseTagNameFromJson(string jsonString)
{
string tagNameToken = "\"tag_name\":\"";
int index = jsonString.IndexOf(tagNameToken);
if (index != -1)
{
int startIndex = index + tagNameToken.Length;
int endIndex = jsonString.IndexOf("\"", startIndex);
if (endIndex != -1)
{
string versionNumber = jsonString.Substring(startIndex, endIndex - startIndex);
return LStrip(versionNumber, 'v');
}
}
return null;
}
private static string LStrip(string input, char charToStrip)
{
int startIndex = 0;
while (startIndex < input.Length && input[startIndex] == charToStrip)
{
startIndex++;
}
return input.Substring(startIndex);
}
private static string BuildUserAgentString()
{
string osInfo = GetOperatingSystemInfo();
string architecture = Environment.Is64BitOperatingSystem ? "x64" : "x86";
return $"{MinimapMod.modGUID}/{MinimapMod.modVersion} ({osInfo}; {architecture})";
}
private static string GetOperatingSystemInfo()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return $"Windows NT {Environment.OSVersion.Version}";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return $"Mac OS X {Environment.OSVersion.Version}";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return $"Linux {Environment.OSVersion.Version}";
}
else
{
return $"Unknown OS {Environment.OSVersion.Version}";
}
}
}
}