Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Proposal : Switch from Lua to CSharp Scripting Language #1

Open
MatthewFrench opened this issue Jan 23, 2017 · 0 comments
Open

Proposal : Switch from Lua to CSharp Scripting Language #1

MatthewFrench opened this issue Jan 23, 2017 · 0 comments

Comments

@MatthewFrench
Copy link

MatthewFrench commented Jan 23, 2017

Pros:

• Faster execution (5 times faster - benchmarks referenced from Scripting-Languages-Comparison, has the speed of compiled C#)
• Error checking and auto completion
• No required object conversions or registering functions for the script engine
• All of C# language features including inheritance
• Easy to use and understand
• Consistent language to the GameServer
• Ability to easily move sections of GameServer code to be hot-reloadable C# script with minimal changes

Cons:

• Slightly slower loading/hot re-loading (Lua loaded 1000 scripts in 1 seconds, C# loaded 1000 scripts in 1.5 seconds)
• 2 second startup time to prepare the C# script compiler (Only needs done once)
• All scripts must be hot re-loaded together in a single Assembly for best results

Script engine is already built and testable in scripting-engine-test. All Lua scripts have been converted to C#.

Script engine benchmark:

Prepare Compiler Elapsed(MS) = 1764.6604 - FPS: 0.566681271931982
Script Loading Elapsed(MS) = 47.7569 - FPS: 20.9393825813652
Compiled class Elapsed(MS) = 1171.9966 - FPS: 0.853244796102651
Loaded assembly Elapsed(MS) = 0.3413 - FPS: 2929.97363023733
Started casting from Ezreal EObject
Script Assembly Type Lookup Elapsed(MS) = 0.0566 - FPS: 17667.8445229682
Hello Benchmark from Ezreal's Q
Program Execution Time Elapsed(MS) = 3530.72 - FPS: 0.283228350024924

Noteworthy statistics: 1.1 second compiling time for all C# spell scripts

Script engine structure:

public class CSharpScriptEngine
{
    public CSharpScriptEngine();
    public void prepareCompiler();
    public void loadSubdirectoryScripts(String folder);
    public void load(List<string> scriptLocations);
    public T getStaticMethod<T>(String scriptNamespace, String scriptClass, String scriptFunction);
    public T createObject<T>(String scriptNamespace, String scriptClass);
    public static T getObjectMethod<T>(object obj, String scriptFunction);
    public static object runFunctionOnObject(object obj, String method, params object[] args);
}

Example code: (Testable in scripting-engine-test)

static void Main(string[] args)
{
    CSharpScriptEngine scriptingEngine = new CSharpScriptEngine();
    scriptingEngine.prepareCompiler();

    //Recursively find and load all CS scripts in the directory
    scriptingEngine.loadSubdirectoryScripts("LeagueSandbox-Default/");

    //Get and call static function
    Action<double> ezrealQOnUpdate = scriptingEngine.getStaticMethod<Action<double>>("Ezreal", "Q", "onUpdate");
    ezrealQOnUpdate( 50.0 );

    //Get and call static function with return value
    Func<double> ezrealQFakeFunction = scriptingEngine.getStaticMethod<Func<double>>("Ezreal", "Q", "fakeFunc");
    double number = ezrealQFakeFunction();

    //Create object that has an Interface definition and call defined function
    ISpellScript ezrealEObject = scriptingEngine.createObject<ISpellScript>("Ezreal", "EObject");
    ezrealEObject.onStartCasting(null);
           
    //Create object without Interface definition and call function 
    object ezrealEObjectGeneric = scriptingEngine.createObject<object>("Ezreal", "EObject");
    CSharpScriptEngine.runFunctionOnObject(ezrealEObjectGeneric, "onStartCasting", new object[] { null });

    //Get function of an object without an interface definition as a delegate and call it
    Action<Champion> onObjectStartCasting = CSharpScriptEngine.getObjectMethod<Action<Champion>>(ezrealEObjectGeneric, "onStartCasting");
    onObjectStartCasting(null);
}

References:

• https://github.com/MatthewFrench/scripting-engine-test
https://github.com/MatthewFrench/Scripting-Languages-Comparison

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant