示例#1
0
        private void EnablePlugin(LoadedPlugin plugin)
        {
            plugin.Enabled = true;
            RaisePropertyChanged(() => Count);

            OnPluginLoaded(plugin);
        }
示例#2
0
        internal bool LoadPlugin(string name, bool dont_enable)
        {
            string lowname = name.ToLower();

            if (lowname.EndsWith(".dll"))
            {
                name    = name.Substring(0, name.Length - 4);
                lowname = lowname.Substring(0, lowname.Length - 4);
            }

            lock (plugins) {
                int index = plugins.FindIndex(s => s.Name.ToLower() == lowname);

                if (index > -1)
                {
                    EnablePlugin(plugins[index]);
                    return(true);
                }
            }

            String dir  = Path.Combine(Directories.Plugins, name);
            String file = Path.Combine(dir, name + ".dll");

            if (!Directory.Exists(dir) || !File.Exists(file))
            {
                return(false);
            }

            try {
                Assembly asm = Assembly.LoadFrom(file);

                foreach (var type in asm.GetExportedTypes())
                {
                    if (typeof(IPlugin).IsAssignableFrom(type))
                    {
                        IPlugin      plugin       = (IPlugin)Activator.CreateInstance(type);
                        LoadedPlugin zorbo_plugin = new LoadedPlugin(name, plugin);

                        lock (plugins) plugins.Add(zorbo_plugin);
                        if (!dont_enable)
                        {
                            EnablePlugin(zorbo_plugin);
                        }

                        return(true);
                    }
                }
            }
            catch (PluginLoadException plex) {
                OnError("LoadPlugin", "LoadPlugin", plex);
            }
            catch (Exception ex) {
                OnError("LoadPlugin", "LoadPlugin", new PluginLoadException("The plugin failed to load (See inner exception for details).", ex));
            }
            return(false);
        }
示例#3
0
        private void OnPluginKilled(LoadedPlugin plugin)
        {
            try {
                plugin.Plugin.OnPluginKilled();
            }
            catch (Exception ex) {
                OnError(plugin, "OnPluginKilled", ex);
            }

            try {
                var x = Killed;
                if (x != null)
                {
                    x(this, plugin);
                }
            }
            catch (Exception ex) {
                OnError(plugin, "Killed::EventHandler", ex);
            }
        }
示例#4
0
        private void OnPluginLoaded(LoadedPlugin plugin)
        {
            try {
                var x = Loaded;
                if (x != null)
                {
                    x(this, plugin);
                }
            }
            catch (Exception ex) {
                OnError(plugin, "Loaded::EventHandler", ex);
            }

            try {
                plugin.Plugin.Directory = Path.Combine(Directories.Plugins, plugin.Name);
                plugin.Plugin.OnPluginLoaded(Server);
            }
            catch (Exception ex) {
                OnError(plugin, "OnPluginLoaded", ex);
            }
        }
示例#5
0
        public void KillPlugin(string name)
        {
            LoadedPlugin plugin = null;

            lock (plugins) {
                string lowname = name.ToLower();
                int    index   = plugins.FindIndex(s => s.Name.ToLower() == lowname);

                if (index == -1)
                {
                    return;
                }

                plugin = plugins[index];

                if (plugin.Enabled)
                {
                    plugin.Enabled = false;
                    RaisePropertyChanged(() => Count);

                    OnPluginKilled(plugin);
                }
            }
        }
示例#6
0
        private void Wrapper_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            var x = this.CollectionChanged;

            if (x != null)
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add: {
                    LoadedPlugin[] newitems = new LoadedPlugin[e.NewItems.Count];

                    e.NewItems.CopyTo(newitems, 0);

                    x(this, new NotifyCollectionChangedEventArgs(
                          e.Action,
                          newitems.Select((s) => (IPlugin)s.Plugin).ToList(),
                          e.NewStartingIndex));
                }
                break;

                case NotifyCollectionChangedAction.Remove: {
                    LoadedPlugin[] olditems = new LoadedPlugin[e.OldItems.Count];

                    e.OldItems.CopyTo(olditems, 0);

                    x(this, new NotifyCollectionChangedEventArgs(
                          e.Action,
                          olditems.Select((s) => (IPlugin)s.Plugin).ToList(),
                          e.OldStartingIndex));
                }
                break;

                case NotifyCollectionChangedAction.Move: {
                    LoadedPlugin[] olditems = new LoadedPlugin[e.OldItems.Count];

                    e.OldItems.CopyTo(olditems, 0);

                    x(this, new NotifyCollectionChangedEventArgs(
                          e.Action,
                          olditems.Select((s) => (IPlugin)s.Plugin).ToList(),
                          e.NewStartingIndex,
                          e.OldStartingIndex));
                }
                break;

                case NotifyCollectionChangedAction.Replace: {
                    LoadedPlugin[] newitems = new LoadedPlugin[e.NewItems.Count];
                    LoadedPlugin[] olditems = new LoadedPlugin[e.OldItems.Count];

                    e.NewItems.CopyTo(newitems, 0);
                    e.OldItems.CopyTo(olditems, 0);

                    x(this, new NotifyCollectionChangedEventArgs(
                          e.Action,
                          newitems.Select((s) => (IPlugin)s.Plugin).ToList(),
                          olditems.Select((s) => (IPlugin)s.Plugin).ToList()));
                }
                break;

                case NotifyCollectionChangedAction.Reset:
                    x(this, new NotifyCollectionChangedEventArgs(e.Action));
                    break;
                }
            }
        }
示例#7
0
 private void OnError(LoadedPlugin plugin, String method, Exception ex)
 {
     OnError(plugin.Name, method, ex);
 }