/// <summary> /// Extracts the file from the MPQ using SaveFileDialog /// </summary> /// <param name="fileName">File name to extract</param> /// <returns></returns> public static bool fileExtact(string fileName) { SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = Path.GetFileName(fileName); sfd.Filter = "All files (*.*)|*.*"; sfd.Title = "Extract file..."; if (sfd.ShowDialog() == DialogResult.OK) { int _hFile = -1; if (Storm.SFileOpenFile(fileName, ref _hFile)) { uint fileSizeHigh = 0; uint fileSize = Storm.SFileGetFileSize(_hFile, ref fileSizeHigh); if ((fileSizeHigh == 0) && (fileSize > 0)) { byte[] bs = new byte[fileSize]; uint countRead = 0; Storm.SFileReadFile(_hFile, bs, fileSize, ref countRead, 0); FileStream F = new FileStream(sfd.FileName, FileMode.Create, FileAccess.ReadWrite); F.Write(bs, 0, bs.Length); F.Close(); Storm.SFileCloseFile(_hFile); return(true); } } } return(false); }
/// <summary> /// Deletes a file from the MPQ. CALL openMPQ(); after this !! /// </summary> /// <param name="mpqHandle">Handle of the MPQ file.</param> /// <param name="fileName">Filename to delete</param> /// <param name="mpqFileName">MPQ file location (open for modification)</param> /// <param name="mpqCloseFunc">Function to close the MPQ file</param> /// <returns></returns> public static bool deleteFile(uint mpqHandle, string fileName, string mpqFileName, Action mpqCloseFunc) { uint hFile = 0; if (!Storm.SFileOpenFileEx(mpqHandle, fileName, 0, ref hFile)) { throw new System.IO.FileNotFoundException("File not found in the MPQ: " + fileName); } if (hFile != 0) { Storm.SFileCloseFile(hFile); } System.Threading.Thread.Sleep(1000); mpqCloseFunc(); int modMPQ = Storm.MpqOpenArchiveForUpdate(mpqFileName, Storm.MOAU_OPEN_EXISTING, uint.MaxValue); if (modMPQ != 0) { Storm.MpqDeleteFile(modMPQ, fileName); } else { throw new System.IO.IOException("ErrCode: " + Convert.ToString(Marshal.GetLastWin32Error())); } Storm.MpqCloseUpdatedArchive(modMPQ, 0); return(true); }
/// <summary> /// Checks if the file exists. /// </summary> /// <param name="mpqHandle">The handle of the MPQ file.</param> /// <param name="fileName">The file to check</param> /// <returns></returns> public static bool fileExists(uint mpqHandle, string fileName) { uint hFile = 0; // unused if (!Storm.SFileOpenFileEx(mpqHandle, fileName, 0, ref hFile)) { return(false); } return(true); }
private void addFileToMpq(string fileName) { if (File.Exists(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp")) { File.Delete(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp"); } try { string shortFileName = Path.GetFileName(fileName); int listFileMPQ_handle = -1; int _hMPQ = -1; List <string> listFile = new List <string>(); listFile.Add("(listfile)"); bool fileExists = false; if (mpqHandle == 0) { MessageBox.Show("MPQ is null", "EQUINE MPQEdit", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (Storm.SFileOpenFile("(listfile)", ref listFileMPQ_handle)) { uint fileSizeHigh = 0; uint fileSize = Storm.SFileGetFileSize(listFileMPQ_handle, ref fileSizeHigh); if ((fileSizeHigh == 0) && (fileSize > 0)) { byte[] bs = new byte[fileSize]; uint countRead = 0; Storm.SFileReadFile(listFileMPQ_handle, bs, fileSize, ref countRead, 0); FileStream F = new FileStream(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp", FileMode.Create, FileAccess.ReadWrite); F.Write(bs, 0, bs.Length); F.Close(); Storm.SFileCloseFile(listFileMPQ_handle); // read the internal list file listFile = File.ReadAllLines(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp").ToList(); } } else { // create new internal list file using (StreamWriter sw = new StreamWriter(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp")) { sw.WriteLine("(listfile)"); sw.WriteLine("(signature)"); listFile.Add("(listfile)"); listFile.Add("(signature)"); sw.Close(); } } string folderName = ""; string originalSFD = shortFileName; var dResult = ShowInputDialog(ref folderName); if (dResult == DialogResult.Cancel) { if (_hMPQ != -1) { MpqCloseUpdatedArchive(_hMPQ, 0); } MessageBox.Show("Operation cancelled.", "EQUINE MPQEdit"); return; } if (folderName != "") { shortFileName = folderName + "\\" + shortFileName; } if (Storm.SFileOpenFile(shortFileName, ref listFileMPQ_handle)) { fileExists = true; } SFileCloseArchive(mpqHandle); mpqHandle = 0; System.Threading.Thread.Sleep(1000); bool replaceFile = false; if (fileExists) { if (MessageBox.Show("File " + shortFileName + " already exists. The file will be replaced. Continue?", "EQUINE MPQEdit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { replaceFile = true; } else { replaceFile = false; return; } } _hMPQ = MpqOpenArchiveForUpdate(openMPQPath, Storm.MOAU_OPEN_EXISTING, GlobalVariableContainer.MAX_MPQ_FILES); if (_hMPQ == 0) { throw new Exception("ErrCode: " + Convert.ToString(Marshal.GetLastWin32Error())); } if (replaceFile) { MpqDeleteFile(_hMPQ, shortFileName); } // MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, Storm.MAFA_COMPRESS_STANDARD, 0x00000001, // Storm.MAFA_COMPRESS_STANDARD); switch (GlobalVariableContainer.compressionType) { case GlobalVariableContainer.CompressionType.STANDARD: MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, 0x00000100, Storm.MAFA_COMPRESS_STANDARD, Storm.MAFA_COMPRESS_STANDARD); break; case GlobalVariableContainer.CompressionType.BZIP2: MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, 0x00000200, 0x10, Storm.MAFA_COMPRESS_STANDARD); break; case GlobalVariableContainer.CompressionType.ZLIB: MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, 0x00000200, MAFA_COMPRESS_DEFLATE, 1); break; case GlobalVariableContainer.CompressionType.WAVE: MpqAddWaveToArchive(_hMPQ, fileName, shortFileName, 0x00000001, 1); MessageBox.Show("WAVE compression enabled.", "Note"); break; case GlobalVariableContainer.CompressionType.NO_COMPRESSION: MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, 0x80000000, 0, 0); break; default: MpqAddFileToArchiveEx(_hMPQ, fileName, shortFileName, 0x00000100, Storm.MAFA_COMPRESS_STANDARD, Storm.MAFA_COMPRESS_STANDARD); break; } if (!GlobalVariableContainer.dontGenerateListFile) { MpqDeleteFile(_hMPQ, "(listfile)"); } listFile.Add(shortFileName); if (!GlobalVariableContainer.dontGenerateListFile) { using (StreamWriter sw = new StreamWriter(Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp")) { foreach (var item in listFile.Distinct()) { sw.WriteLine(item); } sw.Close(); } } if (!GlobalVariableContainer.dontGenerateListFile) { MpqAddFileToArchiveEx(_hMPQ, Application.StartupPath + "/EquineData/eqmpqedit/listfile.tmp", "listfile.tmp", 0x00000100, Storm.MAFA_COMPRESS_STANDARD, Storm.MAFA_COMPRESS_STANDARD); MpqDeleteFile(_hMPQ, "(listfile)"); MpqRenameFile(_hMPQ, "listfile.tmp", "(listfile)"); } MpqCloseUpdatedArchive(_hMPQ, 0); listView1.Items.Clear(); openMPQ(openMPQPath); } catch (Exception ex) { MessageBox.Show("Error adding file:\t" + fileName + "\nMessage:\t" + ex.Message, "EQUINE MPQEdit", MessageBoxButtons.OK, MessageBoxIcon.Stop); } }
private unsafe void BuildList_DoWork(object sender, DoWorkEventArgs e) { // enable the wait cursor Application.UseWaitCursor = true; List <string> mpqFileNames = new List <string>(); // contains valid MPQ filenames List <string> listFileContent = new List <string>(); // contains files from listfiles uint[] mpqFileSize; int listFileMPQ_handle = -1; // check if (listfile) exists if (!GlobalVariableContainer.ignoreEmbedListFile) { if (Storm.SFileOpenFile("(listfile)", ref listFileMPQ_handle)) { listFileMPQ = true; uint fileSizeHigh = 0; uint fileSize = Storm.SFileGetFileSize(listFileMPQ_handle, ref fileSizeHigh); if ((fileSizeHigh == 0) && (fileSize > 0)) { byte[] bs = new byte[fileSize]; uint countRead = 0; Storm.SFileReadFile(listFileMPQ_handle, bs, fileSize, ref countRead, 0); FileStream F = new FileStream("EquineData/eqmpqedit/listfile.tmp", FileMode.Create, FileAccess.ReadWrite); F.Write(bs, 0, bs.Length); F.Close(); Storm.SFileCloseFile(listFileMPQ_handle); } else { listFileMPQ = false; } } else { if (GlobalVariableContainer.listFiles.Count == 0) { MessageBox.Show("No internal list file found. Please provide a list file in the settings.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } } else { if (GlobalVariableContainer.listFiles.Count == 0) { MessageBox.Show("Using internal list files disabled in settings. Please provide a list file in the settings.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { MessageBox.Show("Using internal list files disabled.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Information); } } // get the files from each list file if (!listFileMPQ) { for (int i3 = 0; i3 < GlobalVariableContainer.listFiles.Count; i3++) { StreamReader listFile = new StreamReader(GlobalVariableContainer.listFiles[i3]); while (!listFile.EndOfStream) { string temp = listFile.ReadLine(); listFileContent.Add(temp); } listFile.Close(); listFile.Dispose(); } } else { StreamReader listFile = new StreamReader("EquineData/eqmpqedit/listfile.tmp"); while (!listFile.EndOfStream) { string temp = listFile.ReadLine(); listFileContent.Add(temp); } listFile.Close(); listFile.Dispose(); File.Delete("EquineData/eqmpqedit/listfile.tmp"); } // resize the mpqfilesize array mpqFileSize = new uint[listFileContent.Count]; int i = 0; // check if all files are valid foreach (var item in listFileContent.Distinct()) { uint nothing = 0; int tempHFile = 0; if (Storm.SFileOpenFile(item, ref tempHFile)) { if (GlobalVariableContainer.showMPQFileSize) { mpqFileSize[i] = SFileGetFileSize(tempHFile, ref nothing); } mpqFileNames.Add(item); i++; } SFileCloseFile(tempHFile); } // display the files for (int i2 = 0; i2 < mpqFileNames.Count; i2++) { ListViewItem tempItem = new ListViewItem(); tempItem.Text = mpqFileNames[i2]; if (GlobalVariableContainer.showMPQFileSize) { tempItem.SubItems.Add(Convert.ToString(mpqFileSize[i2])); } else { tempItem.SubItems.Add("N/A"); } this.BeginInvoke((MethodInvoker) delegate() { listView1.Items.Add(tempItem); }); } this.BeginInvoke((MethodInvoker) delegate() { this.Enabled = true; }); appStatus.Text = "Done."; // disable the wait cursor Application.UseWaitCursor = false; }
public void ExtractMPQ(string fileName, string listFile) { Progress_MaxFiles = 1; if (File.Exists(fileName)) { if (Storm.SFileOpenArchive(fileName, 2, 0x8000, ref hMpq)) { // List file contents List <string> listFileContent = new List <string>(); // Valid files in MPQ List <string> validMPQFiles = new List <string>(); // Verify if list file exists if (!File.Exists(listFile)) { MessageBox.Show("Unable to extract DIABDAT.MPQ without a list file!", "EQUINE MPQEdit", MessageBoxButtons.OK, MessageBoxIcon.Stop); done = true; return; } // Set the progress string progressString = "Reading listfile..."; // Read contents of listfile into List listFileContent = File.ReadAllLines(listFile).ToList(); // loop through the listfile and probe the MPQ for valid files inside the MPQ for (int i = 0; i < listFileContent.Count(); i++) { progressString = "Probing file:\n" + listFileContent[i]; hFile = 0; if (Storm.SFileOpenFileEx(hMpq, listFileContent[i], 0x00, ref hFile)) { validMPQFiles.Add(listFileContent[i]); } if (hFile != 0) { Storm.SFileCloseFile(hFile); } } // create the folder for DIABDAT.MPQ inside EquineData progressString = "Preparing folder..."; try { Directory.CreateDirectory("EquineData/DIABDAT"); } catch { MessageBox.Show("Can't create folder for extracted files!", "EQUINE MPQEdit", MessageBoxButtons.OK, MessageBoxIcon.Stop); done = true; return; } // create the valid list file for DIABDAT.MPQ File.WriteAllLines("EquineData/DIABDAT/DIABDAT.listfile.txt", validMPQFiles); // report to Progress_MaxFiles Progress_MaxFiles = validMPQFiles.Count(); // and finally extract the files for (int i = 0; i < validMPQFiles.Count(); i++) { int _hFile = -1; progressString = "Extracting file:\n" + validMPQFiles[i]; if (!cancel) { if (Storm.SFileOpenFile(validMPQFiles[i], ref _hFile)) { uint fileSizeHigh = 0; uint fileSize = Storm.SFileGetFileSize(_hFile, ref fileSizeHigh); if ((fileSizeHigh == 0) && (fileSize > 0)) { byte[] bs = new byte[fileSize]; uint countRead = 0; Storm.SFileReadFile(_hFile, bs, fileSize, ref countRead, 0); if (!Directory.Exists(Path.GetDirectoryName("EquineData/DIABDAT/" + validMPQFiles[i]))) { Directory.CreateDirectory(Path.GetDirectoryName("EquineData/DIABDAT/" + validMPQFiles[i])); } FileStream F = new FileStream("EquineData/DIABDAT/" + validMPQFiles[i], FileMode.Create, FileAccess.ReadWrite); F.Write(bs, 0, bs.Length); F.Close(); Storm.SFileCloseFile(_hFile); } } Progress_File = i; } if (cancel) { break; } } // we done :) done = true; Success = true; Storm.SFileCloseArchive(hMpq); } else { MessageBox.Show("Couldn't open DIABDAT.MPQ for reading.", "EQUINE MPQEdit", MessageBoxButtons.OK, MessageBoxIcon.Stop); done = true; return; } } // close the file if (hFile != 0) { Storm.SFileCloseFile(hFile); } }
public void closeMPQ() { Storm.SFileCloseArchive(hMpq); }
/// <summary> /// Rebuild DIABDAT.MPQ from DIABDAT folder. /// </summary> public void RebuildDiabdat() { try { // create new mpq hMPQ = Storm.MpqOpenArchiveForUpdate(Environment.CurrentDirectory + "/diabdat.mpq", Storm.MOAU_CREATE_NEW, ushort.MaxValue); // read list file listFile = File.ReadAllLines(Environment.CurrentDirectory + "/EquineData/DIABDAT/DIABDAT.listfile.txt").ToList(); // add files to mpq foreach (var file in listFile) { // update the string currentFile = file; // check if file exists if (File.Exists(Environment.CurrentDirectory + "\\EquineData\\DIABDAT\\" + file)) { switch (Path.GetExtension(file)) { // if the file is wav case ".wav": case ".WAV": currentFile = currentFile + " (WAVE)"; Storm.MpqAddWaveToArchive(hMPQ, Environment.CurrentDirectory + "\\EquineData\\DIABDAT\\" + file, file, 0x00000001, 1); break; case ".smk": case ".SMK": case ".DUN": case ".dun": case ".MIN": case ".min": case ".SOL": case ".sol": case ".TIL": case ".til": case ".PAL": case ".pal": case ".AMP": case ".amp": case ".MPQ": case ".mpq": Storm.MpqAddFileToArchiveEx(hMPQ, Environment.CurrentDirectory + "\\EquineData\\DIABDAT\\" + file, file, 0x80000000, 0, 0); break; // if the file is not wav default: Storm.MpqAddFileToArchiveEx(hMPQ, Environment.CurrentDirectory + "\\EquineData\\DIABDAT\\" + file, file, 0x00000100 | 0x00010000, Storm.MAFA_COMPRESS_STANDARD, 0); break; } } } Storm.MpqCloseUpdatedArchive(hMPQ, 0); } catch (Exception ex) { throw new System.IO.IOException("MPQ/Listfile I/O error.\n" + ex.ToString()); } }