private void ApiTimerCallback(object sender, EventArgs e) { if (DateTime.Now >= nextCmdTime) // Handle Button Event { while (commandIndex < scriptCommands.Count && scriptCommands[commandIndex].Command != "delay") { ScriptCommand cmd = scriptCommands[commandIndex]; if (cmd.Button != null) { apiCmd[cmd.Button] = cmd.Value; } else if (cmd.Command == "touch") { touchRunning = (cmd.Value != 0); if (touchRunning) { apiCmd[PS4Button.TraceOne] = 1; apiCmd[PS4Button.TraceOneX] = 10; apiCmd[PS4Button.TraceOneY] = 10; } else { apiCmd[PS4Button.TraceOne] = 0; } } commandIndex++; } CmCommand cmcmd = new CmCommand(apiCmd); Device.SendApiModeData(cmcmd); // Checking end of script if (commandIndex == scriptCommands.Count) { Stop(); return; } ScriptCommand delay = scriptCommands[commandIndex]; nextCmdTime = nextCmdTime.AddMilliseconds(delay.Value); RunningLineNumber = delay.LineNumber; commandIndex++; } else if (touchRunning) // Handle Touch Event //if (touchRunningToRight) { // apiCmd[PS4Button.TouchX] += 10; // apiCmd[PS4Button.TouchY] += 10; // if (apiCmd[PS4Button.TouchX] >= 90) touchRunningToRight = false; //} else { // apiCmd[PS4Button.TouchX] -= 10; // apiCmd[PS4Button.TouchY] -= 10; // if (apiCmd[PS4Button.TouchX] <= -90) touchRunningToRight = true; //} { apiCmd[PS4Button.TraceOneX] = rnd.Next(-100, 100); apiCmd[PS4Button.TraceOneY] = rnd.Next(-100, 100); CmCommand cmcmd = new CmCommand(apiCmd); Device.SendApiModeData(cmcmd); } }
public void LoadScript(string script) { if (Status == RunnerStatus.Running) { Stop(); } scriptCommands = ScriptCommand.ParseScript(script); if (scriptCommands.Count > 0) { Status = RunnerStatus.WaitingUser; System.Media.SystemSounds.Exclamation.Play(); } }
public static string NormalizeScript(string script) { var cmds = ParseScript(script); if (cmds.Count == 0) { return(""); } List <CommandDelayPair> result = new List <CommandDelayPair>(); for (int i = 0; i < cmds.Count; i++) { ScriptCommand cmd = cmds[i]; if (cmd.Command == "delay") { result.Add(new CommandDelayPair("delay", cmd.Value)); } else if (cmd.Button != null) { // Search down and Simplify int[] delay = { 0, 0 }; bool noMore = false; int count = 0; string midResult = ""; for ( ; i < cmds.Count; i++) { cmd = cmds[i]; if (cmd.Button != null) { if (!noMore && cmd.Value == 100) { midResult += cmd.Command + " "; count++; } else if (cmd.Value == 0) { count--; noMore = true; if (count == 0 && delay[1] == 0) { break; } } else { i--; break; } } else if (cmd.Command == "delay") { delay[noMore ? 1 : 0] += cmd.Value; } else { i--; break; } } if (midResult[midResult.Length - 1] == ' ') { midResult = midResult.Substring(0, midResult.Length - 1); } result.Add(new CommandDelayPair(midResult, delay[0], true)); if (delay[1] > 0) { result.Add(new CommandDelayPair("delay", delay[1])); } } else { // Search down for special command like: touch if (cmd.Value == 100) { int totalDelay = 0; while (cmds[++i].Command == "delay") { totalDelay += cmds[i].Value; } if (cmds[i].Command == cmd.Command && cmds[i].Value == 0) { result.Add(new CommandDelayPair(cmd.Command, totalDelay)); } } } } // Handle star int idx; int startIdx = 0; int iidx = result.FindIndex(startIdx, r => r.Command == "star"); while (startIdx < result.Count && (idx = result.FindIndex(startIdx, r => r.Command == "star")) != -1) { if (idx > 1) { CommandDelayPair before = result[idx - 1]; if (before.Command == "touch") { if (idx != 1 && result[idx - 2].Command == "delay") { result[idx - 2].Delay += before.Delay; result.RemoveAt(--idx); } else { before.Command = "delay"; } } } if (idx < result.Count - 2) { CommandDelayPair after = result[idx + 1]; if (after.Command == "delay" && result[idx + 2].Command == "touch") { if (idx < result.Count - 3 && result[idx + 3].Command == "delay") { after.Delay += result[idx + 2].Delay + result[idx + 3].Delay; result.RemoveAt(idx + 2); result.RemoveAt(idx + 2); } else { result.RemoveAt(idx + 1); result.RemoveAt(idx + 1); } } } startIdx = idx + 1; } // Normalize Button to 100ms for (int i = 0; i < result.Count; i++) { CommandDelayPair cmd = result[i]; if (cmd.Delay < 250 && cmd.IsButton) { if (i == result.Count - 1) { cmd.Delay = 100; } else if (result[i + 1].Command == "delay") { result[i + 1].Delay += cmd.Delay - 100; cmd.Delay = 100; } } } // Remove Last Delay if (result[result.Count - 1].Command == "delay") { result.RemoveAt(result.Count - 1); } return(string.Join("\r\n", result)); }