From 4e9df610d0a695c18de95133eb9fd68e93b045a5 Mon Sep 17 00:00:00 2001 From: AdAstra-LD Date: Thu, 28 Jan 2021 15:38:07 +0100 Subject: [PATCH] More script editor performance improvements (Big thanks to Mikelan for pointing these things out) Unreported - small performance improvement in the previous commit as well. --- DS_Map/Main Window.cs | 38 +++++++----- DS_Map/ScriptFile.cs | 132 ++++++++++++++++++++++++------------------ 2 files changed, 101 insertions(+), 69 deletions(-) diff --git a/DS_Map/Main Window.cs b/DS_Map/Main Window.cs index 40af203..2caa17e 100644 --- a/DS_Map/Main Window.cs +++ b/DS_Map/Main Window.cs @@ -2629,7 +2629,7 @@ namespace DSPRE { selectMapComboBox.SelectedIndex = selectMapComboBox.Items.Count - 1; } private void replaceMapBinButton_Click(object sender, EventArgs e) { - /* Prompt user to select .evt file */ + /* Prompt user to select .bin file */ OpenFileDialog of = new OpenFileDialog(); of.Filter = "Map BIN File (*.bin)|*.bin"; if (of.ShowDialog(this) != DialogResult.OK) @@ -5227,23 +5227,27 @@ namespace DSPRE { /* Add scripts */ disableHandlers = true; statusLabel.Text = "Parsing Script commands..."; + + string buffer = ""; for (int i = 0; i < currentScriptFile.scripts.Count; i++) { Script currentScript = currentScriptFile.scripts[i]; /* Write header */ string scrHeader = "----- " + "@Script_#" + (i + 1) + " -----" + Environment.NewLine; - scriptTextBox.AppendText(scrHeader, Color.Green); - scriptTextBox.Text += Environment.NewLine; + buffer += scrHeader; + buffer += Environment.NewLine; /* If current script is identical to another, print UseScript instead of commands */ if (currentScript.useScript != -1) - scriptTextBox.Text += "UseScript_#" + currentScript.useScript; + buffer += ("UseScript_#" + currentScript.useScript); else { for (int j = 0; j < currentScript.commands.Count; j++) - scriptTextBox.AppendText(currentScript.commands[j].cmdName + Environment.NewLine, Color.Black); + buffer += currentScript.commands[j].cmdName + Environment.NewLine; } - scriptTextBox.Text += Environment.NewLine; // Write blank line to separate next script + buffer += Environment.NewLine; // Write blank line to separate next script } + scriptTextBox.AppendText(buffer, Color.Green); + buffer = ""; /* Add functions */ statusLabel.Text = "Parsing Functions..."; @@ -5251,13 +5255,15 @@ namespace DSPRE { Script currentFunction = currentScriptFile.functions[i]; string funcHeader = "----- " + "@Function_#" + (i + 1) + " -----" + Environment.NewLine; - functionTextBox.AppendText(funcHeader, Color.Blue); - functionTextBox.Text += Environment.NewLine; + buffer += funcHeader; + buffer += Environment.NewLine; for (int j = 0; j < currentFunction.commands.Count; j++) - functionTextBox.Text += currentFunction.commands[j].cmdName + Environment.NewLine; + buffer += currentFunction.commands[j].cmdName + Environment.NewLine; - functionTextBox.Text += Environment.NewLine; + buffer += Environment.NewLine; } + functionTextBox.AppendText(buffer, Color.Blue); + buffer = ""; /* Add movements */ statusLabel.Text = "Parsing Movements..."; @@ -5265,14 +5271,18 @@ namespace DSPRE { Script currentMovement = currentScriptFile.movements[i]; string movHeader = "----- " + "@Movement_#" + (i + 1) + " -----" + Environment.NewLine; - movementTextBox.AppendText(movHeader, Color.Brown); - movementTextBox.Text += Environment.NewLine; + buffer += movHeader; + buffer += Environment.NewLine; for (int j = 0; j < currentMovement.commands.Count; j++) - movementTextBox.Text += currentMovement.commands[j].cmdName + Environment.NewLine; + buffer += currentMovement.commands[j].cmdName + Environment.NewLine; - movementTextBox.Text += Environment.NewLine; + buffer += Environment.NewLine; } + movementTextBox.AppendText(buffer, Color.Brown); + buffer = ""; } + + statusLabel.Text = "Ready"; disableHandlers = false; AddLineNumbers(scriptTextBox, LineNumberTextBoxScript); diff --git a/DS_Map/ScriptFile.cs b/DS_Map/ScriptFile.cs index 2f5926f..0efe016 100644 --- a/DS_Map/ScriptFile.cs +++ b/DS_Map/ScriptFile.cs @@ -66,7 +66,8 @@ namespace DSPRE { } } this.scripts.Add(new Script(commandsList)); - } else scripts.Add(new Script(duplicateIndex)); + } else + scripts.Add(new Script(duplicateIndex)); } /* Read functions */ @@ -281,14 +282,16 @@ namespace DSPRE { } } break; - case 0x2C4: { + case 0x2C4: + { byte parameter1 = dataReader.ReadByte(); parameters.Add(new byte[] { parameter1 }); if (parameter1 == 0 || parameter1 == 1) parameters.Add(dataReader.ReadBytes(2)); } break; - case 0x2C5: { + case 0x2C5: + { if (gameVersion == "Plat") { parameters.Add(dataReader.ReadBytes(2)); parameters.Add(dataReader.ReadBytes(2)); @@ -342,7 +345,7 @@ namespace DSPRE { /* Allocate enough space for script pointers, which we do not know yet */ writer.BaseStream.Position += scripts.Count * 0x4; - writer.Write((ushort)0xFD13); // End of header signal + writer.Write((ushort)0xFD13); // Signal the end of header section /* Write scripts */ for (int i = 0; i < scripts.Count; i++) { @@ -392,13 +395,16 @@ namespace DSPRE { /* Write command parameters */ List parameterList = functions[i].commands[j].parameterList; - for (int k = 0; k < parameterList.Count; k++) writer.Write(parameterList[k]); + for (int k = 0; k < parameterList.Count; k++) + writer.Write(parameterList[k]); /* If command calls a function/movement, store reference position */ if (referenceCodes.Contains(id)) { int index; - if (id == 0x16 || id == 0x1A) index = 0; - else index = 1; + if (id == 0x16 || id == 0x1A) + index = 0; + else + index = 1; int type = 0; if (id == 0x5E) @@ -408,6 +414,12 @@ namespace DSPRE { } } + + // Movements must be halfword-aligned + if (writer.BaseStream.Position % 2 == 1) { //Check if the writer's head is on an odd byte + writer.Write((byte)0); //Add padding + } + /* Write movements */ for (int i = 0; i < movements.Count; i++) { movementOffsets.Add((uint)writer.BaseStream.Position); @@ -474,17 +486,17 @@ namespace DSPRE { #endregion #region Constructors (2) - public Command(ushort id, List parameters, string gameVersion, bool isMovement) { + public Command(ushort id, List parameterList, string gameVersion, bool isMovement) { this.id = id; this.isMovement = isMovement; - this.parameterList = parameters; + this.parameterList = parameterList; ResourceManager commandNamesDatabase = RomInfo.scriptCommandNamesDatabase; if (isMovement) { commandNamesDatabase = new ResourceManager("DSPRE.Resources.MovementNames", Assembly.GetExecutingAssembly()); } - this.cmdName = commandNamesDatabase.GetString(id.ToString("X4")); + cmdName = commandNamesDatabase.GetString(id.ToString("X4")); if (cmdName == null) cmdName = id.ToString("X4"); @@ -497,51 +509,61 @@ namespace DSPRE { } } */ - - switch (id) { - case 0x16: // Jump - case 0x1A: // Call - this.cmdName += " " + "Function_#" + (1 + BitConverter.ToInt32(parameters[0], 0)).ToString("D"); - break; - case 0x1C: // CompareLastResultJump - case 0x1D: // CompareLastResultCall - byte opcode = parameters[0][0]; - this.cmdName += " " + RomInfo.scriptComparisonOperators.GetString(opcode.ToString("X")) + " " + "Function_#" + (1 + (BitConverter.ToInt32(parameters[1], 0))).ToString("D"); - break; - case 0x5E: // ApplyMovement - string owToMove = BitConverter.ToUInt16(parameters[0], 0).ToString("D"); - switch (owToMove) { - case "255": - owToMove = "Player"; - break; - case "253": - owToMove = "Following"; - break; - case "241": - owToMove = "Cam"; - break; - default: - owToMove = "Overworld_#" + owToMove; - break; - } - this.cmdName += " " + owToMove + " " + "Movement_#" + (1 + (BitConverter.ToInt32(parameters[1], 0))).ToString("D"); - break; - case 0x62: // Lock - case 0x63: // Release - case 0x64: // AddPeople - case 0x65: // RemoveOW - this.cmdName += " " + "Overworld_#" + BitConverter.ToInt16(parameters[0], 0).ToString("D"); - break; - default: - for (int i = 0; i < parameters.Count; i++) { - if (parameters[i].Length == 1) - this.cmdName += " " + "0x" + (parameters[i][0]).ToString("X1"); - else if (parameters[i].Length == 2) - this.cmdName += " " + "0x" + (BitConverter.ToInt16(parameters[i], 0)).ToString("X1"); - else if (parameters[i].Length == 4) - this.cmdName += " " + "0x" + (BitConverter.ToInt32(parameters[i], 0)).ToString("X1"); - } - break; + if (!isMovement) { + switch (id) { + case 0x16: // Jump + case 0x1A: // Call + this.cmdName += " " + "Function_#" + (1 + BitConverter.ToInt32(parameterList[0], 0)).ToString("D"); + break; + case 0x1C: // CompareLastResultJump + case 0x1D: // CompareLastResultCall + byte opcode = parameterList[0][0]; + this.cmdName += " " + RomInfo.scriptComparisonOperators.GetString(opcode.ToString("X")) + " " + "Function_#" + (1 + (BitConverter.ToInt32(parameterList[1], 0))).ToString("D"); + break; + case 0x5E: // ApplyMovement + string owToMove = BitConverter.ToUInt16(parameterList[0], 0).ToString("D"); + switch (owToMove) { + case "255": + owToMove = "Player"; + break; + case "253": + owToMove = "Following"; + break; + case "241": + owToMove = "Cam"; + break; + default: + owToMove = "Overworld_#" + owToMove; + break; + } + this.cmdName += " " + owToMove + " " + "Movement_#" + (1 + (BitConverter.ToInt32(parameterList[1], 0))).ToString("D"); + break; + case 0x62: // Lock + case 0x63: // Release + case 0x64: // AddPeople + case 0x65: // RemoveOW + this.cmdName += " " + "Overworld_#" + BitConverter.ToInt16(parameterList[0], 0).ToString("D"); + break; + default: + for (int i = 0; i < parameterList.Count; i++) { + if (parameterList[i].Length == 1) + this.cmdName += " " + "0x" + (parameterList[i][0]).ToString("X1"); + else if (parameterList[i].Length == 2) + this.cmdName += " " + "0x" + (BitConverter.ToInt16(parameterList[i], 0)).ToString("X1"); + else if (parameterList[i].Length == 4) + this.cmdName += " " + "0x" + (BitConverter.ToInt32(parameterList[i], 0)).ToString("X1"); + } + break; + } + } else { + for (int i = 0; i < parameterList.Count; i++) { + if (parameterList[i].Length == 1) + this.cmdName += " " + "0x" + (parameterList[i][0]).ToString("X1"); + else if (parameterList[i].Length == 2) + this.cmdName += " " + "0x" + (BitConverter.ToInt16(parameterList[i], 0)).ToString("X1"); + else if (parameterList[i].Length == 4) + this.cmdName += " " + "0x" + (BitConverter.ToInt32(parameterList[i], 0)).ToString("X1"); + } } } public Command(string description, string gameVersion, bool isMovement) {