private static async Task HandleVariableCommand(ExtendedCommand command, VirtualKeyCodeContainer macro, WriterSettings settings)
        {
            string upperExtendedData = macro.ExtendedData.ToUpperInvariant();

            // Variables
            if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_INPUT)
            {
                string defaultValue = String.Empty;
                if (!String.IsNullOrEmpty(upperExtendedData) && dicVariables.ContainsKey(upperExtendedData))
                {
                    defaultValue = dicVariables[upperExtendedData];
                }

                using InputBox input = new InputBox("Variable Input", $"Enter value for \"{upperExtendedData}\":", defaultValue);
                input.ShowDialog();

                // Value exists (cancel button was NOT pressed)
                if (!string.IsNullOrEmpty(input.Input))
                {
                    dicVariables[upperExtendedData] = input.Input;
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT)
            {
                if (dicVariables.ContainsKey(upperExtendedData))
                {
                    SuperMacroWriter textWriter = new SuperMacroWriter();
                    await textWriter.SendInput(dicVariables[upperExtendedData], settings, null, false);
                }
                else
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Output called for {upperExtendedData} without an Input beforehand");
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSETALL)
            {
                dicVariables.Clear();
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSET)
            {
                if (string.IsNullOrEmpty(upperExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Unset called without variable name");
                    return;
                }
                if (dicVariables.ContainsKey(upperExtendedData))
                {
                    dicVariables.Remove(upperExtendedData);
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE)
            {
                if (string.IsNullOrEmpty(macro.ExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without variable name");
                    return;
                }

                // last parameter "2" ensures maximum of two splits!
                var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2);
                if (splitData.Length != 2)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without incorrect extended data: {macro.ExtendedData}");
                    return;
                }
                string varInput = splitData[1];
                string fileName = TryExtractVariable(splitData[1]);

                // Set From File but file doesn't exist
                if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE && !File.Exists(fileName))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable SetFromFile called but file does not exist {fileName}");
                    return;
                }
                else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) // File DOES exist
                {
                    varInput = File.ReadAllText(fileName);
                }

                dicVariables[splitData[0].ToUpperInvariant()] = varInput;
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_CLIPBOARD)
            {
                var value = ReadFromClipboard();

                // Value exists (cancel button was NOT pressed)
                if (!string.IsNullOrEmpty(value))
                {
                    dicVariables[upperExtendedData] = value;
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT_TO_FILE)
            {
                if (string.IsNullOrEmpty(macro.ExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without any params");
                    return;
                }

                var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2);
                if (splitData.Length != 2)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without incorrect extended data: {macro.ExtendedData}");
                    return;
                }

                string variableName = splitData[0].ToUpperInvariant();
                string fileName     = TryExtractVariable(splitData[1]);

                // Check if variable exists
                if (!dicVariables.ContainsKey(variableName))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without non existing variable: {variableName}");
                    return;
                }

                // Try to save the data in the variable to the filename
                try
                {
                    File.WriteAllText(fileName, dicVariables[variableName]);
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile exception: {macro.ExtendedData} {ex}");
                }
                return;
            }
            else
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"HandleVariableCommand - Invalid command {command}");
            }
        }
