Making a Plugin engine out of Jurassic

Jul 18, 2014 at 9:42 AM
Hi all,
I was searching for this in the code and in the issues and discussions, but couldn't find anything.
What I'm trying to do is get all the function's name from a script, we are adding modding possibility to a game.
What I want to do is something like:
PluginEngine.cs
...

public static event JurassicEngine.AllLoadedDelegate OnAllPluginsLoaded;

public delegate void AllLoadedDelegate();

public static void AllPluginsLoaded()
{
    OnAllPluginsLoaded();
}

public override void Initialize()
{
    JurassicPlugin.plugins.Clear ();
    string[] directories = Directory.GetDirectories("modules/JurassicEngine/plugins/");
    foreach (string directory in directories)
    {
        string pluginName = Path.GetFileName(directory);
        JurassicPlugin plugin = new JurassicPlugin( (string) Util.GetRootFolder() + "\\Modules\\JurassicEngine\\plugins" + "\\" + pluginName + "\\" + pluginName + ".js");
        InstallHooks (plugin);

        JurassicPlugin.plugins.Add(pluginName, plugin);
        plugin.engine.Evaluate ((string) plugin.pluginInfo.Code);
    }
    AllPluginsLoaded();
}

public static void InstallHooks(JurassicPlugin plugin)
{
    JurassicEngine.OnAllPluginsLoaded += new JurassicEngine.AllLoadedDelegate(plugin.AllLoaded);
    // we have way more hooks here, just wanted to keep simple
}

...
Plugin.cs
...

public class JurassicPlugin
    {
        public bool state;
        public ScriptEngine engine;

        public PluginInfo pluginInfo;

        public static Dictionary<string, JurassicPlugin> plugins = new Dictionary<string, JurassicPlugin>();

        public JurassicPlugin(string path)
        {
            string source = File.ReadAllText (path);
            /*this.pluginInfo.Name = "Name";
            this.pluginInfo.Description = "Description";
            this.pluginInfo.Version = "1.00";
            this.pluginInfo.Author = "JhonDoe";*/ // this will be loaded from the script
            this.pluginInfo.Code = source;
            this.engine = new ScriptEngine ();
            this.state = false;
        }

    public void AllLoaded()
        {
            this.engine.CallGlobalFunction("On_AllPluginLoaded");
        }

    }

...
Now I want to add a plugin to the event to be called only if it has the function declared. Something like:

public static void InstallHooks(JurassicPlugin plugin)
{
    foreach(FunctionDeclaration func in Something.ThatContains.FunctionDeclarations)
    {
        UnityEngine.Debug.Log("Found function: " + func.Name);
        switch(func.Name)
        case "On_AllPluginLoaded":
            JurassicEngine.OnAllPluginsLoaded += new JurassicEngine.AllLoadedDelegate(plugin.AllLoaded);
            break;
    }
}
Thanks in advance
Coordinator
Jul 20, 2014 at 10:21 AM
You just want to check if a name has been set in the global scope, right?

Try this:
if (JurassicEngine.HasGlobalValue("On_AllPluginLoaded"))
  // The global value "On_AllPluginLoaded" exists.
Marked as answer by balu92 on 7/20/2014 at 12:17 PM
Jul 20, 2014 at 10:31 AM
Oh, I will! That's something I should have thought about, thanks!