コード例 #1
0
        /// <summary>
        /// Loads this plugin
        /// </summary>
        public override void Load()
        {
            // Load the plugin into a table
            var source = File.ReadAllText(Filename);

            if (Regex.IsMatch(source, @"PLUGIN\.Version\s*=\s*[\'|""]", RegexOptions.IgnoreCase))
            {
                throw new Exception("Plugin version is a string, Oxide 1.18 plugins are not compatible with Oxide 2");
            }
            var pluginfunc = LuaEnvironment.LoadString(source, Path.GetFileName(Filename));

            if (pluginfunc == null)
            {
                throw new Exception("LoadString returned null for some reason");
            }
            LuaEnvironment.NewTable("PLUGIN");
            Table = (LuaTable)LuaEnvironment["PLUGIN"];
            ((LuaFunction)LuaEnvironment["setmetatable"]).Call(Table, luaExt.PluginMetatable);
            Table["Name"] = Name;
            pluginfunc.Call();

            // Read plugin attributes
            if (!(Table["Title"] is string))
            {
                throw new Exception("Plugin is missing title");
            }
            if (!(Table["Author"] is string))
            {
                throw new Exception("Plugin is missing author");
            }
            if (!(Table["Version"] is VersionNumber))
            {
                throw new Exception("Plugin is missing version");
            }
            Title   = (string)Table["Title"];
            Author  = (string)Table["Author"];
            Version = (VersionNumber)Table["Version"];
            if (Table["Description"] is string)
            {
                Description = (string)Table["Description"];
            }
            if (Table["ResourceId"] is double)
            {
                ResourceId = (int)(double)Table["ResourceId"];
            }
            if (Table["HasConfig"] is bool)
            {
                HasConfig = (bool)Table["HasConfig"];
            }

            // Set attributes
            Table["Object"] = this;
            Table["Plugin"] = this;

            // Get all functions and hook them
            functions = new Dictionary <string, LuaFunction>();
            foreach (var keyobj in Table.Keys)
            {
                var key = keyobj as string;
                if (key == null)
                {
                    continue;
                }
                var value = Table[key];
                var func  = value as LuaFunction;
                if (func != null)
                {
                    functions.Add(key, func);
                }
            }
            if (!HasConfig)
            {
                HasConfig = functions.ContainsKey("LoadDefaultConfig");
            }

            // Bind any base methods (we do it here because we don't want them to be hooked)
            BindBaseMethods();

            // Deal with any attributes
            var attribs = Table["_attribArr"] as LuaTable;

            if (attribs != null)
            {
                var i = 0;
                while (attribs[++i] != null)
                {
                    var attrib     = attribs[i] as LuaTable;
                    var attribName = attrib["_attribName"] as string;
                    var attribFunc = attrib["_func"] as LuaFunction;
                    if (attribFunc != null && !string.IsNullOrEmpty(attribName))
                    {
                        HandleAttribute(attribName, attribFunc, attrib);
                    }
                }
            }

            // Clean up
            LuaEnvironment["PLUGIN"] = null;
        }
コード例 #2
0
        /// <summary>
        /// Loads this plugin
        /// </summary>
        public void Load()
        {
            // Load the plugin into a table
            string      source     = File.ReadAllText(Filename);
            LuaFunction pluginfunc = LuaEnvironment.LoadString(source, Path.GetFileName(Filename));

            if (pluginfunc == null)
            {
                throw new Exception("LoadString returned null for some reason");
            }
            LuaEnvironment.NewTable("PLUGIN");
            Table         = LuaEnvironment["PLUGIN"] as LuaTable;
            Name          = Path.GetFileNameWithoutExtension(Filename);
            Table["Name"] = Name;
            pluginfunc.Call();

            // Read plugin attributes
            if (Table["Title"] == null || !(Table["Title"] is string))
            {
                throw new Exception("Plugin is missing title");
            }
            if (Table["Author"] == null || !(Table["Author"] is string))
            {
                throw new Exception("Plugin is missing author");
            }
            if (Table["Version"] == null || !(Table["Version"] is VersionNumber))
            {
                throw new Exception("Plugin is missing version");
            }
            Title   = (string)Table["Title"];
            Author  = (string)Table["Author"];
            Version = (VersionNumber)Table["Version"];
            if (Table["ResourceId"] is int)
            {
                ResourceId = (int)Table["ResourceId"];
            }
            if (Table["HasConfig"] is bool)
            {
                HasConfig = (bool)Table["HasConfig"];
            }

            // Set attributes
            Table["Object"] = this;
            Table["Plugin"] = this;

            // Get all functions and hook them
            functions = new Dictionary <string, LuaFunction>();
            foreach (var keyobj in Table.Keys)
            {
                string key = keyobj as string;
                if (key != null)
                {
                    object      value = Table[key];
                    LuaFunction func  = value as LuaFunction;
                    if (func != null)
                    {
                        functions.Add(key, func);
                    }
                }
            }

            // Bind any base methods (we do it here because we don't want them to be hooked)
            BindBaseMethods();

            // Clean up
            LuaEnvironment["PLUGIN"] = null;
        }