Recalculate TOC on write

This commit is contained in:
Jarrod Doyle 2024-09-27 17:46:10 +01:00
parent bcc60eff52
commit 8b7ffcd7a3
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 21 additions and 12 deletions

View File

@ -32,13 +32,13 @@ public class DbFile
} }
} }
public readonly struct TableOfContents public struct TableOfContents
{ {
public readonly struct Entry public struct Entry
{ {
public string Name { get; } public string Name;
public uint Offset { get; } public uint Offset;
public uint Size { get; } public uint Size;
public Entry(BinaryReader reader) public Entry(BinaryReader reader)
{ {
@ -47,10 +47,9 @@ public class DbFile
Size = reader.ReadUInt32(); Size = reader.ReadUInt32();
} }
public override string ToString() public override readonly string ToString()
{ {
// return $"Name: {Name}, Offset: {O}" return $"Name: {Name}, Offset: {Offset}, Size: {Size}";
return base.ToString();
} }
public readonly void Write(BinaryWriter writer) public readonly void Write(BinaryWriter writer)
@ -110,18 +109,28 @@ public class DbFile
public void Save(string filename) public void Save(string filename)
{ {
// !HACK: Right now we don't need to adjust TOC offset or anything because we're only
// overwriting data, not writing new lengths of data
using var stream = File.Open(filename, FileMode.Create); using var stream = File.Open(filename, FileMode.Create);
using var writer = new BinaryWriter(stream, Encoding.UTF8, false); using var writer = new BinaryWriter(stream, Encoding.UTF8, false);
Header.Write(writer); Header.Write(writer);
foreach (var (name, chunk) in Chunks) for (var i = 0; i < Toc.ItemCount; i++)
{ {
var item = Toc.Items[i];
var pos = stream.Position;
var chunk = Chunks[item.Name];
chunk.Write(writer); chunk.Write(writer);
var size = stream.Position - pos - 24;
item.Offset = (uint)pos;
item.Size = (uint)size;
Toc.Items[i] = item;
} }
var tocOffset = (uint)stream.Position;
Toc.Write(writer); Toc.Write(writer);
stream.Seek(0, SeekOrigin.Begin);
writer.Write(tocOffset);
} }
private static IChunk NewChunk(string entryName) private static IChunk NewChunk(string entryName)