public string PrepareListOfTextures(TexExplorer texEplorer, CachePackageMgr cachePackageMgr, MainWindow mainWindow, Installer installer, ref string log, bool force = false) { string errors = ""; treeScan = null; List <FoundTexture> textures = new List <FoundTexture>(); string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetExecutingAssembly().GetName().Name); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, "me" + (int)GameData.gameType + "map.bin"); if (force && File.Exists(filename)) { File.Delete(filename); } if (File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite)) { uint tag = fs.ReadUInt32(); uint version = fs.ReadUInt32(); if (tag != TexExplorer.textureMapBinTag || version != TexExplorer.textureMapBinVersion) { if (mainWindow != null) { MessageBox.Show("Wrong " + filename + " file!"); mainWindow.updateStatusLabel(""); mainWindow.updateStatusLabel2(""); texEplorer.Close(); } fs.Close(); log += "Wrong " + filename + " file!" + Environment.NewLine; return("Wrong " + filename + " file!" + Environment.NewLine); } uint countTexture = fs.ReadUInt32(); for (int i = 0; i < countTexture; i++) { FoundTexture texture = new FoundTexture(); texture.name = fs.ReadStringASCIINull(); texture.crc = fs.ReadUInt32(); texture.packageName = fs.ReadStringASCIINull(); uint countPackages = fs.ReadUInt32(); texture.list = new List <MatchedTexture>(); for (int k = 0; k < countPackages; k++) { MatchedTexture matched = new MatchedTexture(); matched.exportID = fs.ReadInt32(); matched.path = fs.ReadStringASCIINull(); texture.list.Add(matched); } textures.Add(texture); } if (fs.Position < new FileInfo(filename).Length) { List <string> packages = new List <string>(); int numPackages = fs.ReadInt32(); for (int i = 0; i < numPackages; i++) { string pkgPath = fs.ReadStringASCIINull(); pkgPath = GameData.GamePath + pkgPath; packages.Add(pkgPath); } for (int i = 0; i < packages.Count; i++) { if (GameData.packageFiles.Find(s => s.Equals(packages[i], StringComparison.OrdinalIgnoreCase)) == null) { if (mainWindow != null) { MessageBox.Show("Detected removal of game files since last game data scan." + "\n\nYou need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods." + "\n\nThen from the main menu, select 'Remove Textures Scan File' and start Texture Manager again."); return(""); } else if (!force) { errors += "Detected removal of game files since last game data scan." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods."; return(""); } } } for (int i = 0; i < GameData.packageFiles.Count; i++) { if (packages.Find(s => s.Equals(GameData.packageFiles[i], StringComparison.OrdinalIgnoreCase)) == null) { if (mainWindow != null) { MessageBox.Show("Detected additional game files not present in latest game data scan." + "\n\nYou need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods." + "\n\nThen from the main menu, select 'Remove Textures Scan File' and start Texture Manager again."); return(""); } else if (!force) { errors += "Detected additional game files not present in latest game data scan." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods."; return(""); } } } } else { fs.SeekEnd(); fs.WriteInt32(GameData.packageFiles.Count); for (int i = 0; i < GameData.packageFiles.Count; i++) { fs.WriteStringASCIINull(GameData.RelativeGameData(GameData.packageFiles[i])); } } treeScan = textures; if (mainWindow != null) { mainWindow.updateStatusLabel(""); mainWindow.updateStatusLabel2(""); } return(errors); } } if (File.Exists(filename)) { File.Delete(filename); } if (Misc.detectBrokenMod(GameData.gameType)) { if (mainWindow != null) { MessageBox.Show("Detected ME1 Controller or/and Faster Elevators mod!\nMEM will not work properly due broken content in mod."); } return(""); } if (MipMaps.checkGameDataModded(cachePackageMgr)) { if (mainWindow != null) { MessageBox.Show("Detected modded game. Can not continue." + "\n\nYou need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods." + "\n\nThen start Texture Manager again."); return(""); } else if (!force) { errors += "Detected modded game. Can not continue." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state and reinstall vanilla DLCs and DLC mods."; return(""); } } if (mainWindow != null) { DialogResult result = MessageBox.Show("Replacing textures and creating mods requires generating a map of the game's textures.\n" + "You only need to do it once.\n\n" + "IMPORTANT! Your game needs to be in vanilla state and have all original DLCs and DLC mods installed.\n\n" + "Are you sure you want to proceed?", "Textures mapping", MessageBoxButtons.YesNo); if (result == DialogResult.No) { texEplorer.Close(); return(""); } } GameData.packageFiles.Sort(); if (mainWindow != null) { Misc.startTimer(); } for (int i = 0; i < GameData.packageFiles.Count; i++) { if (mainWindow != null) { mainWindow.updateStatusLabel("Finding textures in package " + (i + 1) + " of " + GameData.packageFiles.Count + " - " + GameData.packageFiles[i]); } if (installer != null) { installer.updateStatusScan("Progress... " + (i * 100 / GameData.packageFiles.Count) + " % "); } errors += FindTextures(textures, GameData.packageFiles[i], cachePackageMgr, ref log); } using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { fs.WriteUInt32(TexExplorer.textureMapBinTag); fs.WriteUInt32(TexExplorer.textureMapBinVersion); fs.WriteInt32(textures.Count); for (int i = 0; i < textures.Count; i++) { fs.WriteStringASCIINull(textures[i].name); fs.WriteUInt32(textures[i].crc); fs.WriteStringASCIINull(textures[i].packageName); fs.WriteInt32(textures[i].list.Count); for (int k = 0; k < textures[i].list.Count; k++) { fs.WriteInt32(textures[i].list[k].exportID); fs.WriteStringASCIINull(textures[i].list[k].path); } } fs.WriteInt32(GameData.packageFiles.Count); for (int i = 0; i < GameData.packageFiles.Count; i++) { fs.WriteStringASCIINull(GameData.RelativeGameData(GameData.packageFiles[i])); } } if (mainWindow != null) { MipMaps mipmaps = new MipMaps(); if (GameData.gameType == MeType.ME1_TYPE) { errors += mipmaps.removeMipMapsME1(1, textures, null, mainWindow, null); errors += mipmaps.removeMipMapsME1(2, textures, null, mainWindow, null); } else { errors += mipmaps.removeMipMapsME2ME3(textures, null, mainWindow, null); } var time = Misc.stopTimer(); mainWindow.updateStatusLabel("Done. Process total time: " + Misc.getTimerFormat(time)); mainWindow.updateStatusLabel2(""); } treeScan = textures; return(errors); }
private bool generateBuiltinMapFiles = false; // change to true to enable map files generation public string PrepareListOfTextures(TexExplorer texEplorer, CachePackageMgr cachePackageMgr, MainWindow mainWindow, Installer installer, ref string log, bool force = false) { string errors = ""; treeScan = null; List <FoundTexture> textures = new List <FoundTexture>(); string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), Assembly.GetExecutingAssembly().GetName().Name); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = Path.Combine(path, "me" + (int)GameData.gameType + "map.bin"); if (force && File.Exists(filename)) { File.Delete(filename); } if (File.Exists(filename)) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite)) { uint tag = fs.ReadUInt32(); uint version = fs.ReadUInt32(); if (tag != TexExplorer.textureMapBinTag || version != TexExplorer.textureMapBinVersion) { if (mainWindow != null) { MessageBox.Show("Detected wrong or old version of textures scan file!" + "\n\nYou need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + "\n\nThen from the main menu, select 'Remove Textures Scan File' and start Texture Manager again."); mainWindow.updateStatusLabel(""); mainWindow.updateStatusLabel2(""); texEplorer.Close(); } fs.Close(); log += "Detected wrong or old version of textures scan file!" + Environment.NewLine; log += "You need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + Environment.NewLine; log += "Then from the main menu, select 'Remove Textures Scan File' and start Texture Manager again." + Environment.NewLine; return("Detected wrong or old version of textures scan file!" + Environment.NewLine + "You need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + Environment.NewLine + "Then from the main menu, select 'Remove Textures Scan File' and start Texture Manager again." + Environment.NewLine); } uint countTexture = fs.ReadUInt32(); for (int i = 0; i < countTexture; i++) { FoundTexture texture = new FoundTexture(); int len = fs.ReadInt32(); texture.name = fs.ReadStringASCII(len); texture.crc = fs.ReadUInt32(); uint countPackages = fs.ReadUInt32(); texture.list = new List <MatchedTexture>(); for (int k = 0; k < countPackages; k++) { MatchedTexture matched = new MatchedTexture(); matched.exportID = fs.ReadInt32(); matched.linkToMaster = fs.ReadInt32(); len = fs.ReadInt32(); matched.path = fs.ReadStringASCII(len); texture.list.Add(matched); } textures.Add(texture); } List <string> packages = new List <string>(); int numPackages = fs.ReadInt32(); for (int i = 0; i < numPackages; i++) { int len = fs.ReadInt32(); string pkgPath = fs.ReadStringASCII(len); pkgPath = GameData.GamePath + pkgPath; packages.Add(pkgPath); } for (int i = 0; i < packages.Count; i++) { if (GameData.packageFiles.Find(s => s.Equals(packages[i], StringComparison.OrdinalIgnoreCase)) == null) { if (mainWindow != null) { MessageBox.Show("Detected removal of game files since last game data scan." + "\n\nYou need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + "\n\nThen from the main menu, select 'Remove Textures Scan File' and start Texture Manager again."); return(""); } else if (!force) { errors += "Detected removal of game files since last game data scan." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state then reinstall optional DLC/PCC mods."; return(""); } } } for (int i = 0; i < GameData.packageFiles.Count; i++) { if (packages.Find(s => s.Equals(GameData.packageFiles[i], StringComparison.OrdinalIgnoreCase)) == null) { if (mainWindow != null) { MessageBox.Show("Detected additional game files not present in latest game data scan." + "\n\nYou need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + "\n\nThen from the main menu, select 'Remove Textures Scan File' and start Texture Manager again."); return(""); } else if (!force) { errors += "Detected additional game files not present in latest game data scan." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state then reinstall optional DLC/PCC mods."; return(""); } } } treeScan = textures; if (mainWindow != null) { mainWindow.updateStatusLabel(""); mainWindow.updateStatusLabel2(""); } return(errors); } } if (File.Exists(filename)) { File.Delete(filename); } if (mainWindow != null) { List <string> badMods = Misc.detectBrokenMod(GameData.gameType); if (badMods.Count != 0) { errors = ""; for (int l = 0; l < badMods.Count; l++) { errors += badMods[l] + Environment.NewLine; } MessageBox.Show("Detected not compatible mods: \n\n" + errors); return(""); } } if (MipMaps.checkGameDataModded(cachePackageMgr)) { if (mainWindow != null) { MessageBox.Show("Detected modded game. Can not continue." + "\n\nYou need to restore the game to vanilla state then reinstall optional DLC/PCC mods." + "\n\nThen start Texture Manager again."); return(""); } else if (!force) { errors += "Detected modded game. Can not continue." + Environment.NewLine + Environment.NewLine + "You need to restore the game to vanilla state then reinstall optional DLC/PCC mods."; return(""); } } if (mainWindow != null) { DialogResult result = MessageBox.Show("Replacing textures and creating mods requires generating a map of the game's textures.\n" + "You only need to do it once.\n\n" + "IMPORTANT! Your game needs to be in vanilla state and have optional DLC/PCC mods installed.\n\n" + "Are you sure you want to proceed?", "Textures mapping", MessageBoxButtons.YesNo); if (result == DialogResult.No) { texEplorer.Close(); return(""); } } GameData.packageFiles.Sort(); if (mainWindow != null) { Misc.startTimer(); } for (int i = 0; i < GameData.packageFiles.Count; i++) { if (mainWindow != null) { mainWindow.updateStatusLabel("Finding textures in package " + (i + 1) + " of " + GameData.packageFiles.Count + " - " + GameData.packageFiles[i]); } if (installer != null) { installer.updateStatusScan("Scanning textures " + (i * 100 / GameData.packageFiles.Count) + "% "); } errors += FindTextures(textures, GameData.packageFiles[i], cachePackageMgr, ref log); } if (GameData.gameType == MeType.ME1_TYPE) { for (int k = 0; k < textures.Count; k++) { for (int t = 0; t < textures[k].list.Count; t++) { uint mipmapOffset = textures[k].list[t].mipmapOffset; if (textures[k].list[t].slave) { MatchedTexture slaveTexture = textures[k].list[t]; string basePkgName = slaveTexture.basePackageName; if (basePkgName == Path.GetFileNameWithoutExtension(slaveTexture.path).ToUpperInvariant()) { throw new Exception(); } bool found = false; for (int j = 0; j < textures[k].list.Count; j++) { if (!textures[k].list[j].slave && textures[k].list[j].mipmapOffset == mipmapOffset && textures[k].list[j].packageName == basePkgName) { slaveTexture.linkToMaster = j; textures[k].list[t] = slaveTexture; found = true; break; } } if (!found) { log += "Error: not able match 'slave' texture: + " + textures[k].name + " to 'master'."; } } } if (!textures[k].list.Exists(s => s.slave) && textures[k].list.Exists(s => s.weakSlave)) { List <MatchedTexture> texList = new List <MatchedTexture>(); for (int t = 0; t < textures[k].list.Count; t++) { MatchedTexture tex = textures[k].list[t]; if (tex.weakSlave) { texList.Add(tex); } else { texList.Insert(0, tex); } } FoundTexture f = textures[k]; f.list = texList; textures[k] = f; if (textures[k].list[0].weakSlave) { continue; } for (int t = 0; t < textures[k].list.Count; t++) { if (textures[k].list[t].weakSlave) { MatchedTexture slaveTexture = textures[k].list[t]; string basePkgName = slaveTexture.basePackageName; if (basePkgName == Path.GetFileNameWithoutExtension(slaveTexture.path).ToUpperInvariant()) { throw new Exception(); } for (int j = 0; j < textures[k].list.Count; j++) { if (!textures[k].list[j].weakSlave && textures[k].list[j].packageName == basePkgName) { slaveTexture.linkToMaster = j; textures[k].list[t] = slaveTexture; break; } } } } } } } using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) { MemoryStream mem = new MemoryStream(); mem.WriteUInt32(TexExplorer.textureMapBinTag); mem.WriteUInt32(TexExplorer.textureMapBinVersion); mem.WriteInt32(textures.Count); for (int i = 0; i < textures.Count; i++) { mem.WriteInt32(textures[i].name.Length); mem.WriteStringASCII(textures[i].name); mem.WriteUInt32(textures[i].crc); if (generateBuiltinMapFiles) { mem.WriteInt32(textures[i].width); mem.WriteInt32(textures[i].height); mem.WriteInt32((int)textures[i].pixfmt); mem.WriteInt32(textures[i].alphadxt1 ? 1 : 0); mem.WriteInt32(textures[i].numMips); } mem.WriteInt32(textures[i].list.Count); for (int k = 0; k < textures[i].list.Count; k++) { mem.WriteInt32(textures[i].list[k].exportID); mem.WriteInt32(textures[i].list[k].linkToMaster); mem.WriteInt32(textures[i].list[k].path.Length); mem.WriteStringASCII(textures[i].list[k].path); } } if (!generateBuiltinMapFiles) { mem.WriteInt32(GameData.packageFiles.Count); for (int i = 0; i < GameData.packageFiles.Count; i++) { string s = GameData.RelativeGameData(GameData.packageFiles[i]); mem.WriteInt32(s.Length); mem.WriteStringASCII(s); } } mem.SeekBegin(); if (generateBuiltinMapFiles) { fs.WriteUInt32(0x504D5443); fs.WriteUInt32((uint)mem.Length); byte[] compressed = new ZlibHelper.Zlib().Compress(mem.ToArray(), 9); fs.WriteUInt32((uint)compressed.Length); fs.WriteFromBuffer(compressed); } else { fs.WriteFromStream(mem, mem.Length); } } if (mainWindow != null) { if (!generateBuiltinMapFiles) { MipMaps mipmaps = new MipMaps(); if (GameData.gameType == MeType.ME1_TYPE) { errors += mipmaps.removeMipMapsME1(1, textures, null, mainWindow, null); errors += mipmaps.removeMipMapsME1(2, textures, null, mainWindow, null); } else { errors += mipmaps.removeMipMapsME2ME3(textures, null, mainWindow, null); } } var time = Misc.stopTimer(); mainWindow.updateStatusLabel("Done. Process total time: " + Misc.getTimerFormat(time)); mainWindow.updateStatusLabel2(""); } treeScan = textures; return(errors); }