示例#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);
        }
示例#2
0
        public static void RepackMPDats()
        {
            //String filePath1 = Config.HglDataDir + @"\data\hellgate000.idx";
            //IndexFile spHellgate4256 = new IndexFile(File.ReadAllBytes(filePath1));
            //spHellgate4256.FilePath = filePath1;

            String filePath1 = Config.HglDataDir + @"\data\mp_hellgate_1.10.180.3416_1.0.86.4580.idx";
            String filePath2 = Config.HglDataDir + @"\data\mp_hellgate_localized_1.10.180.3416_1.0.86.4580.idx";

            IndexFile mpHellgate4580 = new IndexFile(filePath1, File.ReadAllBytes(filePath1));
            IndexFile mpHellgateLocal4580 = new IndexFile(filePath2, File.ReadAllBytes(filePath2));

            IndexFile[] indexFiles = new[] { mpHellgate4580, mpHellgateLocal4580 };
            //IndexFile[] indexFiles = new[] { spHellgate4256 };

            foreach (IndexFile indexFile in indexFiles)
            {
                String filePath = indexFile.FilePath.Replace("mp_hellgate", "sp_hellgate");
                IndexFile newIndexFile = new IndexFile(filePath);
                newIndexFile.BeginDatWriting();

                indexFile.BeginDatReading();
                foreach (PackFileEntry fileEntry in indexFile.Files)
                {
                    byte[] fileBytes = indexFile.GetFileBytes(fileEntry);

                    newIndexFile.AddFile(fileEntry.Directory, fileEntry.Name, fileBytes);
                }
                indexFile.EndDatAccess();

                byte[] indexFileBytes = newIndexFile.ToByteArray();
                Crypt.Encrypt(indexFileBytes);
                File.WriteAllBytes(newIndexFile.FilePath, indexFileBytes);
                newIndexFile.EndDatAccess();
            }
        }
示例#3
0
        /// <summary>
        /// Generates of a list of all the files inside the .idx .dat files from the Hellgate path.
        /// </summary>
        /// <returns>Result of the initialization. Occurance of an error will return false.</returns>
        private bool _LoadFileTable()
        {
            if (!Directory.Exists(HellgateDataPath))
            {
                Console.WriteLine(@"Critical Error: HellgateDataPath data\ does not exist!");
                return false;
            }

            List<String> idxPaths = new List<String>();
            string[] query = IsVersionTestCenter ? Common.MPFiles : Common.SPFiles;
            foreach (String fileQuery in query)
            {
                idxPaths.AddRange(Directory.GetFiles(HellgateDataPath, fileQuery).Where(p => p.EndsWith(IndexFile.Extension) || p.EndsWith(HellgatePackFile.Extension)));
            }
            if (idxPaths.Count == 0)
            {
                Console.WriteLine("Error: No index files found at path: " + HellgateDataPath);
                return false;
            }

            foreach (String idxPath in idxPaths)
            {
                HellgateFile hellgateFile;
                String datFullPath;
                if (idxPath.EndsWith(IndexFile.Extension))
                {
                    hellgateFile = new IndexFile(idxPath);
                    datFullPath = idxPath.Replace(IndexFile.Extension, ((IndexFile)hellgateFile).DatExtension);
                }
                else
                {
                    hellgateFile = new HellgatePackFile(idxPath);
                    datFullPath = idxPath.Replace(HellgatePackFile.Extension, ((HellgatePackFile)hellgateFile).DatExtension);
                }

                // if there is no accompanying .dat at all, then ignore .idx
                if (!File.Exists(datFullPath)) continue;

                // read in and parse index
                Debug.Write(String.Format("Loading pack file: {0}... ", Path.GetFileName(idxPath)));
                PackFile packFile = (PackFile)hellgateFile;
                try
                {
                    byte[] fileBytes = File.ReadAllBytes(idxPath);
                    hellgateFile.ParseFileBytes(fileBytes);

            #if DEBUG
                    IndexFile indexFile = hellgateFile as IndexFile;
                    if (indexFile != null) Debug.WriteLine("{0} files loaded.", indexFile.Files.Count);
                    HellgatePackFile hgPackFile = hellgateFile as HellgatePackFile;
                    if (hgPackFile != null) Debug.WriteLine("{0} files loaded.", hgPackFile.Files.Count);
            #endif
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Warning: Failed to read in index file: " + idxPath);
                    Debug.WriteLine(ex);
                    continue;
                }

                if (packFile.Count == 0) continue;
                IndexFiles.Add(packFile);
                _LoadIndexFile(packFile);
            }

            return FileEntries.Count != 0;
        }
