Ejemplo n.º 1
0
        // 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
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks 00.dat files, indcluding fpk contents and adds the different mod entry types (if missing) to database (snakebite.xml)
        /// Slows down as number of fpks increase
        /// </summary>
        public static void CleanupDatabase()
        {
            Debug.LogLine("[Cleanup] Database cleanup started", Debug.LogLevel.Basic);

            // Retrieve installation data
            SettingsManager manager = new SettingsManager(SnakeBiteSettings);
            var mods = manager.GetInstalledMods();
            var game = manager.GetGameData();
            var zeroFiles = GzsLib.ListArchiveContents<QarFile>(ZeroPath);

            //Should only happen if user manually mods 00.dat
            Debug.LogLine("[Cleanup] Removing duplicate file entries", Debug.LogLevel.Debug);
            // Remove duplicate file entries
            var cleanFiles = zeroFiles.ToList();
            foreach (string file in zeroFiles)
            {
                while (cleanFiles.Count(entry => entry == file) > 1)
                {
                    cleanFiles.Remove(file);
                    Debug.LogLine(String.Format("[Cleanup] Found duplicate file in 00.dat: {0}", file), Debug.LogLevel.Debug);
                }
            }

            Debug.LogLine("[Cleanup] Examining FPK archives", Debug.LogLevel.Debug);
            var GameFpks = game.GameFpkEntries.ToList();
            // Search for FPKs in game data
            var fpkFiles = cleanFiles.FindAll(entry => entry.EndsWith(".fpk") || entry.EndsWith(".fpkd"));
            foreach (string fpkFile in fpkFiles)
            {
                string fpkName = Path.GetFileName(fpkFile);
                // Extract FPK from archive
                Debug.LogLine(String.Format("[Cleanup] Examining {0}", fpkName));
                GzsLib.ExtractFile<QarFile>(ZeroPath, fpkFile, fpkName);

                // Read FPK contents
                var fpkContent = GzsLib.ListArchiveContents<FpkFile>(fpkName);

                // Add contents to game FPK list
                foreach (var c in fpkContent)
                {
                    if (GameFpks.Count(entry => Tools.CompareNames(entry.FilePath, c) && Tools.CompareHashes(entry.FpkFile, fpkFile)) == 0)
                    {
                        GameFpks.Add(new ModFpkEntry() { FpkFile = fpkFile, FilePath = c });
                    }
                }
                try
                {
                    File.Delete(fpkName);
                } catch
                {
                    Console.WriteLine("[Uninstall] Could not delete: " + fpkName);
                }
            }

            Debug.LogLine("[Cleanup] Checking installed mods", Debug.LogLevel.Debug);
            Debug.LogLine("[Cleanup] Removing all installed mod data from game data list", Debug.LogLevel.Debug);
            foreach (var mod in mods)
            {
                foreach (var qarEntry in mod.ModQarEntries)
                {
                    cleanFiles.RemoveAll(file => Tools.CompareHashes(file, qarEntry.FilePath));
                }

                foreach (var fpkEntry in mod.ModFpkEntries)
                {
                    GameFpks.RemoveAll(fpk => Tools.CompareHashes(fpk.FpkFile, fpkEntry.FpkFile) && Tools.ToQarPath(fpk.FilePath) == Tools.ToQarPath(fpkEntry.FilePath));
                }
            }

            Debug.LogLine("[Cleanup] Checking mod QAR files against game files", Debug.LogLevel.Debug);
            foreach (var s in cleanFiles)
            {
                if (game.GameQarEntries.Count(entry => Tools.CompareHashes(entry.FilePath, s)) == 0)
                {
                    Debug.LogLine($"[Cleanup] Adding missing {s}", Debug.LogLevel.Debug);
                    game.GameQarEntries.Add(new ModQarEntry() {
                        FilePath = Tools.ToQarPath(s),
                        SourceType = FileSource.System,
                        Hash = Tools.NameToHash(s)
                    });
                }
            }

            game.GameFpkEntries = GameFpks;
            manager.SetGameData(game);
        }
