Skip to content

Commit b011c26

Browse files
committed
ModApi.UpdateManager: use ProgressDialog for launcher kit update
1 parent 38bb89b commit b011c26

2 files changed

Lines changed: 140 additions & 117 deletions

File tree

ModApi.UpdateManager/DownloadClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void DownloadFile(string file)
7272
fileStream.Write(buffer, 0, bufferLength);
7373

7474
// only trigger event when percentage has changed
75-
percentage = (int)(totalBytesRead / streamLength * 100);
75+
percentage = (int)((double)totalBytesRead / (double)streamLength * 100.0);
7676
if (percentageDownloaded != percentage)
7777
{
7878
percentageDownloaded = percentage;

ModApi.UpdateManager/UpdateManager.cs

Lines changed: 139 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -134,173 +134,196 @@ public static void CheckForUpdates()
134134
}
135135
}
136136

137-
if (!File.Exists(UpdaterBlockPath))
137+
if (File.Exists(UpdaterBlockPath))
138138
{
139-
try
140-
{
141-
List<Exception> exceptions = new List<Exception>();
142-
bool didDownload = false;
139+
// don't check for updates when block file exists
140+
return;
141+
}
142+
143+
try
144+
{
145+
List<Exception> exceptions = new List<Exception>();
146+
bool didDownload = false;
143147

144-
// Try to download the update info file from the override path first
145-
if (File.Exists(UpdaterOverridePath))
148+
// Try to download the update info file from the override path first
149+
if (File.Exists(UpdaterOverridePath))
150+
{
151+
PathPrefix = File.ReadAllText(UpdaterOverridePath);
152+
153+
// remove override if the URL is in our URL list
154+
foreach (string url in LauncherKitUpdateUrls)
146155
{
147-
PathPrefix = File.ReadAllText(UpdaterOverridePath);
156+
if (url == PathPrefix)
157+
{
158+
File.Delete(UpdaterOverridePath);
159+
break;
160+
}
161+
}
148162

149-
// remove override if the URL is in our URL list
150-
foreach (string url in LauncherKitUpdateUrls)
163+
try
164+
{
165+
using (var downloadClient = new DownloadClient(Path.Combine(PathPrefix, "update.info")))
151166
{
152-
if (url == PathPrefix)
153-
{
154-
File.Delete(UpdaterOverridePath);
155-
break;
156-
}
167+
downloadClient.SetTimeout(TimeSpan.FromSeconds(15));
168+
downloadClient.DownloadFile(UpdateInfoDestPath);
157169
}
158170

171+
// Hides exceptions if the download was successful
172+
didDownload = true;
173+
}
174+
catch (Exception ex)
175+
{
176+
exceptions.Add(ex);
177+
}
178+
}
179+
// Try to download the update info file from each URL in the list
180+
else
181+
{
182+
foreach (string url in LauncherKitUpdateUrls)
183+
{
159184
try
160185
{
161-
using (var downloadClient = new DownloadClient(Path.Combine(PathPrefix, "update.info")))
186+
using (var downloadClient = new DownloadClient(Path.Combine(url, "update.info")))
162187
{
163-
downloadClient.SetTimeout(TimeSpan.FromSeconds(10));
188+
downloadClient.SetTimeout(TimeSpan.FromSeconds(15));
164189
downloadClient.DownloadFile(UpdateInfoDestPath);
165190
}
166191

167192
// Hides exceptions if the download was successful
168193
didDownload = true;
194+
PathPrefix = url;
195+
break;
169196
}
170197
catch (Exception ex)
171198
{
172199
exceptions.Add(ex);
173200
}
174201
}
175-
// Try to download the update info file from each URL in the list
176-
else
177-
{
178-
foreach (string url in LauncherKitUpdateUrls)
179-
{
180-
try
181-
{
182-
using (var downloadClient = new DownloadClient(Path.Combine(PathPrefix, "update.info")))
183-
{
184-
downloadClient.SetTimeout(TimeSpan.FromSeconds(10));
185-
downloadClient.DownloadFile(UpdateInfoDestPath);
186-
}
202+
}
187203

188-
// Hides exceptions if the download was successful
189-
didDownload = true;
190-
PathPrefix = url;
191-
break;
192-
}
193-
catch (Exception ex)
194-
{
195-
exceptions.Add(ex);
196-
}
197-
}
198-
}
204+
// If no download was successful, show all exceptions, one at a time
205+
if (!didDownload)
206+
{
207+
ShowUpdateCheckFailedMessage(exceptions);
208+
209+
// early return when failed
210+
return;
211+
}
199212

200-
// If no download was successful, show all exceptions, one at a time
201-
if (!didDownload)
213+
if (File.Exists(UpdateInfoDestPath))
214+
{
215+
var updateInfoLines = File.ReadAllLines(UpdateInfoDestPath);
216+
if (Version.TryParse(updateInfoLines[0], out Version ModApiSetupVersion) &&
217+
ModApiSetupVersion == new Version(1, 0, 0, 0))
202218
{
203-
foreach (var ex in exceptions)
219+
if (Version.Parse(updateInfoLines[1]) > CurrentVersion)
204220
{
205-
ShowUpdateCheckFailedMessage(ex);
206-
}
221+
string versionString = "Current version: " + CurrentVersion + "\nNew version: " + updateInfoLines[1];
207222

208-
// early return when failed
209-
return;
210-
}
211-
212-
if (File.Exists(UpdateInfoDestPath))
213-
{
214-
var updateInfoLines = File.ReadAllLines(UpdateInfoDestPath);
215-
if (Version.TryParse(updateInfoLines[0], out Version ModApiSetupVersion) &&
216-
ModApiSetupVersion == new Version(1, 0, 0, 0))
217-
{
218-
if (Version.Parse(updateInfoLines[1]) > CurrentVersion)
223+
if (MessageBox.Show("An update to the Spore ModAPI Launcher Kit is now available. Would you like to install it now?\n\n" + versionString, "Update Available", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
219224
{
220-
string versionString = "Current version: " + CurrentVersion + "\nNew version: " + updateInfoLines[1];
221-
222-
if (MessageBox.Show("An update to the Spore ModAPI Launcher Kit is now available. Would you like to install it now?\n\n" + versionString, "Update Available", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
225+
if (bool.Parse(updateInfoLines[2]))
223226
{
224-
if (bool.Parse(updateInfoLines[2]))
225-
{
226-
Process.Start(updateInfoLines[3]);
227-
}
228-
else
229-
{
230-
231-
if (File.Exists(UpdaterDestPath))
232-
File.Delete(UpdaterDestPath);
233-
234-
var downloadClient = new DownloadClient(updateInfoLines[3]);
235-
downloadClient.SetTimeout(TimeSpan.FromMinutes(5));
236-
downloadClient.DownloadFile(UpdaterDestPath);
237-
238-
if (File.Exists(UpdaterDestPath))
227+
Process.Start(updateInfoLines[3]);
228+
}
229+
else
230+
{
231+
var dialog = new ProgressDialog(
232+
"Spore ModAPI Launcher Kit is updating to " + updateInfoLines[1],
233+
"Spore ModAPI Launcher Kit updating",
234+
(s, e) =>
239235
{
240-
if (new FileInfo(UpdaterDestPath).Length > 0)
236+
try
241237
{
242-
var args = Environment.GetCommandLineArgs().ToList();
238+
using (var downloadClient = new DownloadClient(updateInfoLines[3]))
239+
{
240+
downloadClient.DownloadProgressChanged += (_, progress) =>
241+
{
242+
(s as BackgroundWorker).ReportProgress((int)(progress * 0.9f));
243+
};
244+
245+
downloadClient.SetTimeout(TimeSpan.FromMinutes(5));
246+
downloadClient.DownloadFile(UpdaterDestPath);
247+
}
248+
249+
if (File.Exists(UpdaterDestPath))
250+
{
251+
var args = Environment.GetCommandLineArgs().ToList();
243252

244-
string currentArgs = string.Empty;
245-
foreach (string s in args)
246-
currentArgs += "\"" + s.TrimEnd('\\') + "\" ";
253+
string currentArgs = string.Empty;
254+
foreach (string arg in args)
255+
currentArgs += "\"" + arg.TrimEnd('\\') + "\" ";
247256

248-
string argOnePath = Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString().TrimEnd('\\');
249-
if (!argOnePath.EndsWith(" "))
250-
argOnePath = argOnePath + " ";
257+
string argOnePath = Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString().TrimEnd('\\');
258+
if (!argOnePath.EndsWith(" "))
259+
argOnePath = argOnePath + " ";
251260

252-
Process.Start(UpdaterDestPath, "\"" + argOnePath + "\" " + currentArgs);
253-
Process.GetCurrentProcess().Kill();
261+
Process.Start(UpdaterDestPath, "\"" + argOnePath + "\" " + currentArgs);
262+
Process.GetCurrentProcess().Kill();
263+
}
254264
}
255-
else
265+
catch (Exception ex)
256266
{
257-
File.Delete(UpdaterDestPath);
267+
ShowUpdateCheckFailedMessage(new List<Exception>() { ex });
258268
}
259-
}
260-
}
269+
});
270+
dialog.ShowDialog();
261271
}
262-
263272
}
264273
}
265-
else
266-
ShowUnrecognizedUpdateInfoVersionMessage();
267274
}
268275
else
269-
{
270-
File.Delete(UpdateInfoDestPath);
271-
}
276+
ShowUnrecognizedUpdateInfoVersionMessage();
277+
}
272278

273-
if (DllsUpdater.HasDllsUpdate(out var githubRelease))
279+
if (DllsUpdater.HasDllsUpdate(out var githubRelease))
280+
{
281+
var result = MessageBox.Show(CommonStrings.DllsUpdateAvailable, CommonStrings.DllsUpdateAvailableTitle, MessageBoxButton.YesNo);
282+
if (result == MessageBoxResult.Yes)
274283
{
275-
var result = MessageBox.Show(CommonStrings.DllsUpdateAvailable, CommonStrings.DllsUpdateAvailableTitle, MessageBoxButton.YesNo);
276-
if (result == MessageBoxResult.Yes)
277-
{
278-
var dialog = new ProgressDialog(
279-
CommonStrings.UpdatingDllsDialog + githubRelease.tag_name,
280-
CommonStrings.UpdatingDllsDialogTitle,
281-
(s, e) =>
284+
var dialog = new ProgressDialog(
285+
CommonStrings.UpdatingDllsDialog + githubRelease.tag_name,
286+
CommonStrings.UpdatingDllsDialogTitle,
287+
(s, e) =>
288+
{
289+
DllsUpdater.UpdateDlls(githubRelease, progress =>
282290
{
283-
DllsUpdater.UpdateDlls(githubRelease, progress =>
284-
{
285-
(s as BackgroundWorker).ReportProgress(progress);
286-
});
291+
(s as BackgroundWorker).ReportProgress(progress);
287292
});
288-
dialog.ShowDialog();
289-
}
293+
});
294+
dialog.ShowDialog();
290295
}
291-
292-
File.WriteAllText(LastUpdateCheckTimePath, DateTime.Now.ToString(LastUpdateDateTimeFormat));
293-
}
294-
catch (Exception ex)
295-
{
296-
ShowUpdateCheckFailedMessage(ex);
297296
}
297+
298+
File.WriteAllText(LastUpdateCheckTimePath, DateTime.Now.ToString(LastUpdateDateTimeFormat));
299+
}
300+
catch (Exception ex)
301+
{
302+
ShowUpdateCheckFailedMessage(new List<Exception>() { ex });
298303
}
299304
}
300305

301-
static void ShowUpdateCheckFailedMessage(Exception ex)
306+
static void ShowUpdateCheckFailedMessage(List<Exception> exceptions)
302307
{
303-
MessageBox.Show("The Launcher Kit could not connect to the update service. Try again in a few minutes, or check https://launcherkit.sporecommunity.com/support for help.\n\nCurrent version: "+ CurrentVersion + "\n\n" + ex.ToString());
308+
// show simplified exceptions
309+
// to prevent a big error dialog
310+
string exceptionText = "";
311+
foreach (var ex in exceptions)
312+
{
313+
exceptionText += ex.GetType().ToString() + ": " + ex.Message + "\n";
314+
if (ex.InnerException != null)
315+
{
316+
exceptionText += ex.InnerException.GetType().ToString() + ": " + ex.InnerException.Message + "\n";
317+
if (ex.InnerException.InnerException != null)
318+
{
319+
exceptionText += ex.InnerException.InnerException.GetType().ToString() + ": " + ex.InnerException.InnerException.Message + "\n";
320+
}
321+
}
322+
exceptionText += "\n";
323+
324+
}
325+
326+
MessageBox.Show("The Launcher Kit could not connect to the update service. Try again in a few minutes, or check https://launcherkit.sporecommunity.com/support for help.\n\nCurrent version: "+ CurrentVersion + "\n\n" + exceptionText);
304327
}
305328

306329
static void ShowUnrecognizedUpdateInfoVersionMessage()

0 commit comments

Comments
 (0)