-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.cs
266 lines (238 loc) · 8.77 KB
/
Logic.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Newtonsoft.Json;
using Octokit;
using System;
using System.IO;
using System.Net;
namespace MW5LOMLauncherV2
{
public class Logic
{
public static Logic logic;
private ProgramData ProgramData = new ProgramData();
private string ProgramDataPath = "";
private string LatestVersion = "0";
private bool GithubUnreachable = false;
public Logic(OutputForm form1)
{
Logic.logic = this;
this.ProgramDataPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
@"\MW5LoadOrderManager";
try
{
GetLatestVersionFromGitHub();
}
catch (Exception e)
{
form1.listBox1.Items.Add("Github unreachable for update trying to launch from existing executable...");
Console.WriteLine("Github unreachable for update trying to launch from existing executable...");
GithubUnreachable = true;
this.LatestVersion = "0";
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
LoadGenProgramData();
if (!ExeExists())
{
if (HandleNoExeFound(form1))
{
return;
}
}
if (!GithubUnreachable)
{
form1.listBox1.Items.Add("Checking for updates...");
form1.listBox1.Items.Add(String.Format("The latest release is tagged at {0} we are running {1}",
LatestVersion,
this.ProgramData.version));
Console.WriteLine("Checking for updates...");
Console.WriteLine(
"The latest release is tagged at {0} we are running {1}",
LatestVersion,
this.ProgramData.version);
}
if (float.Parse(this.LatestVersion) > this.ProgramData.version)
{
form1.listBox1.Items.Add("A new version is available, starting update...");
Console.WriteLine("A new version is available, starting update...");
if (Update())
{
form1.listBox1.Items.Add("Done, starting MW5 Load Order Manager");
Console.WriteLine("Done, starting MW5 Mod Loader Manager");
}
else
{
form1.listBox1.Items.Add("Update failed trying to launch present Exe");
Console.WriteLine("Update failed trying to launch present Exe");
}
}
else
{
form1.listBox1.Items.Add("We are at the latest version, starting MW5 Load Order Manager");
Console.WriteLine("We are at the latest version, starting MW5 Mod Loader Manager");
}
UpdateProgamDataFile();
form1.timer1.Enabled = true;
form1.timer1.Start();
}
/// <summary>
///
/// </summary>
/// <param name="form1"></param>
/// <returns>bool succes</returns>
private bool HandleNoExeFound(OutputForm form1)
{
form1.listBox1.Items.Add("No executable found! Trying to update...");
Console.WriteLine("No executable found! Trying to update...");
if (GithubUnreachable)
{
form1.listBox1.Items.Add("Update failed closing program.");
Console.WriteLine("Update failed closing program.");
form1.Close();
Environment.Exit(0);
return false;
}
if (Update())
{
form1.listBox1.Items.Add("Done, starting MW5 Load Order Manager");
Console.WriteLine("Done, starting MW5 Mod Loader Manager");
UpdateProgamDataFile();
form1.timer1.Enabled = true;
form1.timer1.Start();
return true;
}
return false;
}
/// <summary>
///
///
/// </summary>
/// <returns>bool succes</returns>
private bool Update()
{
if (GithubUnreachable)
{
return false;
}
try
{
using (var webClient = new WebClient())
{
string url = String.Format("https://github.com/rjtwins/MW5-Mod-Manager/releases/download/{0}/MW5.Mod.Manager.exe", this.LatestVersion);
webClient.DownloadFile(url, this.ProgramDataPath + @"\MW5 Mod Manager.exe");
}
}catch(Exception e)
{
return false;
}
this.ProgramData.version = float.Parse(this.LatestVersion);
return true;
}
private bool UpdateProgamDataFile()
{
try
{
//Update program data
JsonSerializer serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
using (StreamWriter sw = new StreamWriter(ProgramDataPath + @"\ProgramData.json"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, this.ProgramData);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
return true;
}
private void GetLatestVersionFromGitHub()
{
var client = new GitHubClient(new ProductHeaderValue("MW5LoadOrderManagerUpdater"));
this.LatestVersion = client.Repository.Release.GetLatest("rjtwins", "MW5-Mod-Manager").Result.TagName;
}
internal static void StartMainProgram()
{
//Start the main program
System.Diagnostics.Process.Start(Logic.logic.ProgramDataPath + @"\MW5 Mod Manager.exe");
}
private void HandleNoProgramDataFile()
{
//Load install dir from previous session:
if (File.Exists(ProgramDataPath + @"\ProgramData.json"))
{
return;
}
CreateEmptyProgramDataFile();
}
private void CreateEmptyProgramDataFile()
{
System.IO.Directory.CreateDirectory(ProgramDataPath);
System.IO.File.Create(ProgramDataPath + @"\ProgramData.json").Close();
this.ProgramData.vendor = "";
this.ProgramData.installdir = new string[2] { "", "" };
this.ProgramData.version = 0f;
Console.WriteLine(ProgramDataPath + @"\ProgramData.json was not found, created one with empty values.");
UpdateProgamDataFile();
}
/// <summary>
///
/// </summary>
/// <returns>bool succes</returns>
private bool TryReadProgramData()
{
try
{
ReadProgramData();
}
catch (Exception e)
{
Console.WriteLine(ProgramDataPath + @"\ProgramData.json unreadable or has no content.");
return false;
}
return true;
}
private void ReadProgramData()
{
string json = File.ReadAllText(ProgramDataPath + @"\ProgramData.json");
Console.WriteLine(json);
this.ProgramData = JsonConvert.DeserializeObject<ProgramData>(json);
if(this.ProgramData == null)
{
this.ProgramData = new ProgramData();
throw new Exception("ProgramData was read but returned null");
}
}
public void LoadGenProgramData()
{
HandleNoProgramDataFile();
if (!TryReadProgramData())
{
CreateEmptyAndTryReadProgramData();
}
if (VersionOutOfRange())
{
Console.WriteLine("Version outside of possible range. Setting version to v.0.");
this.ProgramData.version = 0f;
}
bool VersionOutOfRange()
{
return this.ProgramData.version <= 0f;
}
}
private void CreateEmptyAndTryReadProgramData()
{
CreateEmptyProgramDataFile();
if (!TryReadProgramData())
{
throw new Exception("Unable to read or create program data from " + this.ProgramDataPath + @"\ProgramData.json");
}
}
public bool ExeExists()
{
return File.Exists(ProgramDataPath + @"\MW5 Mod Manager.exe");
}
}
}