public static bool CopyOutputToMap(FileInfo rootFile, FileInfo map, Form1 owner) { //Backup map MakeBackups(map, owner); UInt32 mpqHandle = 0; bool success = SFileOpenArchive(ToByteArray(map.FullName), 0, 0, ref mpqHandle); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text1"), LocRM.GetString("ALL_Error")); return(false); } //Remove all galaxy files List <string> filesToDelete = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); string[] fileDeletePatterns = new string[] { "*.galaxy", "BankList.xml", "Triggers", "Triggers.version", "*TriggerStrings.txt" }; foreach (string pattern in fileDeletePatterns) { success = true; UInt32 searchHandle = SFileFindFirstFile(mpqHandle, ToByteArray(pattern), ref fileData, 0); while (searchHandle != 0) { if (!success) { //Some stop condition here... SFileFindClose(searchHandle); break; } filesToDelete.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } continue; } foreach (string file in filesToDelete) { success = SFileRemoveFile(mpqHandle, ToByteArray(file), 0); } success = CopyFiles(owner.openProjectOutputDir, owner.openProjectOutputDir, rootFile, mpqHandle); SFileFlushArchive(mpqHandle); SFileCloseArchive(mpqHandle); return(success); }
public string[] FindFiles(params string[] patterns) { List <string> filesFound = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); bool success = true; foreach (string pattern in patterns) { success = true; UInt32 searchHandle = SFileFindFirstFile(hMpq, ToByteArray(pattern), ref fileData, 0); while (searchHandle != 0) { if (!success) { //Some stop condition here... SFileFindClose(searchHandle); break; } filesFound.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } } return(filesFound.ToArray()); }
public MpqStreamReadonly(FileInfo file, string fileName) { //Opening readonly bool success = SFileOpenArchive(ToByteArray(file.FullName), 0, 0x100, ref hMpq); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text16") + fileName + LocRM.GetString("ME_Text17"), LocRM.GetString("ALL_Error")); return; } List <string> files = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); UInt32 searchHandle = SFileFindFirstFile(hMpq, ToByteArray(fileName), ref fileData, 0); while (searchHandle != 0) { if (!success) { SFileFindClose(searchHandle); break; } files.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } if (files.Count == 0) { //MessageBox.Show("Unable to find the file " + fileName + " in the mpq " + file.Name + ".", "Error"); SFileCloseArchive(hMpq); return; } string f = files[0]; { success = SFileOpenFileEx(hMpq, ToByteArray(f), 0, ref hFile); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text18") + fileName + LocRM.GetString("ME_Text19") + file.Name + LocRM.GetString("ME_Text20"), LocRM.GetString("ALL_Error")); SFileCloseArchive(hMpq); return; } /*unsafe * { * void* buffer = Marshal.AllocHGlobal(256).ToPointer(); * UInt32* bytesRead = (uint*)Marshal.AllocHGlobal(sizeof(UInt32)).ToPointer(); * while (true) * { * success |= SFileReadFile(hFile, buffer, 256, bytesRead, null); * * if (!success) break; * if (*bytesRead == 0) break; * byte[] managedBuffer = new byte[256]; * byte* p = (byte*)buffer; * for (int i = 0; i < *bytesRead; i++) * { * managedBuffer[i] = *p; * p++; * } * stream.Write(managedBuffer, 0, (int)*bytesRead); * } * Marshal.FreeHGlobal(new IntPtr(buffer)); * Marshal.FreeHGlobal(new IntPtr(bytesRead)); * }*/ //SFileCloseFile(hFile); } //SFileCloseArchive(mpqHandle); CanReadMore = true; }
public static void ExtractGalaxyppScriptFiles(FileInfo file, DirectoryInfo srcDir, bool autoOverride) { UInt32 mpqHandle = 0; //Opening readonly bool success = SFileOpenArchive(ToByteArray(file.FullName), 0, 0x100, ref mpqHandle); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text6"), LocRM.GetString("ALL_Error")); return; } //Find all galaxy++ and Dialog files List <string> files = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); UInt32 searchHandle = SFileFindFirstFile(mpqHandle, ToByteArray("Galaxy++\\*.galaxy++"), ref fileData, 0); while (searchHandle != 0) { if (!success) { SFileFindClose(searchHandle); break; } files.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } searchHandle = SFileFindFirstFile(mpqHandle, ToByteArray("Galaxy++\\*.Dialog"), ref fileData, 0); success = true; while (searchHandle != 0) { if (!success) { SFileFindClose(searchHandle); break; } files.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } //Extract all found files foreach (string f in files) { string path = Path.Combine(srcDir.FullName, f); if (f.StartsWith("Galaxy++\\"))//Okay, so this should actually be true for all found files, because of the search filters { path = Path.Combine(srcDir.FullName, f.Remove(0, "Galaxy++\\".Length)); } UInt32 hFile = 0; success = SFileOpenFileEx(mpqHandle, ToByteArray(f), 0, ref hFile); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text8") + f + LocRM.GetString("ME_Text9"), LocRM.GetString("ALL_Error")); continue; } FileInfo destFile = new FileInfo(path); if (destFile.Exists && !autoOverride && MessageBox.Show(LocRM.GetString("ME_Text10") + f + LocRM.GetString("ME_Text11"), LocRM.GetString("ME_Text12"), MessageBoxButtons.YesNo) != DialogResult.Yes) { continue; } CreateDir(destFile.Directory); FileStream stream = destFile.Open(FileMode.Create, FileAccess.Write); unsafe { void * buffer = Marshal.AllocHGlobal(256).ToPointer(); UInt32 *bytesRead = (uint *)Marshal.AllocHGlobal(sizeof(UInt32)).ToPointer(); while (true) { success |= SFileReadFile(hFile, buffer, 256, bytesRead, null) != 0; if (!success) { break; } if (*bytesRead == 0) { break; } byte[] managedBuffer = new byte[256]; byte * p = (byte *)buffer; for (int i = 0; i < *bytesRead; i++) { managedBuffer[i] = *p; p++; } stream.Write(managedBuffer, 0, (int)*bytesRead); } Marshal.FreeHGlobal(new IntPtr(buffer)); Marshal.FreeHGlobal(new IntPtr(bytesRead)); } stream.Close(); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text13") + f + LocRM.GetString("ME_Text9"), LocRM.GetString("ALL_Error")); continue; } SFileCloseFile(hFile); } SFileCloseArchive(mpqHandle); return; }
private static extern bool SFileFindNextFile( UInt32 hFind, // Find handle ref SFILE_FIND_DATA lpFindFileData // Pointer to the search result );
public static bool SaveGalaxyppScriptFiles(FileInfo file, DirectoryInfo srcDir, bool showErrors = true) { UInt32 hMpq = 0; bool success; try { success = SFileOpenArchive(ToByteArray(file.FullName), 0, 0, ref hMpq); if (!success) { if (showErrors) { MessageBox.Show(LocRM.GetString("ME_Text7"), LocRM.GetString("ALL_Error")); } return(false); } //Remove old Galaxy++\\ dir List <string> files = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); UInt32 searchHandle = SFileFindFirstFile(hMpq, ToByteArray("*"), ref fileData, 0); UInt32 count = 0; while (searchHandle != 0) { if (!success) { //Some stop condition here... SFileFindClose(searchHandle); break; } if (fileData.cFileName.StartsWith("Galaxy++\\") && (fileData.cFileName.EndsWith(".galaxy++") || fileData.cFileName.EndsWith(".Dialog"))) { files.Add(fileData.cFileName); } else { count++; } success = SFileFindNextFile(searchHandle, ref fileData); } foreach (string oldFile in files) { SFileRemoveFile(hMpq, ToByteArray(oldFile), 0); } //Count how many files we need to add count += CountGalaxyFiles(srcDir); //Extend maximum number of files in map as needed int err = SFileSetMaxFileCount(hMpq, count + count / 2); //Copy files to the map Copy(hMpq, srcDir, "Galaxy++"); } finally {//Free memory if (hMpq != 0) { SFileFlushArchive(hMpq); SFileCloseArchive(hMpq); } } return(true); }
private static extern UInt32 SFileFindFirstFile( UInt32 hMpq, // Archive handle byte[] szMask, // Search mask ref SFILE_FIND_DATA lpFindFileData, // Pointer to the search result UInt32 szListFile // Name of additional listfile );
public static bool Copy(FileInfo source, DirectoryInfo dest) { UInt32 mpqHandle = 0; bool success = SFileOpenArchive(ToByteArray(source.FullName), 0, 0, ref mpqHandle); if (!success) { MessageBox.Show(LocRM.GetString("ME_Text6"), LocRM.GetString("ALL_Error")); return(false); } //Remove all galaxy files List <string> files = new List <string>(); SFILE_FIND_DATA fileData = default(SFILE_FIND_DATA); success = true; UInt32 searchHandle = SFileFindFirstFile(mpqHandle, ToByteArray("*"), ref fileData, 0); while (searchHandle != 0) { if (!success) { //Some stop condition here... SFileFindClose(searchHandle); break; } files.Add(fileData.cFileName); success = SFileFindNextFile(searchHandle, ref fileData); } success = true; string currentFile = ""; foreach (string file in files) { currentFile = file; string path = Path.Combine(dest.FullName, file); //byte[] s1 = ToByteArray(file); //byte[] s2 = ToByteArray(path); /*unsafe * { * fixed (char * s1 = file) * { * fixed (char * s2 = path) * { * success |= SFileExtractFile(mpqHandle, s1, s2, 0); * } * } * }*/ //success |= SFileExtractFile(mpqHandle, ref s1, ref s2); UInt32 hFile = 0; success |= SFileOpenFileEx(mpqHandle, ToByteArray(file), 0, ref hFile); if (!success) { break; } FileInfo destFile = new FileInfo(Path.Combine(dest.FullName, file)); CreateDir(destFile.Directory); FileStream stream = destFile.Open(FileMode.Create, FileAccess.Write); unsafe { void * buffer = Marshal.AllocHGlobal(256).ToPointer(); UInt32 *bytesRead = (uint *)Marshal.AllocHGlobal(sizeof(UInt32)).ToPointer(); while (true) { success |= SFileReadFile(hFile, buffer, 256, bytesRead, null) != 0; if (!success) { break; } if (*bytesRead == 0) { break; } byte[] managedBuffer = new byte[256]; byte * p = (byte *)buffer; for (int i = 0; i < *bytesRead; i++) { managedBuffer[i] = *p; p++; } stream.Write(managedBuffer, 0, (int)*bytesRead); } Marshal.FreeHGlobal(new IntPtr(buffer)); Marshal.FreeHGlobal(new IntPtr(bytesRead)); } stream.Close(); if (!success) { break; } success |= SFileCloseFile(hFile); } SFileCloseArchive(mpqHandle); return(success); }