Skip to content

Commit

Permalink
rewrote part of file writing code; does layer padding
Browse files Browse the repository at this point in the history
  • Loading branch information
igorseabra4 committed Sep 2, 2018
1 parent 6ba40e5 commit 07a576e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 37 deletions.
47 changes: 18 additions & 29 deletions HipHopFile/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public static void WriteString(ref List<byte> 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;

Expand All @@ -93,22 +92,10 @@ public static HipSection[] HipFileToHipArray(string fileName)

public static byte[] HipArrayToFile(HipSection[] hipFile)
{
// Create hip as list of bytes...
List<byte> list = new List<byte>();
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<byte>();
foreach (HipSection i in hipFile)
i.SetBytes(ref list);

return list.ToArray();
}

Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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<uint, byte[]> assetDataDictionary = null)
{
// Create the new STRM stream. Add initial padding to it.
// Let's generate a temporary HIP file that will be discarded.
List<byte> temporaryFile = new List<byte>();

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<byte> newStream = new List<byte>();
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;
Expand Down Expand Up @@ -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.
Expand All @@ -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();

Expand Down
1 change: 0 additions & 1 deletion HipHopFile/HipSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ public class HipSerializer
public List<LayerSerializer> layers;

public int SRTM_DHDR_value;
public int SRTM_DPAK_firstPadding;
}
}
32 changes: 25 additions & 7 deletions HipHopFile/Sections/Section_DPAK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,53 @@ 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)
{
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<byte> 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);
}
}
Expand Down

0 comments on commit 07a576e

Please sign in to comment.