-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
199 lines (181 loc) · 7.01 KB
/
Program.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
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
using System;
using System.IO;
using Octokit;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using System.Net;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
namespace UmbraInjector
{
static class Program
{
public static bool updateAvailable;
public static string latestVersion;
public static string currentVersion = null;
public static bool devBuild;
public static bool upToDate = true;
public static bool rateLimited = false;
public static bool filePresent;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new MainForm());
}
public static void UnzipFromStream(Stream zipStream, string outFolder)
{
using (var zipInputStream = new ZipInputStream(zipStream))
{
while (zipInputStream.GetNextEntry() is ZipEntry zipEntry)
{
var entryFileName = zipEntry.Name;
var buffer = new byte[4096];
var fullZipToPath = Path.Combine(outFolder, entryFileName);
var directoryName = Path.GetDirectoryName(fullZipToPath);
if (directoryName.Length > 0)
Directory.CreateDirectory(directoryName);
if (Path.GetFileName(fullZipToPath).Length == 0)
{
continue;
}
using (FileStream streamWriter = File.Create(fullZipToPath))
{
StreamUtils.Copy(zipInputStream, streamWriter, buffer);
}
}
}
}
public static void FilePresent()
{
if (currentVersion != null)
{
filePresent = true;
}
else
{
filePresent = false;
}
}
public static void DeleteFile()
{
if (filePresent)
{
File.Delete($"Data/UmbraMenu/{SearchingForProcessForm.GetDLLName()}");
}
else
{
return;
}
}
public static void DownloadUpdate()
{
DeleteFile();
if (rateLimited)
{
MessageBox.Show("You have been rate limited by Github's API and cannot automatically download the latest version. Downloading v2.0.5 instead.");
using (WebClient client = new WebClient())
{
using (var data = new WebClient().OpenRead($"https://github.com/Aquatic-Labs/Umbra-Mod-Menu/releases/download/2.0.5/UmbraMenu-v2.0.5.zip"))
{
// This stream cannot be opened with the ZipFile class because CanSeek is false.
Program.UnzipFromStream(data, $"Data/UmbraMenu");
}
}
upToDate = true;
Thread.Sleep(2000);
}
else
{
using (WebClient client = new WebClient())
{
using (var data = new WebClient().OpenRead($"https://github.com/Aquatic-Labs/Umbra-Mod-Menu/releases/latest/download/UmbraMenu-v{latestVersion}.zip"))
{
// This stream cannot be opened with the ZipFile class because CanSeek is false.
Program.UnzipFromStream(data, $"Data/UmbraMenu");
}
}
upToDate = true;
Thread.Sleep(1000);
}
CheckForUpdate();
}
public static async void CheckForUpdate()
{
var client = new GitHubClient(new ProductHeaderValue("UmbraInjectorUpdateCheck"));
var currentFiles = Directory.GetFiles("Data/UmbraMenu/");
foreach (string fileName in currentFiles)
{
if (fileName.StartsWith("Data/UmbraMenu/UmbraMenu-v"))
{
string temp = fileName.Replace("Data/UmbraMenu/UmbraMenu-v", "");
currentVersion = temp?.Replace(".dll", "");
}
}
try
{
var releases = await client.Repository.Release.GetAll("Aquatic-Labs", "Umbra-Mod-Menu").ConfigureAwait(false);
var latest = releases[0];
latestVersion = latest.TagName;
if (currentVersion != null)
{
string[] versionSplit = currentVersion.Split('.');
string[] latestVersionSplit = latestVersion.Split('.');
for (int i = 0; i < versionSplit.Length; i++)
{
int versionNumber = int.Parse(versionSplit[i]);
int latestVersionNumber = int.Parse(latestVersionSplit[i]);
if (versionNumber < latestVersionNumber)
{
upToDate = false;
devBuild = false;
updateAvailable = true;
break;
}
else if (versionNumber > latestVersionNumber)
{
upToDate = false;
devBuild = true;
updateAvailable = false;
break;
}
}
}
else
{
string[] versionSplit = "0.0.0".Split('.');
string[] latestVersionSplit = latestVersion.Split('.');
for (int i = 0; i < versionSplit.Length; i++)
{
int versionNumber = int.Parse(versionSplit[i]);
int latestVersionNumber = int.Parse(latestVersionSplit[i]);
if (versionNumber < latestVersionNumber)
{
upToDate = false;
devBuild = false;
updateAvailable = true;
break;
}
else if (versionNumber > latestVersionNumber)
{
upToDate = false;
devBuild = true;
updateAvailable = false;
break;
}
}
}
}
catch (RateLimitExceededException)
{
rateLimited = true;
upToDate = true;
}
}
}
}