public static LMTEntry InsertLMTEntry(TreeView tree, ArcEntryWrapper node, string filename, Type filetype = null) { LMTEntry lmtentry = new LMTEntry(); try { using (BinaryReader bnr = new BinaryReader(File.OpenRead(filename))) { //We build the lmtentry starting from the uncompressed data. lmtentry.UncompressedData = System.IO.File.ReadAllBytes(filename); lmtentry.DecompressedFileLength = lmtentry.UncompressedData.Length; lmtentry._DecompressedFileLength = lmtentry.UncompressedData.Length; lmtentry.DSize = lmtentry.UncompressedData.Length; //Then Compress. lmtentry.CompressedData = Zlibber.Compressor(lmtentry.UncompressedData); lmtentry.CompressedFileLength = lmtentry.CompressedData.Length; lmtentry._CompressedFileLength = lmtentry.CompressedData.Length; lmtentry.CSize = lmtentry.CompressedData.Length; //Gets the filename of the file to inject without the directory. string trname = filename; while (trname.Contains("\\")) { trname = trname.Substring(trname.IndexOf("\\") + 1); } lmtentry.TrueName = trname; lmtentry._FileName = lmtentry.TrueName; lmtentry.TrueName = Path.GetFileNameWithoutExtension(trname); lmtentry.FileExt = trname.Substring(trname.LastIndexOf(".")); lmtentry._FileType = lmtentry.FileExt; lmtentry.LstM3A = new List <LMTM3AEntry>(); lmtentry.OffsetList = new List <int>(); bnr.BaseStream.Position = 6; lmtentry.Version = bnr.ReadInt16(); lmtentry.EntryCount = lmtentry.Version; int count = 0; int SecondaryCount = 0; //Gets all the offsets. ALL OF THEM. while (count < (lmtentry.Version)) { lmtentry.OffsetList.Add(bnr.ReadInt32()); bnr.BaseStream.Position = bnr.BaseStream.Position + 4; count++; } count = 0; //Goes through the offsets to get the data. Ignores offsets of 0. for (int i = 0; i < lmtentry.OffsetList.Count; i++) { if (lmtentry.OffsetList[i] != 0) { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillM3AProprties(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } else { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillBlankM3A(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } } //Gets the path of the selected node to inject here. string nodepath = tree.SelectedNode.FullPath; nodepath = nodepath.Substring(nodepath.IndexOf("\\") + 1); string[] sepstr = { "\\" }; lmtentry.EntryDirs = nodepath.Split(sepstr, StringSplitOptions.RemoveEmptyEntries); lmtentry.EntryName = lmtentry.FileName; //Looks through the archive_filetypes.cfg file to find the typehash associated with the extension. try { using (var sr2 = new StreamReader("archive_filetypes.cfg")) { while (!sr2.EndOfStream) { var keyword = Console.ReadLine() ?? lmtentry.FileExt; var line = sr2.ReadLine(); if (String.IsNullOrEmpty(line)) { continue; } if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0) { lmtentry.TypeHash = line; lmtentry.TypeHash = lmtentry.TypeHash.Split(' ')[0]; break; } } } } catch (FileNotFoundException) { MessageBox.Show("I cannot find and/or access archive_filetypes.cfg so I cannot finish parsing the arc.", "Oh Boy"); using (StreamWriter sw = File.AppendText("Log.txt")) { sw.WriteLine("Cannot find archive_filetypes.cfg so I cannot continue parsing the file."); } } } } catch (Exception ex) { using (StreamWriter sw = File.AppendText("Log.txt")) { sw.WriteLine("Caught an exception using the BinaryReader. Here's the details:\n" + ex); } } return(lmtentry); }
public static LMTEntry ReplaceLMTEntry(TreeView tree, ArcEntryWrapper node, ArcEntryWrapper OldNode, string filename, Type filetype = null) { LMTEntry lmtentry = new LMTEntry(); LMTEntry oldentry = new LMTEntry(); oldentry = OldNode.Tag as LMTEntry; tree.BeginUpdate(); try { using (BinaryReader bnr = new BinaryReader(File.OpenRead(filename))) { //We build the lmtentry starting from the uncompressed data. lmtentry.UncompressedData = System.IO.File.ReadAllBytes(filename); lmtentry.DecompressedFileLength = lmtentry.UncompressedData.Length; lmtentry._DecompressedFileLength = lmtentry.UncompressedData.Length; //Then Compress. lmtentry.CompressedData = Zlibber.Compressor(lmtentry.UncompressedData); lmtentry.CompressedFileLength = lmtentry.CompressedData.Length; lmtentry._CompressedFileLength = lmtentry.CompressedData.Length; //Gets the filename of the file to inject without the directory. string trname = filename; while (trname.Contains("\\")) { trname = trname.Substring(trname.IndexOf("\\") + 1); } //Enters name related parameters of the lmtentry. lmtentry.TrueName = trname; lmtentry._FileName = lmtentry.TrueName; lmtentry.TrueName = Path.GetFileNameWithoutExtension(trname); lmtentry.FileExt = trname.Substring(trname.LastIndexOf(".")); lmtentry._FileType = lmtentry.FileExt; string TypeHash = ""; //Looks through the archive_filetypes.cfg file to find the typehash associated with the extension. try { using (var sr2 = new StreamReader("archive_filetypes.cfg")) { while (!sr2.EndOfStream) { var keyword = Console.ReadLine() ?? lmtentry.FileExt; var line = sr2.ReadLine(); if (String.IsNullOrEmpty(line)) { continue; } if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0) { TypeHash = line; TypeHash = TypeHash.Split(' ')[0]; lmtentry.TypeHash = TypeHash; break; } } } } catch (FileNotFoundException) { MessageBox.Show("I cannot find and/or access archive_filetypes.cfg so I cannot finish parsing the arc.", "Oh Boy"); using (StreamWriter sw = File.AppendText("Log.txt")) { sw.WriteLine("Cannot find archive_filetypes.cfg so I cannot continue parsing the file."); } return(null); } int count = 0; int SecondaryCount = 0; using (MemoryStream msm3a = new MemoryStream(lmtentry.UncompressedData)) { using (BinaryReader brm3a = new BinaryReader(msm3a)) { bnr.BaseStream.Position = 6; lmtentry.Version = bnr.ReadInt16(); lmtentry.EntryCount = lmtentry.Version; lmtentry.OffsetList = new List <int>(); lmtentry.LstM3A = new List <LMTM3AEntry>(); //Gets all the offsets. ALL OF THEM. while (count < (lmtentry.Version)) { lmtentry.OffsetList.Add(bnr.ReadInt32()); bnr.BaseStream.Position = bnr.BaseStream.Position + 4; count++; } count = 0; //Goes through the offsets to get the data. Ignores offsets of 0. for (int i = 0; i < lmtentry.OffsetList.Count; i++) { if (lmtentry.OffsetList[i] != 0) { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillM3AProprties(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } else { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillBlankM3A(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } } } } lmtentry.TrueName = oldentry.TrueName; lmtentry._FileName = oldentry._FileName; lmtentry.EntryName = oldentry.EntryName; var tag = node.Tag; if (tag is LMTEntry) { oldentry = tag as LMTEntry; } string path = ""; int index = oldentry.EntryName.LastIndexOf("\\"); if (index > 0) { path = oldentry.EntryName.Substring(0, index); } lmtentry.EntryName = path + "\\" + lmtentry.TrueName; tag = lmtentry; if (node.Tag is LMTEntry) { node.Tag = lmtentry; node.Name = Path.GetFileNameWithoutExtension(lmtentry.EntryName); node.Text = Path.GetFileNameWithoutExtension(lmtentry.EntryName); } var aew = node as ArcEntryWrapper; string type = node.GetType().ToString(); if (type == "ThreeWorkTool.Resources.Wrappers.ArcEntryWrapper") { aew.entryfile = lmtentry; } node = aew; node.entryfile = lmtentry; /* * //ArcEntryWrapper aew = new ArcEntryWrapper(); * if (node is ArcEntryWrapper) * { * node.entryfile as ArcEntryWrapper = node.Tag; * } */ tree.EndUpdate(); } } catch (Exception ex) { MessageBox.Show("Read error. Is the file readable?"); using (StreamWriter sw = File.AppendText("Log.txt")) { sw.WriteLine("Read Error! Here's the exception info:\n" + ex); } } return(node.entryfile as LMTEntry); }
public static LMTEntry FillLMTEntry(string filename, List <string> subnames, TreeView tree, BinaryReader br, int c, int ID, Type filetype = null) { LMTEntry lmtentry = new LMTEntry(); List <byte> BTemp = new List <byte>(); FillEntry(filename, subnames, tree, br, c, ID, lmtentry, filetype); lmtentry._FileName = lmtentry.TrueName + lmtentry.FileExt; lmtentry._FileType = lmtentry.FileExt; //Decompression Time. lmtentry.UncompressedData = ZlibStream.UncompressBuffer(lmtentry.CompressedData); lmtentry._DecompressedFileLength = lmtentry.UncompressedData.Length; lmtentry._CompressedFileLength = lmtentry.CompressedData.Length; int count = 0; byte[] STemp = new byte[2]; byte[] OTemp = new byte[4]; lmtentry.LstM3A = new List <LMTM3AEntry>(); lmtentry.OffsetList = new List <int>(); int SecondaryCount = 0; using (MemoryStream LmtStream = new MemoryStream(lmtentry.UncompressedData)) { using (BinaryReader bnr = new BinaryReader(LmtStream)) { bnr.BaseStream.Position = 6; lmtentry.Version = bnr.ReadInt16(); lmtentry.EntryCount = lmtentry.Version; //Gets all the offsets. ALL OF THEM. while (count < (lmtentry.Version)) { lmtentry.OffsetList.Add(bnr.ReadInt32()); bnr.BaseStream.Position = bnr.BaseStream.Position + 4; count++; } count = 0; //Goes through the offsets to get the data. Ignores offsets of 0. for (int i = 0; i < lmtentry.OffsetList.Count; i++) { if (lmtentry.OffsetList[i] != 0) { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillM3AProprties(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } else { LMTM3AEntry aEntry = new LMTM3AEntry(); aEntry = aEntry.FillBlankM3A(aEntry, lmtentry.Length, i, lmtentry.RowCount, lmtentry.SecondOffsetList, bnr, SecondaryCount, lmtentry); lmtentry.LstM3A.Add(aEntry); } } } return(lmtentry); } }