Exemple #2
0
        private static void HandleVariableCommand(ExtendedCommand command, InputSimulator iis, VirtualKeyCodeContainer macro, WriterSettings settings, SDConnection connection)
        {
            string upperExtendedData = macro.ExtendedData.ToUpperInvariant();

            // Variables
            if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_INPUT)
            {
                string defaultValue = String.Empty;
                if (!String.IsNullOrEmpty(upperExtendedData) && dicVariables.ContainsKey(upperExtendedData))
                {
                    defaultValue = dicVariables[upperExtendedData];
                }

                using InputBox input = new InputBox("Variable Input", $"Enter value for \"{upperExtendedData}\":", defaultValue);
                input.ShowDialog();

                // Value exists (cancel button was NOT pressed)
                if (!string.IsNullOrEmpty(input.Input))
                {
                    dicVariables[upperExtendedData] = input.Input;
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT)
            {
                if (dicVariables.ContainsKey(upperExtendedData))
                {
                    SuperMacroWriter textWriter = new SuperMacroWriter(connection);
                    textWriter.SendInput(dicVariables[upperExtendedData], settings, null, false);
                }
                else
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Output called for {upperExtendedData} without an Input beforehand");
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSETALL)
            {
                dicVariables.Clear();
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_UNSET)
            {
                if (string.IsNullOrEmpty(upperExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Unset called without variable name");
                    return;
                }
                if (dicVariables.ContainsKey(upperExtendedData))
                {
                    dicVariables.Remove(upperExtendedData);
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET || command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE)
            {
                if (string.IsNullOrEmpty(macro.ExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without variable name");
                    return;
                }

                var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2);
                if (splitData.Length != 2)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable Set called without incorrect extended data: {macro.ExtendedData}");
                    return;
                }
                string varInput = splitData[1];

                // Set From File but file doesn't exist
                if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE && !File.Exists(splitData[1]))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"Variable SetFromFile called but file does not exist {splitData[1]}");
                    return;
                }
                else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_FILE) // File DOES exist
                {
                    varInput = File.ReadAllText(splitData[1]);
                }

                dicVariables[splitData[0].ToUpperInvariant()] = varInput;
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_FROM_CLIPBOARD)
            {
                var value = ReadFromClipboard();

                // Value exists (cancel button was NOT pressed)
                if (!string.IsNullOrEmpty(value))
                {
                    dicVariables[upperExtendedData] = value;
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_OUTPUT_TO_FILE)
            {
                if (string.IsNullOrEmpty(macro.ExtendedData))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without any params");
                    return;
                }

                var splitData = macro.ExtendedData.Split(new char[] { ':' }, 2);
                if (splitData.Length != 2)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without incorrect extended data: {macro.ExtendedData}");
                    return;
                }

                string variableName = splitData[0].ToUpperInvariant();
                string fileName     = splitData[1];

                // Check if variable exists
                if (!dicVariables.ContainsKey(variableName))
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile called without non existing variable: {variableName}");
                    return;
                }

                // Try to save the data in the variable to the filename
                try
                {
                    File.WriteAllText(fileName, dicVariables[variableName]);
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, $"OutputToFile exception: {macro.ExtendedData} {ex}");
                }
                return;
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_RUN)
            {
                try
                {
                    upperExtendedData = upperExtendedData.ToLower();
                    upperExtendedData = upperExtendedData.Trim();
                    int pos = upperExtendedData.IndexOf(".exe", StringComparison.InvariantCultureIgnoreCase);
                    if (pos == -1)
                    {
                        return;
                    }
                    string exePath = upperExtendedData.Substring(0, pos + 4);
                    string args    = "";
                    if (!upperExtendedData.EndsWith(".exe"))
                    {
                        args = upperExtendedData.Substring(pos + 5, upperExtendedData.Length - (pos + 5));
                    }

                    if (File.Exists(exePath))
                    {
                        if (args != "")
                        {
                            Process.Start(exePath, args);
                        }
                        else
                        {
                            Process.Start(exePath.ToLower());
                        }
                    }
                    return;
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, string.Format("Run exception: {0} {1}", macro.ExtendedData, ex));
                    return;
                }
            }
            else if (command == ExtendedCommand.EXTENDED_MACRO_VARIABLE_SET_PROFILE)
            {
                try
                {
                    connection.SwitchProfileAsync(upperExtendedData).Wait();
                    return;
                }
                catch (Exception arg2)
                {
                    Logger.Instance.LogMessage(TracingLevel.WARN, string.Format("SetProfile exception: {0} {1}", macro.ExtendedData, arg2));
                    return;
                }
            }
            else
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"HandleVariableCommand - Invalid command {command}");
            }
        }