예제 #1
0
        public static (AppDomain domain, PluginHost runner) Create(SMCollection collection)
        {
            DirectoryEx.EnsureExists(SMAConst.Paths.AppDomainCachePath);
            DirectoryEx.EnsureExists(SMAConst.Paths.PluginPath);
            DirectoryEx.EnsureExists(collection.GetSMAFolder());
            DirectoryEx.EnsureExists(collection.GetSMAElementsFolder());
            DirectoryEx.EnsureExists(collection.GetSMAPluginsFolder());
            DirectoryEx.EnsureExists(collection.GetSMASystemFolder());

            var assemblyPaths = String.Join(";",
                                            GetAssemblyPaths());

            var setup = new AppDomainSetup()
            {
                ApplicationBase       = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                CachePath             = SMAConst.Paths.AppDomainCachePath,
                PrivateBinPath        = assemblyPaths,
                ShadowCopyFiles       = "true",
                ShadowCopyDirectories = assemblyPaths
            };

            var permissions = GetPermissions(collection);

            var domain = AppDomain.CreateDomain(
                AppDomainName,
                AppDomain.CurrentDomain.Evidence,
                setup,
                permissions
                );

            var runner = (PluginHost)domain.CreateInstanceAndUnwrap(
                typeof(PluginHost).Assembly.FullName,
                // ReSharper disable once AssignNullToNotNullAttribute
                typeof(PluginHost).FullName
                );

            return(domain, runner);
        }
예제 #2
0
        private static PermissionSet GetPermissions(SMCollection collection)
        {
            // TODO: Switch back to restricted
            var permissions = new PermissionSet(PermissionState.Unrestricted);

            //permissions.SetPermission(new EnvironmentPermission(PermissionState.Unrestricted));
            //permissions.SetPermission(new UIPermission(PermissionState.Unrestricted));
            //permissions.SetPermission(new FileDialogPermission(PermissionState.Unrestricted));
            //permissions.SetPermission(new MediaPermission(PermissionState.Unrestricted));
            //permissions.SetPermission(new ReflectionPermission(PermissionState.Unrestricted));

            /*permissions.SetPermission(
             * new SecurityPermission(SecurityPermissionFlag.AllFlags));
             *  SecurityPermissionFlag.Execution | SecurityPermissionFlag.UnmanagedCode | SecurityPermissionFlag.BindingRedirects
             | SecurityPermissionFlag.Assertion | SecurityPermissionFlag.RemotingConfiguration | SecurityPermissionFlag.ControlThread));*/

            permissions.RemovePermission(typeof(FileIOPermission));
            permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
                                                           Path.GetTempPath()));
            permissions.AddPermission(new FileIOPermission(
                                          FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read,
                                          Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles))
                                      );
            permissions.AddPermission(new FileIOPermission(
                                          FileIOPermissionAccess.PathDiscovery | FileIOPermissionAccess.Read,
                                          Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86))
                                      );
            permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
                                                           collection.GetSMAFolder()));
            permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
                                                           SMAConst.Paths.AppDataPath));
            permissions.AddPermission(new FileIOPermission(FileIOPermissionAccess.AllAccess,
                                                           AppDomain.CurrentDomain.BaseDirectory));

            return(permissions);
        }