Example #1
0
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            FileManager HellgateFileManager;
            string cookingMessage = "Cooking {0}...";
            string path = RevivalMod.DataPath;
            string[] excelToCook = null;
            string[] stringsToCook = null;
            string[] xmlToCook = null;
            string[] filesToPack = null;

            // Step One: Initialize the FileManager
            toolStripStatusLabel.Text = "Initializing the Hellgate File Manager...";
            HellgateFileManager = new FileManager(Config.HglDir);

            if (HellgateFileManager.HasIntegrity == false)
            {
                Console.WriteLine("Could not initialize the File Manager, Integrity check failed.");
                e.Cancel = true;

                string caption = "Integrity Error";
                string message = "Could not initialize the File Manager. Check Hellgate London is not running and that the installation is not corrupt.";
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            // Check if the user wants to cancel.
            if (backgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            // Step Two: Revert modifications automatically if flagged in the mod install xml.
            if (RevivalMod.Data.Modifications.DoRevert == true)
            {
                DialogResult dialogResult = DialogResult.Retry;
                while (dialogResult == DialogResult.Retry)
                {
                    toolStripStatusLabel.Text = "Reverting previous Hellgate London modifications...";
                    bool error = Modification.RemoveModifications(HellgateFileManager);
                    if (error == true)
                    {
                        string caption = "Reversion Error";
                        string message = "One or more errors occurred while reverting Hellgate London modifications. Check Hellgate London is not running and the files are not in use. It is highly recommended you ammend this before continuing, even if it means reinstalling the game.";
                        dialogResult = MessageBox.Show(message, caption, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);

                        if (dialogResult == DialogResult.Abort)
                        {
                            e.Cancel = true;
                            return;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                toolStripStatusLabel.Text = "Reloading the Hellgate File Manager...";
                HellgateFileManager.Reload();
            }

            // Check if the user wants to cancel.
            if (backgroundWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            // Load the Excel tables
            toolStripStatusLabel.Text = "Caching the Hellgate London excel tables...";
            HellgateFileManager.LoadTableFiles();

            // Step Three: Cook Excel, String and XML files.

            // Search for files to cook
            excelToCook = Hellpack.SearchForExcelFiles(path);
            stringsToCook = Hellpack.SearchForStringFiles(path);
            xmlToCook = Hellpack.SearchForXmlFiles(path);

            if (excelToCook != null)
            {
                for (int i = 0; i < excelToCook.Length; i++)
                {
                    // Check if the user wants to cancel.
                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    string epath = excelToCook[i];
                    string hglPath = epath.Substring(epath.IndexOf("data"), epath.Length - epath.IndexOf("data"));
                    string report = String.Format(cookingMessage, hglPath);
                    toolStripStatusLabel.Text = report;
                    Hellpack.CookExcelFile(epath);
                }
            }

            if (stringsToCook != null)
            {
                for (int i = 0; i < stringsToCook.Length; i++)
                {
                    // Check if the user wants to cancel.
                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    string epath = stringsToCook[i];
                    string hglPath = epath.Substring(epath.IndexOf("data"), epath.Length - epath.IndexOf("data"));
                    string report = String.Format(cookingMessage, hglPath);
                    toolStripStatusLabel.Text = report;
                    Hellpack.CookStringFile(epath);
                }
            }

            if (xmlToCook != null)
            {
                for (int i = 0; i < xmlToCook.Length; i++)
                {
                    // Check if the user wants to cancel.
                    if (backgroundWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }

                    string epath = xmlToCook[i];
                    string hglPath = epath.Substring(epath.IndexOf("data"), epath.Length - epath.IndexOf("data"));
                    string report = String.Format(cookingMessage, hglPath);
                    toolStripStatusLabel.Text = report;
                    Hellpack.CookXmlFile(epath, HellgateFileManager);
                }
            }

            // Step Four: Pack the base idx/dat.
            filesToPack = Hellpack.SearchForFilesToPack(path, true);
            toolStripStatusLabel.Text = String.Format("Packing {0}...", RevivalMod.Data.Modifications.ID + ".idx");
            string hglDataPath = Path.Combine(Config.HglDir, "data");
            string modDatPath = Path.Combine(hglDataPath, RevivalMod.Data.Modifications.ID) + ".idx";
            bool packResult = Hellpack.PackDatFile(filesToPack, modDatPath, true);

            if (packResult == false)
            {
                HellgateFileManager.Dispose();

                string caption = "Fatal Error";
                string message = "An error occurred while packing the modification.";
                MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Step Five: Apply scripts. Scripts are only applied if they are checked or are hidden.
            if (optionalCheckedListBox.CheckedItems.Count != 0 ||
                RevivalMod.Data.Modifications.Scripts.Where(s => s.Type == "hidden").Any() == true)
            {
                List<Script> scriptList = new List<Script>();

                // Append optional list
                foreach (Script script in optionalCheckedListBox.CheckedItems)
                {
                    scriptList.Add(script);
                }

                // Append hidden list
                foreach (Script script in RevivalMod.Data.Modifications.Scripts.Where(s => s.Type == "hidden"))
                {
                    scriptList.Add(script);
                }

                // Reinitalize the File Manager
                HellgateFileManager.Reload();
                HellgateFileManager.LoadTableFiles();

                string indexPath = Path.Combine(Config.HglDir, "data", RevivalMod.Data.Modifications.ID + "_125.idx");
                IndexFile indexFile = new IndexFile(indexPath);

                // Apply the scripts
                List<string> modifiedTables = new List<string>();
                foreach (Script script in scriptList)
                {
                    // Interface stuff
                    toolStripStatusLabel.Text =  String.Format("Applying {0} script...", script.Title);
                    if (script.Extraction != null)
                    {
                        toolStripStatusLabel.Text = "MP conversion in progress, this may between 5 and 10 minutes.";
                    }

                    // The script
                    Modification.ApplyScript(script, ref HellgateFileManager);
                    if (script.Tables != null)
                    {
                        foreach (Script.Table table in script.Tables)
                        {
                            string tableID = table.ID.ToUpper();
                            if (modifiedTables.Contains(tableID) == false)
                                modifiedTables.Add(tableID);
                        }
                    }

                    // Copy files if any
                    if (String.IsNullOrEmpty(script.ID) == false)
                    {
                        string optionalPath = Path.Combine(path, "optional", script.ID);
                        if (Directory.Exists(optionalPath) == false) continue;

                        string[] fileList = Directory.GetFiles(optionalPath, "*", SearchOption.AllDirectories);
                        if (fileList == null) continue;
                        if (fileList.Length == 0) continue;

                        foreach (string filePath in fileList)
                        {
                            string relativePath = filePath.Replace(optionalPath + "\\", "");
                            string directory = Path.GetDirectoryName(relativePath) + "\\";
                            string fileName = Path.GetFileName(relativePath);
                            byte[] fbuffer = null;
                            try
                            {
                                fbuffer = File.ReadAllBytes(filePath);
                            }
                            catch (Exception ex)
                            {
                                ExceptionLogger.LogException(ex);
                                continue;
                            }
                            indexFile.AddFile(directory, fileName, fbuffer, DateTime.Now);
                        }
                    }
                }

                // Repack the modified Tables.
                // These go in their own idx/dat under the same name as the base modification with the string _125 appended.
                foreach (string tableID in modifiedTables)
                {
                    DataFile dataTable = HellgateFileManager.DataFiles[tableID];
                    byte[] ebuffer = dataTable.ToByteArray();
                    string fileName = Path.GetFileName(dataTable.FilePath);
                    string fileDir = Path.GetDirectoryName(dataTable.FilePath) + "\\";
                    indexFile.AddFile(fileDir, fileName, ebuffer, DateTime.Now);
                }

                if (indexFile.Count > 0)
                {
                    // Write the index
                    byte[] ibuffer = indexFile.ToByteArray();
                    Crypt.Encrypt(ibuffer);
                    try
                    {
                        File.WriteAllBytes(indexPath, ibuffer);
                        HellgateFileManager.Dispose();
                        indexFile.EndDatAccess();
                        indexFile.Dispose();
                    }
                    catch(Exception ex)
                    {
                        ExceptionLogger.LogException(ex);
                        HellgateFileManager.Dispose();
                        indexFile.EndDatAccess();
                        indexFile.Dispose();

                        string caption = "Fatal Error";
                        string message = "An error occurred while packing the optional modification.";
                        MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }

            string msgCaption = "Success";
            string msgDescription = "Modification successfully installed!";
            MessageBox.Show(msgDescription, msgCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }