diff --git a/HipHopFile/Functions.cs b/HipHopFile/Functions.cs index e693232..d0b3bed 100644 --- a/HipHopFile/Functions.cs +++ b/HipHopFile/Functions.cs @@ -66,7 +66,6 @@ public static void WriteString(ref List listBytes, string writeString) if (writeString.Length % 2 == 1) listBytes.AddRange(new byte[] { 0 }); } - public static int globalRelativeStartOffset; public static Game currentGame = Game.Unknown; public static Platform currentPlatform = Platform.Unknown; @@ -93,22 +92,10 @@ public static HipSection[] HipFileToHipArray(string fileName) public static byte[] HipArrayToFile(HipSection[] hipFile) { - // Create hip as list of bytes... List list = new List(); foreach (HipSection i in hipFile) i.SetBytes(ref list); - // Fix AHDR offsets... - foreach (HipSection h in hipFile) - if (h is Section_DICT DICT) - foreach (Section_AHDR AHDR in DICT.ATOC.AHDRList) - AHDR.fileOffset += globalRelativeStartOffset; - - // Create hip as list of bytes again, now with correct offsets. - list = new List(); - foreach (HipSection i in hipFile) - i.SetBytes(ref list); - return list.ToArray(); } @@ -187,7 +174,6 @@ public static void HipArrayToIni(HipSection[] hipFile, string unpackFolder, bool else if (i is Section_STRM STRM) { INIWriter.WriteLine("STRM.DHDR=" + STRM.DHDR.value.ToString()); - INIWriter.WriteLine("STRM.Padding=" + STRM.DPAK.firstPadding.ToString()); INIWriter.WriteLine(); } } @@ -283,7 +269,6 @@ public static void HipArrayToJson(HipSection[] hipFile, string unpackFolder, boo else if (i is Section_STRM STRM) { serializer.SRTM_DHDR_value = STRM.DHDR.value; - serializer.SRTM_DPAK_firstPadding = STRM.DPAK.firstPadding; } } @@ -399,10 +384,6 @@ public static HipSection[] IniToHipArray(string INIFile) { STRM.DHDR = new Section_DHDR(Convert.ToInt32(s.Split('=')[1])); } - else if (s.StartsWith("STRM.Padding")) - { - STRM.DPAK = new Section_DPAK(Convert.ToInt32(s.Split('=')[1])); - } else if (s.StartsWith("LayerType")) { CurrentLHDR.layerType = (LayerType)Convert.ToInt32(s.Split('=')[1].Split()[0]); @@ -489,10 +470,21 @@ private static HipSection[] GetFilesData(string INIFile, ref Section_HIPA HIPA, public static HipSection[] SetupStream(ref Section_HIPA HIPA, ref Section_PACK PACK, ref Section_DICT DICT, ref Section_STRM STRM, bool alreadyHasData = true, Dictionary assetDataDictionary = null) { - // Create the new STRM stream. Add initial padding to it. + // Let's generate a temporary HIP file that will be discarded. + List temporaryFile = new List(); + + HIPA.SetBytes(ref temporaryFile); + + PACK.PCNT = new Section_PCNT(0, 0, 0, 0, 0); + PACK.SetBytes(ref temporaryFile); + + DICT.SetBytes(ref temporaryFile); + + STRM.DPAK.data = new byte[0]; + STRM.SetBytes(ref temporaryFile); + + // Create the new STRM stream. List newStream = new List(); - for (int j = 0; j < STRM.DPAK.firstPadding; j++) - newStream.Add(0x33); // We'll create these variables but won't really use them. Meh. int LargestSourceFileAsset = 0; @@ -530,7 +522,7 @@ public static HipSection[] SetupStream(ref Section_HIPA HIPA, ref Section_PACK P } // Set stream dependant AHDR data... - AHDR.fileOffset = newStream.Count(); + AHDR.fileOffset = newStream.Count + STRM.DPAK.globalRelativeStartOffset; AHDR.fileSize = AHDR.containedFile.Length; // And add the data to the stream. @@ -556,14 +548,11 @@ public static HipSection[] SetupStream(ref Section_HIPA HIPA, ref Section_PACK P for (int j = 0; j < AHDR.plusValue; j++) newStream.Add(0x33); } - } - // More alignment data. - int value2 = (newStream.Count - STRM.DPAK.firstPadding) % 0x20; - if (value2 != 0) - for (int j = 0; j < 0x20 - value2; j++) + while ((newStream.Count + STRM.DPAK.globalRelativeStartOffset) % 0x20 != 0) newStream.Add(0x33); - + } + // Assign list as stream! We're done with the worst part. STRM.DPAK.data = newStream.ToArray(); diff --git a/HipHopFile/HipSerializer.cs b/HipHopFile/HipSerializer.cs index e497687..8b8d1fb 100644 --- a/HipHopFile/HipSerializer.cs +++ b/HipHopFile/HipSerializer.cs @@ -43,6 +43,5 @@ public class HipSerializer public List layers; public int SRTM_DHDR_value; - public int SRTM_DPAK_firstPadding; } } diff --git a/HipHopFile/Sections/Section_DPAK.cs b/HipHopFile/Sections/Section_DPAK.cs index 1b953bb..441fcb8 100644 --- a/HipHopFile/Sections/Section_DPAK.cs +++ b/HipHopFile/Sections/Section_DPAK.cs @@ -8,13 +8,12 @@ namespace HipHopFile { public class Section_DPAK : HipSection { - public int firstPadding; + public int globalRelativeStartOffset; public byte[] data; - - public Section_DPAK(int a) + + public Section_DPAK() { sectionName = Section.DPAK; - firstPadding = a; } public Section_DPAK(BinaryReader binaryReader) @@ -22,21 +21,40 @@ public Section_DPAK(BinaryReader binaryReader) sectionName = Section.DPAK; sectionSize = Switch(binaryReader.ReadInt32()); - firstPadding = Switch(binaryReader.ReadInt32()); + int firstPadding = Switch(binaryReader.ReadInt32()); + + binaryReader.BaseStream.Position += firstPadding; globalRelativeStartOffset = (int)binaryReader.BaseStream.Position; - data = binaryReader.ReadBytes(sectionSize - 4); + int sizeOfData = sectionSize - 4 - firstPadding; + data = binaryReader.ReadBytes(sizeOfData); } public override void SetListBytes(ref List listBytes) { sectionName = Section.DPAK; - listBytes.AddRange(BitConverter.GetBytes(firstPadding).Reverse()); + int firstPaddingPosition = listBytes.Count; + + listBytes.Add(0); + listBytes.Add(0); + listBytes.Add(0); + listBytes.Add(0); + while (listBytes.Count % 0x20 != 0) + listBytes.Add(0x33); + globalRelativeStartOffset = listBytes.Count(); + int firstPadding = listBytes.Count - firstPaddingPosition; + + byte[] firstPaddingBytes = BitConverter.GetBytes(firstPadding); + listBytes[firstPaddingPosition + 0] = firstPaddingBytes[3]; + listBytes[firstPaddingPosition + 1] = firstPaddingBytes[2]; + listBytes[firstPaddingPosition + 2] = firstPaddingBytes[1]; + listBytes[firstPaddingPosition + 3] = firstPaddingBytes[0]; + listBytes.AddRange(data); } }