Plugin Development

From UBot Studio
(Difference between revisions)
Jump to: navigation, search
(Wizard Commands)
Line 399: Line 399:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==== Function With List Parameters ====
 +
 +
<syntaxhighlight lang="csharp">
 +
using System;
 +
using System.Collections.Generic;
 +
using System.Linq;
 +
using System.Text;
 +
using UBotPlugin;
 +
 +
namespace SimpleCommands
 +
{
 +
    /// <summary>
 +
    /// This command is an example of a command using list parameters.
 +
    /// It shuffles the input list sent to it..
 +
    /// Parameters: The hexadecimal color to use to display the window.
 +
    /// </summary>
 +
    public class ShuffleListFunction : IUBotFunction
 +
    {
 +
        // List to hold the parameters we define for our command.
 +
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
 +
        private List<string> _returnValue;
 +
 +
        public ShuffleListFunction()
 +
        {
 +
            // Add our parameter (List To Shuffle) with a UBotList type since it is a list.
 +
            _parameters.Add(new UBotParameterDefinition("List To Shuffle", UBotType.UBotList));
 +
        }
 +
 +
        public string Category
 +
        {
 +
            // This is what category our command is categorized as.
 +
            // If you choose something not already in the toolbox list, a new category will be created.
 +
            get { return "Variable Functions"; }
 +
        }
 +
 +
        public string FunctionName
 +
        {
 +
            // The name of our node in UBot Studio.
 +
            // You need to use a $ sign in front of function names.
 +
            get { return "$shuffle list"; }
 +
        }
 +
 +
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
 +
        {
 +
            // Get the name of the list they are using.
 +
            var listName = parameters["List To Shuffle"];
 +
 +
            // Get the list items from the list by using the list variable name.
 +
            var list = ubotStudio.GetList(listName);
 +
 +
            // Shuffle the list
 +
            var randomGenerator = new Random();
 +
            list = list.OrderBy(x => randomGenerator.Next()).ToList();
 +
 +
            // Set the return value to the shuffled list.
 +
            _returnValue = list;
 +
        }
 +
 +
        public bool IsContainer
 +
        {
 +
            // This command does not have any nested commands inside of it, so it is not a container command.
 +
            get { return false; }
 +
        }
 +
 +
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
 +
        {
 +
            // We reference our parameter list we defined above here.
 +
            get { return _parameters; }
 +
        }
 +
 +
        public object ReturnValue
 +
        {
 +
            // We return our variable _returnValue as the result of the function.
 +
            get { return _returnValue; }
 +
        }
 +
 +
        public UBotType ReturnValueType
 +
        {
 +
            // Our result is a list, so the return value type is UBotList.
 +
            get { return UBotType.UBotList; }
 +
        }
 +
    }     
 +
}
 +
</syntaxhighlight>
 +
 +
==== Command With Table Parameters
 +
 +
...
  
 
=== Container Commands ===
 
=== Container Commands ===

Revision as of 02:19, 17 October 2012

Contents

Getting Started

You can download the examples at: url.com

Making Your First Plugin

Plugin Interfaces

IUBotCommand

You should implement this interface when creating a new command for your plugin.

public interface IUBotCommand
{
    string CommandName { get; }
    string Category { get; }
    IEnumerable<UBotParameterDefinition> ParameterDefinitions { get; }
    void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters);
    bool IsContainer { get; }
}

IUBotFunction

public interface IUBotFunction
{
    string FunctionName { get; }
    object ReturnValue { get; }
    UBotType ReturnValueType { get; }
    string Category { get; }
    IEnumerable<UBotParameterDefinition> ParameterDefinitions { get; }
    void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters);
}

IUBotStudio

public interface IUBotStudio
{
    void RunScript(string script);
    void ChangeUserInterface(string name);
    IUBotCommand ContainerParent { get; set; }
    void RunContainerCommands();
    void SetVariable(string variableName, string value);
    void SetList(string listName, List<string> value);
    void SetTable(string tableName, string[,] value);
    string GetVariable(string variableName);
    List<string> GetList(string listName);
    string[,] GetTable(string tableName);
}

