コード例 #1
0
        public void TestReflect1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "UniquePluginId", "My very own plugin", "Simon", "http://google.com", "get of my lawn");
                    builder.ImplementInterface <IFileFormatPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;

                using (var loader = new PluginArchiveLoader(null))
                {
                    var description = loader.ReflectPlugin(stream, true);
                    description.Should().NotBeNull();
                    description.Id.Should().Be(new PluginId("Kittyfisto.UniquePluginId"));
                    description.Name.Should().Be("My very own plugin");
                    description.Version.Should().Be(new Version(0, 0, 0), "because the plugin version should default to 0.0.0 when none has been specified");
                    description.Author.Should().Be("Simon");
                    description.Website.Should().Be(new Uri("http://google.com"));
                    description.Description.Should().Be("get of my lawn");
                }
            }
        }
コード例 #2
0
        public void TestAddAssembly1()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                var builder = new AbstractPluginTest.PluginBuilder("Simon", "Foo", "Plugin");
                builder.AssemblyVersion              = "4.0.3.1";
                builder.AssemblyFileVersion          = "1.2.3.42";
                builder.AssemblyInformationalVersion = "4.0.0.0-beta";
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Should().NotBeNull();
                index.Name.Should().Be("Plugin");
                index.Assemblies.Should().NotBeNull();
                index.Assemblies.Should().HaveCount(1);
                index.Assemblies[0].EntryName.Should().Be("Plugin.dll");
                index.Assemblies[0].AssemblyName.Should().Be("Plugin");
                index.Assemblies[0].AssemblyVersion.Should().Be(new Version(4, 0, 3, 1));
                index.Assemblies[0].AssemblyFileVersion.Should().Be(new Version(1, 2, 3, 42));
                index.Assemblies[0].AssemblyInformationalVersion.Should().Be("4.0.0.0-beta");
                index.NativeImages.Should().NotBeNull();
                index.NativeImages.Should().HaveCount(0);

                var actualAssembly = reader.LoadAssembly("Plugin.dll");
                actualAssembly.Should().NotBeNull();
            }
        }
コード例 #3
0
 private void btnPack_Click(object sender, EventArgs e)
 {
     PackageDataManager.GetFormatAt(cbDataFormats.SelectedIndex).SaveData(inputPtr, unpackedInputPath);
     PluginPacker.GetFormatAt(cbPackerFormats.SelectedIndex).Pack(unpackedInputPath, tbOutputDir.Text);
     Directory.Delete(unpackedInputPath, true);
     Close();
 }
コード例 #4
0
        public void TestReflect1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "UniquePluginId", "My very own plugin", "Simon", "http://google.com", "get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                _filesystem.Write(Path.Combine(Constants.PluginPath, "Kittyfisto.UniquePluginId.2.0.tvp"), stream);

                using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
                {
                    loader.Plugins.Should().HaveCount(1, "because one plugin should've been loaded");
                    var description = loader.Plugins.First();
                    description.Should().NotBeNull();
                    description.Id.Should().Be(new PluginId("Kittyfisto.UniquePluginId"));
                    description.Name.Should().Be("My very own plugin");
                    description.Version.Should().Be(new Version(0, 0, 0), "because the plugin version should default to 0.0.0 when none has been specified");
                    description.Author.Should().Be("Simon");
                    description.Website.Should().Be(new Uri("http://google.com"));
                    description.Description.Should().Be("get of my lawn");
                }
            }
        }
コード例 #5
0
        public void TestLoadAllOfType1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "SomePlugin", "none of your business", "get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                _filesystem.Write(Path.Combine(Constants.PluginPath, "Kittyfisto.SomePlugin.1.tvp"), stream);

                using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
                {
                    var plugins = loader.LoadAllOfType <ILogEntryParserPlugin>()?.ToList();
                    plugins.Should().NotBeNull();
                    plugins.Should().HaveCount(1);
                    plugins[0].Should().NotBeNull();
                    plugins[0].GetType().FullName.Should().Be("Plugin.FileFormatPlugin");
                }
            }
        }
