public static void ReplaceScriptsInPM1(string path, string originalPath) { using (var reader = new TGELib.IO.EndianBinaryReader(File.OpenRead(path), TGELib.IO.Endianness.LittleEndian)) using (var writer = new TGELib.IO.EndianBinaryWriter(File.Create(path + ".Replaced"), TGELib.IO.Endianness.LittleEndian)) { reader.SeekCurrent(0x50); int type = reader.ReadInt32(); int size = reader.ReadInt32(); int count = reader.ReadInt32(); int address = reader.ReadInt32(); if (type != 6 || count == 0 || count == 2) { return; } reader.SeekBegin(address); var bytes = ReplaceMessageScript(reader.ReadBytes(size)); int remainingBytes = (int)(reader.BaseStreamLength - reader.Position); reader.SeekBegin(0); if (bytes.Length <= size) { writer.Write(reader.ReadBytes(address)); writer.Write(bytes); for (int i = 0; i < size - bytes.Length; i++) { writer.Write((byte)0); } reader.SeekCurrent(size); writer.Write(reader.ReadBytes(remainingBytes)); } else { // write source bytes up until message script header fields writer.Write(reader.ReadBytes(0x50)); // write message script fields writer.Write(6); writer.Write(bytes.Length); writer.Write(1); writer.Write((int)reader.BaseStreamLength); // seek past original fields reader.SeekCurrent(0x10); // write source bytes up until end of file remainingBytes = (int)(reader.BaseStreamLength - reader.Position);; writer.Write(reader.ReadBytes(remainingBytes)); // append the new script to the file writer.Write(bytes); } } }
public static void FixScriptsInBIN(string path, string originalPath) { using (var reader = new TGELib.IO.EndianBinaryReader(File.OpenRead(path), TGELib.IO.Endianness.LittleEndian)) using (var writer = new TGELib.IO.EndianBinaryWriter(File.Create(path + ".Fixd"), TGELib.IO.Endianness.LittleEndian)) using (var originalReader = new TGELib.IO.EndianBinaryReader(File.OpenRead(originalPath), TGELib.IO.Endianness.LittleEndian)) { while (reader.Position != reader.BaseStream.Length) { // read source entry fields var fileName = reader.ReadString(TGELib.IO.StringBinaryFormat.FixedLength, 0xFC); int fileSize = reader.ReadInt32(); var fileBytes = reader.ReadBytes(fileSize); reader.Position = AlignmentHelper.Align(reader.Position, 64); originalReader.SeekCurrent(0xFC); int originalFileSize = originalReader.ReadInt32(); var originalFileBytes = originalReader.ReadBytes(originalFileSize); // transform bytes depending on the extension if (fileName.EndsWith("bmd")) { fileBytes = FixMessageScript(fileBytes, originalFileBytes); } else if (fileName.EndsWith("bf")) { fileBytes = FixFlowScript(fileBytes, originalFileBytes); } originalReader.Position = AlignmentHelper.Align(originalReader.Position, 64); // write output entry fields writer.Write(fileName, TGELib.IO.StringBinaryFormat.FixedLength, 0xFC); writer.Write(fileBytes.Length); writer.Write(fileBytes); writer.Position = AlignmentHelper.Align(writer.Position, 64); } } }