private void SaveScriptMenuItem_Click(object sender, RoutedEventArgs e) { if (loadedWrd == null) { return; } // Parse the command textbox and replace loadedWRD's commands with the contents loadedWrd.Commands.Clear(); string[] lines = wrdCommandTextBox.Text.Split('\n'); for (int lineNum = 0; lineNum < lines.Length; ++lineNum) { // Do this as opposed to using StringSplitOptions above because we need to count line numbers correctly if (string.IsNullOrWhiteSpace(lines[lineNum])) { continue; } string opcode = lines[lineNum].Split('|').First(); // Verify the opcode is valid if (!WrdCommandHelper.OpcodeNames.Contains(opcode)) { MessageBox.Show($"ERROR: Invalid opcode at line {lineNum}."); wrdCommandTextBox.ScrollToLine(lineNum); return; } string[] args = lines[lineNum].Substring(opcode.Length + 1, lines[lineNum].Length - (opcode.Length + 1)).Split(", ", StringSplitOptions.RemoveEmptyEntries); // Verify that we are using the correct argument types for each command int opcodeId = Array.IndexOf(WrdCommandHelper.OpcodeNames, opcode); for (int argNum = 0; argNum < args.Length; ++argNum) { // Verify the number of arguments is correct int expectedArgCount = WrdCommandHelper.ArgTypeLists[opcodeId].Count; if (args.Length < expectedArgCount) { MessageBox.Show($"ERROR: Command at line {lineNum} expects {expectedArgCount} arguments, but got {args.Length}."); wrdCommandTextBox.ScrollToLine(lineNum); return; } else if (args.Length > expectedArgCount) { if (opcodeId != 1 && opcodeId != 3) { MessageBox.Show($"ERROR: Command at line {lineNum} expects {expectedArgCount} arguments, but got {args.Length}."); wrdCommandTextBox.ScrollToLine(lineNum); return; } } switch (WrdCommandHelper.ArgTypeLists[opcodeId][argNum % expectedArgCount]) { case 1: case 2: bool isNumber = ushort.TryParse(args[argNum], out _); if (!isNumber) { MessageBox.Show($"ERROR: Argument {argNum} at line {lineNum} must be a number between {ushort.MinValue} and {ushort.MaxValue}."); wrdCommandTextBox.ScrollToLine(lineNum); return; } break; } } (string Opcode, List <string> Arguments)tuple = (opcode, args.ToList()); loadedWrd.Commands.Add(tuple); } loadedWrd.Save(loadedWrdLocation); }
public void SaveFile() { wrd.Save(wrdPath); }