-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateChecker.cs
More file actions
208 lines (183 loc) · 7.49 KB
/
Copy pathUpdateChecker.cs
File metadata and controls
208 lines (183 loc) · 7.49 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Deadlock_Mod_Loader2
{
public class UpdateChecker
{
private static readonly HttpClient httpClient = new HttpClient();
private const string CURRENT_VERSION = "1.4"; // Update this with each release
private const string GITHUB_API_URL = "https://api.github.com/repos/Tylevo/DeadlockModManager/releases/latest";
private const string GITHUB_RELEASES_URL = "https://github.com/Tylevo/DeadlockModManager/releases";
private const string GAMEBANANA_PAGE_URL = "https://gamebanana.com/tools/20525"; // Update when you have the actual URL
private const string BACKUP_CHECK_URL = "https://raw.githubusercontent.com/Tylevo/DeadlockModManager/main/version.txt";
static UpdateChecker()
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("DeadlockModManager/1.4");
httpClient.Timeout = TimeSpan.FromSeconds(10);
}
public static async Task<UpdateInfo> CheckForUpdatesAsync()
{
try
{
var githubResult = await CheckGitHubUpdatesAsync();
if (githubResult != null) return githubResult;
var simpleResult = await CheckSimpleVersionAsync();
if (simpleResult != null) return simpleResult;
return new UpdateInfo
{
IsUpdateAvailable = false,
CurrentVersion = CURRENT_VERSION,
Message = "Could not check for updates automatically. Click 'Visit Page' to check manually.",
DownloadUrl = GAMEBANANA_PAGE_URL
};
}
catch (Exception ex)
{
return new UpdateInfo
{
IsUpdateAvailable = false,
CurrentVersion = CURRENT_VERSION,
Message = $"Update check failed: {ex.Message}",
DownloadUrl = GAMEBANANA_PAGE_URL
};
}
}
private static async Task<UpdateInfo> CheckGitHubUpdatesAsync()
{
try
{
var response = await httpClient.GetStringAsync(GITHUB_API_URL);
var release = JsonConvert.DeserializeObject<GitHubRelease>(response);
if (release != null && !string.IsNullOrEmpty(release.TagName))
{
string latestVersion = release.TagName.TrimStart('v'); // Remove 'v' prefix if present
bool isNewer = IsVersionNewer(latestVersion, CURRENT_VERSION);
return new UpdateInfo
{
IsUpdateAvailable = isNewer,
CurrentVersion = CURRENT_VERSION,
LatestVersion = latestVersion,
ReleaseNotes = release.Body,
DownloadUrl = release.HtmlUrl,
Message = isNewer
? $"Version {latestVersion} is available!"
: "You have the latest version."
};
}
}
catch
{
}
return null;
}
private static async Task<UpdateInfo> CheckSimpleVersionAsync()
{
try
{
var response = await httpClient.GetStringAsync(BACKUP_CHECK_URL);
var lines = response.Split('\n');
if (lines.Length > 0)
{
string latestVersion = lines[0].Trim();
string releaseNotes = lines.Length > 1 ? string.Join("\n", lines, 1, lines.Length - 1) : "";
bool isNewer = IsVersionNewer(latestVersion, CURRENT_VERSION);
return new UpdateInfo
{
IsUpdateAvailable = isNewer,
CurrentVersion = CURRENT_VERSION,
LatestVersion = latestVersion,
ReleaseNotes = releaseNotes,
DownloadUrl = GAMEBANANA_PAGE_URL,
Message = isNewer
? $"Version {latestVersion} is available!"
: "You have the latest version."
};
}
}
catch
{
}
return null;
}
private static bool IsVersionNewer(string latestVersion, string currentVersion)
{
try
{
var latest = new Version(NormalizeVersion(latestVersion));
var current = new Version(NormalizeVersion(currentVersion));
return latest > current;
}
catch
{
return false;
}
}
private static string NormalizeVersion(string version)
{
var parts = version.Split('.');
if (parts.Length == 1)
return version + ".0";
return version;
}
public static void ShowUpdateDialog(UpdateInfo updateInfo, IWin32Window owner = null)
{
if (updateInfo.IsUpdateAvailable)
{
var result = MessageBox.Show(
$"{updateInfo.Message}\n\n" +
$"Current Version: {updateInfo.CurrentVersion}\n" +
$"Latest Version: {updateInfo.LatestVersion}\n\n" +
$"Would you like to visit GitHub to download the update?",
"Update Available",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
try
{
System.Diagnostics.Process.Start(GITHUB_RELEASES_URL);
}
catch (Exception ex)
{
MessageBox.Show($"Could not open browser: {ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
else
{
MessageBox.Show(updateInfo.Message, "Update Check",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
public class UpdateInfo
{
public bool IsUpdateAvailable { get; set; }
public string CurrentVersion { get; set; }
public string LatestVersion { get; set; }
public string ReleaseNotes { get; set; }
public string DownloadUrl { get; set; }
public string Message { get; set; }
}
public class GitHubRelease
{
[JsonProperty("tag_name")]
public string TagName { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("body")]
public string Body { get; set; }
[JsonProperty("html_url")]
public string HtmlUrl { get; set; }
[JsonProperty("published_at")]
public DateTime PublishedAt { get; set; }
[JsonProperty("prerelease")]
public bool IsPrerelease { get; set; }
}
}