Skip to content

Commit f281680

Browse files
Switched to Uri.TryCreate to validate uri creation for modular mods. It now returns null if the uri fails to be created properly and will not crash the program when attempting to update.
1 parent de574c5 commit f281680

1 file changed

Lines changed: 70 additions & 65 deletions

File tree

SA-Mod-Manager/Updater/ModUpdater.cs

Lines changed: 70 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -223,90 +223,95 @@ public async Task<ModDownload> CheckModularVersion(ModInfo mod, string modsFolde
223223
mod.UpdateUrl += "/";
224224
}
225225

226-
var url = new Uri(mod.UpdateUrl);
227-
url = new Uri(url, "mod.ini");
228-
229-
ModInfo remoteInfo;
230-
231-
try
232-
{
233-
Dictionary<string, Dictionary<string, string>> dict = IniFile.Load(await client.GetStreamAsync(url));
234-
remoteInfo = IniSerializer.Deserialize<ModInfo>(dict);
235-
}
236-
catch (Exception ex)
237-
{
238-
errors.Add($"[{mod.Name}] Error pulling mod.ini from \"{mod.UpdateUrl}\": {ex.Message}");
239-
return null;
240-
}
241-
242-
if (!ForceUpdate && remoteInfo.Version == mod.Version)
226+
string url = mod.UpdateUrl + "mod.ini";
227+
Uri modQuery;
228+
if (Uri.TryCreate(url, new UriCreationOptions(), out modQuery))
243229
{
244-
return null;
245-
}
246-
247-
string manString;
230+
ModInfo remoteInfo;
248231

249-
try
250-
{
251-
manString = await client.GetStringAsync(new Uri(new Uri(mod.UpdateUrl), "mod.manifest"));
252-
}
253-
catch (Exception ex)
254-
{
255-
errors.Add($"[{mod.Name}] Error pulling mod.manifest from \"{mod.UpdateUrl}\": {ex.Message}");
256-
return null;
257-
}
258-
259-
List<ModManifestEntry> remoteManifest;
232+
try
233+
{
234+
Dictionary<string, Dictionary<string, string>> dict = IniFile.Load(await client.GetStreamAsync(url));
235+
remoteInfo = IniSerializer.Deserialize<ModInfo>(dict);
236+
}
237+
catch (Exception ex)
238+
{
239+
errors.Add($"[{mod.Name}] Error pulling mod.ini from \"{mod.UpdateUrl}\": {ex.Message}");
240+
return null;
241+
}
260242

261-
try
262-
{
263-
remoteManifest = ModManifest.FromString(manString);
264-
}
265-
catch (Exception ex)
266-
{
267-
errors.Add($"[{mod.Name}] Error parsing remote manifest from \"{mod.UpdateUrl}\": {ex.Message}");
268-
return null;
269-
}
243+
if (!ForceUpdate && remoteInfo.Version == mod.Version)
244+
{
245+
return null;
246+
}
270247

271-
List<ModManifestDiff> diff = ModManifestGenerator.Diff(remoteManifest, localManifest);
248+
string manString;
272249

273-
if (diff.Count < 1 || diff.All(x => x.State == ModManifestState.Unchanged))
274-
{
275-
return null;
276-
}
250+
try
251+
{
252+
manString = await client.GetStringAsync(new Uri(new Uri(mod.UpdateUrl), "mod.manifest"));
253+
}
254+
catch (Exception ex)
255+
{
256+
errors.Add($"[{mod.Name}] Error pulling mod.manifest from \"{mod.UpdateUrl}\": {ex.Message}");
257+
return null;
258+
}
277259

278-
string changes;
260+
List<ModManifestEntry> remoteManifest;
279261

280-
if (!string.IsNullOrEmpty(mod.ChangelogUrl))
281-
{
282262
try
283263
{
284-
changes = await client.GetStringAsync(new Uri(mod.ChangelogUrl));
264+
remoteManifest = ModManifest.FromString(manString);
285265
}
286266
catch (Exception ex)
287267
{
288-
changes = ex.Message;
268+
errors.Add($"[{mod.Name}] Error parsing remote manifest from \"{mod.UpdateUrl}\": {ex.Message}");
269+
return null;
289270
}
290-
}
291-
else
292-
{
293-
try
271+
272+
List<ModManifestDiff> diff = ModManifestGenerator.Diff(remoteManifest, localManifest);
273+
274+
if (diff.Count < 1 || diff.All(x => x.State == ModManifestState.Unchanged))
294275
{
295-
changes = await client.GetStringAsync(new Uri(new Uri(mod.UpdateUrl), "changelog.txt"));
276+
return null;
296277
}
297-
catch
278+
279+
string changes;
280+
281+
if (!string.IsNullOrEmpty(mod.ChangelogUrl))
298282
{
299-
// ignored
300-
changes = string.Empty;
283+
try
284+
{
285+
changes = await client.GetStringAsync(new Uri(mod.ChangelogUrl));
286+
}
287+
catch (Exception ex)
288+
{
289+
changes = ex.Message;
290+
}
291+
}
292+
else
293+
{
294+
try
295+
{
296+
changes = await client.GetStringAsync(new Uri(new Uri(mod.UpdateUrl), "changelog.txt"));
297+
}
298+
catch
299+
{
300+
// ignored
301+
changes = string.Empty;
302+
}
301303
}
302-
}
303304

304-
if (!string.IsNullOrEmpty(changes))
305-
{
306-
changes = Regex.Replace(changes, "(?<!\r)\n", "\r\n");
307-
}
305+
if (!string.IsNullOrEmpty(changes))
306+
{
307+
changes = Regex.Replace(changes, "(?<!\r)\n", "\r\n");
308+
}
308309

309-
return new ModDownload(mod, basePath == null ? Path.Combine(modsFolder, folder) : Path.Combine(basePath, modsFolder, folder), mod.UpdateUrl, changes, diff);
310+
return new ModDownload(mod, basePath == null ? Path.Combine(modsFolder, folder) : Path.Combine(basePath, modsFolder, folder), mod.UpdateUrl, changes, diff);
311+
}
312+
else
313+
return null;
314+
310315
}
311316

312317
// TODO: cancel

0 commit comments

Comments
 (0)