private static void GetExtraFiles(OpenFileDialog dialog, Dictionary<string, DatFile2> dict1) { string fileName = Path.GetFileNameWithoutExtension(dialog.FileName); string[] files = Directory.GetFiles(Path.GetDirectoryName(dialog.FileName), fileName + "*.datx"); foreach (string file in files) { DatFile2 df = new DatFile2(file); df.CheckJumpTables(); dict1.Add(file, df); } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (currentFile != null) { currentFile.Dispose(); currentFile = null; } currentFile = new DatFile2(dialog.FileName); currentFile.CheckJumpTables(); map = currentFile.CreateMap(); InitScrollBar(); UpdateImage(); } }
public void HashDats() { backgroundWorker1.ReportProgress(0, "Starting"); Stopwatch watch = new Stopwatch(); watch.Start(); string[] curFiles = GetAllDats(TargetDirectory); Array.Sort(curFiles); SortedDictionary<string, byte[]> sectionHashes = new SortedDictionary<string,byte[]>(); MD5 hasher = MD5.Create(); MemoryStream stream = new MemoryStream(); for (int i=0; i < curFiles.Length; i++) { if (curFiles[i].EndsWith(".dat")) { int j = i + 1; while (j < curFiles.Length && curFiles[j].StartsWith(curFiles[i].Substring(0, curFiles[i].Length - 4))) j++; Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); for (int k = i; k < j; k++) { DatFile2 datFile = new DatFile2(curFiles[k]); datFile.CheckJumpTables(); files.Add(curFiles[k], datFile); } byte[] result = HashDat(hasher, files); sectionHashes.Add(curFiles[i], result); stream.Write(result, 0, result.Length); foreach (DatFile2 df2 in files.Values) df2.Dispose(); backgroundWorker1.ReportProgress((i + 1) * 100 / curFiles.Length, "Progressing: " + (i + 1) * 100 / curFiles.Length + "%"); } } stream.Flush(); byte[] megaHash = hasher.ComputeHash(stream.ToArray()); watch.Stop(); StringBuilder display = new StringBuilder(); display.AppendLine("Total time: " + watch.Elapsed.ToString()); display.AppendLine("AllFileHash: " + Convert.ToBase64String(megaHash)); foreach (KeyValuePair<string, byte[]> subHash in sectionHashes) { display.AppendLine(Path.GetFileName(subHash.Key) + ": " + Convert.ToBase64String(subHash.Value)); } backgroundWorker1.ReportProgress(100, display.ToString()); }
public void HashDats() { backgroundWorker1.ReportProgress(0); using (StreamWriter output = File.CreateText(TargetFile)) { string[] curFiles = GetAllDats(TargetDirectory); Array.Sort(curFiles); MD5 hasher = MD5.Create(); for (int i = 0; i < curFiles.Length; i++) { if (curFiles[i].EndsWith(".dat")) { output.WriteLine(Path.GetFileName(curFiles[i])); int j = i + 1; while (j < curFiles.Length && curFiles[j].StartsWith(curFiles[i].Substring(0, curFiles[i].Length - 4))) j++; Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); for (int k = i; k < j; k++) { DatFile2 datFile = new DatFile2(curFiles[k]); datFile.CheckJumpTables(); files.Add(curFiles[k], datFile); } IEnumerable<KeyValuePair<int, string>> toProcess = MakeBatchFileEnumerator(files); foreach (KeyValuePair<int, string> entry in toProcess.OrderBy(a => a.Key)) { byte[] dataBit = files[entry.Value].GetDataById(entry.Key); byte[] miniHash = hasher.ComputeHash(dataBit); output.WriteLine(entry.Key.ToString() + ":" + dataBit.Length.ToString() + ":" + Convert.ToBase64String(miniHash)); } foreach (DatFile2 df2 in files.Values) df2.Dispose(); backgroundWorker1.ReportProgress((i + 1) * 99 / curFiles.Length); } } output.Flush(); } backgroundWorker1.ReportProgress(100); }
public void HashDats() { backgroundWorker1.ReportProgress(0, "Starting..."); StringBuilder finalDisplay = new StringBuilder(); using (StreamReader input = File.OpenText(TargetFile)) { string compareFileName = input.ReadLine(); if (string.IsNullOrEmpty(compareFileName) || !compareFileName.EndsWith(".dat")) { backgroundWorker1.ReportProgress(100, "Selected file is not a valid template."); return; } string[] curFiles = GetAllDats(TargetDirectory); Array.Sort(curFiles); MD5 hasher = MD5.Create(); for (int i = 0; i < curFiles.Length; i++) { if (curFiles[i].EndsWith(".dat")) { string curFileName = Path.GetFileName(curFiles[i]); while (curFileName.CompareTo(compareFileName) > 0) { finalDisplay.AppendLine(compareFileName + ": Not in install directory"); compareFileName = SkipToNext(input); } if (curFileName.CompareTo(compareFileName) < 0) { finalDisplay.AppendLine(curFileName + ": Not in template"); continue; } int j = i + 1; while (j < curFiles.Length && curFiles[j].StartsWith(curFiles[i].Substring(0, curFiles[i].Length - 4))) j++; Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); for (int k = i; k < j; k++) { DatFile2 datFile = new DatFile2(curFiles[k]); datFile.CheckJumpTables(); files.Add(curFiles[k], datFile); } IEnumerable<KeyValuePair<int, string>> toProcess = MakeBatchFileEnumerator(files); HashSet<int> missingFromInstall = new HashSet<int>(); HashSet<int> missingFromTemplate = new HashSet<int>(); HashSet<int> correct = new HashSet<int>(); HashSet<int> wrongLength = new HashSet<int>(); HashSet<int> wrongHash = new HashSet<int>(); string templateRow = NextLine(input, ref compareFileName); int templateId; int templateLength; byte[] templateHash; ProcessRow(templateRow, out templateId, out templateLength, out templateHash); foreach (KeyValuePair<int, string> entry in toProcess.OrderBy(a => a.Key)) { while (templateId < entry.Key) { missingFromInstall.Add(templateId); templateRow = NextLine(input, ref compareFileName); ProcessRow(templateRow, out templateId, out templateLength, out templateHash); } if (templateId > entry.Key) { missingFromTemplate.Add(entry.Key); continue; } byte[] dataBit = files[entry.Value].GetDataById(entry.Key); if (dataBit.Length != templateLength) { wrongLength.Add(entry.Key); } else { byte[] miniHash = hasher.ComputeHash(dataBit); bool success = true; for (int l = 0; l < miniHash.Length; l++) { if (miniHash[l] != templateHash[l]) { wrongHash.Add(entry.Key); success = false; break; } } if (success) correct.Add(entry.Key); } templateRow = NextLine(input, ref compareFileName); ProcessRow(templateRow, out templateId, out templateLength, out templateHash); } while (templateRow != null) { missingFromInstall.Add(templateId); templateRow = NextLine(input, ref compareFileName); ProcessRow(templateRow, out templateId, out templateLength, out templateHash); } foreach (DatFile2 df2 in files.Values) df2.Dispose(); finalDisplay.AppendLine(string.Format("{0}: {1} correct, {2} extra, {3} missing, {4} wrong length, {5} wrong hash", curFileName, correct.Count, missingFromTemplate.Count, missingFromInstall.Count, wrongLength.Count, wrongHash.Count)); backgroundWorker1.ReportProgress((i + 1) * 100 / curFiles.Length, "Progressing: " + (i + 1) * 100 / curFiles.Length + "%"); } } } backgroundWorker1.ReportProgress(100, finalDisplay.ToString()); }
private void button3_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { OpenFileDialog dialog2 = new OpenFileDialog(); if (dialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK) { DatFile2 file = new DatFile2(dialog.FileName); file.CheckJumpTables(false); DatFile2 file2 = new DatFile2(dialog2.FileName); file2.CheckJumpTables(false); try { #if DEBUG file.CompareDetails(file2); #endif } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }
private void button4_Click(object sender, EventArgs e) { #if DEBUG OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "CSV|*.csv"; if (dialog.ShowDialog() == DialogResult.OK) { Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); try { Dictionary<string, List<int>> ids = new Dictionary<string, List<int>>(); List<string> fullPattern = new List<string>(); using (CSV csv = new CSV(dialog.FileName)) { csv.GetRow(); while (true) { string[] row = csv.GetRow(); if (row == null) break; string path = row[5]; string fileName = Path.GetFileName(path); if (!fileName.StartsWith("client_") || (!fileName.EndsWith(".dat") && !fileName.EndsWith(".datx"))) continue; DatFile2 file; if (!files.TryGetValue(path, out file)) { try { file = new DatFile2(path); files.Add(path, file); file.CheckJumpTables(); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", path, ex.ToString())); return; } ids[fileName] = new List<int>(); } string result = row[7]; long offset; long length; bool hitDisk; if (ParseResult(result, out offset, out length, out hitDisk)) { if (!hitDisk) { int id; if (file.TryMapToId(offset, out id)) { string check = fileName + ":" + id.ToString(); ids[fileName].Add(id); fullPattern.Add(check); } } } } } PatternFinder<string> patterns = new PatternFinder<string>(fullPattern, EqualityComparer<string>.Default); PatternFinder<string>.PatternTree tree = patterns.FindPatterns(); } finally { foreach (DatFile2 file in files.Values) { file.Dispose(); } } } Viewer view = new Viewer(); view.Show(this); #endif }
private void button13_Click(object sender, EventArgs e) { #if DEBUG OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "DAT Files|client_*.dat|DAT Files|client_*.dat.orig|DATX Files|client_*.datx"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { OpenFileDialog dialog2 = new OpenFileDialog(); dialog2.Filter = "DAT Files|client_*.dat|DAT Files|client_*.dat.orig|DATX Files|client_*.datx"; if (dialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (DatFile2 file1 = new DatFile2(dialog.FileName)) using (DatFile2 file2 = new DatFile2(dialog2.FileName)) { file1.CheckJumpTables(); file2.CheckJumpTables(); Dictionary<string, DatFile2> dict1 = new Dictionary<string, DatFile2>(); dict1.Add(dialog.FileName, file1); GetExtraFiles(dialog, dict1); Dictionary<string, DatFile2> dict2 = new Dictionary<string, DatFile2>(); dict2.Add(dialog2.FileName, file2); GetExtraFiles(dialog2, dict2); IEnumerable<KeyValuePair<int, string>> enum1 = MakeBatchFileEnumerator(dict1); IEnumerable<KeyValuePair<int, string>> enum2 = MakeBatchFileEnumerator(dict2); HashSet<int> set1 = new HashSet<int>(enum1.Select(b=>b.Key)); if (set1.SetEquals(enum2.Select(b=>b.Key))) { Dictionary<int, string> lookup = new Dictionary<int, string>(); foreach (KeyValuePair<int, string> kvp in enum2) { if (lookup.ContainsKey(kvp.Key)) MessageBox.Show(kvp.Key.ToString()); else lookup.Add(kvp.Key, kvp.Value); } foreach (KeyValuePair<int, string> kvp in enum1) { byte[] data1 = dict1[kvp.Value].GetDataById(kvp.Key); byte[] data2 = dict2[lookup[kvp.Key]].GetDataById(kvp.Key); if (data1.Length != data2.Length) { MessageBox.Show("Different data lengths."); return; } else { for (int i=0; i < data1.Length; i++) if (data1[i] != data2[i]) { MessageBox.Show("Different data contents."); return; } } } } else { MessageBox.Show("Different id list."); } MessageBox.Show(Convert.ToBase64String(HashDat(MD5.Create(), dict1)) + "\n\r" + Convert.ToBase64String(HashDat(MD5.Create(), dict2))); } } } return; #endif if (!Directory.Exists(textBox1.Text)) { MessageBox.Show("Must specify an install location."); return; } string[] curFiles = GetAllDats(textBox1.Text); Array.Sort(curFiles); SortedDictionary<string, byte[]> sectionHashes = new SortedDictionary<string,byte[]>(); MD5 hasher = MD5.Create(); MemoryStream stream = new MemoryStream(); for (int i=0; i < curFiles.Length; i++) { if (curFiles[i].EndsWith(".dat")) { int j = i + 1; while (j < curFiles.Length && curFiles[j].StartsWith(curFiles[i].Substring(0, curFiles[i].Length - 4))) j++; Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); for (int k = i; k < j; k++) { DatFile2 datFile = new DatFile2(curFiles[k]); datFile.CheckJumpTables(); files.Add(curFiles[k], datFile); } byte[] result = HashDat(hasher, files); sectionHashes.Add(curFiles[i], result); stream.Write(result, 0, result.Length); foreach (DatFile2 df2 in files.Values) df2.Dispose(); } } stream.Flush(); byte[] megaHash = hasher.ComputeHash(stream.ToArray()); StringBuilder display = new StringBuilder(); display.AppendLine("AllFileHash: " + Convert.ToBase64String(megaHash)); foreach (KeyValuePair<string, byte[]> subHash in sectionHashes) { display.AppendLine(Path.GetFileName(subHash.Key) + ": " + Convert.ToBase64String(subHash.Value)); } MessageBox.Show(display.ToString()); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { KeyValuePair<string, int> arg = (KeyValuePair<string, int>)e.Argument; int option = arg.Value; string inputFileName = arg.Key; if (option == 1) { Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); try { Dictionary<string, List<int>> ids = new Dictionary<string, List<int>>(); Dictionary<string, bool> seen = new Dictionary<string, bool>(); using (CSV csv = new CSV(inputFileName)) { csv.GetRow(); while (true) { string[] row = csv.GetRow(); if (row == null) break; string path = row[5]; string fileName = Path.GetFileName(path); if (!fileName.StartsWith("client_") || (!fileName.EndsWith(".dat") && !fileName.EndsWith(".datx"))) continue; string collapsedFile = Collapse(fileName); DatFile2 file; if (!files.TryGetValue(path, out file)) { try { file = new DatFile2(path); files.Add(path, file); file.CheckJumpTables(); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", path, ex.ToString())); return; } ids[collapsedFile] = new List<int>(); } string result = row[7]; long offset; long length; bool hitDisk; if (ParseResult(result, out offset, out length, out hitDisk)) { if (!hitDisk) { int id; if (file.TryMapToId(offset, out id)) { string check = fileName + ":" + id.ToString(); if (!seen.ContainsKey(check)) { ids[collapsedFile].Add(id); seen.Add(check, true); } } } } } e.Result = ids; } } catch { MessageBox.Show("An unexpected error occured while creating a profile."); return; } finally { foreach (DatFile2 file in files.Values) { file.Dispose(); } } } if (option == 2) { Dictionary<string, DatFile2> files = new Dictionary<string, DatFile2>(); Dictionary<string, DatFile2.DatFileRewriter> writers = new Dictionary<string, DatFile2.DatFileRewriter>(); bool abort = false; try { string[] args = inputFileName.Split('|'); string basePath = args[0]; for (int i = 1; i < args.Length; i++) { string datProfileFile = args[i]; using (StreamReader reader = File.OpenText(datProfileFile)) { // Skip the description section. string version = reader.ReadLine(); if (string.IsNullOrEmpty(version) || version != "1") { MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile)); abort = true; return; } string description = reader.ReadLine(); if (description == null) { MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile)); abort = true; return; } string graphicsLevel = reader.ReadLine(); if (graphicsLevel == null) { MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile)); abort = true; return; } string currentFile = null; while (true) { string next = reader.ReadLine(); if (string.IsNullOrEmpty(next)) break; if (char.IsLetter(next[0])) { currentFile = next; continue; } if (currentFile == null) { MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile)); abort = true; return; } int value; if (!int.TryParse(next, out value)) { MessageBox.Show(string.Format("{0} is not a valid profile.", datProfileFile)); abort = true; return; } int level = 0; while (true) { string fullPath = Path.Combine(basePath, currentFile); if (level > 0) { fullPath = Path.Combine(basePath, Path.GetFileNameWithoutExtension(fullPath) + "_aux_" + level + ".datx"); } if (!File.Exists(fullPath)) break; DatFile2 file; if (!files.TryGetValue(fullPath, out file)) { try { file = new DatFile2(fullPath); files.Add(fullPath, file); file.CheckJumpTables(); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fullPath, ex.ToString())); abort = true; return; } } DatFile2.DatFileRewriter datWriter; if (!writers.TryGetValue(fullPath, out datWriter)) { try { datWriter = file.GetRewriter(fullPath + ".optimized"); writers.Add(fullPath, datWriter); } catch (Exception ex) { MessageBox.Show(string.Format("Error creating optimized output file {0}, Exception: {1}", fullPath + ".optimized", ex.ToString())); abort = true; return; } } try { if (datWriter.ForceById(value)) break; } catch (Exception ex) { MessageBox.Show(string.Format("Error writing to optimized output file {0}, Exception: {1}", datWriter.Filename, ex.ToString())); abort = true; return; } level++; } } string[] alldats = GetAllDats(basePath); foreach (string fullPath in alldats) { DatFile2 file; if (!files.TryGetValue(fullPath, out file)) { try { file = new DatFile2(fullPath); files.Add(fullPath, file); file.CheckJumpTables(); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fullPath, ex.ToString())); abort = true; return; } } DatFile2.DatFileRewriter datWriter; if (!writers.TryGetValue(fullPath, out datWriter)) { try { datWriter = file.GetRewriter(fullPath + ".optimized"); writers.Add(fullPath, datWriter); } catch (Exception ex) { MessageBox.Show(string.Format("Error creating optimized output file {0}, Exception: {1}", fullPath + ".optimized", ex.ToString())); abort = true; return; } } } } } foreach (DatFile2.DatFileRewriter rewriter in writers.Values) { try { rewriter.Finish(); } catch (Exception ex) { MessageBox.Show(string.Format("Error writing to optimized output file {0}, Exception: {1}", rewriter.Filename, ex.ToString())); abort = true; return; } } } catch { MessageBox.Show("Unexpected error while optimizing."); abort = true; return; } finally { foreach (DatFile2.DatFileRewriter rewriter in writers.Values) { try { if (abort) rewriter.Cancel(); else rewriter.CancelIfNotFinished(); } catch { } } foreach (DatFile2 file in files.Values) { file.Dispose(); } } } if (option == 3) { foreach (string fileName in inputFileName.Split('|')) { using (DatFile2 file = new DatFile2(fileName)) { try { file.CheckJumpTables(); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fileName, ex.ToString())); return; } using (DatFile2.DatFileRewriter writer = file.GetRewriter(fileName + ".optimized")) { } } } } if (option == 4) { foreach (string fileName in inputFileName.Split('|')) { File.Copy(fileName, fileName + ".compress"); using (DatFile2 file = new DatFile2(fileName + ".compress")) { try { file.CheckJumpTables(false); } catch (Exception ex) { MessageBox.Show(string.Format("Error processing {0}, Exception: {1}", fileName, ex.ToString())); return; } file.DebugFileContent(); } } } }