Вы находитесь на странице: 1из 3

Creating a “plug-in interface” for scripting programs

By: Turpentine Page 1

Preface:
This document intends to show methods and source code for developing a completely dynamic plug-in
system in Scripting applications. This will also show how easy it is to implement.

Plug-in syntax:
Because I will be using a method of calling all the functions from *.code which were loaded into memory
it will have to follow the syntax of my Script Engine although this can be changed to have its own special
handler.

/DevelopPlugin/plugin.name
DevelopPlugin
1
1

/DevelopPlugin/plugin.code
dbgprint DevelopementPlugin called

As you can see in the above files I have a definition file (*.name) and the function file (*.code). The first
line in the definition file is for the name to be called (plugin DevelopPlugin), the second is for the
beginning line and the third is for the ending line of the function file (the code to be called). Thus the code
one line one is called (dbgprint DevelopmentPlugin called).

Class for plugins:


Because I will be using a method of calling all the functions from *.code which were loaded into memory
it will have to follow the syntax of my Script Engine although this can be changed to have its own special
handler. Below is the basic code of what you PluginSystem could look like, as you see there is the
callname, the lines (start + end) and the code (to be called when the plug-in is called). You may also want
to make a Plugins system to handle all the plug-ins.

/PluginSystem.cs
namespace ScriptMin
{
class Plugin
{
public string CallName;
private int[] Lines;
public string[] Code;

public Plugin(string directory)


{

if( File.Exists(directory + "/plugin.name") == true && File.Exists(directory + "/plugin.code" ) )


{
StreamReader s = new StreamReader(directory + "/plugin.name");
Creating a “plug-in interface” for scripting programs
By: Turpentine Page 2

try
{
CallName = s.ReadLine();
}
catch {
MessageBox.Show("Could not read plugin name in: "+directory);
}

try
{
Lines = new int[] { Convert.ToInt32(s.ReadLine()), Convert.ToInt32(s.ReadLine()) };
}
catch {
MessageBox.Show("Could not read plugin lines in: " + directory);
}

s.Close();

s = new StreamReader(directory + "/plugin.code");

try
{
Code = new string[Lines[1] - Lines [0]];
for (int i = 0; i < Lines[1]; i++)
{
Code[i] = s.ReadLine();
}
}
catch
{

}
else
{
MessageBox.Show("Required plug-in files in: "+directory+" could not be located");
}

}
}
Creating a “plug-in interface” for scripting programs
By: Turpentine Page 3

Using this system:


Because the plug-in can be loaded simply minimal code is required and auto-loading can be implemented
by using a text file which lists all the directories. The two code sections show what your implementation
can look like:

/Main.cs
Plugins win = new Plugins();
if (win.ShowDialog() == DialogResult.OK)
{
Plugin test = new Plugin(win.PluginName);
MessageBox.Show(test.Code[0]);
}

/Plugins.cs
PluginName = textBox1.Text;
DialogResult = DialogResult.OK;
this.Close();

Вам также может понравиться