private void LoadScript(object sender, EventArgs e) { try { var scriptpath = Path.Combine(Application.StartupPath, "Scripts"); Directory.CreateDirectory(scriptpath); var dialog = new OpenFileDialog() { Filter = "PKM-NTR script | *.pkscript", InitialDirectory = scriptpath }; string[] lines; if (dialog.ShowDialog() == DialogResult.OK) { lines = File.ReadAllLines(dialog.FileName); } else { return; } actions.Clear(); UpdateActionList(); foreach (string str in lines) { int[] instruction = Array.ConvertAll(str.Split(','), int.Parse); switch ((ScriptAction.ActionType)instruction[0]) { case ScriptAction.ActionType.Button: AddActionToList(new ButtonAction((ButtonAction.ConsoleButton)instruction[1], instruction[2])); break; case ScriptAction.ActionType.Touch: AddActionToList(new TouchAction(instruction[1], instruction[2], instruction[3])); break; case ScriptAction.ActionType.Stick: AddActionToList(new StickAction(instruction[1], instruction[2], instruction[3])); break; case ScriptAction.ActionType.Delay: AddActionToList(new DelayAction(instruction[1])); break; case ScriptAction.ActionType.StartFor: AddActionToList(new StartFor(instruction[1], instruction[2])); break; case ScriptAction.ActionType.EndFor: AddActionToList(new EndFor(instruction[1])); break; default: ScriptAction.Report($"Script: Invalid line - {str}"); break; } } } catch (Exception ex) { MessageBox.Show("A error has ocurred while loading the script:\r\n\r\n" + ex.Message, "Script builder", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void ExecuteScript() { if (this.InvokeRequired) { BeginInvoke(new MethodDelegate(ExecuteScript)); return; } if (actions.Count < 1) { MessageBox.Show("No actions to execute.", "Script Builder", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } SetControls(false); Program.gCmdWindow.ScriptMode(true); scriptRunning = true; ScriptAction.Report("Script: Start script"); for (int index = 0; index < actions.Count; index++) { lstActions.SelectedIndex = index; if (actions[index] is StartFor) { // Search for EndFor if (((StartFor)actions[index]).EndInstruction < 0) { int endins = SearchEndFor(index, ((StartFor)actions[index]).ID); if (endins >= 0) { ((StartFor)actions[index]).EndInstruction = endins; } else { ScriptAction.Report("Script: End of loop not found"); break; } } // Check if finished, and restart if (((StartFor)actions[index]).IsFinished) { await Task.Delay(200); int tempins = ((StartFor)actions[index]).EndInstruction; ((StartFor)actions[index]).EndInstruction = -1; ((StartFor)actions[index]).Loops = 0; Delg.SetValue(numFor, 0); index = tempins; ((EndFor)actions[index]).StartInstruction = -1; ScriptAction.Report("Script: End loops"); continue; } else { Delg.SetValue(numFor, ((StartFor)actions[index]).TotalLoops - ((StartFor)actions[index]).Loops); } } else if (actions[index] is EndFor) { // Search for StartFor if (((EndFor)actions[index]).StartInstruction < 0) { int startins = SearchStartFor(index, ((EndFor)actions[index]).ID); if (startins >= 0) { ((EndFor)actions[index]).StartInstruction = startins; } else { ScriptAction.Report("Script: Start of loop not found"); break; } } await actions[index].Excecute(); index = ((EndFor)actions[index]).StartInstruction - 1; continue; } if (stopScript || !Program.gCmdWindow.IsConnected) { break; } await actions[index].Excecute(); } ScriptAction.Report("Script: Stop script"); scriptRunning = false; Program.gCmdWindow.ScriptMode(false); MessageBox.Show("Script finished.", "Script Builder", MessageBoxButtons.OK, MessageBoxIcon.Information); if (Program.gCmdWindow.IsConnected) { btnStartStop.Text = "Start Script"; btnStartStop.Enabled = true; } else { btnStartStop.Text = "Not connected"; btnStartStop.Enabled = false; } SetControls(true); }