From 48d9ebfe6961ba4d620ee5987ab3bcb51fb32aeb Mon Sep 17 00:00:00 2001 From: AdAstra-LD Date: Wed, 10 Feb 2021 04:51:28 +0100 Subject: [PATCH] Update ScriptFile.cs - relative offsets are now signed - fixed a problem with commands that had "return" in their name fucking up the script file --- DS_Map/ROMFiles/ScriptFile.cs | 54 +++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/DS_Map/ROMFiles/ScriptFile.cs b/DS_Map/ROMFiles/ScriptFile.cs index 1ae0ea1..6301e4d 100644 --- a/DS_Map/ROMFiles/ScriptFile.cs +++ b/DS_Map/ROMFiles/ScriptFile.cs @@ -25,9 +25,9 @@ namespace DSPRE.ROMFiles { #region Constructors (1) public ScriptFile(Stream fs) { - List scriptOffsets = new List(); - List functionOffsets = new List(); - List movementOffsets = new List(); + List scriptOffsets = new List(); + List functionOffsets = new List(); + List movementOffsets = new List(); ushort[] endCodes = new ushort[] { 0x2, 0x16, 0x1B }; using (BinaryReader scrReader = new BinaryReader(fs)) { @@ -47,7 +47,7 @@ namespace DSPRE.ROMFiles { isLevelScript = false; break; } else { - uint offsetFromStart = value + (uint)scrReader.BaseStream.Position; + int offsetFromStart = (int)(value + scrReader.BaseStream.Position); scriptOffsets.Add(offsetFromStart); // Don't change order of addition } } @@ -143,13 +143,13 @@ namespace DSPRE.ROMFiles { //once it's done, this Predicate below will be the only one needed, since there will be no distinction between //a script and a function Func functionEndCondition = - (source, x) => !source[x].Equals(RomInfo.ScriptCommandNamesDict[0x0002]) //End - && !source[x].Contains(RomInfo.ScriptCommandNamesDict[0x001B]) //Return + (source, x) => !source[x].TrimEnd().Equals(RomInfo.ScriptCommandNamesDict[0x0002]) //End + && !source[x].TrimEnd().Equals(RomInfo.ScriptCommandNamesDict[0x001B]) //Return && !source[x].Contains(RomInfo.ScriptCommandNamesDict[0x0016] + " Function"); //Jump Function_# Func scriptEndCondition = - (source, x) => !source[x].Equals(RomInfo.ScriptCommandNamesDict[0x0002]) //End + (source, x) => !source[x].TrimEnd().Equals(RomInfo.ScriptCommandNamesDict[0x0002]) //End && !source[x].Contains(RomInfo.ScriptCommandNamesDict[0x0016] + " Function"); //Jump Function_# allScripts = readCommandsFromLines(scriptLines, scriptEndCondition); //Jump + whitespace @@ -169,7 +169,7 @@ namespace DSPRE.ROMFiles { #endregion #region Methods (1) - private ScriptCommand ReadCommand(BinaryReader dataReader, ref List functionOffsets, ref List movementOffsets) { + private ScriptCommand ReadCommand(BinaryReader dataReader, ref List functionOffsets, ref List movementOffsets) { ushort id = dataReader.ReadUInt16(); List parameterList = new List(); @@ -180,28 +180,32 @@ namespace DSPRE.ROMFiles { case "Plat": switch (id) { case 0x16: //Jump - case 0x1A: //Call - uint offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition - if (!functionOffsets.Contains(offset)) - functionOffsets.Add(offset); + case 0x1A: //Call + { + int offset = (int)(dataReader.ReadInt32() + dataReader.BaseStream.Position); // Do not change order of addition + if (!functionOffsets.Contains(offset)) + functionOffsets.Add(offset); - parameterList.Add(BitConverter.GetBytes(functionOffsets.IndexOf(offset))); - break; + parameterList.Add(BitConverter.GetBytes(functionOffsets.IndexOf(offset))); + break; + } case 0x1C: //CompareLastResultJump case 0x1D: //CompareLastResultCall - byte opcode = dataReader.ReadByte(); - offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition - if (!functionOffsets.Contains(offset)) - functionOffsets.Add(offset); + { + byte opcode = dataReader.ReadByte(); + int offset = (int)(dataReader.ReadInt32() + dataReader.BaseStream.Position); // Do not change order of addition + if (!functionOffsets.Contains(offset)) + functionOffsets.Add(offset); - parameterList.Add(new byte[] { opcode }); - parameterList.Add(BitConverter.GetBytes(functionOffsets.IndexOf(offset))); - break; + parameterList.Add(new byte[] { opcode }); + parameterList.Add(BitConverter.GetBytes(functionOffsets.IndexOf(offset))); + break; + } case 0x5E: // ApplyMovement case 0x2A1: // ApplyMovement2 { ushort overworld = dataReader.ReadUInt16(); - offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition + int offset = (int)(dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position); // Do not change order of addition if (!movementOffsets.Contains(offset)) movementOffsets.Add(offset); @@ -330,7 +334,7 @@ namespace DSPRE.ROMFiles { switch (id) { case 0x16: //Jump case 0x1A: //Call - uint offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition + int offset = (int)(dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position); // Do not change order of addition if (!functionOffsets.Contains(offset)) functionOffsets.Add(offset); @@ -339,7 +343,7 @@ namespace DSPRE.ROMFiles { case 0x1C: //CompareLastResultJump case 0x1D: //CompareLastResultCall byte opcode = dataReader.ReadByte(); - offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition + offset = (int)(dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position); // Do not change order of addition if (!functionOffsets.Contains(offset)) functionOffsets.Add(offset); @@ -349,7 +353,7 @@ namespace DSPRE.ROMFiles { case 0x5E: // ApplyMovement { ushort overworld = dataReader.ReadUInt16(); - offset = dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position; // Do not change order of addition + offset = (int)(dataReader.ReadUInt32() + (uint)dataReader.BaseStream.Position); // Do not change order of addition if (!movementOffsets.Contains(offset)) movementOffsets.Add(offset);