-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.cs
More file actions
135 lines (125 loc) · 4.69 KB
/
UpdateChecker.cs
File metadata and controls
135 lines (125 loc) · 4.69 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using Newtonsoft.Json;
using SpeedMann.PluginChecker.Models;
using SpeedMann.PluginChecker.Models.UStore;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Logger = Rocket.Core.Logging.Logger;
namespace SpeedMann.PluginChecker
{
public class UpdateChecker
{
public delegate void UpdateCheckCompletion(bool success, string newestVersion);
private const string StoreUrl = "https://unturnedstore.com/products/";
private string currentPluginVersion = "";
private string pluginName = "";
private string branchName = "";
private uint productId;
private string _storeVersion = "";
private bool requiresUpdate = false;
private bool loaded = false;
public UpdateChecker(string currentPluginVersion, string pluginName, uint productId, string branch = "master")
{
this.pluginName = pluginName;
this.currentPluginVersion = currentPluginVersion;
this.productId = productId;
branchName = branch;
}
public bool updateRequired(out string storeVersion)
{
storeVersion = _storeVersion;
return isStoreVersionLoaded(out _) && requiresUpdate;
}
public bool isStoreVersionLoaded(out string storeVersion)
{
storeVersion = _storeVersion;
return loaded;
}
public bool tryCheckPluginVersion(out string storeVersion)
{
loaded = false;
requiresUpdate = false;
storeVersion = _storeVersion;
string newestVersion = currentPluginVersion;
if (PluginInfoLoader.tryGetPluginInfo(productId, out Product pluginInfo))
{
checkVerionInner(pluginInfo, out newestVersion);
return true;
}
return false;
}
private bool checkVerionInner(Product pluginInfo, out string newestVersion)
{
newestVersion = "";
foreach (Branch b in pluginInfo.branches)
{
if (b.name == branchName)
{
for (int y = b.versions.Count - 1; y >= 0; y--)
{
if (b.versions[y].isEnabled)
{
_storeVersion = b.versions[y].name;
break;
}
}
break;
}
}
if (_storeVersion == "")
{
Logger.LogError($"newest store version of {pluginName} {branchName} was not found");
return false;
}
requiresUpdate = checkVersionNumbers(currentPluginVersion, _storeVersion, out newestVersion);
loaded = true;
if (requiresUpdate)
{
Logger.LogWarning($"{pluginName} {newestVersion} is available, update now: {StoreUrl + productId}");
return false;
}
Logger.Log($"{pluginName} {currentPluginVersion} is up to date");
return true;
}
private bool checkVersionNumbers(string currentVersion, string foundVersion, out string newestVersion)
{
newestVersion = currentVersion;
string[] currentVersionSplit = currentVersion.Split('.');
string[] foundVersionSplit = foundVersion.Split('.');
int biggestCount = currentVersionSplit.Length > foundVersionSplit.Length ? currentVersionSplit.Length : foundVersionSplit.Length;
for (int i = 0; i < biggestCount; i++)
{
int current, found;
if(i >= currentVersionSplit.Length)
{
current = 0;
}
else if (!int.TryParse(currentVersionSplit[i], out current))
{
Logger.LogError($"Failed to parse local version number {currentVersionSplit[i]} for {pluginName}!");
continue;
}
if (i >= foundVersionSplit.Length)
{
found = 0;
}
else if(!int.TryParse(foundVersionSplit[i], out found))
{
Logger.LogError($"Failed to parse remote version number {foundVersionSplit[i]} for {pluginName}!");
continue;
}
if (found > current)
{
newestVersion = foundVersion;
return true;
}
else if (found < current)
{
return false;
}
}
return false;
}
}
}