コード例 #6
0
        public void TestAddNativeImage1()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                var builder = new AbstractPluginTest.PluginBuilder("Simon", "Foo", "Plugin");
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);

                var fname = Path.Combine(_testData, "Native", "x86", "NativeImage.dll");
                packer.AddFile("NativeImage.dll", fname);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Should().NotBeNull();
                index.Assemblies.Should().NotBeNull();
                index.Assemblies.Should().HaveCount(1);

                index.NativeImages.Should().NotBeNull();
                index.NativeImages.Count.Should().Be(1);
                index.NativeImages[0].EntryName.Should().Be("NativeImage.dll");
                index.NativeImages[0].ImageName.Should().Be("NativeImage");
            }
        }
コード例 #7
0
        public void TestReflectTwoPluginImplementations()
        {
            var stream = new MemoryStream();

            using (var packer = PluginPacker.Create(stream, true))
            {
                var builder = new PluginBuilder("Kittyfisto", "UniquePluginId", "MyAwesomePlugin");
                builder.ImplementInterface <ILogEntryParserPlugin>("A");
                builder.ImplementInterface <ILogEntryParserPlugin>("B");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            stream.Position = 0;
            _filesystem.Write(Path.Combine(Constants.PluginPath, "Kittyfisto.UniquePluginId.2.0.tvp"), stream);

            using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
            {
                loader.Plugins.Should().HaveCount(1, "because one plugin should've been loaded");
                var description = loader.Plugins.First();
                description.PluginImplementations.Should().HaveCount(2, "because we've implemented the IFileFormatPlugin twice");
                description.PluginImplementations[0].InterfaceType.Should().Be <ILogEntryParserPlugin>();
                description.PluginImplementations[0].FullTypeName.Should().Be("A");

                description.PluginImplementations[1].InterfaceType.Should().Be <ILogEntryParserPlugin>();
                description.PluginImplementations[1].FullTypeName.Should().Be("B");
            }
        }
コード例 #8
0
        public void TestLoadAllOfTypeWithDescription()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "Simon", "none of your business", "get of my lawn");
                    builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                var path = Path.Combine(Constants.PluginPath, "Kittyfisto.Simon.1.0.tvp");
                _filesystem.WriteAllBytes(path, stream.ToArray());

                using (var loader = new PluginArchiveLoader(_filesystem, Constants.PluginPath))
                {
                    var pluginsWithDescription = loader.LoadAllOfTypeWithDescription <ILogEntryParserPlugin>();
                    pluginsWithDescription.Should().HaveCount(1, "because we've added one plugin which implements the IFileFormatPlugin interface");
                    var description = pluginsWithDescription[0].Description;
                    description.Should().NotBeNull();
                    description.Id.Should().Be(new PluginId("Kittyfisto.Simon"));
                    description.Author.Should().Be("get of my lawn");
                }
            }
        }
コード例 #9
0
        public void TestLoadAllOfType1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "SomePlugin", "none of your business", "get of my lawn");
                    builder.ImplementInterface <IFileFormatPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;

                using (var loader = new PluginArchiveLoader(null))
                {
                    var description = loader.ReflectPlugin(stream, true);
                    var plugins     = loader.LoadAllOfType <IFileFormatPlugin>()?.ToList();
                    plugins.Should().NotBeNull();
                    plugins.Should().HaveCount(1);
                    plugins[0].Should().NotBeNull();
                    plugins[0].GetType().FullName.Should().Be("Plugin.FileFormatPlugin");
                }
            }
        }
コード例 #10
0
        private void btnUnpack_Click(object sender, EventArgs e)
        {
            string tempDir = Path.Combine(PluginPaths.GetPluginTempDirectory(Pointer), "TempUnpack");

            if (Directory.Exists(tempDir))
            {
                Directory.Delete(tempDir, true);
            }

            Directory.CreateDirectory(tempDir);
            rtbInputInfo.Text = "";
            try
            {
                rtbInputInfo.Text += "Unpacking: ";
                PluginPacker.Unpack(tbInputDir.Text, tempDir);
                rtbInputInfo.Text += "SUCCESS\nReading Data Format: ";
                unpackedInputPath  = tempDir;
                inputPtr           = PackageDataManager.LoadData(tempDir);
                rtbInputInfo.Text += "SUCCESS";
                panelPack.Enabled  = true;
            }
            catch (Exception exception)
            {
                Directory.Delete(tempDir, true);
                rtbInputInfo.Text += "FAILED\n";
            }
        }
