Skip to content

Commit 13c8934

Browse files
committed
Validate downloaded mods
1 parent f2d31c5 commit 13c8934

4 files changed

Lines changed: 49 additions & 4 deletions

File tree

Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
// You can specify all the values or you can default the Build and Revision Numbers
3535
// by using the '*' as shown below:
3636
// [assembly: AssemblyVersion("1.0.*")]
37-
[assembly: AssemblyVersion("18.05.1.*")]
37+
[assembly: AssemblyVersion("18.05.2.*")]
3838
[assembly: AssemblyFileVersion("1.0.0.0")]

src/GameModder.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,32 @@ public void DownloadMod(string url) {
132132
Directory.CreateDirectory(modRoot);
133133

134134
string modPath = Path.Combine(modRoot, Path.GetFileName(uri.AbsolutePath));
135-
if (File.Exists(modPath))
136-
File.Delete(modPath);
137135

138-
Console.WriteLine($"Downloading: {url}");
136+
Console.WriteLine($"Downloading mod");
139137

140138
OnStart?.Invoke();
141139
try {
142140

143141
byte[] zipData = _Download(url);
144142

143+
Console.WriteLine("Verifying");
144+
145+
using (MemoryStream ms = new MemoryStream(zipData))
146+
using (ZipArchive zip = new ZipArchive(ms)) {
147+
string name;
148+
if (!Info.VerifyMod(zip, out name)) {
149+
Console.WriteLine("Error: Invalid mod.");
150+
OnError?.Invoke(null);
151+
return;
152+
}
153+
if (!string.IsNullOrEmpty(name)) {
154+
modPath = Path.Combine(modRoot, name + ".zip");
155+
}
156+
}
157+
145158
Console.WriteLine("Writing data to file");
159+
if (File.Exists(modPath))
160+
File.Delete(modPath);
146161
File.WriteAllBytes(modPath, zipData);
147162

148163
} catch (Exception e) {

src/Infos/EverestInfo.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Text;
1212
using System.Text.RegularExpressions;
1313
using System.Threading;
14+
using System.IO.Compression;
1415

1516
namespace MonoMod.Installer.Everest {
1617
public partial class EverestInfo : GameModInfo {
@@ -247,5 +248,28 @@ private static ModVersion ParseLine(string line, string root) {
247248
return new ModVersion { Name = name, URL = url, Version = version };
248249
}
249250

251+
public override bool VerifyMod(ZipArchive zip, out string name) {
252+
name = null;
253+
bool valid = false;
254+
255+
foreach (ZipArchiveEntry entry in zip.Entries) {
256+
if (entry.FullName == "everest.yaml" ||
257+
entry.FullName == "everest.yml") {
258+
// Hack: Let's just read the first name.
259+
using (Stream stream = entry.Open())
260+
using (StreamReader reader = new StreamReader(stream))
261+
while (!reader.EndOfStream) {
262+
string line = reader.ReadLine().Trim();
263+
if (line.Contains("Name:") && name == null) {
264+
valid = true;
265+
name = line.Substring(line.IndexOf("Name:") + 5).Trim();
266+
}
267+
}
268+
}
269+
}
270+
271+
return valid;
272+
}
273+
250274
}
251275
}

src/Infos/GameModInfo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.IO;
5+
using System.IO.Compression;
56
using System.Linq;
67
using System.Net;
78
using System.Text;
@@ -92,6 +93,11 @@ public virtual string CurrentStatus {
9293

9394
public abstract void Install(Action<float> progress);
9495

96+
public virtual bool VerifyMod(ZipArchive zip, out string name) {
97+
name = null;
98+
return false;
99+
}
100+
95101
public class ModVersion {
96102
public string Name;
97103
public string URL;

0 commit comments

Comments
 (0)