IWizard

public interface IWizard
{
    Window WizardWindow { get; }
    Dictionary<string, string> WizardValues { get; }
}

IWizardEnabled

public interface IWizardEnabled
{
    IWizard GenerateWizard();
}

IEventEnabled

public interface IEventEnabled
{
    void StartEventListener();
    void StopEventListener();
}

UBotParameterDefinition

public class UBotParameterDefinition
{
    public string Name { get; private set; }
    public UBotType Type { get; private set; }
    public IEnumerable<string> Options { get; set; }
    public object DefaultValue { get; set; }
 
    public UBotParameterDefinition(string name, UBotType type)
    {
        Name = name;
        Type = type;
    }
}

UBotType

public enum UBotType
{
    String,
    UBotVariable,
    UBotList,
    UBotTable
}

Plugin Examples

Basic Commands and Functions

Command Without Parameters

This is an example of a command that uses Process.Start to open Internet Explorer. It takes no parameters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
using System.Diagnostics;
 
namespace SimpleCommands
{
    /// <summary>
    /// This command is an example of a command with no parameters. It will open internet explorer when run.
    /// </summary>
    public class OpenInternetExplorerCommand : IUBotCommand
    {
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Browser Commands"; }
        }
 
        public string CommandName
        {
            // The name of our node in UBot Studio.
            get { return "open internet explorer"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Start the Internet Explorer process.
            Process.Start("iexplore.exe");
        }
 
        public bool IsContainer
        {
            // This command does not have any nested commands inside of it, so it is not a container command.
            get { return false; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // Since we have no parameters we return an empty list.
            get { return new List<UBotParameterDefinition>(); }
        }
    }
}

Command With Parameters

This is an example of a command that displays a MessageBox using the error icon. It takes one parameter, the text that is displayed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
using System.Windows;
 
namespace SimpleCommands
{
    /// <summary>
    /// This command is an example of a command with one parameter. It will show a MessageBox with an error icon.
    /// Parameters: The MessageBox text to use for your error.
    /// </summary>
    public class ErrorAlertCommand : IUBotCommand
    {
        // List to hold the parameters we define for our command.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
 
        public ErrorAlertCommand()
        {
            // Add our parameter (Alert Text) with a String type since it is text.
            _parameters.Add(new UBotParameterDefinition("Alert Text", UBotType.String));
        }
 
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Flow Commands"; }
        }
 
