Skip to content

Commit

Permalink
Fix bug with empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
Razmoth committed Nov 24, 2023
1 parent b138c2b commit 051bc41
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
4 changes: 0 additions & 4 deletions Audio/Extensions/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ public static class BinaryReaderExtensions
{
public static string ReadRawString(this BinaryReader reader, int length)
{
if (reader.BaseStream.Position + length > reader.BaseStream.Length)
{
throw new System.Exception($"String size {length} is out of bound !!");
}
return Encoding.UTF8.GetString(reader.ReadBytes(length));
}
}
69 changes: 37 additions & 32 deletions Audio/Models/Chunks/Chunk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Audio.Extensions;
using Audio.Models.Utils;

Expand All @@ -23,39 +24,43 @@ public void Parse(BinaryReader reader)
public static Chunk ParseChunk(BinaryReader reader)
{
var chunk = new Chunk();
chunk.Parse(reader);

switch (chunk.Signature)
try
{
case "AKPK":
var package = new Package(chunk);
package.Parse(reader);
chunk = package;
break;
case "BKHD":
var bkhd = new BKHD(chunk);
bkhd.Parse(reader);
chunk = bkhd;
break;
case "STID":
var stid = new STID(chunk);
stid.Parse(reader);
chunk = stid;
break;
case "DIDX":
var didx = new DIDX(chunk);
didx.Parse(reader);
chunk = didx;
break;
case "DATA":
var data = new DATA(chunk);
data.Parse(reader);
chunk = data;
break;
default:
reader.BaseStream.Position += chunk.Length;
break;
chunk.Parse(reader);

switch (chunk.Signature)
{
case "AKPK":
var package = new Package(chunk);
package.Parse(reader);
chunk = package;
break;
case "BKHD":
var bkhd = new BKHD(chunk);
bkhd.Parse(reader);
chunk = bkhd;
break;
case "STID":
var stid = new STID(chunk);
stid.Parse(reader);
chunk = stid;
break;
case "DIDX":
var didx = new DIDX(chunk);
didx.Parse(reader);
chunk = didx;
break;
case "DATA":
var data = new DATA(chunk);
data.Parse(reader);
chunk = data;
break;
default:
reader.BaseStream.Position += chunk.Length;
break;
}
}
catch (Exception) { }

return chunk;
}
Expand Down
13 changes: 7 additions & 6 deletions Audio/Models/Chunks/Package.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public record Package : Chunk
public Bank[] Banks { get; set; }
public Sound[] Sounds { get; set; }
public External[] Externals { get; set; }
public bool Parsed => IsLittleEndian == true;

public Package(Chunk chunk) : base(chunk) { }

Check warning on line 21 in Audio/Models/Chunks/Package.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Audio/Models/Chunks/Package.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FoldersDict' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Audio/Models/Chunks/Package.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Folders' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Audio/Models/Chunks/Package.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Banks' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 21 in Audio/Models/Chunks/Package.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Sounds' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Expand All @@ -26,14 +25,16 @@ public static bool Parse(string path, out Package package)
using var fs = File.OpenRead(path);
using var reader = new BinaryReader(fs);

package = ParseChunk(reader) as Package;
if (package == null)
var chunk = ParseChunk(reader);
if (chunk is Package chk)
{
return false;
chk.Path = path;
package = chk;
return true;
}

package.Path = path;
return true;
package = null;
return false;
}

public new void Parse(BinaryReader reader)
Expand Down

0 comments on commit 051bc41

Please sign in to comment.