private static void RemoveDemoddedQars(ref List <string> zeroFiles, List <string> fullRemoveQarPaths) { // Remove the specified files from the mod ecosystem since they no longer contain modded data zeroFiles = zeroFiles.Except(fullRemoveQarPaths.Select(entry => Tools.ToWinPath(entry))).ToList(); List <ModEntry> installedMods = SBBuildManager.GetInstalledMods(); GameData gameData = SBBuildManager.GetGameData(); foreach (string fullRemoveQarPath in fullRemoveQarPaths) { //Debug.LogLine(string.Format("DEMODDED QAR: {0}", fullRemoveQarPath), Debug.LogLevel.Basic); int indexToRemove = gameData.GameQarEntries.FindIndex(entry => entry.FilePath == fullRemoveQarPath); if (indexToRemove >= 0) { gameData.GameQarEntries.RemoveAt(indexToRemove); } gameData.GameFpkEntries = gameData.GameFpkEntries.Where(entry => entry.FpkFile != fullRemoveQarPath).ToList(); foreach (ModEntry installedMod in installedMods) { indexToRemove = installedMod.ModQarEntries.FindIndex(entry => entry.FilePath == fullRemoveQarPath); if (indexToRemove >= 0) { installedMod.ModQarEntries.RemoveAt(indexToRemove); } installedMod.ModFpkEntries = installedMod.ModFpkEntries.Where(entry => entry.FpkFile != fullRemoveQarPath).ToList(); } } SBBuildManager.SetInstalledMods(installedMods); SBBuildManager.SetGameData(gameData); }
/// <summary> /// Creates a .MGSVPreset file for the mods that are currently installed /// </summary> public static bool SavePreset(string presetFilePath) { bool success = false; Directory.CreateDirectory("_build\\master\\0"); SettingsManager manager = new SettingsManager(SnakeBiteSettings); string presetName = Path.GetFileName(presetFilePath); Debug.LogLine($"[SavePreset] Saving {presetName}...", Debug.LogLevel.Basic); try { foreach (string gameFile in manager.GetModExternalFiles()) { string sourcePath = Path.Combine(GameDir, Tools.ToWinPath(gameFile)); string DestDir = "_build\\" + Path.GetDirectoryName(gameFile); string fileName = Path.GetFileName(gameFile); Directory.CreateDirectory(DestDir); if (File.Exists(sourcePath)) { Debug.LogLine(string.Format("[SavePreset] Copying to build directory: {0}", gameFile), Debug.LogLevel.Basic); File.Copy(sourcePath, Path.Combine(DestDir, fileName), true); } else { Debug.LogLine(string.Format("[SavePreset] File not found: {0}", sourcePath), Debug.LogLevel.Basic); } } Debug.LogLine("[SavePreset] Copying to build directory: 00.dat", Debug.LogLevel.Basic); File.Copy(ZeroPath, "_build\\master\\0\\00.dat", true); Debug.LogLine("[SavePreset] Copying to build directory: 01.dat", Debug.LogLevel.Basic); File.Copy(OnePath, "_build\\master\\0\\01.dat", true); Debug.LogLine("[SavePreset] Copying to build directory: snakebite.xml", Debug.LogLevel.Basic); File.Copy(SnakeBiteSettings, "_build\\snakebite.xml", true); if (presetFilePath == SavePresetPath + build_ext) { Debug.LogLine($"Note: '{Path.GetFileNameWithoutExtension(presetName)}' can be disabled in the Settings menu to save time during installation and uninstallation.", Debug.LogLevel.Basic); } FastZip zipper = new FastZip(); Debug.LogLine(string.Format("[SavePreset] Writing {0}...", presetName), Debug.LogLevel.Basic); zipper.CreateZip(presetFilePath, "_build", true, "(.*?)"); Debug.LogLine("[SavePreset] Write Complete", Debug.LogLevel.Basic); success = true; } catch (Exception e) { MessageBox.Show("An error has occurred and the preset was not saved.\nException: " + e); } finally { ModManager.CleanupFolders(); } return(success); }
/// <summary> /// Merges the new mod files with the existing modded files and vanilla game files. /// The resulting file structure is the new _working0 folder to pack as 00.dat /// </summary> private static void InstallMods(List <string> modFilePaths, SettingsManager manager, List <string> pullFromVanillas, List <string> pullFromMods, ref HashSet <string> zeroFiles, ref List <string> oneFilesList, Dictionary <string, bool> pathUpdatesExist) { //Assumption: modded packs have already been extracted to _working0 directory - qarEntryEditList //Assumption: vanilla packs have already been extracted to _gameFpk directory (during AddToSettings) - fpkEntryRetrievalList //theoretically there should be no qar overlap between the _gamefpk(vanilla) and _working0(modded) files FastZip unzipper = new FastZip(); GameData gameData = manager.GetGameData(); foreach (string modFilePath in modFilePaths) { Debug.LogLine($"[Install] Installation started: {Path.GetFileName(modFilePath)}", Debug.LogLevel.Basic); Debug.LogLine($"[Install] Unzipping mod .mgsv ({Tools.GetFileSizeKB(modFilePath)} KB)", Debug.LogLevel.Basic); unzipper.ExtractZip(modFilePath, "_extr", "(.*?)"); Debug.LogLine("[Install] Load mod metadata", Debug.LogLevel.Basic); ModEntry extractedModEntry = new ModEntry("_extr\\metadata.xml"); if (pathUpdatesExist[extractedModEntry.Name]) { Debug.LogLine(string.Format("[Install] Checking for Qar path updates: {0}", extractedModEntry.Name), Debug.LogLevel.Basic); foreach (ModQarEntry modQar in extractedModEntry.ModQarEntries.Where(entry => !entry.FilePath.StartsWith("/Assets/"))) { string unhashedName = HashingExtended.UpdateName(modQar.FilePath); if (unhashedName != null) { Debug.LogLine(string.Format("[Install] Update successful: {0} -> {1}", modQar.FilePath, unhashedName), Debug.LogLevel.Basic); string workingOldPath = Path.Combine("_extr", Tools.ToWinPath(modQar.FilePath)); string workingNewPath = Path.Combine("_extr", Tools.ToWinPath(unhashedName)); if (!Directory.Exists(Path.GetDirectoryName(workingNewPath))) { Directory.CreateDirectory(Path.GetDirectoryName(workingNewPath)); } if (!File.Exists(workingNewPath)) { File.Move(workingOldPath, workingNewPath); } modQar.FilePath = unhashedName; } } } GzsLib.LoadModDictionary(extractedModEntry); ValidateModEntries(ref extractedModEntry); Debug.LogLine("[Install] Check mod FPKs against game .dat fpks", Debug.LogLevel.Basic); zeroFiles.UnionWith(MergePacks(extractedModEntry, pullFromVanillas, pullFromMods)); //foreach (string zeroFile in zeroFiles) Debug.LogLine(string.Format("Contained in zeroFiles: {0}", zeroFile)); Debug.LogLine("[Install] Copying loose textures to 01.", Debug.LogLevel.Basic); InstallLooseFtexs(extractedModEntry, ref oneFilesList); Debug.LogLine("[Install] Copying game dir files", Debug.LogLevel.Basic); InstallGameDirFiles(extractedModEntry, ref gameData); } manager.SetGameData(gameData); }
public static void CopyGameDirManagedFiles(string destinationDir) { Debug.LogLine($"[SB_Build] Copying {Path.GetDirectoryName(GameDir)} to {Path.GetDirectoryName(destinationDir)}", Debug.LogLevel.Basic); foreach (string externalFile in new SettingsManager(SnakeBiteSettings).GetModExternalFiles()) { string fileModPath = Tools.ToWinPath(externalFile); string sourceFullPath = Path.Combine(GameDir, fileModPath); string destFullPath = Path.Combine(destinationDir, fileModPath); Directory.CreateDirectory(Path.GetDirectoryName(destFullPath)); if (File.Exists(sourceFullPath)) { File.Copy(sourceFullPath, destFullPath, true); } } }
private static void MoveDatFilesClean(SettingsManager manager) { string sourceName = null; string destName = null; string destFolder = null; // lua files 00 -> 01, texture files 01 -> texture7, foxpatch 00 -> 00, chunkfiles 00 -> chunk7 Debug.LogLine("[DatMerge] First Time Setup Started", Debug.LogLevel.Debug); if (manager.SettingsExist()) manager.ClearAllMods(); List<string> zeroFiles = GzsLib.ExtractArchive<QarFile>(ZeroPath, "_extr"); List<string> chunk7Files = new List<string>(); List<string> oneFiles = new List<string>(); List<string> zeroOut = zeroFiles.ToList(); foreach (string zeroFile in zeroFiles) { if (zeroFile == "foxpatch.dat") continue; sourceName = Path.Combine("_extr", Tools.ToWinPath(zeroFile)); if (zeroFile.Contains(".lua")) { destName = Path.Combine("_working1", Tools.ToWinPath(zeroFile)); // 00 -> 01 oneFiles.Add(zeroFile); } else { destName = Path.Combine("_working2", Tools.ToWinPath(zeroFile)); // 00 -> chunk7 chunk7Files.Add(zeroFile); } zeroOut.Remove(zeroFile); destFolder = Path.GetDirectoryName(destName); if (!Directory.Exists(destFolder)) Directory.CreateDirectory(destFolder); if (!File.Exists(destName)) File.Move(sourceName, destName); } // Build a_chunk7.dat.SB_Build GzsLib.WriteQarArchive(c7Path + build_ext, "_working2", chunk7Files, GzsLib.chunk7Flags); // Build a_texture7.dat.SB_Build File.Copy(OnePath, t7Path + build_ext, true); // Build 00.dat.SB_Build GzsLib.WriteQarArchive(ZeroPath + build_ext, "_extr", zeroOut, GzsLib.zeroFlags); // Build 01.dat.SB_Build GzsLib.WriteQarArchive(OnePath + build_ext, "_working1", oneFiles, GzsLib.oneFlags); }
public static void RestoreBackupGameDir(SettingsManager SBBuildSettings) { Debug.LogLine("[SB_Build] Promoting SB_Build Game Directory", Debug.LogLevel.Basic); if (!Directory.Exists(GameDirBackup_Build)) { Debug.LogLine($"[SB_Build] Directory not found: {GameDirBackup_Build}", Debug.LogLevel.Basic); return; } List<string> fileEntryDirs = new List<string>(); foreach (string externalBuildFiles in SBBuildSettings.GetModExternalFiles()) { string fileModPath = Tools.ToWinPath(externalBuildFiles); string sourceFullPath = Path.Combine(GameDir, fileModPath); string sourceDir = Path.GetDirectoryName(sourceFullPath); if (!fileEntryDirs.Contains(sourceDir)) fileEntryDirs.Add(sourceDir); if (File.Exists(sourceFullPath)) File.Delete(sourceFullPath); // deletes all of the new snakebite.xml's managed files else Debug.LogLine(string.Format("[SB_Build] File not found: {0}", sourceFullPath), Debug.LogLevel.Basic); } Tools.DirectoryCopy(GameDirBackup_Build, GameDir, true); // moves all gamedir_backup_build files over foreach (string fileEntryDir in fileEntryDirs) //all the directories that have had files deleted within them { if (Directory.Exists(fileEntryDir) && Directory.GetFiles(fileEntryDir).Length == 0) // if the directory has not yet been deleted and there are no more files inside the directory { Debug.LogLine(String.Format("[SB_Build] deleting empty folder: {0}", fileEntryDir), Debug.LogLevel.All); try { Directory.Delete(fileEntryDir); } catch { Console.WriteLine("[Uninstall] Could not delete: " + fileEntryDir); } } } }
public static void PromoteGameDirFiles() // call this method BEFORE snakebite.xml.SB_Build is promoted, so it will reference the old snakebite.xml { Debug.LogLine("[SB_Build] Promoting SB_Build Game Directory", Debug.LogLevel.Basic); if (!Directory.Exists(GameDirSB_Build)) { Debug.LogLine($"[SB_Build] Directory not found: {GameDirSB_Build}", Debug.LogLevel.Basic); return; } List<string> fileEntryDirs = new List<string>(); foreach (string externalFile in new SettingsManager(SnakeBiteSettings).GetModExternalFiles()) { string fileModPath = Tools.ToWinPath(externalFile); string sourceFullPath = Path.Combine(GameDir, fileModPath); string sourceDir = Path.GetDirectoryName(sourceFullPath); if (!fileEntryDirs.Contains(sourceDir)) fileEntryDirs.Add(sourceDir); if (File.Exists(sourceFullPath)) File.Delete(sourceFullPath); // deletes all of the old snakebite.xml's managed files (unmanaged files will be overwritten later or left alone) else Debug.LogLine(string.Format("[SB_Build] File not found: {0}", sourceFullPath), Debug.LogLevel.Basic); } Tools.DirectoryCopy(GameDirSB_Build, GameDir, true); // moves all gamedir_sb_build files over foreach (string fileEntryDir in fileEntryDirs) { if (Directory.Exists(fileEntryDir) && Directory.GetFiles(fileEntryDir).Length == 0) { Debug.LogLine(String.Format("[SB_Build] deleting empty folder: {0}", fileEntryDir), Debug.LogLevel.All); try { Directory.Delete(fileEntryDir); } catch { Console.WriteLine("[Uninstall] Could not delete: " + fileEntryDir); } } } }
// i/o: _extr to _working1 private static void InstallLooseFtexs(ModEntry modEntry, ref List <string> oneFilesList) { foreach (ModQarEntry modQarEntry in modEntry.ModQarEntries) { if (modQarEntry.FilePath.Contains(".ftex")) { if (!oneFilesList.Contains(Tools.ToWinPath(modQarEntry.FilePath))) { oneFilesList.Add(Tools.ToWinPath(modQarEntry.FilePath)); } string sourceFile = Path.Combine("_extr", Tools.ToWinPath(modQarEntry.FilePath)); string destFile = Path.Combine("_working1", Tools.ToWinPath(modQarEntry.FilePath)); string destDir = Path.GetDirectoryName(destFile); Debug.LogLine(String.Format("[Install] Copying texture file: {0}", modQarEntry.FilePath), Debug.LogLevel.All); if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } File.Copy(sourceFile, destFile, true); } } }
private static void UninstallGameDirEntries(ModEntry mod, ref GameData gameData) { HashSet <string> fileEntryDirs = new HashSet <string>(); foreach (ModFileEntry fileEntry in mod.ModFileEntries) //checks all of current mod's files { string destFile = Path.Combine(GameDirSB_Build, Tools.ToWinPath(fileEntry.FilePath)); //create the filepath to the file in question string dir = Path.GetDirectoryName(destFile); //filepath of the directory containing the file fileEntryDirs.Add(dir); //the directory is added to the list of fileentrydirectories if (File.Exists(destFile)) // attempt to delete the file in question { try { File.Delete(destFile); // deletes the specified file } catch { Console.WriteLine("[Uninstall] Could not delete: " + destFile); } } gameData.GameFileEntries.RemoveAll(file => Tools.CompareHashes(file.FilePath, fileEntry.FilePath)); //remove all mentions of the destFile from snakebite.xml }//foreach ModFileEntries foreach (string fileEntryDir in fileEntryDirs) //all the directories that have had files deleted within them { if (Directory.Exists(fileEntryDir) && Directory.GetFiles(fileEntryDir).Length == 0) // if the directory has not yet been deleted and there are no more files inside the directory { Debug.LogLine(String.Format("[Uninstall] deleting empty folder: {0}", fileEntryDir), Debug.LogLevel.All); try { Directory.Delete(fileEntryDir); //attempt to delete the empty directory [NOT RECURSIVE] } catch { Console.WriteLine("[Uninstall] Could not delete: " + fileEntryDir); } } } //foreach fileEntryDirs } //UninstallGameDirEntries
// i/o: _extr to GameDir private static void InstallGameDirFiles(ModEntry modEntry, ref GameData gameData) { foreach (ModFileEntry fileEntry in modEntry.ModFileEntries) { bool skipFile = false; foreach (string ignoreFile in Tools.ignoreFileList) { if (fileEntry.FilePath.Contains(ignoreFile)) { skipFile = true; } } /* * foreach (string ignoreExt in ignoreExtList) * { * if (fileEntry.FilePath.Contains(ignoreExt)) * { * skipFile = true; * } * } */ if (skipFile == false) { string sourceFile = Path.Combine("_extr\\GameDir", Tools.ToWinPath(fileEntry.FilePath)); string destFile = Path.Combine(GameDirSB_Build, Tools.ToWinPath(fileEntry.FilePath)); Directory.CreateDirectory(Path.GetDirectoryName(destFile)); File.Copy(sourceFile, destFile, true); if (gameData.GameFileEntries.FirstOrDefault(e => e.FilePath == fileEntry.FilePath) == null) { gameData.GameFileEntries.Add(fileEntry); } } } }//InstallGameDirFiles
public static void RestoreOriginals() { // delete existing data File.Delete(ZeroPath); File.Delete(OnePath); File.Delete(chunk0Path); File.Delete(c7Path); File.Delete(t7Path); // delete mod data File.Delete(ZeroPath + modded_ext); File.Delete(OnePath + modded_ext); // delete GameDir data List <string> fileEntryDirs = new List <string>(); foreach (string externalFile in new SettingsManager(SnakeBiteSettings).GetModExternalFiles()) { string fileModPath = Tools.ToWinPath(externalFile); string sourceFullPath = Path.Combine(GameDir, fileModPath); string fullPathDir = Path.GetDirectoryName(sourceFullPath); try { if (File.Exists(sourceFullPath)) { File.Delete(sourceFullPath); if (!fileEntryDirs.Contains(fullPathDir)) { fileEntryDirs.Add(fullPathDir); } } } catch { Debug.LogLine("[Uninstall] Could not delete " + fileModPath); } } foreach (string fileEntryDir in fileEntryDirs) { if (Directory.Exists(fileEntryDir)) { try { if (Directory.GetFiles(fileEntryDir).Length == 0) { Directory.Delete(fileEntryDir); } } catch { Debug.LogLine("[Uninstall] Could not delete: " + fileEntryDir); } } } // restore backups bool fileExists = true; while (fileExists) { Thread.Sleep(100); fileExists = false; if (File.Exists(ZeroPath)) { fileExists = true; } if (File.Exists(OnePath)) { fileExists = true; } if (File.Exists(chunk0Path)) { fileExists = true; } if (File.Exists(c7Path)) { fileExists = true; } if (File.Exists(t7Path)) { fileExists = true; } } File.Move(ZeroPath + original_ext, ZeroPath); File.Move(OnePath + original_ext, OnePath); File.Move(chunk0Path + original_ext, chunk0Path); }
public static void MoveDatFiles() { Debug.LogLine("[DatMerge] System data merge started", Debug.LogLevel.Debug); var zeroList = GzsLib.ListArchiveContents <QarFile>(ZeroPath); var modQarFiles = SettingsManager.GetModQarFiles(); int mc = 0; foreach (string zeroFile in zeroList) { if (zeroFile == "foxpatch.dat") { continue; } if (modQarFiles.Contains(Tools.ToQarPath(zeroFile))) { continue; } mc++; } if (mc > 0) { CleanupFolders(); // Extract 00.dat var zeroFiles = GzsLib.ExtractArchive <QarFile>(ZeroPath, "_extr"); // Extract 01.dat var oneFiles = GzsLib.ExtractArchive <QarFile>(OnePath, "_working"); var zeroOut = zeroFiles.ToList(); Debug.LogLine(string.Format("[DatMerge] Moving {0} system files", mc), Debug.LogLevel.Debug); // Move files from 00 to 01 (excluding foxpatch.dat) foreach (string zeroFile in zeroFiles) { if (zeroFile == "foxpatch.dat") { continue; } if (modQarFiles.Contains(Tools.ToQarPath(zeroFile))) { continue; } string sourceName = Path.Combine("_extr", Tools.ToWinPath(zeroFile)); string destName = Path.Combine("_working", Tools.ToWinPath(zeroFile)); string destFolder = Path.GetDirectoryName(destName); Debug.LogLine(string.Format("[DatMerge] Moving system file {0}", zeroFile)); if (!Directory.Exists(destFolder)) { Directory.CreateDirectory(destFolder); } File.Move(sourceName, destName); zeroOut.Remove(zeroFile); oneFiles.Add(zeroFile); } Debug.LogLine("[DatMerge] Rebuilding game archives", Debug.LogLevel.Debug); // Rebuild 01.dat with files GzsLib.WriteQarArchive(OnePath, "_working", oneFiles, 3150048); // Rebuild 00.dat GzsLib.WriteQarArchive(ZeroPath, "_extr", zeroOut, 3150304); SettingsManager.UpdateDatHash(); CleanupFolders(); Debug.LogLine("[DatMerge] Merge finished", Debug.LogLevel.Debug); } else { Debug.LogLine("[DatMerge] No files to merge, aborting", Debug.LogLevel.Debug); } }
private static void UnmergePackFiles(List <ModQarEntry> partialEditQarEntries, List <ModFpkEntry> partialRemoveFpkEntries) { GameData gameData = SBBuildManager.GetGameData(); List <ModFpkEntry> addedRepairFpkEntries = new List <ModFpkEntry>(); foreach (ModQarEntry partialEditQarEntry in partialEditQarEntries) { // create a list of fpk filepaths that need to be modified for the specific qar file (either restored to vanilla or removed from the pack) List <string> fpkPathsForThisQar = partialRemoveFpkEntries.Where(entry => entry.FpkFile == partialEditQarEntry.FilePath).Select(fpkEntry => Tools.ToWinPath(fpkEntry.FilePath)).ToList(); var fpkReferences = new List <string>();//tex references in fpk that need to be preserved/transfered to the rebuilt fpk // pull the vanilla qar file from the game archive, send to _gameFpk folder string winQarEntryPath = Tools.ToWinPath(partialEditQarEntry.FilePath); string gameQarPath = Path.Combine("_gameFpk", winQarEntryPath); if (partialEditQarEntry.SourceName != null) { string vanillaArchivePath = Path.Combine(GameDir, "master\\" + partialEditQarEntry.SourceName); //Debug.LogLine(string.Format("Pulling {0} from {1}", partialRemoveQarEntry.FilePath, partialRemoveQarEntry.SourceName)); GzsLib.ExtractFileByHash <QarFile>(vanillaArchivePath, partialEditQarEntry.Hash, gameQarPath); fpkReferences = GzsLib.GetFpkReferences(gameQarPath); } // pull the modded qar file from _working0 (assumed to already exist when the uninstall process reads 00.dat), send to _build folder string workingZeroQarPath = Path.Combine("_working0", winQarEntryPath); List <string> moddedFpkFiles = GzsLib.ExtractArchive <FpkFile>(workingZeroQarPath, "_build"); // split the fpk paths for this Qar into two categories: List <string> repairFilePathList = new List <string>(); // files that need to be repaired (aka overwritten by a vanilla file) if (partialEditQarEntry.SourceName != null) { repairFilePathList = GzsLib.ListArchiveContents <FpkFile>(gameQarPath).Intersect(fpkPathsForThisQar).ToList(); } List <string> removeFilePathList = fpkPathsForThisQar.Except(repairFilePathList).ToList(); // files that need to be removed (i.e. files that were non-native to the vanilla Qar) foreach (string repairFilePath in repairFilePathList) { string fpkBuildPath = Path.Combine("_build", repairFilePath); Debug.LogLine(string.Format("[Unmerge Fpk] Extracting repair file: {0}", repairFilePath), Debug.LogLevel.Basic); GzsLib.ExtractFile <FpkFile>(gameQarPath, repairFilePath, fpkBuildPath); // overwrites modded fpk files ModFpkEntry repairEntry = new ModFpkEntry { FpkFile = partialEditQarEntry.FilePath, FilePath = repairFilePath, // this will be a window path SourceType = FileSource.Merged, SourceName = partialEditQarEntry.SourceName }; gameData.GameFpkEntries.Add(repairEntry); addedRepairFpkEntries.Add(repairEntry); } var buildFiles = moddedFpkFiles.Except(removeFilePathList).ToList(); //tex refs werent grabbed from vanilla (or there weren't any in there in that case we're probably doubling the work lol) if (fpkReferences.Count == 0) { fpkReferences = GzsLib.GetFpkReferences(workingZeroQarPath); } GzsLib.WriteFpkArchive(workingZeroQarPath, "_build", buildFiles, fpkReferences); // writes the pack back to _working folder (leaving out the non-native fpk files) foreach (string removeFilePath in removeFilePathList) { int indexToRemove = gameData.GameFpkEntries.FindIndex(entry => entry.FilePath == removeFilePath); if (indexToRemove >= 0) { gameData.GameFpkEntries.RemoveAt(indexToRemove); } } } List <ModEntry> installedMods = SBBuildManager.GetInstalledMods(); foreach (ModEntry installedMod in installedMods) { List <string> qarPathsFound = new List <string>(); foreach (ModFpkEntry addedRepairEntry in addedRepairFpkEntries) { if (installedMod.ModQarEntries.FirstOrDefault(entry => entry.FilePath == addedRepairEntry.FpkFile) == null) { continue; } //Debug.LogLine(string.Format("checking {0} for {1} of {2}", installedMod.Name, addedRepairEntry.FilePath, addedRepairEntry.FpkFile)); if (installedMod.ModFpkEntries.RemoveAll(entry => entry.FilePath == Tools.ToQarPath(addedRepairEntry.FilePath) && entry.FpkFile == addedRepairEntry.FpkFile) > 0) { //Debug.LogLine(string.Format("found {0} of {1} in {2}", addedRepairEntry.FilePath, addedRepairEntry.FpkFile, installedMod.Name)); if (!qarPathsFound.Contains(addedRepairEntry.FpkFile)) { qarPathsFound.Add(addedRepairEntry.FpkFile); } } } foreach (string qarPathFound in qarPathsFound) { if (installedMod.ModFpkEntries.FirstOrDefault(entry => entry.FpkFile == qarPathFound) == null) //when the duplicate fpk file(s) were removed, there was nothing left in the modded qar. { //Debug.LogLine(string.Format("Removing {0} from {1}", qarPathFound, installedMod.Name)); installedMod.ModQarEntries.RemoveAll(entry => entry.FilePath == qarPathFound); // filters the qar file out of the list } } } SBBuildManager.SetInstalledMods(installedMods); SBBuildManager.SetGameData(gameData); }
}//InstallGameDirFiles private static void AddToSettingsFpk(List <ModEntry> installEntryList, SettingsManager manager, List <Dictionary <ulong, GameFile> > allQarGameFiles, out List <string> PullFromVanillas, out List <string> pullFromMods, out Dictionary <string, bool> pathUpdatesExist) { GameData gameData = manager.GetGameData(); pathUpdatesExist = new Dictionary <string, bool>(); List <string> newModQarEntries = new List <string>(); List <string> modQarFiles = manager.GetModQarFiles(); pullFromMods = new List <string>(); foreach (ModEntry modToInstall in installEntryList) { Dictionary <string, string> newNameDictionary = new Dictionary <string, string>(); int foundUpdate = 0; foreach (ModQarEntry modQar in modToInstall.ModQarEntries.Where(entry => !entry.FilePath.StartsWith("/Assets/"))) { //Debug.LogLine(string.Format("Attempting to update Qar filename: {0}", modQar.FilePath), Debug.LogLevel.Basic); string unhashedName = HashingExtended.UpdateName(modQar.FilePath); if (unhashedName != null) { //Debug.LogLine(string.Format("Success: {0}", unhashedName), Debug.LogLevel.Basic); newNameDictionary.Add(modQar.FilePath, unhashedName); foundUpdate++; modQar.FilePath = unhashedName; if (!pathUpdatesExist.ContainsKey(modToInstall.Name)) { pathUpdatesExist.Add(modToInstall.Name, true); } else { pathUpdatesExist[modToInstall.Name] = true; } } } if (foundUpdate > 0) { foreach (ModFpkEntry modFpkEntry in modToInstall.ModFpkEntries) { string unHashedName; if (newNameDictionary.TryGetValue(modFpkEntry.FpkFile, out unHashedName)) { modFpkEntry.FpkFile = unHashedName; } } } else if (!pathUpdatesExist.ContainsKey(modToInstall.Name)) { pathUpdatesExist.Add(modToInstall.Name, false); } manager.AddMod(modToInstall); //foreach (ModQarEntry modqar in modToInstall.ModQarEntries) Debug.LogLine("Mod Qar in modToInstall: " + modqar.FilePath); foreach (ModQarEntry modQarEntry in modToInstall.ModQarEntries) // add qar entries (fpk, fpkd) to GameData if they don't already exist { string modQarFilePath = modQarEntry.FilePath; if (!(modQarFilePath.EndsWith(".fpk") || modQarFilePath.EndsWith(".fpkd"))) { continue; // only pull for Qar's with Fpk's } if (modQarFiles.Any(entry => entry == modQarFilePath)) { pullFromMods.Add(modQarFilePath); //Debug.LogLine("Pulling from 00.dat: {0} " + modQarFilePath); } else if (!newModQarEntries.Contains(modQarFilePath)) { newModQarEntries.Add(modQarFilePath); //Debug.LogLine("Pulling from base archives: {0} " + modQarFilePath); } } } //Debug.LogLine(string.Format("Foreach nest 1 complete")); List <ModFpkEntry> newModFpkEntries = new List <ModFpkEntry>(); foreach (ModEntry modToInstall in installEntryList) { foreach (ModFpkEntry modFpkEntry in modToInstall.ModFpkEntries) { //Debug.LogLine(string.Format("Checking out {0} from {1}", modFpkEntry.FilePath, modFpkEntry.FpkFile)); if (newModQarEntries.Contains(modFpkEntry.FpkFile)) // it isn't already part of the snakebite environment { //Debug.LogLine(string.Format("seeking repair files around {0}", modFpkEntry.FilePath)); newModFpkEntries.Add(modFpkEntry); } else { //Debug.LogLine(string.Format("Removing {0} from gameFpkEntries so it will only be listed in the mod's entries", modFpkEntry.FilePath)); int indexToRemove = gameData.GameFpkEntries.FindIndex(m => m.FilePath == Tools.ToWinPath(modFpkEntry.FilePath)); // this will remove the gamedata's listing of the file under fpkentries (repair entries), so the filepath will only be listed in the modentry if (indexToRemove >= 0) { gameData.GameFpkEntries.RemoveAt(indexToRemove); } } } } //Debug.LogLine(string.Format("Foreach nest 2 complete")); HashSet <ulong> mergeFpkHashes = new HashSet <ulong>(); PullFromVanillas = new List <string>(); var repairFpkEntries = new List <ModFpkEntry>(); foreach (ModFpkEntry newFpkEntry in newModFpkEntries) // this will add the fpkentry listings (repair entries) to the settings xml { //Debug.LogLine(string.Format("checking {0} for repairs", newFpkEntry.FilePath)); ulong packHash = Tools.NameToHash(newFpkEntry.FpkFile); if (mergeFpkHashes.Contains(packHash)) { continue; // the process has already plucked this particular qar file } foreach (var archiveQarGameFiles in allQarGameFiles) // check every archive (except 00) to see if the particular qar file already exists { //Debug.LogLine(string.Format("checking archive for an existing qar file")); if (archiveQarGameFiles.Count > 0) { GameFile existingPack = null; archiveQarGameFiles.TryGetValue(packHash, out existingPack); if (existingPack != null) // the qar file is found { //Debug.LogLine(string.Format("Qar file {0} found in {1}. adding to gameqarentries", newFpkEntry.FpkFile, existingPack.QarFile)); mergeFpkHashes.Add(packHash); gameData.GameQarEntries.Add(new ModQarEntry { FilePath = newFpkEntry.FpkFile, SourceType = FileSource.Merged, SourceName = existingPack.QarFile, Hash = existingPack.FileHash }); PullFromVanillas.Add(newFpkEntry.FpkFile); string windowsFilePath = Tools.ToWinPath(newFpkEntry.FpkFile); // Extract the pack file from the vanilla game files, place into _gamefpk for future use string sourceArchive = Path.Combine(GameDir, "master\\" + existingPack.QarFile); string workingPath = Path.Combine("_gameFpk", windowsFilePath); if (!Directory.Exists(Path.GetDirectoryName(workingPath))) { Directory.CreateDirectory(Path.GetDirectoryName(workingPath)); } GzsLib.ExtractFileByHash <QarFile>(sourceArchive, existingPack.FileHash, workingPath); // extracts the specific .fpk from the game data foreach (string listedFile in GzsLib.ListArchiveContents <FpkFile>(workingPath)) { repairFpkEntries.Add(new ModFpkEntry { FpkFile = newFpkEntry.FpkFile, FilePath = listedFile, SourceType = FileSource.Merged, SourceName = existingPack.QarFile }); //Debug.LogLine(string.Format("File Listed: {0} in {1}", extractedFile, newFpkEntry.FpkFile)); } break; } } } } //Debug.LogLine(string.Format("Foreach nest 3 complete")); foreach (ModFpkEntry newFpkEntry in newModFpkEntries) // finally, strip away the modded entries from the repair entries { //Debug.LogLine(string.Format("checking to remove {0} from gamefpkentries", Tools.ToWinPath(newFpkEntry.FilePath))); int indexToRemove = repairFpkEntries.FindIndex(m => m.FilePath == Tools.ToWinPath(newFpkEntry.FilePath)); if (indexToRemove >= 0) { repairFpkEntries.RemoveAt(indexToRemove); } } gameData.GameFpkEntries = gameData.GameFpkEntries.Union(repairFpkEntries).ToList(); manager.SetGameData(gameData); }
// 00 non-snakebite Files to 01, 01 lua files unchanged, 01 textures -> t7, 01 chunkfiles -> c7, private static void MoveDatFilesDirty(SettingsManager manager) { var modQarFiles = manager.GetModQarFiles(); string sourceName = null; string destName = null; string destFolder = null; Debug.LogLine("[DatMerge] Dispersing files from 00 to 01, and then from 01 to a_chunk7 and a_texture7 if necessary.", Debug.LogLevel.Debug); List<string> oneFiles = GzsLib.ExtractArchive<QarFile>(OnePath, "_extr"); List<string> zeroList = new List<string>(); int moveCount = 0; try { zeroList = GzsLib.ListArchiveContents<QarFile>(ZeroPath); } catch (Exception e) { Debug.LogLine(String.Format("[Error] GzsLib.ListArchiveContents exception: {0}", e.ToString()), Debug.LogLevel.Debug); throw e; } foreach (string zeroFile in zeroList) { if (zeroFile == "foxpatch.dat") continue; if (modQarFiles.Contains(Tools.ToQarPath(zeroFile)) || oneFiles.Contains(zeroFile)) continue; if (oneFiles.Contains(zeroFile)) continue; moveCount++; } if (moveCount > 0) //if any non-snakebite files exist in 00, move them to 01. { Debug.LogLine("[DatMerge] Moving files to 01.dat.", Debug.LogLevel.Debug); List<string> zeroFiles = GzsLib.ExtractArchive<QarFile>(ZeroPath, "_working1"); List<string> zeroOut = zeroFiles.ToList(); foreach (string zeroFile in zeroFiles) { if (zeroFile == "foxpatch.dat") continue; if (modQarFiles.Contains(Tools.ToQarPath(zeroFile))) continue; if (oneFiles.Contains(zeroFile)) { zeroOut.Remove(zeroFile); continue; } //if it already exists in 01 then there's nowhere for it to go. sourceName = Path.Combine("_working1", Tools.ToWinPath(zeroFile)); destName = Path.Combine("_extr", Tools.ToWinPath(zeroFile)); oneFiles.Add(zeroFile); // 00 -> 01 zeroOut.Remove(zeroFile); destFolder = Path.GetDirectoryName(destName); if (!Directory.Exists(destFolder)) Directory.CreateDirectory(destFolder); if (!File.Exists(destName)) File.Move(sourceName, destName); } GzsLib.WriteQarArchive(ZeroPath + build_ext, "_working1", zeroOut, GzsLib.zeroFlags); // rebuild 00 archive Directory.Delete("_working1", true); // clean up _working1, to be used by texture7 while (Directory.Exists("_working1")) Thread.Sleep(100); } moveCount = 0; // check if any files need to be moved to C7/T7 int textureCount = 0; List<string> chunk7List = new List<string>(); List<string> tex7List = new List<string>(); try { if (File.Exists(t7Path)) tex7List = GzsLib.ListArchiveContents<QarFile>(t7Path); if (File.Exists(c7Path)) chunk7List = GzsLib.ListArchiveContents<QarFile>(c7Path); } catch (Exception e) { Debug.LogLine(String.Format("[Error] GzsLib.ListArchiveContents exception: {0}", e.ToString()), Debug.LogLevel.Debug); throw e; } foreach (string oneFile in oneFiles) { if (modQarFiles.Contains(Tools.ToQarPath(oneFile)) || tex7List.Contains(oneFile) || chunk7List.Contains(oneFile)) continue; if (oneFile.Contains(".lua")) continue; // vanilla lua files must stay in 01 if (oneFile.Contains(".ftex")) textureCount++; moveCount++; } if (moveCount > 0) { List<string> oneOut = oneFiles.ToList(); if (textureCount > 0) // if non-snakebite textures exist, move them to t7 { Debug.LogLine("[DatMerge] Moving files to a_texture7.dat.", Debug.LogLevel.Debug); List<string> texture7Files = new List<string>(); if (File.Exists(t7Path)) texture7Files = GzsLib.ExtractArchive<QarFile>(t7Path, "_working1"); foreach (string oneFile in oneFiles) // once 00 files have been moved, move 01 files into t7, c7. { if (modQarFiles.Contains(Tools.ToQarPath(oneFile))) continue; if (oneFile.Contains(".ftex")) { sourceName = Path.Combine("_extr", Tools.ToWinPath(oneFile)); destName = Path.Combine("_working1", Tools.ToWinPath(oneFile)); // 01 -> texture7 destFolder = Path.GetDirectoryName(destName); if (!texture7Files.Contains(oneFile)) texture7Files.Add(oneFile); oneOut.Remove(oneFile); if (!Directory.Exists(destFolder)) Directory.CreateDirectory(destFolder); if (!File.Exists(destName)) File.Move(sourceName, destName); } } GzsLib.WriteQarArchive(t7Path + build_ext, "_working1", texture7Files, GzsLib.texture7Flags); } oneFiles = oneOut.ToList(); // update oneFiles to remove any .ftex already found if (oneFiles.Count > 0) // if any other files need to be moved, they go in chunk7 { Debug.LogLine("[DatMerge] Moving files to a_chunk7.dat.", Debug.LogLevel.Debug); List<string> chunk7Files = new List<string>(); if (File.Exists(c7Path)) chunk7Files = GzsLib.ExtractArchive<QarFile>(c7Path, "_working2"); foreach (string oneFile in oneFiles) // once 00 files have been moved, move 01 files into t7, c7. { if (modQarFiles.Contains(Tools.ToQarPath(oneFile))) continue; if (oneFile.Contains(".lua")) continue; sourceName = Path.Combine("_extr", Tools.ToWinPath(oneFile)); destName = Path.Combine("_working2", Tools.ToWinPath(oneFile)); // 00 -> chunk7 destFolder = Path.GetDirectoryName(destName); if (!chunk7Files.Contains(oneFile)) chunk7Files.Add(oneFile); oneOut.Remove(oneFile); if (!Directory.Exists(destFolder)) Directory.CreateDirectory(destFolder); if (!File.Exists(destName)) File.Move(sourceName, destName); } GzsLib.WriteQarArchive(c7Path + build_ext, "_working2", chunk7Files, GzsLib.chunk7Flags); // rebuild chunk7 archive } GzsLib.WriteQarArchive(OnePath + build_ext, "_extr", oneOut, GzsLib.oneFlags); // rebuild 01 archive } }
private static HashSet <string> MergePacks(ModEntry extractedModEntry, List <string> pullFromVanillas, List <string> pullFromMods) { HashSet <string> modQarZeroPaths = new HashSet <string>(); foreach (ModQarEntry modQar in extractedModEntry.ModQarEntries) { string workingDestination = Path.Combine("_working0", Tools.ToWinPath(modQar.FilePath)); if (!Directory.Exists(Path.GetDirectoryName(workingDestination))) { Directory.CreateDirectory(Path.GetDirectoryName(workingDestination)); } string modQarSource = Path.Combine("_extr", Tools.ToWinPath(modQar.FilePath)); string existingQarSource; if (pullFromMods.FirstOrDefault(e => e == modQar.FilePath) != null) { //Debug.LogLine(string.Format("{0}'s Qar file '{1}' will pull from _working0 (modded)", extractedModEntry.Name, modQar.FilePath)); existingQarSource = workingDestination; } else { int indexToRemove = pullFromVanillas.FindIndex(m => m == modQar.FilePath); // remove from retrievalfilepaths and add to editlist if (indexToRemove >= 0) { //Debug.LogLine(string.Format("{0}'s Qar file '{1}' will pull from _gameFpk (fresh game file)", extractedModEntry.Name, modQar.FilePath)); existingQarSource = Path.Combine("_gameFpk", Tools.ToWinPath(modQar.FilePath)); pullFromVanillas.RemoveAt(indexToRemove); pullFromMods.Add(modQar.FilePath); } else { //Debug.LogLine(string.Format("{0}'s Qar file '{1}' is non-native or not mergeable", extractedModEntry.Name, modQar.FilePath)); existingQarSource = null; if (modQar.FilePath.EndsWith(".fpk") || modQar.FilePath.EndsWith(".fpkd")) { pullFromMods.Add(modQar.FilePath); // for merging non-native fpk files consecutively } } } if (existingQarSource != null) { var pulledPack = GzsLib.ExtractArchive <FpkFile>(existingQarSource, "_build"); var extrPack = GzsLib.ExtractArchive <FpkFile>(modQarSource, "_build"); pulledPack = pulledPack.Union(extrPack).ToList(); //foreach(string file in extrPack) Debug.LogLine(string.Format("{0} is listed in the archive extr", file)); //TODO: merge referenced from mod archive to (but then that would probably be tricky keeping track of for Unmerge) //I think only fpks have references (not fpkd), but whatev. var fpkReferences = GzsLib.GetFpkReferences(existingQarSource); GzsLib.WriteFpkArchive(workingDestination, "_build", pulledPack, fpkReferences); } else { File.Copy(modQarSource, workingDestination, true); } if (!modQar.FilePath.Contains(".ftex")) { //Debug.LogLine(string.Format("Adding {0}'s Qar file '{1}' to 00.dat", extractedModEntry.Name, modQar.FilePath)); modQarZeroPaths.Add(Tools.ToWinPath(modQar.FilePath)); } } return(modQarZeroPaths); }
public static bool UninstallMod(ModEntry mod) { Debug.LogLine(String.Format("[Mod] Uninstall started: {0}", mod.Name), Debug.LogLevel.Basic); CleanupFolders(); // Extract game archive var zeroFiles = GzsLib.ExtractArchive <QarFile>(ZeroPath, "_working"); // List all FPKs in mod List <string> modFpks = new List <string>(); foreach (ModFpkEntry fpkEntry in mod.ModFpkEntries) { if (!modFpks.Contains(fpkEntry.FpkFile)) { modFpks.Add(fpkEntry.FpkFile); } } var gameData = SettingsManager.GetGameData(); // Extract FPK foreach (string fpk in modFpks) { string fpkName = Path.GetFileName(fpk); string fpkDatPath = zeroFiles.FirstOrDefault(file => Tools.CompareHashes(file, fpk)); if (fpkDatPath == null) { continue; } var fpkFile = GzsLib.ExtractArchive <FpkFile>(Path.Combine("_working", Tools.ToWinPath(fpkDatPath)), "_modfpk"); // Remove all mod fpk files from fpk foreach (ModFpkEntry fpkEntry in mod.ModFpkEntries) { Debug.LogLine(String.Format("[Mod] Removing {1}\\{0}", Tools.ToWinPath(fpkEntry.FilePath), fpkName), Debug.LogLevel.Debug); fpkFile.RemoveAll(file => Tools.ToQarPath(file) == Tools.ToQarPath(fpkEntry.FilePath)); } var gameFpks = gameData.GameFpkEntries.ToList(); // remove all merged files from fpk foreach (ModFpkEntry gameFpkFile in gameFpks) { if (Tools.ToQarPath(gameFpkFile.FpkFile) == Tools.ToQarPath(fpk) && gameFpkFile.SourceType == FileSource.Merged) { Debug.LogLine(String.Format("[Mod] Removing merged file {0}", gameFpkFile.FilePath)); fpkFile.RemoveAll(entry => entry == gameFpkFile.FilePath); gameData.GameFpkEntries.Remove(gameFpkFile); } } // remove fpk if no files left if (fpkFile.Count == 0) { Debug.LogLine(String.Format("[Mod] {0} is empty, removing", fpkName), Debug.LogLevel.Debug); zeroFiles.RemoveAll(file => Tools.CompareHashes(file, fpk)); gameData.GameQarEntries.RemoveAll(file => Tools.CompareHashes(file.FilePath, fpk)); } else { Debug.LogLine(String.Format("[Mod] Rebuilding {0}", fpk), Debug.LogLevel.Debug); // rebuild fpk from base file var baseData = GzsLib.ReadBaseData(); var oneFiles = baseData.FileList.FindAll(entry => entry.QarFile == "01.dat"); var baseFiles = baseData.FileList.FindAll(entry => entry.QarFile != "01.dat"); // Check for FPKs in 00.dat first GameFile file = oneFiles.FirstOrDefault(entry => entry.FileHash == Tools.NameToHash(fpk)); if (file != null) { // Extract base FPK files GzsLib.ExtractFileByHash <QarFile>(Path.Combine(GameDir, "master\\0\\01.dat"), file.FileHash, "_working\\temp.fpk"); var gameFpk = GzsLib.ExtractArchive <FpkFile>("_working\\temp.fpk", "_gamefpk"); // Add merged base files to game file database var mCount = 0; foreach (var fpkF in gameFpk) { if (!fpkFile.Contains(fpkF)) { gameData.GameFpkEntries.Add(new ModFpkEntry() { FpkFile = fpk, FilePath = fpkF, SourceType = FileSource.Merged, SourceName = file.QarFile }); mCount++; } } Debug.LogLine(String.Format("[Mod] {0} files restored from {1}", mCount, file.QarFile), Debug.LogLevel.Debug); // Copy remaining files over base FPK foreach (string mFile in fpkFile) { string fDir = Path.GetDirectoryName(mFile); if (!Directory.Exists(Path.Combine("_gamefpk", fDir))) { Directory.CreateDirectory(Path.Combine("_gamefpk", fDir)); } Debug.LogLine(String.Format("[Mod] Merging existing file: {0}", mFile)); File.Copy(Path.Combine("_modfpk", mFile), Path.Combine(Path.Combine("_gamefpk", mFile)), true); if (!gameFpk.Contains(mFile)) { gameFpk.Add(mFile); } } // Rebuild FPK GzsLib.WriteFpkArchive(Path.Combine("_working", Tools.ToWinPath(fpkDatPath)), "_gamefpk", gameFpk); Directory.Delete("_gamefpk", true); Directory.Delete("_modfpk", true); continue; // don't check base data if it's in 01 } // check base files for FPK file = baseFiles.FirstOrDefault(entry => entry.FileHash == Tools.NameToHash(fpk)); if (file != null) { // Extract base FPK files GzsLib.ExtractFileByHash <QarFile>(Path.Combine(GameDir, "master\\" + file.QarFile), file.FileHash, "_working\\temp.fpk"); var gameFpk = GzsLib.ExtractArchive <FpkFile>("_working\\temp.fpk", "_gamefpk"); // Add merged base files to game file database var mCount = 0; foreach (var fpkF in gameFpk) { if (!fpkFile.Contains(fpkF)) { gameData.GameFpkEntries.Add(new ModFpkEntry() { FpkFile = fpk, FilePath = fpkF, SourceType = FileSource.Merged, SourceName = file.QarFile }); mCount++; } } Debug.LogLine(String.Format("[Mod] {0} files restored from {1}", mCount, file.QarFile), Debug.LogLevel.Debug); // Copy remaining files over base FPK foreach (string mFile in fpkFile) { string fDir = Path.GetDirectoryName(mFile); if (!Directory.Exists(Path.Combine("_gamefpk", fDir))) { Directory.CreateDirectory(Path.Combine("_gamefpk", fDir)); } Debug.LogLine(String.Format("[Mod] Merging existing file: {0}", mFile)); File.Copy(Path.Combine("_modfpk", mFile), Path.Combine(Path.Combine("_gamefpk", mFile)), true); if (!gameFpk.Contains(mFile)) { gameFpk.Add(mFile); } } // Rebuild FPK GzsLib.WriteFpkArchive(Path.Combine("_working", Tools.ToWinPath(fpk)), "_gamefpk", gameFpk); Directory.Delete("_gamefpk", true); Directory.Delete("_modfpk", true); } } } SettingsManager.SetGameData(gameData); // Remove all mod files from 01.dat foreach (ModQarEntry qarEntry in mod.ModQarEntries) { string fExt = Path.GetExtension(qarEntry.FilePath); if (!fExt.Contains(".fpk")) { zeroFiles.RemoveAll(file => Tools.CompareHashes(file, qarEntry.FilePath)); } } Debug.LogLine("[Mod] Rebuilding game archive", Debug.LogLevel.Basic); // Rebuild 01.dat GzsLib.WriteQarArchive(ZeroPath, "_working", zeroFiles, 3150048); SettingsManager.UpdateDatHash(); SettingsManager.RemoveMod(mod); CleanupDatabase(); CleanupFolders(); Debug.LogLine("[Mod] Uninstall complete", Debug.LogLevel.Basic); return(true); }
/// <summary> /// overwrites existing mods with the set of mods stored in the .MGSVPreset file /// </summary> public static bool LoadPreset(string presetFilePath) { bool panicMode = (!File.Exists(ZeroPath) || !File.Exists(OnePath) || !File.Exists(SnakeBiteSettings)); bool success = false; ModManager.CleanupFolders(); SettingsManager manager = new SettingsManager(SnakeBiteSettings); List <string> existingExternalFiles = new List <string>(); List <string> fileEntryDirs = new List <string>(); try { existingExternalFiles = manager.GetModExternalFiles(); } catch { panicMode = true; } try { if (!panicMode) { Debug.LogLine("[LoadPreset] Storing backups of existing files...", Debug.LogLevel.Basic); foreach (string gameFile in existingExternalFiles) { string gameFilePath = Path.Combine(GameDir, Tools.ToWinPath(gameFile)); if (File.Exists(gameFilePath)) // only stores backups of managed files { Debug.LogLine(string.Format("[LoadPreset] Storing backup: {0}", gameFile), Debug.LogLevel.Basic); fileEntryDirs.Add(Path.GetDirectoryName(gameFilePath)); if (File.Exists(gameFilePath + build_ext)) { File.Delete(gameFilePath + build_ext); } File.Move(gameFilePath, gameFilePath + build_ext); } } Debug.LogLine("[LoadPreset] Storing backup: 00.dat", Debug.LogLevel.Basic); File.Copy(ZeroPath, ZeroPath + build_ext, true); Debug.LogLine("[LoadPreset] Storing backup: 01.dat", Debug.LogLevel.Basic); File.Copy(OnePath, OnePath + build_ext, true); Debug.LogLine("[LoadPreset] Storing backup: snakebite.xml", Debug.LogLevel.Basic); File.Copy(SnakeBiteSettings, SnakeBiteSettings + build_ext, true); } else { Debug.LogLine("[LoadPreset] Critical file(s) are disfunctional or not found, skipping backup procedure", Debug.LogLevel.Basic); } Debug.LogLine("[LoadPreset] Importing preset files", Debug.LogLevel.Basic); FastZip unzipper = new FastZip(); unzipper.ExtractZip(presetFilePath, GameDir, "(.*?)"); Debug.LogLine("[LoadPreset] Import Complete", Debug.LogLevel.Basic); success = true; } catch (Exception e) { MessageBox.Show("An error has occurred and the preset was not imported.\nException: " + e); if (!panicMode) { Debug.LogLine("[LoadPreset] Restoring backup files", Debug.LogLevel.Basic); File.Copy(ZeroPath + build_ext, ZeroPath, true); File.Copy(OnePath + build_ext, OnePath, true); File.Copy(SnakeBiteSettings + build_ext, SnakeBiteSettings, true); foreach (string gameFile in existingExternalFiles) { string gameFilePath = Path.Combine(GameDir, Tools.ToWinPath(gameFile)); if (File.Exists(gameFilePath + build_ext)) { File.Copy(gameFilePath + build_ext, gameFilePath, true); } } } } finally { if (!panicMode) { Debug.LogLine("[LoadPreset] Removing backup files", Debug.LogLevel.Basic); foreach (string gameFile in existingExternalFiles) { string gameFilePath = Path.Combine(GameDir, Tools.ToWinPath(gameFile)); if (File.Exists(gameFilePath)) { File.Delete(gameFilePath + build_ext); } } foreach (string fileEntryDir in fileEntryDirs) { if (Directory.Exists(fileEntryDir)) { try { if (Directory.GetFiles(fileEntryDir).Length == 0) { Debug.LogLine(String.Format("[SB_Build] deleting empty folder: {0}", fileEntryDir), Debug.LogLevel.All); Directory.Delete(fileEntryDir); } } catch { Debug.LogLine("[Uninstall] Could not delete: " + fileEntryDir); } } } File.Delete(ZeroPath + build_ext); File.Delete(OnePath + build_ext); File.Delete(SnakeBiteSettings + build_ext); } } return(success); }
public static bool InstallMod(string ModFile) { CleanupFolders(); Debug.LogLine(String.Format("[Mod] Installation started: {0}", ModFile), Debug.LogLevel.Basic); // Extract game archive var zeroFiles = GzsLib.ExtractArchive <QarFile>(ZeroPath, "_working"); // Extract mod data FastZip unzipper = new FastZip(); unzipper.ExtractZip(ModFile, "_extr", "(.*?)"); // Load mod metadata ModEntry metaData = new ModEntry("_extr\\metadata.xml"); // Build a list of FPKs contained in mod List <string> modFpks = new List <string>(); foreach (ModFpkEntry fpkEntry in metaData.ModFpkEntries) { if (!modFpks.Contains(fpkEntry.FpkFile)) { modFpks.Add(fpkEntry.FpkFile); } } List <string> mergeFpks = new List <string>(); List <ModQarEntry> MergeFiles = new List <ModQarEntry>(); Debug.LogLine("[Mod] Checking existing game data", Debug.LogLevel.Basic); // Check for FPKs in 00.dat foreach (string fpk in modFpks) { string datFile = zeroFiles.FirstOrDefault(file => Tools.CompareHashes(file, fpk)); if (datFile != null) { if (mergeFpks.Contains(Tools.ToQarPath(datFile))) { continue; } mergeFpks.Add(fpk); MergeFiles.Add(new ModQarEntry() { FilePath = fpk, SourceType = FileSource.Merged, SourceName = "00.dat" }); } } var gameData = GzsLib.ReadBaseData(); var oneFiles = gameData.FileList.FindAll(entry => entry.QarFile == "01.dat"); var baseFiles = gameData.FileList.FindAll(entry => entry.QarFile != "01.dat"); // Check for FPKs in 01.dat foreach (string fpk in modFpks) { GameFile file = oneFiles.FirstOrDefault(entry => entry.FileHash == Tools.NameToHash(fpk)); if (file != null) { if (mergeFpks.Contains(Tools.ToQarPath(file.FilePath))) { continue; } // Create destination directory string destDirectory = Path.Combine("_working", Path.GetDirectoryName(Tools.ToWinPath(file.FilePath))); if (!Directory.Exists(destDirectory)) { Directory.CreateDirectory(destDirectory); } // Extract file into dat directory var ex = GzsLib.ExtractFileByHash <QarFile>(Path.Combine(GameDir, "master\\0\\01.dat"), file.FileHash, Path.Combine("_working", Tools.ToWinPath(file.FilePath))); mergeFpks.Add(Tools.ToQarPath(file.FilePath)); MergeFiles.Add(new ModQarEntry() { FilePath = file.FilePath, SourceType = FileSource.Merged, SourceName = "01.dat" }); if (zeroFiles.FirstOrDefault(datFile => Tools.CompareHashes(datFile, file.FilePath)) == null) { zeroFiles.Add(Tools.ToWinPath(file.FilePath)); } } } // Check for FPKs in base data foreach (string fpk in modFpks) { GameFile file = baseFiles.FirstOrDefault(entry => entry.FileHash == Tools.NameToHash(fpk)); if (file != null) { if (mergeFpks.Contains(Tools.ToQarPath(file.FilePath))) { continue; } // Create destination directory string destDirectory = Path.Combine("_working", Path.GetDirectoryName(Tools.ToWinPath(file.FilePath))); if (!Directory.Exists(destDirectory)) { Directory.CreateDirectory(destDirectory); } // Extract file into dat directory var ex = GzsLib.ExtractFileByHash <QarFile>(Path.Combine(GameDir, "master\\" + file.QarFile), file.FileHash, Path.Combine("_working", Tools.ToWinPath(file.FilePath))); mergeFpks.Add(Tools.ToQarPath(file.FilePath)); MergeFiles.Add(new ModQarEntry() { FilePath = file.FilePath, SourceType = FileSource.Merged, SourceName = file.QarFile }); if (zeroFiles.FirstOrDefault(datFile => Tools.CompareHashes(datFile, file.FilePath)) == null) { zeroFiles.Add(Tools.ToWinPath(file.FilePath)); } } } Debug.LogLine(String.Format("[Mod] Merging {0} FPK files", MergeFiles.Count), Debug.LogLevel.Basic); var g = SettingsManager.GetGameData(); // Merge FPK files foreach (ModQarEntry gf in MergeFiles) { Debug.LogLine(String.Format("[Mod] Starting merge: {0} ({1})", gf.FilePath, gf.SourceName), Debug.LogLevel.Debug); // Extract game FPK string fpkDatPath = zeroFiles.FirstOrDefault(file => Tools.CompareHashes(file, gf.FilePath)); string fpkPath = Path.Combine("_working", Tools.ToWinPath(fpkDatPath)); var gameFpk = GzsLib.ExtractArchive <FpkFile>(fpkPath, "_gamefpk"); // Extract mod FPK var exFpk = GzsLib.ExtractArchive <FpkFile>(Path.Combine("_extr", Tools.ToWinPath(gf.FilePath)), "_modfpk"); // Add file to gamedata info var q = g.GameQarEntries.FirstOrDefault(entry => entry.FilePath == gf.FilePath); if (q == null) { g.GameQarEntries.Add(new ModQarEntry() { FilePath = Tools.ToQarPath(gf.FilePath), SourceType = gf.SourceType, SourceName = gf.SourceName, Hash = Tools.NameToHash(gf.FilePath) }); } foreach (string f in gameFpk) { var c = exFpk.FirstOrDefault(entry => Tools.CompareHashes(entry, f)); if (c == null) { if (g.GameFpkEntries.FirstOrDefault(entry => Tools.CompareHashes(entry.FpkFile, gf.FilePath) && Tools.CompareNames(entry.FilePath, f)) == null) { g.GameFpkEntries.Add(new ModFpkEntry() { FpkFile = Tools.ToQarPath(gf.FilePath), FilePath = f, SourceType = gf.SourceType, SourceName = gf.SourceName }); } } } // Merge contents foreach (string fileName in exFpk) { string fileDir = (Path.Combine("_gamefpk", Path.GetDirectoryName(fileName))); string sourceFile = Path.Combine("_modfpk", fileName); string destFile = Path.Combine("_gamefpk", fileName); Debug.LogLine(String.Format("[Mod] Copying file: {0}", fileName), Debug.LogLevel.All); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } File.Copy(sourceFile, destFile, true); if (!gameFpk.Contains(fileName)) { gameFpk.Add(Tools.ToQarPath(fileName)); } } // Rebuild game FPK GzsLib.WriteFpkArchive(fpkPath, "_gamefpk", gameFpk); if (!zeroFiles.Contains(Tools.ToWinPath(gf.FilePath))) { zeroFiles.Add(Tools.ToWinPath(gf.FilePath)); } Directory.Delete("_modfpk", true); Directory.Delete("_gamefpk", true); Debug.LogLine(String.Format("[Mod] Merge complete"), Debug.LogLevel.Debug); } SettingsManager.SetGameData(g); Debug.LogLine("[Mod] Copying remaining mod files", Debug.LogLevel.Basic); // Copy files for 01.dat, ignoring merged FPKs foreach (ModQarEntry modEntry in metaData.ModQarEntries) { if (!zeroFiles.Contains(Tools.ToWinPath(modEntry.FilePath))) { zeroFiles.Add(Tools.ToWinPath(modEntry.FilePath)); } if (modEntry.FilePath.Contains(".fpk")) { if (mergeFpks.Count(fpk => Tools.CompareHashes(fpk, modEntry.FilePath)) > 0) { continue; } } string sourceFile = Path.Combine("_extr", Tools.ToWinPath(modEntry.FilePath)); string destFile = Path.Combine("_working", Tools.ToWinPath(modEntry.FilePath)); string destDir = Path.GetDirectoryName(destFile); Debug.LogLine(String.Format("[Mod] Copying file: {0}", modEntry.FilePath), Debug.LogLevel.All); if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } File.Copy(sourceFile, destFile, true); } // Rebuild 01.dat Debug.LogLine("[Mod] Rebuilding game archive", Debug.LogLevel.Basic); GzsLib.WriteQarArchive(ZeroPath, "_working", zeroFiles, 3150048); SettingsManager.UpdateDatHash(); SettingsManager.AddMod(metaData); Debug.LogLine("[Mod] Running database cleanup", Debug.LogLevel.Debug); CleanupDatabase(); CleanupFolders(); Debug.LogLine("[Mod] Installation finished", Debug.LogLevel.Basic); return(true); }