Exemplo n.º 1
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";
            }
        }
        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";
                }
            }
        }
Exemplo n.º 3
0
        /// <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);
        }