/// <summary> /// Add to load order list after installing the Package /// </summary> private static void PluginManager_AfterInstallPackage(InstallPackageEventArgs eventArgs) { List <string> lo = GetLoadOrderList(LoadOrderQueue.Default); if (!lo.Contains(eventArgs.Data.PluginName)) { lo.Add(eventArgs.Data.PluginName); SetLoadOrderList(LoadOrderQueue.Default, lo); } }
/// <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); }