コード例 #11
0
 private void tbPath_TextChanged(object sender, EventArgs e)
 {
     btnLoad.Enabled = //(Directory.Exists(tbPath.Text) ||
                       //                   File.Exists(tbPath.Text) ||
                       //                   tbPath.Text.StartsWith("http://") ||
                       //                   tbPath.Text.StartsWith("https://")) &&
                       PluginPacker.CanLoad(tbPath.Text);
 }
コード例 #12
0
        private static void AddFile(PluginPacker packer, string filename)
        {
            var entryName = Path.GetFileName(filename);

            using (var stream = File.OpenRead(filename))
            {
                packer.AddFile(entryName, stream);
            }
        }
コード例 #13
0
 public void TestAddNativeImage2()
 {
     using (var packer = PluginPacker.Create(_fname))
     {
         var fname = Path.Combine(_testData, "Native", "x64", "NativeImage.dll");
         new Action(() => packer.AddFile("NativeImage.dll", fname))
         .ShouldThrow <PackException>()
         .WithMessage("Native images must be compiled for x86");
     }
 }
コード例 #14
0
 public void TestAddAssembly2()
 {
     using (var packer = PluginPacker.Create(_fname))
     {
         var fname = Path.Combine(_testData, "Managed", "x64", "ClassLibrary1.dll");
         new Action(() => packer.AddFile("ClassLibrary1.dll", fname))
         .ShouldThrow <PackException>()
         .WithMessage("Assemblies must be compiled for x86 or AnyCPU");
     }
 }
        private void btnPack_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < unpackedInputPath.Length; i++)
            {
                string s = unpackedInputPath[i];
                PackageDataManager.GetFormatAt(cbDataFormats.SelectedIndex).SaveData(inputPtr[i], s);
                string path = Path.Combine(tbOutputDir.Text, inputPtr[i].PluginName);
                Directory.CreateDirectory(path);
                PluginPacker.GetFormatAt(cbPackerFormats.SelectedIndex).Pack(s, path);
                Directory.Delete(unpackedInputPath[i], true);
            }

            Close();
        }
コード例 #16
0
        private static void CreatePlugin(MemoryStream stream)
        {
            using (var packer = PluginPacker.Create(stream, true))
            {
                var builder = new PluginBuilder("Kittyfisto", "UniquePluginId", "My very own plugin", "Simon", "http://google.com",
                                                "get of my lawn");
                builder.ImplementInterface <IFileFormatPlugin>("Plugin.FileFormatPlugin");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            stream.Position = 0;
        }
コード例 #17
0
        private static PluginId CreatePlugin(MemoryStream stream)
        {
            var @namespace = "Kittyfisto";
            var name       = "UniquePluginId";

            using (var packer = PluginPacker.Create(stream, true))
            {
                var builder = new PluginBuilder(@namespace, name, "My very own plugin", "Simon", "http://google.com",
                                                "get of my lawn");
                builder.ImplementInterface <ILogEntryParserPlugin>("Plugin.FileFormatPlugin");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            stream.Position = 0;
            return(new PluginId($"{@namespace}.{name}"));
        }
コード例 #18
0
        public void TestAddAssembly3()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                var fname = Path.Combine(_testData, "Managed", "x86", "ClassLibrary1.dll");
                packer.AddFile("ClassLibrary1.dll", fname);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Should().NotBeNull();
                index.PluginArchiveVersion.Should().Be(PluginArchive.CurrentPluginArchiveVersion);
                index.Assemblies.Should().NotBeNull();
                index.Assemblies.Should().HaveCount(1);
                index.Assemblies[0].EntryName.Should().Be("ClassLibrary1.dll");
                index.Assemblies[0].AssemblyName.Should().Be("ClassLibrary1");
            }
        }
コード例 #19
0
        private void CreatePlugin(string @namespace, string name, Version version)
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, leaveOpen: true))
                {
                    var builder = new PluginBuilder(@namespace, name, "dawawdwdaaw")
                    {
                        PluginVersion = version
                    };
                    builder.ImplementInterface <ILogEntryParserPlugin>("dwwwddwawa");
                    builder.Save();
                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;
                var fileName = Path.Combine(Constants.PluginPath, string.Format("{0}.{1}.{2}.tvp", @namespace, name, version));
                _filesystem.Write(fileName, stream);
            }
        }
