Skip to content

Commit 1f4e91e

Browse files
flightlexFolleach
authored andcommitted
fixed XML reading for geometry dash 2.2
stream optimization (cherry picked from commit 69c3cc5)
1 parent c62ac4b commit 1f4e91e

File tree

9 files changed

+38
-24
lines changed

9 files changed

+38
-24
lines changed

Examples/Examples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
18-
<PackageReference Include="JetBrains.Profiler.Api" Version="1.2.0" />
17+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
18+
<PackageReference Include="JetBrains.Profiler.Api" Version="1.4.3" />
1919
</ItemGroup>
2020

2121
</Project>

GeometryDashAPI.Benchmarks/GeometryDashAPI.Benchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
9+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
1010
</ItemGroup>
1111

1212
<ItemGroup>

GeometryDashAPI.Tests/GeometryDashAPI.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
11-
<PackageReference Include="FluentAssertions" Version="6.0.0-alpha0002" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
10+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
11+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
1313
<PackageReference Include="NUnit" Version="3.13.3" />
1414
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0">
1515
<PrivateAssets>all</PrivateAssets>

GeometryDashAPI/Crypt.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.IO.Compression;
34
using System.Security.Cryptography;
45
using System.Text;
@@ -34,24 +35,22 @@ public static string XOR(string text, string key)
3435
return result.ToString();
3536
}
3637

37-
public static string GZipDecompress(byte[] data)
38+
public static Stream? GZipDecompress(byte[] data)
3839
{
3940
if (data == null || data.Length <= 0)
40-
return string.Empty;
41-
using var stream = new MemoryStream(data);
42-
using var zip = new GZipStream(stream, CompressionMode.Decompress);
43-
using var reader = new StreamReader(zip);
44-
return reader.ReadToEnd();
41+
return null;
42+
43+
var stream = new MemoryStream(data);
44+
return new GZipStream(stream, CompressionMode.Decompress);
4545
}
4646

47-
public static string ZLibDecompress(byte[] data)
47+
public static Stream? ZLibDecompress(byte[] data)
4848
{
4949
if (data == null || data.Length <= 0)
50-
return string.Empty;
51-
using var stream = new MemoryStream(data);
52-
using var zip = new InflaterInputStream(stream);
53-
using var reader = new StreamReader(zip);
54-
return reader.ReadToEnd();
50+
return null;
51+
52+
var stream = new MemoryStream(data);
53+
return new InflaterInputStream(stream);
5554
}
5655

5756
public static byte[] GZipCompress(byte[] data)

GeometryDashAPI/Data/GameData.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public virtual async Task LoadAsync(string fileName)
5454
var gZipDecompress =
5555
Crypt.GZipDecompress(
5656
GameConvert.FromBase64(Encoding.ASCII.GetString(xor, 0, index >= 0 ? index : xor.Length)));
57-
DataPlist = new Plist(Encoding.ASCII.GetBytes(gZipDecompress));
57+
58+
if (gZipDecompress is null)
59+
throw new InvalidOperationException("Data was empty");
60+
61+
DataPlist = new Plist(gZipDecompress);
5862
}
5963

6064
/// <summary>

GeometryDashAPI/Extensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Reflection;
45

56
namespace GeometryDashAPI
@@ -75,6 +76,12 @@ public static GameType GetGameType(this OfficialLevel level)
7576
};
7677
}
7778

79+
internal static string StreamToString(this Stream stream)
80+
{
81+
using var streamReader = new StreamReader(stream);
82+
return streamReader.ReadToEnd();
83+
}
84+
7885
internal static IEnumerable<KeyValuePair<T, T>> Pairs<T>(this IEnumerable<T> source)
7986
{
8087
var first = false;

GeometryDashAPI/GeometryDashAPI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
</PropertyGroup>
2626

2727
<ItemGroup>
28-
<PackageReference Include="csFastFloat" Version="4.1.0" />
28+
<PackageReference Include="csFastFloat" Version="4.1.4" />
2929
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
3030
<PackageReference Include="SharpZipLib" Version="1.4.2" />
31-
<PackageReference Include="UrlBase64" Version="0.1.2" />
31+
<PackageReference Include="UrlBase64" Version="1.0.1" />
3232
</ItemGroup>
3333

3434
<ItemGroup>

GeometryDashAPI/Levels/Level.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ public string SaveAsString(bool compress = true)
109109
public static string Decompress(string data)
110110
{
111111
var bytes = GameConvert.FromBase64(data);
112+
112113
if (bytes[0] == 0x78)
113-
return Crypt.ZLibDecompress(bytes);
114+
return Crypt.ZLibDecompress(bytes)?.StreamToString();
115+
114116
if (bytes[0] == 0x1F && bytes[1] == 0x8B)
115-
return Crypt.GZipDecompress(bytes);
117+
return Crypt.GZipDecompress(bytes)?.StreamToString();
118+
116119
throw new InvalidOperationException(
117120
"Unsupported data signature. There is no gzip and zlib. Please check your level data. If your level is correct and works in the game, please create an issue: https://github.com/Folleach/GeometryDashAPI/issues. Or fix it yourself: https://github.com/Folleach/GeometryDashAPI/blob/master/GeometryDashAPI/Levels/Level.cs");
118121
}

GeometryDashAPI/Serialization/Plist.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Plist : Dictionary<string, dynamic>
1515
{
1616
private static readonly XmlReaderSettings xmlSettings = new XmlReaderSettings()
1717
{
18+
CheckCharacters = false, // to skip XML reserved characters
1819
DtdProcessing = DtdProcessing.Ignore,
1920
XmlResolver = null
2021
};

0 commit comments

Comments
 (0)