Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
-  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
  • Loading branch information
Folleach committed May 8, 2024
1 parent 1f4e91e commit 24582c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
19 changes: 11 additions & 8 deletions GeometryDashAPI/Data/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GameData
// see more https://en.wikipedia.org/wiki/Gzip
private static readonly byte[] XorDatFileMagickBytes = [ 0x43, 0x3f ];

public Plist DataPlist { get; set; }
public Plist? DataPlist { get; set; }

private readonly GameDataType? type;

Expand Down Expand Up @@ -51,13 +51,12 @@ public virtual async Task LoadAsync(string fileName)
// windows files
var xor = Crypt.XOR(data, 0xB);
var index = xor.AsSpan().IndexOf((byte)0);
var gZipDecompress =
Crypt.GZipDecompress(
GameConvert.FromBase64(Encoding.ASCII.GetString(xor, 0, index >= 0 ? index : xor.Length)));

if (gZipDecompress is null)
throw new InvalidOperationException("Data was empty");

var gZipDecompress = Crypt.GZipDecompress(GameConvert.FromBase64(Encoding.ASCII.GetString(xor, 0, index >= 0 ? index : xor.Length)));
if (gZipDecompress == null)
{
DataPlist = [];
return;
}
DataPlist = new Plist(gZipDecompress);
}

Expand All @@ -75,6 +74,8 @@ public virtual async Task LoadAsync(string fileName)
public void Save(string? fullName = null, DatFileFormat? format = null)
{
using var memory = new MemoryStream();
if (DataPlist == null)
throw new InvalidOperationException($"nothing to save: {nameof(DataPlist)} is null");
DataPlist.SaveToStream(memory);
File.WriteAllBytes(fullName ?? ResolveFileName(type), GetFileContent(memory, format ?? ResolveFileFormat()));
}
Expand All @@ -93,6 +94,8 @@ public void Save(string? fullName = null, DatFileFormat? format = null)
public async Task SaveAsync(string? fileName = null, DatFileFormat? format = null)
{
using var memory = new MemoryStream();
if (DataPlist == null)
throw new InvalidOperationException($"nothing to save: {nameof(DataPlist)} is null");
await DataPlist.SaveToStreamAsync(memory);
#if NETSTANDARD2_1
await File.WriteAllBytesAsync(fileName ?? ResolveFileName(type), GetFileContent(memory, format ?? ResolveFileFormat()));
Expand Down
2 changes: 1 addition & 1 deletion GeometryDashAPI/GeometryDashAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageReference Include="csFastFloat" Version="4.1.4" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="UrlBase64" Version="1.0.1" />
<PackageReference Include="UrlBase64" Version="0.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions GeometryDashAPI/Levels/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public static string Decompress(string data)
var bytes = GameConvert.FromBase64(data);

if (bytes[0] == 0x78)
return Crypt.ZLibDecompress(bytes)?.StreamToString();
return Crypt.ZLibDecompress(bytes)?.StreamToString() ?? string.Empty;

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

throw new InvalidOperationException(
"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");
Expand Down

0 comments on commit 24582c6

Please sign in to comment.