static private string convertDataModtoMem(string inputDir, string memFilePath)
        {
            string errors = "";

            string[] files = null;

            Console.WriteLine("Mods conversion started...");

            List <string> list = Directory.GetFiles(inputDir, "*.tpf").Where(item => item.EndsWith(".tpf", StringComparison.OrdinalIgnoreCase)).ToList();

            list.AddRange(Directory.GetFiles(inputDir, "*.mod").Where(item => item.EndsWith(".mod", StringComparison.OrdinalIgnoreCase)));
            list.AddRange(Directory.GetFiles(inputDir, "*.dds").Where(item => item.EndsWith(".dds", StringComparison.OrdinalIgnoreCase)));
            list.Sort();
            files = list.ToArray();

            int    result;
            string fileName = "";
            uint   dstLen   = 0;

            string[] ddsList    = null;
            ulong    numEntries = 0;
            List <TexExplorer.BinaryMod> mods = new List <TexExplorer.BinaryMod>();

            foreach (string file in files)
            {
                if (file.EndsWith(".mod", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            string package = "";
                            int    len     = fs.ReadInt32();
                            string version = fs.ReadStringASCII(len); // version
                            if (version.Length < 5)                   // legacy .mod
                            {
                                fs.SeekBegin();
                            }
                            numEntries = fs.ReadUInt32();
                            for (uint i = 0; i < numEntries; i++)
                            {
                                TexExplorer.BinaryMod mod = new TexExplorer.BinaryMod();
                                len = fs.ReadInt32();
                                string desc = fs.ReadStringASCII(len); // description
                                len = fs.ReadInt32();
                                string scriptLegacy = fs.ReadStringASCII(len);
                                string path         = "";
                                if (desc.Contains("Binary Replacement"))
                                {
                                    try
                                    {
                                        Misc.ParseME3xBinaryScriptMod(scriptLegacy, ref package, ref mod.exportId, ref path);
                                        if (mod.exportId == -1 || package == "" || path == "")
                                        {
                                            throw new Exception();
                                        }
                                    }
                                    catch
                                    {
                                        len = fs.ReadInt32();
                                        fs.Skip(len);
                                        errors += "Skipping not compatible content, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }
                                    mod.packagePath = GameData.RelativeGameData(Path.Combine(path, package));
                                    mod.binaryMod   = true;
                                    len             = fs.ReadInt32();
                                    mod.data        = fs.ReadToBuffer(len);
                                }
                                else
                                {
                                    string       textureName = desc.Split(' ').Last();
                                    FoundTexture f;
                                    try
                                    {
                                        f = Misc.ParseLegacyMe3xScriptMod(textures, scriptLegacy, textureName);
                                        mod.textureCrc = f.crc;
                                        if (mod.textureCrc == 0)
                                        {
                                            throw new Exception();
                                        }
                                    }
                                    catch
                                    {
                                        len = fs.ReadInt32();
                                        fs.Skip(len);
                                        errors += "Skipping not compatible content, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }
                                    textureName     = f.name;
                                    mod.textureName = textureName;
                                    mod.binaryMod   = false;
                                    len             = fs.ReadInt32();
                                    mod.data        = fs.ReadToBuffer(len);
                                    DDSImage image = new DDSImage(new MemoryStream(mod.data));
                                    if (!image.checkExistAllMipmaps())
                                    {
                                        errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", f.crc) + " This texture has not all the required mipmaps, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }
                                    Package pkg     = new Package(GameData.GamePath + f.list[0].path);
                                    Texture texture = new Texture(pkg, f.list[0].exportID, pkg.getExportData(f.list[0].exportID));

                                    if (texture.mipMapsList.Count > 1 && image.mipMaps.Count() <= 1)
                                    {
                                        errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", f.crc) + " This texture must have mipmaps, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }

                                    string    fmt       = texture.properties.getProperty("Format").valueName;
                                    DDSFormat ddsFormat = DDSImage.convertFormat(fmt);
                                    if (image.ddsFormat != ddsFormat)
                                    {
                                        errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", f.crc) + " This texture has wrong texture format, should be: " + ddsFormat + ", skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }

                                    if (image.mipMaps[0].origWidth / image.mipMaps[0].origHeight !=
                                        texture.mipMapsList[0].width / texture.mipMapsList[0].height)
                                    {
                                        errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", f.crc) + " This texture has wrong aspect ratio, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                        continue;
                                    }
                                }
                                mods.Add(mod);
                            }
                        }
                    }
                    catch
                    {
                        errors += "Mod is not compatible: " + file + Environment.NewLine;
                        continue;
                    }
                }
                else if (file.EndsWith(".tpf", StringComparison.OrdinalIgnoreCase))
                {
                    IntPtr handle = IntPtr.Zero;
                    try
                    {
                        byte[] buffer = File.ReadAllBytes(file);
                        handle = ZlibHelper.Zip.Open(buffer, ref numEntries, 1);
                        if (ZlibHelper.Zip.LocateFile(handle, "texmod.def") != 0)
                        {
                            throw new Exception();
                        }
                        result = ZlibHelper.Zip.GetCurrentFileInfo(handle, ref fileName, ref dstLen);
                        if (result != 0)
                        {
                            throw new Exception();
                        }
                        byte[] listText = new byte[dstLen];
                        result = ZlibHelper.Zip.ReadCurrentFile(handle, listText, dstLen);
                        if (result != 0)
                        {
                            throw new Exception();
                        }
                        ddsList = Encoding.ASCII.GetString(listText).Trim('\0').Replace("\r", "").TrimEnd('\n').Split('\n');

                        result = ZlibHelper.Zip.GoToFirstFile(handle);
                        if (result != 0)
                        {
                            throw new Exception();
                        }

                        for (uint i = 0; i < numEntries; i++)
                        {
                            TexExplorer.BinaryMod mod = new TexExplorer.BinaryMod();
                            try
                            {
                                uint crc = 0;
                                result = ZlibHelper.Zip.GetCurrentFileInfo(handle, ref fileName, ref dstLen);
                                if (result != 0)
                                {
                                    throw new Exception();
                                }
                                string filename = Path.GetFileName(fileName);
                                foreach (string dds in ddsList)
                                {
                                    string ddsFile = dds.Split('|')[1];
                                    if (ddsFile.ToLowerInvariant() != filename.ToLowerInvariant())
                                    {
                                        continue;
                                    }
                                    crc = uint.Parse(dds.Split('|')[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
                                    break;
                                }
                                if (crc == 0)
                                {
                                    if (filename != "texmod.def")
                                    {
                                        errors += "Skipping file: " + filename + " not founded in texmod.def, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    }
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }

                                List <FoundTexture> foundCrcList = textures.FindAll(s => s.crc == crc);
                                if (foundCrcList.Count == 0)
                                {
                                    errors += "Texture skipped. File " + filename + string.Format(" - 0x{0:X8}", crc) + " is not present in your game setup - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }

                                string textureName = foundCrcList[0].name;
                                mod.textureName = textureName;
                                mod.binaryMod   = false;
                                mod.textureCrc  = crc;
                                mod.data        = new byte[dstLen];
                                result          = ZlibHelper.Zip.ReadCurrentFile(handle, mod.data, dstLen);
                                if (result != 0)
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + ", skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }
                                uint tag = BitConverter.ToUInt32(mod.data, 0);
                                if (tag != 0x20534444) // DDS
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + " This texture is not in DDS format, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }
                                DDSImage image = new DDSImage(new MemoryStream(mod.data));
                                if (!image.checkExistAllMipmaps())
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + " This texture has not all the required mipmaps, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }
                                Package pkg     = new Package(GameData.GamePath + foundCrcList[0].list[0].path);
                                Texture texture = new Texture(pkg, foundCrcList[0].list[0].exportID, pkg.getExportData(foundCrcList[0].list[0].exportID));

                                if (texture.mipMapsList.Count > 1 && image.mipMaps.Count() <= 1)
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + " This texture must have mipmaps, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }

                                string    fmt       = texture.properties.getProperty("Format").valueName;
                                DDSFormat ddsFormat = DDSImage.convertFormat(fmt);
                                if (image.ddsFormat != ddsFormat)
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + " This texture has wrong texture format, should be: " + ddsFormat + ", skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }

                                if (image.mipMaps[0].origWidth / image.mipMaps[0].origHeight !=
                                    texture.mipMapsList[0].width / texture.mipMapsList[0].height)
                                {
                                    errors += "Error in texture: " + textureName + string.Format("_0x{0:X8}", crc) + " This texture has wrong aspect ratio, skipping texture, entry: " + (i + 1) + " - mod: " + file + Environment.NewLine;
                                    ZlibHelper.Zip.GoToNextFile(handle);
                                    continue;
                                }
                                mods.Add(mod);
                            }
                            catch
                            {
                                errors += "Skipping not compatible content, entry: " + (i + 1) + " file: " + fileName + " - mod: " + file + Environment.NewLine;
                            }
                            ZlibHelper.Zip.GoToNextFile(handle);
                        }
                        ZlibHelper.Zip.Close(handle);
                        handle = IntPtr.Zero;
                    }
                    catch
                    {
                        errors += "Mod is not compatible: " + file + Environment.NewLine;
                        if (handle != IntPtr.Zero)
                        {
                            ZlibHelper.Zip.Close(handle);
                        }
                        handle = IntPtr.Zero;
                        continue;
                    }
                }
                else if (file.EndsWith(".dds", StringComparison.OrdinalIgnoreCase))
                {
                    TexExplorer.BinaryMod mod = new TexExplorer.BinaryMod();
                    string filename           = Path.GetFileNameWithoutExtension(file).ToLowerInvariant();
                    if (!filename.Contains("0x"))
                    {
                        errors += "Texture filename not valid: " + Path.GetFileName(file) + " Texture filename must include texture CRC (0xhhhhhhhh). Skipping texture..." + Environment.NewLine;
                        continue;
                    }
                    int idx = filename.IndexOf("0x");
                    if (filename.Length - idx < 10)
                    {
                        errors += "Texture filename not valid: " + Path.GetFileName(file) + " Texture filename must include texture CRC (0xhhhhhhhh). Skipping texture..." + Environment.NewLine;
                        continue;
                    }
                    uint   crc;
                    string crcStr = filename.Substring(idx + 2, 8);
                    try
                    {
                        crc = uint.Parse(crcStr, System.Globalization.NumberStyles.HexNumber);
                    }
                    catch
                    {
                        errors += "Texture filename not valid: " + Path.GetFileName(file) + " Texture filename must include texture CRC (0xhhhhhhhh). Skipping texture..." + Environment.NewLine;
                        continue;
                    }

                    List <FoundTexture> foundCrcList = textures.FindAll(s => s.crc == crc);
                    if (foundCrcList.Count == 0)
                    {
                        errors += "Texture skipped. Texture " + Path.GetFileName(file) + " is not present in your game setup." + Environment.NewLine;
                        continue;
                    }

                    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                    {
                        mod.data = fs.ReadToBuffer((int)fs.Length);
                        DDSImage image = new DDSImage(new MemoryStream(mod.data));
                        if (!image.checkExistAllMipmaps())
                        {
                            errors += "Error in texture: " + Path.GetFileName(file) + " This texture has not all the required mipmaps, skipping texture..." + Environment.NewLine;
                            continue;
                        }

                        Package pkg     = new Package(GameData.GamePath + foundCrcList[0].list[0].path);
                        Texture texture = new Texture(pkg, foundCrcList[0].list[0].exportID, pkg.getExportData(foundCrcList[0].list[0].exportID));

                        if (texture.mipMapsList.Count > 1 && image.mipMaps.Count() <= 1)
                        {
                            errors += "Error in texture: " + Path.GetFileName(file) + " This texture must have mipmaps, skipping texture..." + Environment.NewLine;
                            continue;
                        }

                        string    fmt       = texture.properties.getProperty("Format").valueName;
                        DDSFormat ddsFormat = DDSImage.convertFormat(fmt);
                        if (image.ddsFormat != ddsFormat)
                        {
                            errors += "Error in texture: " + Path.GetFileName(file) + " This texture has wrong texture format, should be: " + ddsFormat + ", skipping texture..." + Environment.NewLine;
                            continue;
                        }

                        if (image.mipMaps[0].origWidth / image.mipMaps[0].origHeight !=
                            texture.mipMapsList[0].width / texture.mipMapsList[0].height)
                        {
                            errors += "Error in texture: " + Path.GetFileName(file) + " This texture has wrong aspect ratio, skipping texture..." + Environment.NewLine;
                            continue;
                        }

                        mod.textureName = foundCrcList[0].name;
                        mod.binaryMod   = false;
                        mod.textureCrc  = crc;
                        mods.Add(mod);
                    }
                }
            }

            if (mods.Count == 0)
            {
                Console.WriteLine("Mods conversion failed");
                return(errors);
            }

            Console.WriteLine("Saving to MEM file... " + memFilePath);
            List <MipMaps.FileMod> modFiles = new List <MipMaps.FileMod>();
            FileStream             outFs;

            if (File.Exists(memFilePath))
            {
                File.Delete(memFilePath);
            }

            using (outFs = new FileStream(memFilePath, FileMode.Create, FileAccess.Write))
            {
                outFs.WriteUInt32(TexExplorer.TextureModTag);
                outFs.WriteUInt32(TexExplorer.TextureModVersion);
                outFs.WriteInt64(0); // filled later

                for (int l = 0; l < mods.Count; l++)
                {
                    MipMaps.FileMod fileMod = new MipMaps.FileMod();
                    Stream          dst     = MipMaps.compressData(mods[l].data);
                    dst.SeekBegin();
                    fileMod.offset = outFs.Position;
                    fileMod.size   = dst.Length;

                    if (mods[l].binaryMod)
                    {
                        fileMod.tag  = MipMaps.FileBinaryTag;
                        fileMod.name = Path.GetFileNameWithoutExtension(mods[l].packagePath) + "_" + mods[l].exportId + ".bin";

                        outFs.WriteInt32(mods[l].exportId);
                        outFs.WriteStringASCIINull(mods[l].packagePath);
                    }
                    else
                    {
                        fileMod.tag  = MipMaps.FileTextureTag;
                        fileMod.name = mods[l].textureName + string.Format("_0x{0:X8}", mods[l].textureCrc) + ".dds";
                        outFs.WriteStringASCIINull(mods[l].textureName);
                        outFs.WriteUInt32(mods[l].textureCrc);
                    }
                    outFs.WriteFromStream(dst, dst.Length);
                    modFiles.Add(fileMod);
                }

                long pos = outFs.Position;
                outFs.SeekBegin();
                outFs.WriteUInt32(TexExplorer.TextureModTag);
                outFs.WriteUInt32(TexExplorer.TextureModVersion);
                outFs.WriteInt64(pos);
                outFs.JumpTo(pos);
                outFs.WriteUInt32((uint)GameData.gameType);
                outFs.WriteInt32(modFiles.Count);
                for (int i = 0; i < modFiles.Count; i++)
                {
                    outFs.WriteUInt32(modFiles[i].tag);
                    outFs.WriteStringASCIINull(modFiles[i].name);
                    outFs.WriteInt64(modFiles[i].offset);
                    outFs.WriteInt64(modFiles[i].size);
                }
            }

            Console.WriteLine("Mods conversion process completed");
            return(errors);
        }
Пример #2
0
        void toolStripCreateBinaryMod(MeType gameType)
        {
            enableGameDataMenu(false);
            GameData gameData = new GameData(gameType, _configIni);

            if (!Directory.Exists(GameData.GamePath))
            {
                MessageBox.Show("Game path is wrong!");
                enableGameDataMenu(true);
                return;
            }
            updateStatusLabel("Finding packages in game setup...");
            gameData.getPackages();
            updateStatusLabel("");

            using (FolderBrowserDialog modDir = new FolderBrowserDialog())
            {
                modDir.Description = "Please select source directory of modded package files";
                if (modDir.ShowDialog() != DialogResult.OK)
                {
                    updateStatusLabel("");
                    enableGameDataMenu(true);
                    return;
                }

                List <string> exe = Directory.GetFiles(modDir.SelectedPath, "*.*",
                                                       SearchOption.AllDirectories).Where(s => s.EndsWith(".exe",
                                                                                                          StringComparison.OrdinalIgnoreCase)).ToList();
                if (exe.Count != 0)
                {
                    MessageBox.Show("The source directory doesn't seems right, aborting...");
                    updateStatusLabel("");
                    enableGameDataMenu(true);
                    return;
                }

                List <string> mods = Directory.GetFiles(modDir.SelectedPath, "*.*",
                                                        SearchOption.AllDirectories).Where(s =>
                                                                                           s.EndsWith(".upk", StringComparison.OrdinalIgnoreCase) ||
                                                                                           s.EndsWith(".u", StringComparison.OrdinalIgnoreCase) ||
                                                                                           s.EndsWith(".pcc", StringComparison.OrdinalIgnoreCase) ||
                                                                                           s.EndsWith(".sfm", StringComparison.OrdinalIgnoreCase)).ToList();

                for (int i = 0; i < mods.Count; i++)
                {
                    try
                    {
                        using (FileStream fs = new FileStream(mods[i], FileMode.Open, FileAccess.Read))
                        {
                            fs.SeekEnd();
                            fs.Seek(-Package.MEMendFileMarker.Length, SeekOrigin.Current);
                            string marker = fs.ReadStringASCII(Package.MEMendFileMarker.Length);
                            if (marker == Package.MEMendFileMarker)
                            {
                                MessageBox.Show("Mod files must be based on vanilla game data, aborting...");
                                updateStatusLabel("");
                                enableGameDataMenu(true);
                                return;
                            }
                        }
                    }
                    catch
                    {
                    }
                }

                updateStatusLabel("Scanning mods...");
                List <string> files = Directory.GetFiles(modDir.SelectedPath, "*.*",
                                                         SearchOption.AllDirectories).Where(s =>
                                                                                            s.EndsWith(".upk", StringComparison.OrdinalIgnoreCase) ||
                                                                                            s.EndsWith(".u", StringComparison.OrdinalIgnoreCase) ||
                                                                                            s.EndsWith(".pcc", StringComparison.OrdinalIgnoreCase) ||
                                                                                            s.EndsWith(".sfm", StringComparison.OrdinalIgnoreCase)).ToList();
                List <BinaryMod> modFiles = new List <BinaryMod>();
                for (int i = 0; i < mods.Count; i++)
                {
                    Package vanillaPkg = null;
                    Package modPkg     = null;
                    bool    found      = false;
                    try
                    {
                        for (int v = 0; v < GameData.packageFiles.Count; v++)
                        {
                            if (Path.GetFileName(mods[i]).ToLowerInvariant() == Path.GetFileName(GameData.packageFiles[v]).ToLowerInvariant())
                            {
                                modPkg     = new Package(mods[i]);
                                vanillaPkg = new Package(GameData.packageFiles[v]);
                                if (modPkg.exportsTable.Count != vanillaPkg.exportsTable.Count ||
                                    modPkg.namesTable.Count != vanillaPkg.namesTable.Count ||
                                    modPkg.importsTable.Count != vanillaPkg.importsTable.Count)
                                {
                                    found = true;
                                    vanillaPkg.Dispose();
                                    vanillaPkg = null;
                                    continue;
                                }
                                found = true;
                                break;
                            }
                        }
                        if (found && vanillaPkg == null)
                        {
                            modPkg.Dispose();
                            MessageBox.Show("Package file not compatible: " + mods[i] + ", aborting...");
                            updateStatusLabel("");
                            enableGameDataMenu(true);
                            return;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Problem opening file: " + mods[i] + ", aborting...");
                        updateStatusLabel("");
                        enableGameDataMenu(true);
                        return;
                    }
                    if (!found)
                    {
                        MessageBox.Show("Package not present in vanilla game data: " + mods[i] + ", aborting...");
                        updateStatusLabel("");
                        enableGameDataMenu(true);
                        return;
                    }

                    for (int e = 0; e < modPkg.exportsTable.Count; e++)
                    {
                        byte[] vanillaExport = vanillaPkg.getExportData(e);
                        byte[] modExport     = modPkg.getExportData(e);
                        if (vanillaExport.Length == modExport.Length)
                        {
                            if (StructuralComparisons.StructuralEqualityComparer.Equals(vanillaExport, modExport))
                            {
                                continue;
                            }
                        }

                        BinaryMod mod = new BinaryMod();
                        mod.packagePath = GameData.RelativeGameData(vanillaPkg.packagePath);
                        mod.exportId    = e;

                        if (vanillaExport.Length == modExport.Length)
                        {
                            mod.data          = new Xdelta3Helper.Xdelta3().Compress(vanillaExport, modExport);
                            mod.binaryModType = 2;
                        }
                        else
                        {
                            mod.data = new byte[modExport.Length];
                            Array.Copy(modExport, mod.data, modExport.Length);
                            mod.binaryModType = 1;
                        }

                        string name;
                        if (mod.packagePath.Contains("\\DLC\\"))
                        {
                            string dlcName = mod.packagePath.Split('\\')[3];
                            name = "D" + dlcName.Length + "-" + dlcName + "-";
                        }
                        else
                        {
                            name = "B";
                        }
                        name += Path.GetFileName(mod.packagePath).Length + "-" +
                                Path.GetFileName(mod.packagePath) + "-E" + mod.exportId;
                        if (mod.binaryModType == 1)
                        {
                            name += ".bin";
                        }
                        else if (mod.binaryModType == 2)
                        {
                            name += ".xdelta";
                        }

                        mod.textureName = name;
                        modFiles.Add(mod);
                    }
                    vanillaPkg.Dispose();
                    modPkg.Dispose();
                }

                if (modFiles.Count == 0)
                {
                    MessageBox.Show("Nothing to mod, exiting...");
                    updateStatusLabel("");
                    enableGameDataMenu(true);
                    return;
                }

                updateStatusLabel("Creating mem...");
                using (SaveFileDialog modFile = new SaveFileDialog())
                {
                    modFile.Title  = "Please selecct new MEM mod file";
                    modFile.Filter = "MEM mod file | *.mem";
                    if (modFile.ShowDialog() != DialogResult.OK)
                    {
                        updateStatusLabel("");
                        enableGameDataMenu(true);
                        return;
                    }

                    if (File.Exists(modFile.FileName))
                    {
                        File.Delete(modFile.FileName);
                    }

                    using (FileStream outFs = new FileStream(modFile.FileName, FileMode.CreateNew, FileAccess.Write))
                    {
                        outFs.WriteUInt32(TreeScan.TextureModTag);
                        outFs.WriteUInt32(TreeScan.TextureModVersion);
                        outFs.WriteInt64(0); // filled later

                        for (int i = 0; i < modFiles.Count; i++)
                        {
                            Stream dst = MipMaps.compressData(modFiles[i].data);
                            dst.SeekBegin();
                            BinaryMod bmod = modFiles[i];
                            bmod.offset = outFs.Position;
                            bmod.size   = dst.Length;
                            modFiles[i] = bmod;
                            outFs.WriteInt32(modFiles[i].exportId);
                            outFs.WriteStringASCIINull(modFiles[i].packagePath);
                            outFs.WriteFromStream(dst, dst.Length);
                        }

                        long pos = outFs.Position;
                        outFs.SeekBegin();
                        outFs.WriteUInt32(TreeScan.TextureModTag);
                        outFs.WriteUInt32(TreeScan.TextureModVersion);
                        outFs.WriteInt64(pos);
                        outFs.JumpTo(pos);
                        outFs.WriteUInt32((uint)gameType);
                        outFs.WriteInt32(modFiles.Count);

                        for (int i = 0; i < modFiles.Count; i++)
                        {
                            if (modFiles[i].binaryModType == 1)
                            {
                                outFs.WriteUInt32(MipMaps.FileBinaryTag);
                            }
                            else if (modFiles[i].binaryModType == 2)
                            {
                                outFs.WriteUInt32(MipMaps.FileXdeltaTag);
                            }
                            outFs.WriteStringASCIINull(modFiles[i].textureName);
                            outFs.WriteInt64(modFiles[i].offset);
                            outFs.WriteInt64(modFiles[i].size);
                        }
                    }
                }
            }
            updateStatusLabel("Finished");
            enableGameDataMenu(true);
        }