Esempio n. 1
0
        /// <summary>
        /// The method that is called to start the backgound task.
        /// </summary>
        /// <param name="args">Arguments to for the task execution.</param>
        /// <returns>Always <c>null</c>.</returns>
        protected override object DoWork(object[] args)
        {
            OverallMessage          = "Backuping Nexus Mod Manager...";
            OverallProgress         = 0;
            OverallProgressStepSize = 1;
            ShowItemProgress        = true;
            OverallProgressMaximum  = 7;
            ItemProgressStepSize    = 1;
            FileCounter             = 0;

            OverallMessage = "Creating the directories.";
            StepOverallProgress();

            DriveInfo drive = new DriveInfo(EnvironmentInfo.TemporaryPath);

            drive = new DriveInfo(drive.Name);

            if (drive.AvailableFreeSpace > (TotalFileSize))
            {
                string BackupDirectory = Path.Combine(EnvironmentInfo.TemporaryPath, "NMMBACKUP");
                if (Directory.Exists(BackupDirectory))
                {
                    FileUtil.ForceDelete(BackupDirectory);
                }

                bool PathLimit = CheckPathLimit(BackupDirectory);
                if (PathLimit)
                {
                    string NewBackupDirectory = Path.Combine(Directory.GetDirectoryRoot(BackupDirectory), "NMMTemp");
                    string WarningMessage     = "Warning: NMM won't be able to use the default 'temp' folder for this operation, this will cause some file paths to reach the OS limit of 260 characters and prevent files to be copied." + Environment.NewLine + Environment.NewLine;
                    WarningMessage = WarningMessage + "Just for this backup NMM will use a " + NewBackupDirectory + " folder. This folder will be removed after the backup completes.";

                    MessageBox.Show(WarningMessage, "Create Backup", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    BackupDirectory = NewBackupDirectory;
                }

                string strPathToCreate = BackupDirectory;
                Directory.CreateDirectory(strPathToCreate);
                strPathToCreate = Path.Combine(BackupDirectory, Path.GetFileName(ModManager.GameMode.PluginDirectory));
                Directory.CreateDirectory(strPathToCreate);
                strPathToCreate = Path.Combine(BackupDirectory, "VIRTUAL INSTALL");
                Directory.CreateDirectory(strPathToCreate);
                strPathToCreate = Path.Combine(BackupDirectory, "PROFILE");
                Directory.CreateDirectory(strPathToCreate);

                int    i   = 1;
                string dir = string.Empty;

                OverallProgressMaximum = BackupManager.lstBaseGameFiles.Count();
                if ((BackupManager.checkList.Contains(0)) && (BackupManager.lstBaseGameFiles.Count > 0))
                {
                    i = 1;
                    foreach (BackupInfo bkInfo in BackupManager.lstBaseGameFiles)
                    {
                        if (i < BackupManager.lstBaseGameFiles.Count())
                        {
                            ItemMessage = bkInfo.VirtualModPath;
                            StepItemProgress();
                        }

                        OverallMessage = string.Format("Copying GAME BASE files...{0}/{1}", i++, BackupManager.lstBaseGameFiles.Count());
                        StepOverallProgress();
                        dir = Path.GetDirectoryName(Path.Combine(bkInfo.ModID, bkInfo.VirtualModPath));
                        if (!string.IsNullOrEmpty(dir))
                        {
                            Directory.CreateDirectory(Path.Combine(BackupDirectory, bkInfo.Directory, dir));
                        }

                        try
                        {
                            File.Copy(bkInfo.RealModPath, Path.Combine(BackupDirectory, bkInfo.Directory, bkInfo.VirtualModPath), true);
                        }
                        catch (FileNotFoundException)
                        { }
                    }

                    TotalFiles += BackupManager.lstBaseGameFiles.Count;
                }

                if ((BackupManager.checkList.Contains(1)) && (BackupManager.lstInstalledModFiles.Count > 0))
                {
                    OverallProgressMaximum = BackupManager.lstInstalledModFiles.Count();
                    foreach (BackupInfo bkInfo in BackupManager.lstInstalledModFiles)
                    {
                        OverallMessage = string.Format("Copying MODS INSTALLATION files...{0}/{1}", i++, BackupManager.lstInstalledModFiles.Count());
                        StepOverallProgress();
                        dir = Path.GetDirectoryName(Path.Combine(bkInfo.ModID, bkInfo.VirtualModPath));
                        if (!string.IsNullOrEmpty(dir))
                        {
                            Directory.CreateDirectory(Path.Combine(BackupDirectory, bkInfo.Directory, dir));
                        }

                        try
                        {
                            File.Copy(bkInfo.RealModPath, Path.Combine(BackupDirectory, bkInfo.Directory, bkInfo.ModID, bkInfo.VirtualModPath), true);
                        }
                        catch (FileNotFoundException)
                        { }

                        if (ItemProgress < ItemProgressMaximum)
                        {
                            ItemMessage = bkInfo.RealModPath;
                            StepItemProgress();
                        }
                    }

                    OverallProgressMaximum = BackupManager.lstInstalledNMMLINKFiles.Count();
                    foreach (BackupInfo bkInfo in BackupManager.lstInstalledNMMLINKFiles)
                    {
                        OverallMessage = string.Format("Copying NMMLINK files...{0}/{1}", i++, BackupManager.lstInstalledNMMLINKFiles.Count());
                        StepOverallProgress();
                        dir = Path.GetDirectoryName(Path.Combine("NMMLINK", bkInfo.VirtualModPath));
                        if (!string.IsNullOrEmpty(dir))
                        {
                            Directory.CreateDirectory(Path.Combine(BackupDirectory, dir));
                        }

                        try
                        {
                            File.Copy(bkInfo.RealModPath, Path.Combine(BackupDirectory, bkInfo.Directory, bkInfo.ModID, bkInfo.VirtualModPath), true);
                        }
                        catch (FileNotFoundException)
                        { }

                        if (ItemProgress < ItemProgressMaximum)
                        {
                            ItemMessage = bkInfo.RealModPath;
                            StepItemProgress();
                        }
                    }

                    TotalFiles += BackupManager.lstInstalledModFiles.Count + BackupManager.lstInstalledNMMLINKFiles.Count;
                }

                OverallProgressMaximum = BackupManager.lstLooseFiles.Count();
                if ((BackupManager.checkList.Contains(2)) && (BackupManager.lstLooseFiles.Count > 0))
                {
                    i = 1;
                    foreach (BackupInfo bkInfo in BackupManager.lstLooseFiles)
                    {
                        if (i < BackupManager.lstLooseFiles.Count())
                        {
                            ItemMessage = bkInfo.VirtualModPath;
                            StepItemProgress();
                        }

                        OverallMessage = string.Format("Copying " + Path.GetFileName(ModManager.GameMode.PluginDirectory) + " files...{0}/{1}", i++, BackupManager.lstLooseFiles.Count());
                        StepOverallProgress();
                        dir = Path.GetDirectoryName(Path.Combine(bkInfo.ModID, bkInfo.VirtualModPath));
                        if (!string.IsNullOrEmpty(dir))
                        {
                            Directory.CreateDirectory(Path.Combine(BackupDirectory, bkInfo.Directory, dir));
                        }

                        try
                        {
                            File.Copy(bkInfo.RealModPath, Path.Combine(BackupDirectory, bkInfo.Directory, bkInfo.VirtualModPath), true);
                        }
                        catch (FileNotFoundException)
                        { }
                    }

                    TotalFiles += BackupManager.lstLooseFiles.Count;
                }

                OverallProgressMaximum = BackupManager.lstModArchives.Count();
                if ((BackupManager.checkList.Contains(3)) && (BackupManager.lstModArchives.Count > 0))
                {
                    i = 1;
                    foreach (BackupInfo bkInfo in BackupManager.lstModArchives)
                    {
                        if (i < BackupManager.lstModArchives.Count())
                        {
                            ItemMessage = bkInfo.VirtualModPath;
                            StepItemProgress();
                        }

                        OverallMessage = string.Format("Copying MOD ARCHIVES...{0}/{1}", i++, BackupManager.lstModArchives.Count());
                        StepOverallProgress();
                        dir = Path.GetDirectoryName(Path.Combine(bkInfo.Directory, bkInfo.VirtualModPath));
                        if (!string.IsNullOrEmpty(dir))
                        {
                            Directory.CreateDirectory(Path.Combine(BackupDirectory, dir));
                        }

                        try
                        {
                            File.Copy(bkInfo.RealModPath, Path.Combine(BackupDirectory, bkInfo.Directory, bkInfo.VirtualModPath), true);
                        }
                        catch (FileNotFoundException)
                        { }
                    }

                    TotalFiles += BackupManager.lstModArchives.Count;
                }

                byte[] bteLoadOrder = null;
                if (ModManager.GameMode.UsesPlugins)
                {
                    bteLoadOrder = PluginManagerVM.ExportLoadOrder();
                }

                string[] strOptionalFiles = null;

                if (ModManager.GameMode.RequiresOptionalFilesCheckOnProfileSwitch)
                {
                    if ((PluginManager != null) && ((PluginManager.ActivePlugins != null) && (PluginManager.ActivePlugins.Count > 0)))
                    {
                        strOptionalFiles = ModManager.GameMode.GetOptionalFilesList(PluginManager.ActivePlugins.Select(x => x.Filename).ToArray());
                    }
                }

                IModProfile mprModProfile = AddProfile(null, null, bteLoadOrder, ModManager.GameMode.ModeId, -1, strOptionalFiles, Path.Combine(BackupDirectory, "PROFILE"));

                string gameModeName = PurgeIllegalWindowsCharacters(ModManager.GameMode.Name);

                Directory.CreateDirectory(Path.Combine(BackupDirectory, gameModeName));

                string installLog = Path.Combine(ModManager.GameMode.GameModeEnvironmentInfo.InstallInfoDirectory, "InstallLog.xml");

                if (File.Exists(installLog))
                {
                    File.Copy(installLog, Path.Combine(BackupDirectory, "InstallLog.xml"));
                }

                string startPath = BackupDirectory;
                string zipPath   = Path.Combine(EnvironmentInfo.ApplicationPersonalDataFolderPath, "NMM_BACKUP.zip");

                if (File.Exists(zipPath))
                {
                    File.Delete(zipPath);
                }

                OverallMessage = "Zipping the Archive...";
                StepOverallProgress();

                string strDateTimeStamp = DateTime.Now.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.SortableDateTimePattern);
                strDateTimeStamp = strDateTimeStamp.Replace(":", "");
                strDateTimeStamp = strDateTimeStamp.Replace("-", "");
                strDateTimeStamp = strDateTimeStamp.Replace("T", "-");

                SevenZipCompressor szcCompressor = new SevenZipCompressor(startPath);
                szcCompressor.CompressionLevel        = SevenZip.CompressionLevel.Normal;
                szcCompressor.ArchiveFormat           = OutArchiveFormat.Zip;
                szcCompressor.FastCompression         = false;
                szcCompressor.CompressionMethod       = CompressionMethod.Default;
                szcCompressor.CompressionMode         = SevenZip.CompressionMode.Create;
                szcCompressor.FileCompressionStarted += new EventHandler <FileNameEventArgs>(compressor_FileCompressionStarted);

                szcCompressor.CompressDirectory(startPath, Path.Combine(SelectedPath, ModManager.GameMode.ModeId + "_NMM_BACKUP_" + strDateTimeStamp + ".zip"));

                OverallMessage = "Deleting the leftovers.";
                StepOverallProgress();
                FileUtil.ForceDelete(BackupDirectory);
            }
            else
            {
                return(string.Format("Not enough space on drive: {0} - ({1}Mb required)", drive.Name, ((TotalFileSize / 1024) / 1024).ToString()));
            }

            return(null);
        }