Skip to content

Commit c408ff0

Browse files
committed
correctly handle offsetting pak2 formats
1 parent 8327e4f commit c408ff0

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

CathodeLib/Scripts/CATHODE/PAK2.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ override protected bool LoadInternal()
2929
if (MagicValidation != "PAK2") { reader.Close(); return false; }
3030
int offsetListBegin = reader.ReadInt32() + 16;
3131
int entryCount = reader.ReadInt32();
32-
reader.BaseStream.Position += 4; //Skip "4"
32+
int alignment = reader.ReadInt32();
3333

3434
//Read all file names and create entries
3535
for (int i = 0; i < entryCount; i++)
@@ -47,7 +47,10 @@ override protected bool LoadInternal()
4747

4848
//Read in the files to entries
4949
for (int i = 0; i < entryCount; i++)
50-
Entries[i].Content = Utilities.RemoveLeadingNulls(reader.ReadBytes(FileOffsets[i + 1] - FileOffsets[i]));
50+
{
51+
Utilities.Align(reader, alignment);
52+
Entries[i].Content = reader.ReadBytes(FileOffsets[i + 1] - (int)reader.BaseStream.Position);
53+
}
5154
}
5255
return true;
5356
}
@@ -58,14 +61,12 @@ override protected bool SaveInternal()
5861
{
5962
writer.BaseStream.SetLength(0);
6063
Utilities.WriteString("PAK2", writer);
61-
int OffsetListBegin_New = 0;
62-
for (int i = 0; i < Entries.Count; i++) OffsetListBegin_New += Entries[i].Filename.Length + 1;
63-
writer.Write(OffsetListBegin_New);
64+
writer.Write(0);
6465
writer.Write(Entries.Count);
6566
writer.Write(4);
6667

6768
//Write filenames
68-
for (int i = 0; i < Entries.Count; i++) Utilities.WriteString(Entries[i].Filename.Replace("\\", "/"), writer, true);
69+
for (int i = 0; i < Entries.Count; i++) Utilities.WriteString(Entries[i].Filename, writer, true);
6970

7071
//Write placeholder offsets for now, we'll correct them after writing the content
7172
int offsetListBegin = (int)writer.BaseStream.Position;
@@ -75,12 +76,15 @@ override protected bool SaveInternal()
7576
List<int> offsets = new List<int>();
7677
for (int i = 0; i < Entries.Count; i++)
7778
{
78-
while (writer.BaseStream.Position % 4 != 0) writer.Write((byte)0x00);
79+
Utilities.Align(writer, 4);
7980
writer.Write(Entries[i].Content);
8081
offsets.Add((int)writer.BaseStream.Position);
8182
}
83+
Utilities.Align(writer, 4);
8284

8385
//Re-write offsets with correct values
86+
writer.BaseStream.Position = 4;
87+
writer.Write(offsetListBegin - 16);
8488
writer.BaseStream.Position = offsetListBegin;
8589
for (int i = 0; i < Entries.Count; i++) writer.Write(offsets[i]);
8690
}

CathodeLib/Scripts/Utilities.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public static T[] ConsumeArray<T>(byte[] bytes, int count)
4343

4444
//Align the stream
4545
public static void Align(BinaryReader reader, int val = 4)
46-
{
47-
while (reader.BaseStream.Position % val != 0) reader.ReadByte();
46+
{
47+
reader.BaseStream.Seek((val - (reader.BaseStream.Position % val)) % val, SeekOrigin.Current);
4848
}
4949
public static void Align(BinaryWriter writer, int val = 4, byte fillWith = 0x00)
5050
{

0 commit comments

Comments
 (0)