Пример #1
0
        /// <summary>
        /// Loads this plugin
        /// </summary>
        public override void Load()
        {
            // Load the plugin
            LoadSource();
            if (JavaScriptEngine.GetValue(Name).TryCast<ObjectInstance>() == null) throw new Exception("Plugin is missing main object");
            Class = JavaScriptEngine.GetValue(Name).AsObject();
            if (!Class.HasProperty("Name"))
                Class.FastAddProperty("Name", Name, true, false, true);
            else
                Class.Put("Name", Name, true);

            // Read plugin attributes
            if (!Class.HasProperty("Title") || string.IsNullOrEmpty(Class.Get("Title").AsString())) throw new Exception("Plugin is missing title");
            if (!Class.HasProperty("Author") || string.IsNullOrEmpty(Class.Get("Author").AsString())) throw new Exception("Plugin is missing author");
            if (!Class.HasProperty("Version") || Class.Get("Version").ToObject() == null) throw new Exception("Plugin is missing version");
            Title = Class.Get("Title").AsString();
            Author = Class.Get("Author").AsString();
            Version = (VersionNumber) Class.Get("Version").ToObject();
            if (Class.HasProperty("Description")) Description = Class.Get("Description").AsString();
            if (Class.HasProperty("ResourceId")) ResourceId = (int)Class.Get("ResourceId").AsNumber();
            HasConfig = Class.HasProperty("HasConfig") && Class.Get("HasConfig").AsBoolean();

            // Set attributes
            Class.FastAddProperty("Plugin", JsValue.FromObject(JavaScriptEngine, this), true, false, true);

            Globals = new Dictionary<string, ICallable>();
            foreach (var property in Class.GetOwnProperties())
            {
                var callable = property.Value.Value?.TryCast<ICallable>();
                if (callable != null) Globals.Add(property.Key, callable);
            }
            foreach (var property in Class.Prototype.GetOwnProperties())
            {
                var callable = property.Value.Value?.TryCast<ICallable>();
                if (callable != null) Globals.Add(property.Key, callable);
            }
            if (!HasConfig) HasConfig = Globals.ContainsKey("LoadDefaultConfig");

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