示例#4
0
        /// <summary>
        /// Opens a TableForm based on the path to a Index or StringsFile.
        /// </summary>
        /// <param name="filePath">Path to the Index or StringsFile.</param>
        private void _OpenIndexFile(String filePath)
        {
            TableForm tableForm;
            PackFile packFile;
            if (filePath.EndsWith(IndexFile.Extension))
            {
                packFile = new IndexFile(filePath);
            }
            else
            {
                packFile = new HellgatePackFile(filePath);
            }

            // Check if the form is already open.
            // If true, then activate the form.
            bool isOpen = _openTableForms.Where(tf => tf.FilePath == filePath).Any();
            if (isOpen)
            {
                tableForm = _openTableForms.Where(tf => tf.FilePath == filePath).First();
                if (tableForm.Created)
                {
                    tableForm.Select();
                    return;
                }
            }

            // Try read the file.
            // If an exception is caught, log the error and inform the user.
            byte[] buffer;
            try
            {
                buffer = File.ReadAllBytes(filePath);
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex, false);
                return;
            }

            // parse file
            try
            {
                packFile.ParseFileBytes(buffer);
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex, false);
                return;
            }

            tableForm = new TableForm(packFile)
            {
                MdiParent = this,
                Text = Text + ": " + packFile.Path
            };
            if (!_openTableForms.Contains(tableForm)) _openTableForms.Add(tableForm);
            tableForm.Show();
        }
示例#5
0
        public static bool PackDatFile(IEnumerable<String> filesToPack, String outputPath, bool forceCreateNewIndex)
        {
            IndexFile indexFile = null;
            bool isAppend = false;
            if (!forceCreateNewIndex && File.Exists(outputPath))
            {
                Console.Write("Hellpack has detected an existing index. Append to previous index? [Y/N]: ");
                char ans = (char)Console.Read();
                if (ans == 'y' || ans == 'Y')
                {
                    indexFile = new IndexFile(outputPath, File.ReadAllBytes(outputPath));
                    isAppend = true;
                }
            }

            if (indexFile == null) indexFile = new IndexFile(outputPath);

            foreach (String filePath in filesToPack)
            {
                DateTime lastModified = File.GetLastWriteTime(filePath);

                // if we're appending, check if we've already added this file by checking the modified time
                if (isAppend)
                {
                    PackFileEntry fileEntry = indexFile.GetFileEntry(filePath);
                    if (fileEntry != null && fileEntry.LastModified == lastModified) continue;
                }

                String fileName = Path.GetFileName(filePath);
                String directory = Path.GetDirectoryName(filePath);
                int dataCursor = directory.IndexOf("data");
                directory = directory.Remove(0, dataCursor) + "\\";

                byte[] buffer;
                try
                {
                    buffer = File.ReadAllBytes(filePath);
                }
                catch (Exception ex)
                {
                    ExceptionLogger.LogException(ex);
                    Console.WriteLine(String.Format("Warning: Could not read file {0}", filePath));
                    continue;
                }

                Console.WriteLine("Packing " + directory + fileName);
                if (indexFile.AddFile(directory, fileName, buffer, lastModified) == false)
                {
                    Console.WriteLine("Warning: Failed to add file to index...");
                }
            }

            foreach (PackFile file in _fileManager.IndexFiles)
                file.EndDatAccess();

            string thisPack = Path.GetFileNameWithoutExtension(outputPath);
            byte[] indexBytes = indexFile.ToByteArray();
            Crypt.Encrypt(indexBytes);
            Console.WriteLine("Writing " + thisPack);
            try
            {
                File.WriteAllBytes(outputPath, indexBytes);
            }
            catch (Exception ex)
            {
                ExceptionLogger.LogException(ex);
                Console.WriteLine(String.Format("Fatal error: Could not write index {0}", thisPack));
                return false;
            }

            Console.WriteLine(String.Format("{0} generation complete.", thisPack));
            return true;
        }