コード例 #20
0
        public void TestAddAssembly4()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                var fname = Path.Combine(_testData, "Managed", "x86", "Targets.NET.4.6.dll");
                new Action(() => packer.AddFile("Foo.dll", fname))
                .ShouldThrow <PackException>()
                .WithMessage("Assemblies may only target frameworks of up to .NET 4.5.2");

                fname = Path.Combine(_testData, "Managed", "x86", "Targets.NET.4.6.1.dll");
                new Action(() => packer.AddFile("Foo.dll", fname))
                .ShouldThrow <PackException>()
                .WithMessage("Assemblies may only target frameworks of up to .NET 4.5.2");

                fname = Path.Combine(_testData, "Managed", "x86", "Targets.NET.4.6.2.dll");
                new Action(() => packer.AddFile("Foo.dll", fname))
                .ShouldThrow <PackException>()
                .WithMessage("Assemblies may only target frameworks of up to .NET 4.5.2");
            }
        }
コード例 #21
0
        private void PackerWindow_Load(object sender, EventArgs e)
        {
            if (PluginPacker.FormatCount == 0)
            {
                StyledMessageBox.Show(
                    "Error",
                    "Can not start the Packer GUI. There is no Plugin Package Format installed.",
                    MessageBoxButtons.OK,
                    SystemIcons.Error
                    );
                Close();
                return;
            }

            if (PackageDataManager.FormatCount == 0)
            {
                StyledMessageBox.Show(
                    "Error",
                    "Can not start the Packer GUI. There is no Package Data Format installed.",
                    MessageBoxButtons.OK,
                    SystemIcons.Error
                    );
                Close();
                return;
            }

            cbPackerFormats.Items.Clear();
            for (int i = 0; i < PluginPacker.FormatCount; i++)
            {
                cbPackerFormats.Items.Add(PluginPacker.GetFormatAt(i));
            }

            cbPackerFormats.SelectedIndex = 0;

            for (int i = 0; i < PackageDataManager.FormatCount; i++)
            {
                cbDataFormats.Items.Add(PackageDataManager.GetFormatAt(i));
            }

            cbDataFormats.SelectedIndex = 0;
        }
コード例 #22
0
        private string CreatePlugin(string @namespace, string name, Version version)
        {
            var fileName = Path.Combine(_pluginFolder, string.Format("{0}.{1}.{2}.dll", @namespace, name, version));

            using (var packer = PluginPacker.Create(fileName))
            {
                var builder = new PluginBuilder(@namespace, name, "dawawdwdaaw")
                {
                    PluginVersion = version
                };
                builder.ImplementInterface <IFileFormatPlugin>("dwwwddwawa");
                builder.Save();
                packer.AddPluginAssembly(builder.FileName);
            }

            var dest = Path.Combine(_pluginFolder, Path.GetFileName(fileName));

            File.Move(fileName, dest);

            return(dest);
        }
コード例 #23
0
        public void TestAddAssembly5()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                var builder = new AbstractPluginTest.PluginBuilder("Kittyfisto", "MyPlugin", "My First Plugin");
                builder.PluginVersion = new Version(1, 4, 12034);
                builder.ImplementInterface <IFileFormatPlugin>("Plugin.FileFormatPlugin");
                builder.Save();

                packer.AddPluginAssembly(builder.FileName);
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var index = reader.Index;
                index.Version.Should().NotBeNull();
                index.Id.Should().Be("Kittyfisto.MyPlugin");
                index.Name.Should().Be("My First Plugin");
                index.Version.Should().Be(new Version(1, 4, 12034));
            }
        }
コード例 #24
0
        public void TestLoadAssembly1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new AbstractPluginTest.PluginBuilder("Foo1", "Foo1", "Simon", "None of your business", "Get of my lawn");
                    builder.ImplementInterface <IFileFormatPlugin>("Foo1.MyAwesomePlugin");
                    builder.Save();
                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;

                using (var reader = PluginArchive.OpenRead(stream))
                {
                    var assembly = reader.LoadAssembly("Plugin.dll");
                    assembly.Should().NotBeNull();
                    reader.LoadAssembly("Plugin.dll").Should().BeSameAs(assembly, "because LoadAssembly should return the very same assembly every time");
                }
            }
        }
