Throughout my time at Fragnet Networks I have experimented a lot with the quite powerful scripting engine in TCAv2 that is done with IronPython. It has come in very handy and proved to work extremely well with what we have done, however I have also felt the need to extend functionalites. So what did I do? Well I took it upon myself to extend TCAdmin seeing as IronPython is CLR based.
During my first test I decided to create a very simple C# Library DLL using .Net 3.5 (as I assume this is what TCAv2 is based on).
For Lua look here: http://clientforums.tcadmin.com/showpost.php?p=75318&postcount=6
namespace TestLibrary{
public class MyClass{
public string HelloWorld = "HelloWorld";
}
}
I then compiled this into TestLibrary.dll and then put it in C:/Program Files/TCAdmin2/Monitor/Shared. After having added it into the folder it was time to play. I created a new script with the following content in TCAdmin:
import clr;
clr.AddReference("TestLibrary");
from TestLibrary import MyClass;
myClass = MyClass();
Script.WriteToConsole(myClass.HelloWorld);
So what was the result when ran? I got a nice and shiny "Hello World" in the script console! HURRAH! We found our way in!
Now how do we make this work on all remotes? Well we let the script download the file of course using the WebClient class function "DownloadFile" of course into the shared folder. So let's change the code:
import clr;
from System.Net import WebClient;
from System.IO import File, Path;
from System import Activator;
from System import Array;
dllUrl = "http://example.com/TestLibrary.dll";
dllPath = Path.Combine(TCAdminFolder, "Monitor\Shared\TestLibrary.dll");
#Check if the dll file doesn't exist on the remote, and download it
if File.Exists(dllPath) == False:
client = WebClient();
client.DownloadFile(dllUrl, dllPath);
Script.WriteToConsole("DLL downloaded...");
#Load assembly into memory
clr.AddReference("TestLibrary");
from TestLibrary import MyClass;
myClass = MyClass();
Script.WriteToConsole(myClass.HelloWorld);
HUZZAH! More success! But being crazy as I am I wouldn't stop at that, I continued my experimentation and thought... What if I can load C# right into IronPython instead and skip the library? Well I have the pleasure of saying, yes, yes we can.
This was done using the CSScriptLibrary] as it has proven to be very powerful in the past. Let's get to it below:
import clr;
from System.Net import WebClient;
from System.IO import File, Path;
from System import Activator;
from System import Array;
dllUrl = "http://example.com/CSScriptLibrary.v3.5.dll";
dllPath = Path.Combine(TCAdminFolder, "Monitor\Shared\CSScriptLibrary.v3.5.dll");
if File.Exists(dllPath) == False:
client = WebClient();
client.DownloadFile(dllUrl, dllPath);
Script.WriteToConsole("DLL downloaded...");
clr.AddReference("CSScriptLibrary.v3.5");
from CSScriptLibrary import CSScript;
parameters = Array[str]([Path.Combine(TCAdminFolder, "Monitor\Shared\TCAdmin.SDK.dll"), Path.Combine(TCAdminFolder, "Monitor\Shared\TCAdmin.GameHosting.SDK.dll")])
result = CSScript.LoadCodeFrom(Path.Combine(ThisService.RootDirectory, "myscript.cs"));
script = result.GetType("MyScript");
mainMethod = script.GetMethod("MainHook");
activateMe = Activator.CreateInstance(script);
mainMethod.Invoke(activateMe, Array[object]([script, ThisServer, ThisService]));
This will compile our fiel myscript.cs with the TCAdmin.SDK and TCAdmin.GameHosting.SDK which will allow us access to the script window and service/server settings.
Here is the source of myscript.cs as I put in the game folder:
using System;
using TCAdmin.SDK.Scripting.Engines;
using TCAdmin.GameHosting.SDK.Objects;
public class MyScript
{
IronPythonEngine Script;
Server ThisServer;
Service ThisService;
public void MainHook(IronPythonEngine script, Server server, Service service)
{
Script = script;
ThisServer = server;
ThisService = service;
Script.WriteToConsole("Hello World " + ThisService.GameName);
}
}
So then I run the script and what output do we get?
Hello World RUST Experimental
The script has executed successfully. You may close this window.
Ta-da! We can now write pure C# for TCAdmin instead of IronPython (I do prefer it). Although it may definitely not be very efficient it does work and suffice for all tasks as you need them.
Now feel free to do what you want and just have fun with your new knowledge of what can be done in IronPython and just how powerful it all is.