Skip to content

Commit

Permalink
improve error reporting on missing files
Browse files Browse the repository at this point in the history
resolves #18
  • Loading branch information
Krzyhau committed Feb 18, 2024
1 parent 6eb48c6 commit ba8f79f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/AnimatedTextureConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override FileBundle ConvertTyped(AnimatedTexture txt)

public override AnimatedTexture DeconvertTyped(FileBundle bundle)
{
using var animation = Image.Load<Rgba32>(bundle.GetData(""));
using var animation = Image.Load<Rgba32>(bundle.RequireData(""));
return AnimationImageToAnimatedTexture(animation);
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/ArtObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override ArtObject DeconvertTyped(FileBundle bundle)
{
var artObject = ConfiguredJsonSerializer.DeserializeFromFileBundle<ArtObject>(bundle);

AppendGeometryStream(ref artObject, bundle.GetData(".obj"));
AppendGeometryStream(ref artObject, bundle.RequireData(".obj"));
LoadCubemap(ref artObject, bundle.GetData(".png"), bundle.GetData(".apng"));


Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/EffectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override FileBundle ConvertTyped(Effect data)

public override Effect DeconvertTyped(FileBundle bundle)
{
var inReader = new BinaryReader(bundle.GetData(""));
var inReader = new BinaryReader(bundle.RequireData(""));
return new Effect()
{
Data = inReader.ReadBytes(inReader.ReadInt32())
Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/SoundEffectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override SoundEffect DeconvertTyped(FileBundle bundle)
{
var soundEffect = new SoundEffect();

using var inReader = new BinaryReader(bundle.GetData(""), Encoding.UTF8, true);
using var inReader = new BinaryReader(bundle.RequireData(""), Encoding.UTF8, true);

var riffHeader = new string(inReader.ReadChars(4));
var fileSize = inReader.ReadInt32();
Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/SpriteFontConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override SpriteFont DeconvertTyped(FileBundle bundle)
var spriteFontModel = ConfiguredJsonSerializer.DeserializeFromFileBundle<SpriteFontPropertiesJsonModel>(bundle);
var spriteFont = spriteFontModel.Deserialize();

using var importedImage = Image.Load<Rgba32>(bundle.GetData(".png"));
using var importedImage = Image.Load<Rgba32>(bundle.RequireData(".png"));
spriteFont.Texture = TexturesUtil.ImageToTexture2D(importedImage);

return spriteFont;
Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/TextureConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override FileBundle ConvertTyped(Texture2D texture)

public override Texture2D DeconvertTyped(FileBundle bundle)
{
using var importedImage = Image.Load<Rgba32>(bundle.GetData(""));
using var importedImage = Image.Load<Rgba32>(bundle.RequireData(""));
return TexturesUtil.ImageToTexture2D(importedImage);
}
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Conversion/Formats/TrileSetConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override TrileSet DeconvertTyped(FileBundle bundle)
{
var trileSet = ConfiguredJsonSerializer.DeserializeFromFileBundle<TrileSet>(bundle);

AppendGeometryStream(ref trileSet, bundle.GetData(".obj"));
AppendGeometryStream(ref trileSet, bundle.RequireData(".obj"));
LoadCubemap(ref trileSet, bundle.GetData(".png"), bundle.GetData(".apng"));

return trileSet;
Expand Down
16 changes: 13 additions & 3 deletions Core/FileSystem/FileBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ public Stream GetData(params string[] validExtensions)
return default!;
}

public Stream RequireData(params string[] validExtensions)
{
var data = GetData(validExtensions);
if(data == null)
{
var fullExtensions = validExtensions.Select(ext => $"\"{MainExtension}{ext}\"").ToArray();
var extensionsString = string.Join(" or ", fullExtensions);
throw new FileNotFoundException($"Bundle missing required data: {extensionsString}");
}
return data;
}

public HashSet<string> GetSubExtensions()
{
var extSet = new HashSet<string>();
foreach (var item in Files) extSet.Add(item.Extension);
return extSet;
return new HashSet<string>(Files.Select(file => file.Extension));
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion Core/Helpers/Json/ConfiguredJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static FileBundle SerializeToFileBundle<T>(string secondaryFileFormat, T

public static T DeserializeFromFileBundle<T>(FileBundle bundle)
{
using var inReader = new BinaryReader(bundle.GetData(".json", ""), Encoding.UTF8, true);
using var inReader = new BinaryReader(bundle.RequireData(".json", ""), Encoding.UTF8, true);
var json = new string(inReader.ReadChars((int)inReader.BaseStream.Length));

try
Expand Down

0 comments on commit ba8f79f

Please sign in to comment.