Skip to content

Commit 95eb69d

Browse files
committed
fix: ArgumentException when a duplicated manifest file is in a jar file
1 parent 54d9e7f commit 95eb69d

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

CmlLib.Core.Installer.Forge/CmlLib.Core.Installer.Forge.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<PropertyGroup>
12-
<Version>0.0.2</Version>
12+
<Version>0.0.3</Version>
1313
<Description>Minecraft Forge Installer</Description>
1414
<Copyright>Copyright (c) 2023 CmlLib</Copyright>
1515
<PackageProjectUrl>https://github.com/CmlLib/CmlLib.Core.Installer.Forge</PackageProjectUrl>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using ICSharpCode.SharpZipLib.Zip;
2+
using System.Text;
3+
4+
namespace CmlLib.Core.Installer.Forge;
5+
6+
public class JarFile
7+
{
8+
public JarFile(string path)
9+
{
10+
this.Path = path;
11+
}
12+
13+
public string Path { get; private set; }
14+
15+
public Dictionary<string, string?>? GetManifest()
16+
{
17+
string? manifest = null;
18+
19+
using (var fs = File.OpenRead(Path))
20+
using (var s = new ZipInputStream(fs))
21+
{
22+
ZipEntry e;
23+
while ((e = s.GetNextEntry()) != null)
24+
{
25+
if (e.Name == "META-INF/MANIFEST.MF")
26+
{
27+
manifest = readStreamString(s);
28+
break;
29+
}
30+
}
31+
}
32+
33+
if (string.IsNullOrEmpty(manifest))
34+
return null;
35+
36+
var dict = new Dictionary<string, string?>();
37+
foreach (var item in manifest.Split('\n'))
38+
{
39+
if (string.IsNullOrWhiteSpace(item))
40+
continue;
41+
42+
var split = item.Split(':');
43+
44+
var key = split[0].Trim();
45+
46+
if (split.Length == 1)
47+
dict[key] = null;
48+
else if (split.Length == 2)
49+
dict[key] = split[1].Trim();
50+
else
51+
{
52+
var value = string.Join(":", split, 1, split.Length - 1).Trim();
53+
dict[key] = value;
54+
}
55+
}
56+
57+
return dict;
58+
}
59+
60+
private static string readStreamString(Stream s)
61+
{
62+
var str = new StringBuilder();
63+
var buffer = new byte[1024];
64+
while (true)
65+
{
66+
int size = s.Read(buffer, 0, buffer.Length);
67+
if (size == 0)
68+
break;
69+
70+
str.Append(Encoding.UTF8.GetString(buffer, 0, size));
71+
}
72+
73+
return str.ToString();
74+
}
75+
}

0 commit comments

Comments
 (0)