public PluginContainer(PluginContainer container, RuntimeStatus Status)
 {
     this.Name = container.Name;
     this.Author = container.Author;
     this.Info = container.Info;
     this.Status = Status;
 }
        private bool LoadPlugin(PluginInfo info)
        {
            bool verified = VerifyPlugin(info);
            LOGGER.Info(string.Format("Plugin loading: TypeName={0}, Verified={1}, Path={2}", info.TypeName, verified, info.AssemblyPath));

            AppDomainSetup domainSetup = new AppDomainSetup();
            domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
            domainSetup.PrivateBinPath = @"Plugins;bin";

            PermissionSet permissions;
            if (!verified) {
                permissions = new PermissionSet(PermissionState.None);

                permissions.AddPermission(new UIPermission(UIPermissionWindow.AllWindows, UIPermissionClipboard.NoClipboard));
                permissions.AddPermission(new WebPermission(PermissionState.Unrestricted));
                permissions.AddPermission(new WebBrowserPermission(PermissionState.Unrestricted));
                permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.NoFlags));

                permissions.AddPermission(new SecurityPermission(
                  SecurityPermissionFlag.Execution |
                  SecurityPermissionFlag.SerializationFormatter |
                  SecurityPermissionFlag.Assertion));

                permissions.AddPermission(new FileIOPermission(
                  FileIOPermissionAccess.PathDiscovery |
                  FileIOPermissionAccess.Read,
                  new string[] {
                  AppDomain.CurrentDomain.BaseDirectory,
                  EnvironmentManager.ResourcesPath,
                  }));

                permissions.AddPermission(new FileIOPermission(
                  FileIOPermissionAccess.PathDiscovery |
                  FileIOPermissionAccess.Write |
                  FileIOPermissionAccess.Read,
                  EnvironmentManager.Resources3rdPath));

                // debug = REMOVE
                //permissions.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
                //permissions.AddPermission(new SecurityPermission(PermissionState.Unrestricted));
            } else {
                permissions = new PermissionSet(PermissionState.Unrestricted);
            }

            AppDomain domain = AppDomain.CreateDomain(
              string.Format("PluginDomain [{0}]", Path.GetFileNameWithoutExtension(info.AssemblyPath)),
              null,
              domainSetup,
              permissions);
            domain.SetData("DataDirectory", EnvironmentManager.AppDataPath);

            PluginContainer container = null;
            string pluginName = null;
            IPlugin Instance = null;
            try {
                Instance = (IPlugin)domain.CreateInstanceFromAndUnwrap(info.AssemblyPath, info.TypeName);
                pluginName = Instance.Name;

                if (Plugins.TryGetValue(pluginName, out container)) {
                    if (container.Status == PluginContainer.RuntimeStatus.ACTIVE) {
                        AppDomain.Unload(domain);
                        return false;
                    }
                }
                try {
                    Instance.OnActivate(PluginHost);
                } catch (Exception) {
                    Instance.OnStop(PluginHost);
                }
                container = new PluginContainer(Instance, info, PluginContainer.RuntimeStatus.ACTIVE, domain);
            } catch (Exception e) {
                AppDomain.Unload(domain);
                if (pluginName != null && Instance != null) {
                    container = new PluginContainer(Instance, info, PluginContainer.RuntimeStatus.FAILED, e);
                }
                return false;
            }
            return Plugins.AddOrUpdate(pluginName, container, (key, oldValue) => container) != null;
        }
 public bool StopPlugin(PluginContainer container)
 {
     bool result = false;
     if (container.Status == PluginContainer.RuntimeStatus.ACTIVE) {
         container.Plugin.OnStop(PluginHost);
         AppDomain.Unload(container.Domain);
         container = new PluginContainer(container, PluginContainer.RuntimeStatus.STOPPED);
         result = Plugins.AddOrUpdate(container.Name, container, (key, oldValue) => container) != null;
     }
     return result;
 }
 public bool StartPlugin(PluginContainer container)
 {
     return LoadPlugin(container.Info);
 }