static bool WriteImportFile(IntPtr hMpq, string impFile, List <ImportFile> fileList) { var data = GenImpFile(fileList); if (data == null) { return(StormLib.SFileRemoveFile(hMpq, impFile, 0u)); } var buffer = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, buffer, data.Length); var result = true; IntPtr hFile; if (result &= StormLib.SFileCreateFile(hMpq, impFile, 0ul, (uint)data.Length, 0u, 0x80000200u, out hFile)) { result &= StormLib.SFileWriteFile(hFile, buffer, (uint)data.Length, 0x2u); } if (result &= (hFile != IntPtr.Zero)) { result &= StormLib.SFileFinishFile(hFile); } Marshal.FreeHGlobal(buffer); return(result); }
public static SaveResult SaveImportList(string mapPath, List <ImportFile> fileList) { var result = new SaveResult(); var saved = true; IntPtr hMpq; if (saved &= StormLib.SFileOpenArchive(mapPath, 0u, 0u, out hMpq)) { var hashtableSize = 32u; var fileCount = (uint)fileList.Count + hashtableSize; while (hashtableSize < fileCount) { hashtableSize *= 2; } StormLib.SFileSetMaxFileCount(hMpq, hashtableSize); var failedFiles = new List <string>(); for (int i = 0; (i < fileList.Count) && saved; i++) { var file = fileList[i]; if (file.Deleted) { StormLib.SFileRemoveFile(hMpq, file.OrigPath, 0u); } else if (file.Changed) { if (string.IsNullOrEmpty(file.DiskPath)) { StormLib.SFileRenameFile(hMpq, file.OrigPath, file.FilePath); } else { StormLib.SFileRemoveFile(hMpq, file.OrigPath, 0u); if (!StormLib.SFileAddFileEx(hMpq, file.DiskPath, file.FilePath, 0x80000200u, 0x2u, 0x2u)) { failedFiles.Add(file.DiskPath); file.Deleted = true; } } } } result.FailedFiles = failedFiles; var impFile = DetectImpFile(hMpq); if (saved &= (impFile != null)) { saved &= WriteImportFile(hMpq, impFile, fileList); } StormLib.SFileCompactArchive(hMpq, null, false); } if (saved &= (hMpq != IntPtr.Zero)) { saved &= StormLib.SFileCloseArchive(hMpq); } result.Saved = saved; return(result); }