Beispiel #1
0
        public JavaScriptPlugin(string fileName)
        {
            PluginFile = fileName;

            _engine = new JintEngine();
            _engine.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
            _engine.AllowClr = true;

            Properties = new PluginProperties(Path.GetFileName(PluginFile), PluginManager.PluginDirectoryPath);
            Properties.Load();

            _engine.SetParameter("properties", Properties);
            _engine.SetParameter("plugindir", PluginManager.PluginDirectoryPath);

            _engine.SetFunction("WriteToConsole", new Action <object>(Console.WriteLine));
            _engine.SetFunction("SubscribeToEvent", new Action <string, JsFunction>(SubscribeToEvent));
            _engine.SetFunction("UnsubscribeFromEvent", new Action <string>(UnsubscribeFromEvent));
            _engine.SetFunction("RegisterCommand", new Action <string, JsFunction>(RegisterCommand));
            _engine.SetFunction("UnregisterCommand", new Action <string>(UnregisterCommand));
            _engine.SetFunction("RegisterConsoleCommand", new Action <string, JsFunction>(RegisterConsoleCommand));
            _engine.SetFunction("UnregisterConsoleCommand", new Action <string>(UnregisterConsoleCommand));
            _engine.SetFunction("SendPacket", new Action <SharpStarClient, IPacket>(SendPacket));
            _engine.SetFunction("SendClientPacketToAll", new Action <IPacket>(SendClientPacketToAll));
            _engine.SetFunction("SendServerPacketToAll", new Action <IPacket>(SendServerPacketToAll));
            _engine.SetFunction("GetPlayerClients", new Func <IClient[]>(GetPlayerClients));
            _engine.SetFunction("GetServerClients", new Func <IClient[]>(GetServerClients));

            _engine.SetFunction("ToArray", new Func <JsArray, object[]>(JsArrayToArray));

            _registeredEvents          = new Dictionary <string, JsFunction>();
            _registeredCommands        = new Dictionary <string, JsFunction>();
            _registeredConsoleCommands = new Dictionary <string, JsFunction>();
        }
Beispiel #2
0
        public void OnLoad()
        {
            Enabled = true;

            _registeredEvents.Clear();
            _registeredCommands.Clear();

            Properties = new PluginProperties(Path.GetFileName(PluginFile), PluginManager.PluginDirectoryPath);
            Properties.Load();

            _lua = new Lua();
            _lua.LoadCLRPackage();

            _lua["properties"] = Properties;
            _lua["plugindir"]  = PluginManager.PluginDirectoryPath;

            _lua.DoString("package.path = package.path .. \";plugins/?.lua\"");

            RegisterTypes();
            RegisterFunctions();

            _lua.DoFile(PluginFile);

            LuaFunction onLoad = _lua.GetFunction("onLoad");

            if (onLoad != null)
            {
                try
                {
                    onLoad.Call();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Plugin {0} caused error: {1}", Path.GetFileName(PluginFile), ex.Message);
                }
            }
        }