Ejemplo n.º 3
0
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            SettingsManager manager = new SettingsManager(ModManager.GameDir);

            manager.DisableConflictCheck = false;
            if (Properties.Settings.Default.LastSBVersion == null || new Version(Properties.Settings.Default.LastSBVersion) < ModManager.GetSBVersion())
            {
                Properties.Settings.Default.Upgrade();
            }

            Properties.Settings.Default.LastSBVersion = ModManager.GetSBVersion().ToString();
            Properties.Settings.Default.Save();

            Debug.Clear();

            Debug.LogLine(String.Format(
                              "SnakeBite {0}\n" +
                              "{1}\n" +
                              "-------------------------",
                              ModManager.GetSBVersion(),
                              Environment.OSVersion.VersionString));

            // Delete old settings file
            if (File.Exists(ModManager.GameDir + "\\sbmods.xml"))
            {
                Debug.LogLine("Settings v0.7 or less detected, removing");
                File.Delete(ModManager.GameDir + "\\sbmods.xml");
                MessageBox.Show("Due to fundamental changes from version 0.8 onwards, your settings have been reset. Please re-verify or restore the game files and run the setup wizard before continuing.", "Version Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            bool showSetupWizard = false;

            if (!manager.SettingsExist() || !manager.ValidInstallPath)
            {
                showSetupWizard = true;
            }

            // Show wizard on first run, if folder is invalid or settings out of date
            while (showSetupWizard)
            {
                // show setup wizard
                Debug.LogLine("Showing setup wizard");
                SetupWizard.SetupWizard setupWizard = new SetupWizard.SetupWizard();
                var wizResult = setupWizard.ShowDialog();
                if (wizResult == DialogResult.Cancel)
                {
                    return;
                }
                if (wizResult == DialogResult.OK)
                {
                    showSetupWizard = false;
                }
            }


            string InitLog = String.Format(
                "MGS Install Folder: {0}\n" +
                "MGS Version: {1}\n" +
                "-------------------------",
                Properties.Settings.Default.InstallPath,
                ModManager.GetMGSVersion());

            Debug.LogLine(InitLog, Debug.LogLevel.Basic);



            // Process Command Line args
            // TODO: test all command line args

            // Uninstall SnakeBite
            if (args.Length == 1)
            {
                if (args[0] == "-completeuninstall")
                {
                    Debug.LogLine("Complete uninstall");
                    // Restore backup and remove settings
                    manager.DeleteSettings();
                    BackupManager.RestoreOriginals();
                    return;
                }
            }

            // Parse command line arguments
            bool   doCmdLine    = false;        // Process command line args?
            bool   closeApp     = false;        // Close app after?
            bool   install      = false;        // Install = true, uninstall = false
            bool   resetDatHash = false;        // Rehash dat file
            bool   skipCleanup  = true;         // Skip CleanupDatabase
            string installFile  = String.Empty;

            if (args.Length > 0)
            {
                foreach (string arg in args)
                {
                    switch (arg.ToLower())
                    {
                    case "-i":
                        install = true;
                        break;

                    case "-u":
                        install = false;
                        break;

                    case "-d":
                        resetDatHash = true;
                        break;

                    case "-x":
                        closeApp = true;
                        break;

                    case "-s":
                        skipCleanup = true;
                        break;

                    default:
                        installFile = arg;
                        doCmdLine   = true;
                        break;
                    }
                }
            }

            // Update dat hash in settings
            if (resetDatHash)
            {
                Debug.LogLine("Resetting dat hash");
                manager.UpdateDatHash();
            }
            if (ModManager.GetMGSVersion() > new Version(1, 0, 13, 0))
            {
                var contSB = MessageBox.Show("Due to a recent game update, this version of SnakeBite is outdated, and some features will not function properly.\n\nIt is highly recommended that you do not continue, and update to the latest version of Snakebite when it becomes available.\n\nWould you still like to continue? ", "Game Version Update", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (contSB == DialogResult.No)
                {
                    return;
                }
            }
            var checkDat = manager.ValidateDatHash();

            if (!checkDat || manager.IsVanilla0001DatHash())
            {
                if (manager.IsVanilla0001DatHash())
                {
                    MessageBox.Show("Fresh 00.dat/01.dat detected. The setup wizard will now run.", "Game data hash mismatch", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Game archive has been modified. The setup wizard will now run.", "Game data hash mismatch", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                SetupWizard.SetupWizard setupWizard = new SetupWizard.SetupWizard();
                setupWizard.ShowDialog();
            }
            else if (!BackupManager.c7t7Exist()) // chunk7 and/or texture7 are missing, despite the dathash validating.
            {
                MessageBox.Show("To continue, SnakeBite must build a_chunk7.dat and a_texture7.dat from your current archives. The setup wizard will now run.", "Setup required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SetupWizard.SetupWizard setupWizard = new SetupWizard.SetupWizard();
                setupWizard.ShowDialog();
            }

            if (doCmdLine)
            {
                Debug.LogLine("Doing cmd line args");
                formMods ModForm = new formMods();
                ModForm.Show();
                ModForm.Hide();
                if (install)
                {
                    // install
                    ModForm.ProcessInstallMod(installFile, skipCleanup); // install mod
                }
                else
                {
                    // uninstall
                    var      mods = manager.GetInstalledMods();
                    ModEntry mod  = mods.FirstOrDefault(entry => entry.Name == installFile); // select mod

                    if (mod != null)
                    {
                        ModForm.ProcessUninstallMod(mod); // uninstall mod
                    }
                }
                ModForm.Dispose();

                if (closeApp)
                {
                    return;
                }
            }

            //Application.Run(new formMain());
            Application.Run(new formLauncher());
        }