Skip to content

Commit 24582c6

Browse files
committed
some improvements
- do not throw exception if file is empty - DataPlist should initialized before save - fallback for empty input in level decompress - rollback base64 lib to 0.1.2
1 parent 1f4e91e commit 24582c6

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

GeometryDashAPI/Data/GameData.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GameData
1414
// see more https://en.wikipedia.org/wiki/Gzip
1515
private static readonly byte[] XorDatFileMagickBytes = [ 0x43, 0x3f ];
1616

17-
public Plist DataPlist { get; set; }
17+
public Plist? DataPlist { get; set; }
1818

1919
private readonly GameDataType? type;
2020

@@ -51,13 +51,12 @@ public virtual async Task LoadAsync(string fileName)
5151
// windows files
5252
var xor = Crypt.XOR(data, 0xB);
5353
var index = xor.AsSpan().IndexOf((byte)0);
54-
var gZipDecompress =
55-
Crypt.GZipDecompress(
56-
GameConvert.FromBase64(Encoding.ASCII.GetString(xor, 0, index >= 0 ? index : xor.Length)));
57-
58-
if (gZipDecompress is null)
59-
throw new InvalidOperationException("Data was empty");
60-
54+
var gZipDecompress = Crypt.GZipDecompress(GameConvert.FromBase64(Encoding.ASCII.GetString(xor, 0, index >= 0 ? index : xor.Length)));
55+
if (gZipDecompress == null)
56+
{
57+
DataPlist = [];
58+
return;
59+
}
6160
DataPlist = new Plist(gZipDecompress);
6261
}
6362

@@ -75,6 +74,8 @@ public virtual async Task LoadAsync(string fileName)
7574
public void Save(string? fullName = null, DatFileFormat? format = null)
7675
{
7776
using var memory = new MemoryStream();
77+
if (DataPlist == null)
78+
throw new InvalidOperationException($"nothing to save: {nameof(DataPlist)} is null");
7879
DataPlist.SaveToStream(memory);
7980
File.WriteAllBytes(fullName ?? ResolveFileName(type), GetFileContent(memory, format ?? ResolveFileFormat()));
8081
}
@@ -93,6 +94,8 @@ public void Save(string? fullName = null, DatFileFormat? format = null)
9394
public async Task SaveAsync(string? fileName = null, DatFileFormat? format = null)
9495
{
9596
using var memory = new MemoryStream();
97+
if (DataPlist == null)
98+
throw new InvalidOperationException($"nothing to save: {nameof(DataPlist)} is null");
9699
await DataPlist.SaveToStreamAsync(memory);
97100
#if NETSTANDARD2_1
98101
await File.WriteAllBytesAsync(fileName ?? ResolveFileName(type), GetFileContent(memory, format ?? ResolveFileFormat()));

GeometryDashAPI/GeometryDashAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<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="1.0.1" />
31+
<PackageReference Include="UrlBase64" Version="0.1.2" />
3232
</ItemGroup>
3333

3434
<ItemGroup>

GeometryDashAPI/Levels/Level.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public static string Decompress(string data)
111111
var bytes = GameConvert.FromBase64(data);
112112

113113
if (bytes[0] == 0x78)
114-
return Crypt.ZLibDecompress(bytes)?.StreamToString();
114+
return Crypt.ZLibDecompress(bytes)?.StreamToString() ?? string.Empty;
115115

116116
if (bytes[0] == 0x1F && bytes[1] == 0x8B)
117-
return Crypt.GZipDecompress(bytes)?.StreamToString();
117+
return Crypt.GZipDecompress(bytes)?.StreamToString() ?? string.Empty;
118118

119119
throw new InvalidOperationException(
120120
"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");

0 commit comments

Comments
 (0)