        public string CommandName
        {
            // The name of our node in UBot Studio.
            get { return "error alert"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Grab the value of the Alert Text parameter we defined.
            string value = parameters["Alert Text"];
 
            // Display the MessageBox with an error icon and the text given.
            MessageBox.Show(value, "Error Alert", MessageBoxButton.OK, MessageBoxImage.Error);
        }
 
        public bool IsContainer
        {
            // This command does not have any nested commands inside of it, so it is not a container command.
            get { return false; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
    }
}

Function Without Parameters

This is an example of a function that returns the time of a day (e.g. 3:45:12 PM). It takes no parameters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
using System.Threading;
 
namespace SimpleCommands
{
    /// <summary>
    /// This command is an example of a command that runs UScript.
    /// It will wait 5 seconds and then navigate to the given URL.
    /// </summary>
    public class TimeOfDayFunction : IUBotFunction
    {
        // List to hold the parameters we define for our command.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
        private string _returnValue;
 
        public TimeOfDayFunction()
        {
            // Add our parameter (URL) with a String type since it is text.
            _parameters.Add(new UBotParameterDefinition("URL", UBotType.String));
        }
 
        public string FunctionName
        {
            // The name of our node in UBot Studio.
            // You need to use a $ sign in front of function names.
            get { return "$time of day"; }
        }
 
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Browser Commands"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Set the return value to the current time formatted as 12:00:00 AM
            _returnValue = DateTime.Now.ToString("HH:mm:ss tt");
        }
 
        public bool IsContainer
        {
            // This command does not have any nested commands inside of it, so it is not a container command.
            get { return false; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
 
        public object ReturnValue
        {
            // We return our variable _returnValue as the result of the function.
            get { return _returnValue; }
        }
 
        public UBotType ReturnValueType
        {
            // Our result is text, so the return value type is String.
            get { return UBotType.String; }
        }
    }
}

Function With Parameters

This is an example of a function that returns the square of the number given as input. For example, an input of 3 would return 32 which is 9. It takes one parameter, the number to square.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
 
namespace SimpleCommands
{
    /// <summary>
    /// This function is an example of a function with one parameter. It will return the square of the input.
    /// Parameters: The MessageBox text to use for your error.
    /// </summary>
    public class SquareNumberFunction : IUBotFunction
    {
        // The default return value of our function is 0.
        string _returnValue = "0";
 
        // List to hold the parameters we define for our function.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
 
        public SquareNumberFunction()
        {
            // Add our parameter (Number to Square) with a String type since it is text.
            _parameters.Add(new UBotParameterDefinition("Number to Square", UBotType.String));
        }
 
        public string FunctionName
        {
            // The name of our node in UBot Studio.
            // You need to use a $ sign in front of function names.
            get { return "$square number"; }
        }
 
        public object ReturnValue
        {
            // We return our variable _returnValue as the result of the function.
            get { return _returnValue; }
        }
 
        public UBotType ReturnValueType
        {
            // Our result is text, so the return value type is String.
            get { return UBotType.String; }
        }
 
        public string Category
        {
            // This is what category our function is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Math Functions"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Grab the value of the Number to Square parameter we defined.
            string value = parameters["Number to Square"];
 
            // Parse the input as a double, in case it has a decimal in it.
            // If we are able to parse it, we square the number
            double valueAsNumber;
            if (double.TryParse(value, out valueAsNumber))
            {
                valueAsNumber = valueAsNumber * valueAsNumber;
            }
            else
            {
                valueAsNumber = 0;
            }
 
            // Set the return value to the number we calculated.
            _returnValue = valueAsNumber.ToString();
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
    }
}

Function With List Parameters

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
 
namespace SimpleCommands
{
    /// <summary>
    /// This command is an example of a command using list parameters.
    /// It shuffles the input list sent to it..
    /// Parameters: The hexadecimal color to use to display the window.
    /// </summary>
    public class ShuffleListFunction : IUBotFunction
    {
        // List to hold the parameters we define for our command.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
        private List<string> _returnValue;
 
        public ShuffleListFunction()
        {
            // Add our parameter (List To Shuffle) with a UBotList type since it is a list.
            _parameters.Add(new UBotParameterDefinition("List To Shuffle", UBotType.UBotList));
        }
 
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Variable Functions"; }
        }
 
        public string FunctionName
        {
            // The name of our node in UBot Studio.
            // You need to use a $ sign in front of function names.
            get { return "$shuffle list"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Get the name of the list they are using.
            var listName = parameters["List To Shuffle"];
 
            // Get the list items from the list by using the list variable name.
            var list = ubotStudio.GetList(listName);
 
            // Shuffle the list
            var randomGenerator = new Random();
            list = list.OrderBy(x => randomGenerator.Next()).ToList();
 
            // Set the return value to the shuffled list.
            _returnValue = list;
        }
 
        public bool IsContainer
        {
            // This command does not have any nested commands inside of it, so it is not a container command.
            get { return false; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
 
        public object ReturnValue
        {
            // We return our variable _returnValue as the result of the function.
            get { return _returnValue; }
        }
 
        public UBotType ReturnValueType
        {
            // Our result is a list, so the return value type is UBotList.
            get { return UBotType.UBotList; }
        }
    }       
}

==== Command With Table Parameters

...

Container Commands

This is an example of a container command that runs the nodes dragged inside of it twice as many times as specified. It has one parameter, Number of times to loop twice.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
 
namespace ContainerCommands
{
    /// <summary>
    /// This command is an example of a container command.
    /// It runs the nodes dragged inside of it 2 * the number of times specified to loop
    /// Parameters: Number of times to loop twice
    /// </summary>
    public class DoubleLoopCommand : IUBotCommand
    {
        // List to hold the parameters we define for our command.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
 
        public DoubleLoopCommand()
        {
            // Add our only parameter to the list of parameters.
            _parameters.Add(new UBotParameterDefinition("Number of times to loop twice", UBotType.String));
        }
 
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Flow Commands"; }
        }
 
        public string CommandName
        {
            // The name of our node in UBot Studio.
            get { return "double loop"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Grab the value of the Number of times to loop twice parameter we defined.
            string value = parameters["Number of times to loop twice"];
 
            // Since they are giving us a number, we parse it into an integer
            // We default to 0 if we can't parse it.
            int valueAsNumber;
            if (!int.TryParse(value, out valueAsNumber))
            {
                valueAsNumber = 0;
            }
 
            // Loop input * 2 times and run the contained commands for each loop.
            for (int i = 0; i < valueAsNumber * 2; i++)
            {
                ubotStudio.RunContainerCommands();
            }
        }
 
        public bool IsContainer
        {
            // Since this is a container command (you can drag nodes inside of it), we return true.
            get { return true; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
    }
}

Wizard Commands

This is an example of a command that uses a wizard along with displaying a WPF window when it is executed. The command uses a wizard to allow a user to choose a color with a color picker. It then displays a WPF window with the background set to that color.

For information on the WPF windows used in this example, download the Example Files.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UBotPlugin;
using System.Windows.Media;
using System.Windows;
using System.Windows.Threading;
 
namespace WizardCommands
{
    /// <summary>
    /// This command is an example of a command with a wizard.
    /// It will show a window with a background color that you choose.
    /// Parameters: The hexadecimal color to use to display the window.
    /// </summary>
    public class ColoredWindowCommand : IUBotCommand, IWizardEnabled
    {
        // List to hold the parameters we define for our command.
        private List<UBotParameterDefinition> _parameters = new List<UBotParameterDefinition>();
 
        // This holds our wizard Window.
        private ColorPickerWizardWindow _wizardWindow;
 
        public ColoredWindowCommand()
        {
            // Add our parameter (Color) with a String type since it is text.
            _parameters.Add(new UBotParameterDefinition("Color", UBotType.String));
        }
 
        public string Category
        {
            // This is what category our command is categorized as. 
            // If you choose something not already in the toolbox list, a new category will be created.
            get { return "Flow Commands"; }
        }
 
        public string CommandName
        {
            // The name of our node in UBot Studio.
            get { return "colored window"; }
        }
 
        public void Execute(IUBotStudio ubotStudio, Dictionary<string, string> parameters)
        {
            // Grab the value of the Color parameter we defined.
            string value = parameters["Color"];
 
            // Create a colored window and set the background color to the color they choose.
            var window = new ColoredWindow();
            window.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#" + value));
 
            // Display the colored window.
            window.ShowDialog();
        }
 
        public bool IsContainer
        {
            // This command does not have any nested commands inside of it, so it is not a container command.
            get { return false; }
        }
 
        public IEnumerable<UBotParameterDefinition> ParameterDefinitions
        {
            // We reference our parameter list we defined above here.
            get { return _parameters; }
        }
 
        public IWizard GenerateWizard()
        {
            return new ColoredWindowWizard();
        }
    }
 
    /// <summary>
    /// This wizard contains a color picker control that allows a user to choose a color by clicking it.
    /// </summary>
    public class ColoredWindowWizard : IWizard
    {
        private ColorPickerWizardWindow _wizardWindow = new ColorPickerWizardWindow();
 
        public Dictionary<string, string> WizardValues
        {
            // This property is checked after the wizard is displayed.
            // We return a Dictionary of values corresponding to the parameters.
            get
            {
                var parameterValues = new Dictionary<string, string>();
                // We use our _wizardWindow reference to access the color they selected in the wizard.
                // We then add that color value to our list of parameter values from our wizard.
                parameterValues["Color"] = _wizardWindow.MainColorPicker.SelectedColor.ToString().Substring(1);
                return parameterValues;
            }
        }
 
        public Window WizardWindow
        {
            // This property is accessed to determine which window to display for the wizard.
            get
            {
                return _wizardWindow;
            }
        }
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox