Plugin: C# Runner

Started by tzachs, Mon 16/08/2010 20:27:29

Previous topic - Next topic

tzachs

This plugin will let you compile & run c# code from within ags (in runtime)!

It uses smiley's awesome AGS Plugin API for C#.

Why&how should I use it?
---------------------------------

Here's a simple scenario:
Suppose you have a string, and you want to get the last index of 'n' within the string.
You remember that AGS have an IndexOf function, but it doesn't have a LastIndexOf function.
But c#, on the other hand, does have this function.
So you can, with this plugin, compile the code on the spot and then run it whenever you feel like it.
To compile it:
Code: ags

//We choose CSCompileStringToInt because we want a function that gets a string
//and returns an int. 
//The first argument to this is the c# code, the second argument is the name of the class.
//The name of the class will also be used as the name of the created dll file.
//The functions returns true if compilation succeeds or false if failed.
if (CSCompileStringToInt("return value.LastIndexOf('n');", "GetLastIndexOf"))
{
   Display("Compiled successfully!");
}
else
{
   Display("Compilation failed!");
}


And then to run it:
Code: ags

//The first argument is the input value we give to the function we compiled earlier,
//the second argument is the name of the class/dll to use.
int lastIndex = CSRunStringToInt("An example string", "GetLastIndexOf");


Basically this plugin has several compile functions and several run functions for different types of arguments and return values (all combinations of string,int,float,bool).

The compile functions will try to compile a dll with your code, and then you can use the run functions to run the code from this dll.
If compilation/run fails, the error will be written to a text file: CS Plugin Debug.txt, so you can view them.

Pre Compiled dlls
----------------------

If you want, you can compile your own dll with visual studio, place it in the appropriate folder (the _Debug folder while debugging, and the Compiled folder when deploying) and then you will be able able to use the Run functions to run functions from your dll.
This can be useful if you need to run some more complicated code, or if you are missing references.
In order for the plugin to recognise your functions, you need to use the same name for the dll, namespace and class, to name your functions "Execute" and make them public. The functions must be in one of the supported formats (I.E one paramter of type string/int/float/bool and return value of type string/int/float/bool).

The Demo
-------------



The demo is really a small compiler, it also shows what happens behind the scenes when you run the "Compile" function, when it actually wraps it in the code that you see on the screen.

Download
-------------
Download the plugin&demo from here.

The download has both the binaries and the source code.

Requirements
-----------------
- Windows
- Dot Net framework 3.5 installed
- VC++ 2010 redist installed, at http://www.microsoft.com/downloads/details.aspx?FamilyID=a7b7a05e-6de6-4d3a-a423-37bf0912db84&displaylang=en

Install
--------
In order to use the plugin in your game, here's what you need to do:
Go to the plugin binaries folder.
Copy the wrapper dll and the plugin dll to the AGS folder.

You can now use the plugin in your game,

Before distribution, I think that you need to copy the wrapper to the compiled folder as well...

bicilotti

Holy Salsa, lemme try this!

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Wonkyth

"But with a ninja on your face, you live longer!"

Virgil



edmundito

You're a madman tzachs! A madman! :D
The Tween Module now supports AGS 3.6.0!

goodohman

Why Edmundito, do you feel he opened the gate for all hell to break loose in the form of C# scripting? ::)

tzachs

Nah, he's just telling it like it really is (it's not really related to this thread at all)...

SMF spam blocked by CleanTalk