コード例 #25
0
        public void TestAddIcon1()
        {
            using (var packer = PluginPacker.Create(_fname))
            {
                using (var icon = File.OpenRead(Path.Combine(_testData, "cropped-uiforetwicon2.png")))
                {
                    packer.SetIcon(icon);
                }
            }

            using (var reader = PluginArchive.OpenRead(_fname))
            {
                var stream = reader.ReadIcon();
                stream.Should().NotBeNull();

                using (var image = new Bitmap(stream))
                {
                    image.Width.Should().Be(16);
                    image.Height.Should().Be(16);
                }
            }
        }
コード例 #26
0
        private void CreatePlugin <T>(string pluginFolder, PluginId actualId, Version actualVersion, PluginId fakeId, Version fakeVersion) where T : IPlugin
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var idx             = actualId.Value.LastIndexOf(".");
                    var actualNamespace = actualId.Value.Substring(0, idx);
                    var actualName      = actualId.Value.Substring(idx + 1);
                    var builder         = new PluginBuilder(actualNamespace, actualName, "none of your business", "get of my lawn", version: actualVersion);
                    builder.ImplementInterface <T>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }
                stream.Position = 0;

                var path = Path.Combine(pluginFolder, $"{fakeId.Value}.{fakeVersion}.tvp");
                _filesystem.CreateDirectory(pluginFolder);
                _filesystem.WriteAllBytes(path, stream.ToArray());
            }
        }
        private void btnUnpack_Click(object sender, EventArgs e)
        {
            string[] files = File.ReadAllLines(tbInputDir.Text);
            unpackedInputPath = new string[files.Length];
            inputPtr          = new BasePluginPointer[files.Length];
            rtbInputInfo.Text = "";
            for (int i = 0; i < files.Length; i++)
            {
                string tempDir = Path.Combine(PluginPaths.GetPluginTempDirectory(Pointer), "TempUnpack");
                if (Directory.Exists(tempDir))
                {
                    Directory.Delete(tempDir, true);
                }

                Directory.CreateDirectory(tempDir);
                try
                {
                    rtbInputInfo.Text += $"Unpacking({Path.GetFileName(files[i])}): ";
                    PluginPacker.Unpack(files[i], tempDir);
                    rtbInputInfo.Text += "SUCCESS\nReading Data Format: ";
                    inputPtr[i]        = PackageDataManager.LoadData(tempDir);
                    string outPath = Path.Combine(PluginPaths.GetPluginTempDirectory(Pointer), inputPtr[i].PluginName);
                    if (Directory.Exists(outPath))
                    {
                        Directory.Delete(outPath, true);
                    }

                    Directory.Move(tempDir, outPath);
                    unpackedInputPath[i] = outPath;
                    rtbInputInfo.Text   += "SUCCESS\n\n";
                    panelPack.Enabled    = true;
                }
                catch (Exception exception)
                {
                    Directory.Delete(tempDir, true);
                    rtbInputInfo.Text += "FAILED\n" + exception.Message + "\n\n";
                }
            }
        }
コード例 #28
0
        public void TestLoad1()
        {
            using (var stream = new MemoryStream())
            {
                using (var packer = PluginPacker.Create(stream, true))
                {
                    var builder = new PluginBuilder("Kittyfisto", "Simon", "none of your business", "get of my lawn");
                    builder.ImplementInterface <IFileFormatPlugin>("Plugin.FileFormatPlugin");
                    builder.Save();

                    packer.AddPluginAssembly(builder.FileName);
                }

                stream.Position = 0;

                using (var loader = new PluginArchiveLoader(null))
                {
                    var description = loader.ReflectPlugin(stream, true);
                    var plugin      = loader.Load <IFileFormatPlugin>(description);
                    plugin.Should().NotBeNull();
                }
            }
        }
