public int LoadExternal(string file, bool isDef) { EnableSecondProgressBar(false); TPFTexInfo tmpTex = new TPFTexInfo(Path.GetFileName(file), -1, Path.GetDirectoryName(file), null, WhichGame); // KFreon: Get hash if (!isDef) { string hash = ""; // KFreon: Check if hash in filename // Heff: fix weird uppercase X file = Path.GetFileName(file).Replace("0X", "0x"); if (file.Contains("0x")) hash = file.Substring(file.IndexOf("0x"), 10); else // KFreon: If not in filename, look in all non TPF .defs foreach (TPFTexInfo tex in LoadedTexes) { if (tex.isDef && tex.isExternal) { using (StreamReader sr = new StreamReader(Path.Combine(tex.FilePath, tex.FileName))) while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line.Contains(file + '|')) { int start = line.IndexOf('|'); hash = line.Substring(start + 1, line.Length - (start + 1)); break; } } if (hash != "") break; } } // KFreon: Convert hash to uint if (hash != "") tmpTex.Hash = KFreonLib.Textures.Methods.FormatTexmodHashAsUint(hash); tmpTex.OriginalHash = tmpTex.Hash; } // KFreon: Get details if (!tmpTex.isDef) tmpTex.EnumerateDetails(); // KFreon: Add node and its index to current node myTreeNode temp = new myTreeNode(Path.GetFileName(file)); temp.TexInd = LoadedTexes.Count; // Heff: cancellation check if (!cts.IsCancellationRequested) { this.Invoke(new Action(() => { MainTreeView.Nodes.Add(temp); OverallProg.IncrementBar(); })); } LoadedTexes.Add(tmpTex); return temp.TexInd; }
private bool AutofixInternal(TPFTexInfo tex) { bool retval = false; TPFTexInfo backup = tex.Clone(); string path = tex.Autofixedpath(TemporaryPath); Directory.CreateDirectory(Path.GetDirectoryName(path)); byte[] imgData = tex.Extract(Path.GetDirectoryName(path), true); using (ImageEngineImage img = new ImageEngineImage(imgData)) { var destFormat = tex.ExpectedFormat; img.Resize(UsefulThings.General.RoundToNearestPowerOfTwo(img.Width), false); retval = img.Save(path, destFormat, tex.ExpectedMips > 1 ? MipHandling.Default : MipHandling.KeepTopOnly); } if (!retval) { tex.AutofixSuccess = false; return false; } tex.FilePath = Path.GetDirectoryName(tex.Autofixedpath(TemporaryPath)); tex.FileName = Path.GetFileName(path); // Heff: Cancellation check if (cts.IsCancellationRequested) return false; tex.EnumerateDetails(); // Heff: if fix was successfull, but the number of mips are still wrong, // force it and let texplorer skip the lowest resolutions // Heff: this should no longer happen, but keeping this as it might help in some real odd case. if (tex.ExpectedMips > 1 && (tex.NumMips < tex.ExpectedMips || tex.NumMips < TPFTexInfo.CalculateMipCount(tex.Width, tex.Height))) tex.NumMips = Math.Max(tex.ExpectedMips, TPFTexInfo.CalculateMipCount(tex.Width, tex.Height)); if (!tex.Valid) { tex = backup; tex.AutofixSuccess = false; } else tex.AutofixSuccess = true; return tex.AutofixSuccess; }
private void LoadTPF(string file) { EnableSecondProgressBar(true); // KFreon: Open TPF and set some properties SaltTPF.ZipReader zippy = new SaltTPF.ZipReader(file); zippy.Description = "TPF Details\n\nFilename: \n" + zippy._filename + "\n\nComment: \n" + zippy.EOFStrct.Comment + "\nNumber of stored files: " + zippy.Entries.Count; zippy.Scanned = false; int zippyInd = zippys.Count; zippys.Add(zippy); int numEntries = zippy.Entries.Count; // KFreon: Setup nodes and GUI elements this.Invoke(new Action(() => { CurrentProg.ChangeProgressBar(0, numEntries); CurrentStatusLabel.Text = "Processing file: " + Path.GetFileName(file); })); DebugOutput.PrintLn("Loading file: " + Path.GetFileName(file)); // KFreon: Get hash info from TPF // KFreon: Get individual hashes without duplicate lines // Heff: Fix weird uppercase X List<string> parts = GetHashesFromTPF(zippy); if (parts == null) return; // KFreon: Thread TPF loading ParallelOptions po = new ParallelOptions(); po.MaxDegreeOfParallelism = Properties.Settings.Default.NumThreads; DebugOutput.PrintLn("Reading TPF using " + po.MaxDegreeOfParallelism + " threads."); List<TPFTexInfo> temptexes = new List<TPFTexInfo>(); for (int i = 0; i < numEntries; i++) { temptexes.Add(new TPFTexInfo()); } Parallel.For(0, numEntries, po, i => { // KFreon: Add TPF entries to TotalTexes list TPFTexInfo tmpTex = new TPFTexInfo(zippy.Entries[i].Filename, i, null, zippy, WhichGame); // KFreon: Find and set hash foreach (string line in parts) if (line.ToLowerInvariant().Contains(tmpTex.FileName.ToLowerInvariant())) { tmpTex.Hash = KFreonLib.Textures.Methods.FormatTexmodHashAsUint(line); tmpTex.OriginalHash = tmpTex.Hash; tmpTex.FileName = line.Split('|')[1].Replace("\r", ""); break; } // KFreon: If hash gen failed, notify if (!tmpTex.isDef && tmpTex.Hash == 0) DebugOutput.PrintLn("Failure to get hash for entry " + i + " in " + file); // KFreon: Get details if (!tmpTex.isDef) tmpTex.EnumerateDetails(); temptexes[i] = tmpTex; // Heff: cancel background loaders if (cts.IsCancellationRequested) return; CurrentProg.IncrementBar(); }); // KFreon: Load textures into list and treeview LoadedTexes.AddRange(temptexes); EnableSecondProgressBar(false); }
private void LoadTPF(string file) { EnableSecondProgressBar(true); // KFreon: Open TPF and set some properties SaltTPF.ZipReader zippy = new SaltTPF.ZipReader(file); zippy.Description = "TPF Details\n\nFilename: \n" + zippy._filename + "\n\nComment: \n" + zippy.EOFStrct.Comment + "\nNumber of stored files: " + zippy.Entries.Count; zippy.Scanned = false; int zippyInd = zippys.Count; zippys.Add(zippy); int numEntries = zippy.Entries.Count; // KFreon: Setup nodes and GUI elements this.Invoke(new Action(() => { CurrentProg.ChangeProgressBar(0, numEntries); CurrentStatusLabel.Text = "Processing file: " + Path.GetFileName(file); })); DebugOutput.PrintLn("Loading file: " + Path.GetFileName(file)); // KFreon: Get hash info from TPF string alltext = ""; try { byte[] data = zippy.Entries[numEntries - 1].Extract(true); char[] chars = new char[data.Length]; for (int i = 0; i < data.Length; i++) chars[i] = (char)data[i]; alltext = new string(chars); } catch (Exception e) { MessageBox.Show("An error occurred during extraction: " + e.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // KFreon: Get individual hashes without duplicate lines List<string> parts = alltext.Replace("\r", "").Split('\n').ToList(); parts.RemoveAll(s => s == "\0"); List<string> tempparts = new List<string>(); foreach (string part in parts) if (!tempparts.Contains(part)) tempparts.Add(part); parts = tempparts; // KFreon: Thread TPF loading ParallelOptions po = new ParallelOptions(); po.MaxDegreeOfParallelism = 1;//Properties.Settings.Default.NumThreads; DebugOutput.PrintLn("Reading TPF using " + po.MaxDegreeOfParallelism + " threads."); List<TPFTexInfo> temptexes = new List<TPFTexInfo>(); for (int i = 0; i < numEntries; i++) { temptexes.Add(new TPFTexInfo()); } Parallel.For(0, numEntries, po, i => { // KFreon: Add TPF entries to TotalTexes list TPFTexInfo tmpTex = new TPFTexInfo(zippy.Entries[i].Filename, i, null, zippy, WhichGame); // KFreon: Find and set hash foreach (string line in parts) if (line.ToLowerInvariant().Contains(tmpTex.FileName.ToLowerInvariant())) { tmpTex.Hash = KFreonLib.Textures.Methods.FormatTexmodHashAsUint(line); tmpTex.OriginalHash = tmpTex.Hash; tmpTex.FileName = line.Split('|')[1].Replace("\r", ""); break; } // KFreon: If hash gen failed, notify if (!tmpTex.isDef && tmpTex.Hash == 0) DebugOutput.PrintLn("Failure to get hash for entry " + i + " in " + file); // KFreon: Get details if (!tmpTex.isDef) tmpTex.EnumerateDetails(); temptexes[i] = tmpTex; CurrentProg.IncrementBar(); }); // KFreon: Load textures into list and treeview LoadedTexes.AddRange(temptexes); EnableSecondProgressBar(false); }