public static void CleanUpFolder(string folderPath, Action <Progress> ProgressChanged = null) { ProgressChanged?.Invoke(Progress.Indetermined("Cleaning up mod folder.")); foreach (string subFolderPath in Directory.EnumerateDirectories(folderPath)) { string subFolderName = Path.GetFileName(subFolderPath).ToLower(); // Move data folder one up: if (subFolderName == "data") { ModInstallations.MoveDirectory(subFolderPath, folderPath); } } foreach (String filePath in Directory.EnumerateFiles(folderPath)) { string fileExtension = Path.GetExtension(filePath).ToLower().Trim(); // Extract archives within folder: if (fileExtension == ".ba2" || Utils.SevenZipSupportedFileTypes.Contains(fileExtension)) { ModInstallations.ExtractArchive(filePath, folderPath, ProgressChanged); File.Delete(filePath); } // Delete crap: else if (fileExtension == ".txt") { File.Delete(filePath); } } }
/// <summary> /// Creates a new mod from any supported archive. (zip, tar, rar, 7z, ba2) /// BA2 files can be installed frozen if needed. /// </summary> /// <param name="gamePath">Path to the game installation</param> /// <param name="filePath">Path to archive</param> /// <param name="useSourceBA2Archive">When false, creates a new "frozen" mod.</param> /// <returns></returns> private static ManagedMod FromArchive(string gamePath, string filePath, bool useSourceBA2Archive = false, Action <Progress> ProgressChanged = null) { // Get path information: string longFilePath = EnsureLongPathSupport(filePath); string fileNameWOEx = Path.GetFileNameWithoutExtension(longFilePath); string fileExtension = Path.GetExtension(longFilePath); // Install mod: ManagedMod newMod = new ManagedMod(gamePath); newMod.Title = fileNameWOEx; newMod.ArchiveName = fileNameWOEx + ".ba2"; newMod.ManagedFolderName = fileNameWOEx; if (!Utils.IsFileNameValid(newMod.ManagedFolderName) || Directory.Exists(newMod.ManagedFolderPath)) { newMod.ManagedFolderName = newMod.DefaultManagedFolderName; } // Extract mod: ProgressChanged?.Invoke(Progress.Indetermined($"Extracting {Path.GetFileName(filePath)}")); ModInstallations.ExtractArchive(longFilePath, newMod.ManagedFolderPath); // Freeze mod conditionally: if (useSourceBA2Archive && fileExtension == ".ba2") { // Copy *.ba2 into FrozenData: FileInfo frozenPath = new FileInfo(newMod.FrozenArchivePath); ProgressChanged?.Invoke(Progress.Indetermined($"Copying {Path.GetFileName(filePath)} to {frozenPath.DirectoryName}")); Directory.CreateDirectory(frozenPath.DirectoryName); File.Copy(longFilePath, frozenPath.FullName, true); newMod.Frozen = true; newMod.Freeze = true; newMod.PreviousMethod = ManagedMod.DeploymentMethod.SeparateBA2; newMod.Method = ManagedMod.DeploymentMethod.SeparateBA2; } else { ModActions.CleanUpFolder(newMod.ManagedFolderPath, ProgressChanged); ModActions.DetectOptimalModInstallationOptions(newMod); } return(newMod); }
/// <summary> /// Extracts the archive and then copy and replaces from the temp folder into the managed mod folder. /// </summary> public static void AddArchive(ManagedMod mod, string filePath, Action <Progress> ProgressChanged = null) { string longFilePath = EnsureLongPathSupport(filePath); string tempFolderPath = Path.Combine(Path.GetTempPath(), $"tmp_{mod.guid}"); if (Directory.Exists(tempFolderPath)) { Directory.Delete(tempFolderPath, true); } Directory.CreateDirectory(tempFolderPath); ProgressChanged?.Invoke(Progress.Indetermined($"Extracting {Path.GetFileName(filePath)}")); ModInstallations.ExtractArchive(longFilePath, tempFolderPath); ModActions.CleanUpFolder(tempFolderPath, ProgressChanged); CopyDirectory(tempFolderPath, mod.ManagedFolderPath, ProgressChanged); Directory.Delete(tempFolderPath, true); ProgressChanged?.Invoke(Progress.Done("Archive added to mod.")); }