コード例 #29
0
ファイル: PluginManager.cs プロジェクト: Open-FL/PluginSystem
        /// <summary>
        ///     Adds a Package to the Plugin System
        /// </summary>
        /// <param name="file">Package Input Path</param>
        /// <param name="name">When Loaded successfully contains the Name of the Loaded plugin</param>
        /// <returns>True if the Adding was Successful</returns>
        internal static bool AddPackage(string file, out string name)
        {
            if (!IsInitialized)
            {
                throw new Exception("Can not use the plugin System when its not initialized.");
            }

            name = null;
            SendLogDivider();
            SendLog("Adding File: " + file);


            if (PluginPacker.CanLoad(file))
            {
                AddPackageEventArgs <string> args = new AddPackageEventArgs <string>(file, true);
                OnAddPackage?.Invoke(args);
                if (args.Cancel)
                {
                    return(false);
                }

                string tempDir = Path.Combine(
                    PluginPaths.GetSystemProcessTempDirectory("Install"),
                    Path.GetFileNameWithoutExtension(Path.GetRandomFileName())
                    );                          //TODO: Get temp dir for unpacking
                Directory.CreateDirectory(tempDir);

                //TODO: If the package is already installed Write Backup to PluginDir/backup before loading the new package

                SendLog("Trying to Load File Format: " + Path.GetFileName(file));
                PluginPacker.Unpack(file, tempDir);

                //TODO: Try load Package Data/Plugin Data
                if (PackageDataManager.CanLoad(tempDir))
                {
                    SendLog("Trying to Load Data Format: " + Path.GetFileName(tempDir));
                    BasePluginPointer ptr = PackageDataManager.LoadData(tempDir);
                    if (ptr != null)
                    {
                        name = ptr.PluginName;
                        ptr.EnsureDirectoriesExist();

                        AddPackageEventArgs <BasePluginPointer> ptrArgs =
                            new AddPackageEventArgs <BasePluginPointer>(ptr, true);
                        OnAddPackagePointerLoaded?.Invoke(ptrArgs);
                        if (ptrArgs.Cancel)
                        {
                            return(false);
                        }

                        List <string> installedPackages = ListHelper.LoadList(PluginPaths.GlobalPluginListFile).ToList();
                        List <string> activePackages    = ListHelper.LoadList(PluginPaths.PluginListFile).ToList();
                        string        newPackage        = ptr.ToKeyPair();
                        bool          isNew             = installedPackages.All(x => !x.StartsWith(ptr.PluginName));
                        if (isNew)
                        {
                            installedPackages.Add(newPackage);
                        }
                        else
                        {
                            if (activePackages.RemoveAll(x => x.StartsWith(ptr.PluginName)) != 0)
                            {
                                activePackages.Add(newPackage);
                                ListHelper.SaveList(PluginPaths.PluginListFile, activePackages.ToArray());
                            }

                            installedPackages.RemoveAll(x => x.StartsWith(ptr.PluginName));
                            installedPackages.Add(newPackage);
                        }

                        ListHelper.SaveList(PluginPaths.GlobalPluginListFile, installedPackages.ToArray());

                        //TODO: Check if the Install would overwrite things.
                        //TODO: Check if the files that are overwritten are in use.
                        //TODO: Make a system that takes instructions from a file at start up to "complete" installations
                        InstallPackageEventArgs iargs = new InstallPackageEventArgs(isNew, ptr, tempDir, true);
                        OnInstallPackage?.Invoke(iargs);
                        if (iargs.Cancel)
                        {
                            return(false);
                        }

                        PackageDataManager.Install(ptr, tempDir);

                        Directory.Delete(tempDir, true);

                        AfterInstallPackage?.Invoke(new InstallPackageEventArgs(isNew, ptr, tempDir, false));

                        AfterAddPackage?.Invoke(new AfterAddPackageEventArgs(ptr));
                        return(true);
                    }

                    SendError(new PackageDataException("File Corrupt", file));
                }
                else
                {
                    SendError(new PackageDataException("Unable to find a Data Serializer", file, null));
                }
            }
            else
            {
                SendError(new PackerException("Unable to find a Packer", file, null));
            }

            return(false);
        }
コード例 #30
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="fname"></param>
 /// <returns></returns>
 private static PluginPacker CreatePacker(string fname)
 {
     return(PluginPacker.Create(File.OpenWrite(fname)));
 }