Add safer chunk usage

This commit is contained in:
Jarrod Doyle 2024-08-14 18:39:13 +01:00
parent 736ced24e4
commit 622192a489
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 20 additions and 9 deletions

View File

@ -92,19 +92,28 @@ public partial class Mission : Node3D
ClearMap();
_textureLoader = new TextureLoader(_installPaths.famPath, FileName.GetBaseDir());
_file = new(FileName);
var textureList = (TxList)_file.Chunks["TXLIST"];
LoadTextures(textureList);
if (Dump) DumpTextureList(textureList);
var wr = (WorldRep)_file.Chunks["WREXT"];
BuildMeshes(wr.Cells, wr.DataHeader.LightmapFormat == 2);
UseChunk<TxList>("TXLIST", LoadTextures);
UseChunk<WorldRep>("WREXT", BuildWrMeshes);
}
private void BuildMeshes(WorldRep.Cell[] cells, bool lmHdr)
private void UseChunk<T>(string name, Action<T> action)
{
GD.Print($"HDR: {lmHdr}");
if (_file.Chunks.TryGetValue(name, out var value))
{
action((T)value);
}
else
{
GD.Print($"No chunk found/loaded: {name}");
}
}
private void BuildWrMeshes(WorldRep worldRep)
{
var cells = worldRep.Cells;
var lmHdr = worldRep.DataHeader.LightmapFormat == 2;
GD.Print($"HDR Lightmap: {lmHdr}");
var packingRects = new List<PackingRectangle>();
var surfaceDataMap = new Dictionary<int, MeshSurfaceData>();
@ -345,6 +354,8 @@ public partial class Mission : Node3D
GD.Print($"Failed to load texture: {path}");
}
}
if (Dump) DumpTextureList(textureList);
}
private static void DumpTextureList(TxList textureList)