Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace NativeTableMerger
- {
- class Program
- {
- static Dictionary<int, List<ulong>> ParseDump(string filename)
- {
- var result = new Dictionary<int, List<ulong>>();
- var raw = File.ReadAllLines(filename);
- for (int i = 0; i < raw.Length; i++)
- {
- var str = raw[i];
- if (!str.StartsWith("["))
- continue;
- var namespaceIndex = int.Parse(str.Replace("[", string.Empty).Replace("]:", string.Empty));
- var natives = new List<ulong>();
- while (true)
- {
- i++;
- if (i >= raw.Length)
- break;
- str = raw[i];
- if (str.StartsWith("["))
- {
- i--;
- break;
- }
- var hash = ulong.Parse(str.Replace("0x", string.Empty), System.Globalization.NumberStyles.HexNumber);
- natives.Add(hash);
- }
- result.Add(namespaceIndex, natives);
- }
- return result;
- }
- static ulong GetMappedHash(ulong hash, Dictionary<ulong, ulong> table, int index)
- {
- if (table.TryGetValue(hash, out var result))
- return result;
- Console.WriteLine($"[{index}]Cannot find translation from 0x{hash:X16} searching in files...");
- string line;
- bool found = false;
- var files = Directory.EnumerateFiles("old", "*.ysc.c", SearchOption.AllDirectories);
- foreach (var file in files)
- {
- var lineNumber = 0;
- using (var reader = File.OpenText(file))
- {
- while ((line = reader.ReadLine()) != null)
- {
- lineNumber++;
- if (!line.Contains($"unk_0x{hash:X16}"))
- continue;
- Console.WriteLine($"Found hash 0x{hash:X16} in file {file}:{lineNumber}");
- {
- var process = new Process();
- var startInfo = new ProcessStartInfo
- {
- FileName = "cmd.exe",
- Verb = "runas",
- Arguments = $"/c notepad++ \"{Path.GetFullPath(file)}\" -n{lineNumber}",
- UseShellExecute = true
- };
- process.StartInfo = startInfo;
- process.Start();
- }
- {
- var process = new Process();
- var startInfo = new ProcessStartInfo
- {
- FileName = "cmd.exe",
- Verb = "runas",
- Arguments = $"/c notepad++ \"{Path.GetFullPath(file.Replace("old", "new"))}\"",
- UseShellExecute = true
- };
- process.StartInfo = startInfo;
- process.Start();
- }
- found = true;
- break;
- }
- }
- if (found)
- break;
- }
- Console.WriteLine("Enter translation:");
- ulong newHash = hash;
- bool skipped = false;
- while (true)
- {
- var str = Console.ReadLine().Replace("0x", string.Empty);
- if (string.IsNullOrEmpty(str))
- {
- skipped = true;
- break;
- }
- while (!ulong.TryParse(str, System.Globalization.NumberStyles.HexNumber, null, out newHash))
- {
- Console.WriteLine("You've entered wrong data!");
- }
- break;
- }
- if (skipped && newHash == hash)
- return hash;
- Console.WriteLine($"Applied new translation 0x{hash:X16} -> 0x{newHash:X16}");
- table[hash] = newHash;
- SaveTo(table, "cache_table.h");
- return newHash;
- }
- static void FixDumps()
- {
- const string oldfile = "natives1493.log";
- const string newfile = "natives1604.log";
- if (!File.Exists("fixed_" + newfile))
- File.Copy(newfile, "fixed_" + newfile);
- var table = new HeaderTableParser(File.ReadAllLines("cache_table.h")).GetTable();
- var oldDump = ParseDump(oldfile);
- var newDump = ParseDump("fixed_" + newfile);
- foreach (var kvp in oldDump)
- {
- if (kvp.Value.Count == newDump[kvp.Key].Count)
- {
- Console.WriteLine($"Namespaces {kvp.Key} are equal! Natives: {kvp.Value.Count}");
- continue;
- }
- Console.WriteLine($"Namespaces {kvp.Key} aren't equal! The difference is {newDump[kvp.Key].Count - kvp.Value.Count} natives! Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count} Analyzing the problem...");
- for (int i = 0; i < kvp.Value.Count; i += (kvp.Value.Count - i - 10 > 0 ? 10 : 1))
- {
- var mappedHash = GetMappedHash(kvp.Value[i], table, i);
- var hashIndex = newDump[kvp.Key].IndexOf(mappedHash);
- if (hashIndex == i)
- {
- Console.WriteLine($"{i} [GOOD] 0x{kvp.Value[i]:X16} -> 0x{mappedHash:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
- if (kvp.Value.Count - i < 100)
- continue;
- var ti = i + 100;
- if (kvp.Value.Count > ti && newDump[kvp.Key].IndexOf(GetMappedHash(kvp.Value[ti], table, ti)) == ti)
- {
- i = ti;
- }
- continue;
- }
- if (newDump[kvp.Key].IndexOf(mappedHash) < i)
- {
- Console.WriteLine($"!!!DANGER!!! {newDump[kvp.Key].IndexOf(mappedHash)} < {i}");
- }
- for (int j = i - Math.Min(10, kvp.Value.Count - i); j <= i; j++)
- {
- var mappedHashTemp = GetMappedHash(kvp.Value[j], table, j);
- if (newDump[kvp.Key].IndexOf(mappedHashTemp) == j)
- {
- Console.WriteLine($"{j} [GOOD] 0x{kvp.Value[j]:X16} -> 0x{mappedHashTemp:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
- continue;
- }
- Console.WriteLine($"{j} [BAD] 0x{kvp.Value[j]:X16} -> 0x{mappedHashTemp:X16} trying to find... Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
- while (GetMappedHash(kvp.Value[j], table, j) != newDump[kvp.Key][j])
- {
- Console.WriteLine($"Removing 0x{newDump[kvp.Key][j]:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
- Console.ReadLine();
- newDump[kvp.Key].RemoveAt(j);
- SaveTo(newDump, "fixed_" + newfile);
- }
- if (newDump[kvp.Key].IndexOf(mappedHash) == i)
- {
- Console.WriteLine($"{j} [FIXED] 0x{kvp.Value[j]:X16} -> 0x{GetMappedHash(kvp.Value[j], table, j):X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
- SaveTo(newDump, "fixed_" + newfile);
- break;
- }
- }
- if (kvp.Value.Count == newDump[kvp.Key].Count)
- break;
- i -= Math.Min(10, kvp.Value.Count - i);
- }
- if (kvp.Value.Count != newDump[kvp.Key].Count)
- if (newDump[kvp.Key][kvp.Value.Count - 1] == GetMappedHash(kvp.Value.Last(), table, kvp.Value.Count - 1))
- {
- newDump[kvp.Key].RemoveRange(kvp.Value.Count, newDump[kvp.Key].Count - kvp.Value.Count);
- }
- else
- throw new Exception("WTF?");
- SaveTo(newDump, "fixed_" + newfile);
- }
- Console.WriteLine("Dumps are equal! Dobby is free now! Congratz!");
- Console.ReadLine();
- }
- static void Main(string[] args)
- {
- const string oldfile = "natives1493.log";
- const string newfile = "natives1604.log";
- FixDumps();
- var oldDump = ParseDump(oldfile);
- var newDump = ParseDump("fixed_" + newfile);
- var table = new Dictionary<ulong, ulong>();
- foreach (var @namespace in oldDump)
- {
- for (int i = 0; i < @namespace.Value.Count; i++)
- {
- table[@namespace.Value[i]] = newDump[@namespace.Key][i];
- }
- }
- SaveTo(table, "1493to1604.h");
- }
- static void SaveTo(Dictionary<ulong, ulong> table, string filename)
- {
- if (File.Exists(filename))
- File.Delete(filename);
- var sb = new StringBuilder();
- foreach (var item in table)
- sb.AppendLine("{0x" + item.Key.ToString("X16") + ",0x" + item.Value.ToString("X16") + "},");
- File.WriteAllText(filename, sb.ToString());
- }
- static void SaveTo(Dictionary<int, List<ulong>> dump, string filename)
- {
- var sb = new StringBuilder();
- foreach (var @namespace in dump)
- {
- sb.AppendLine($"[{@namespace.Key}]:");
- foreach (var native in @namespace.Value)
- {
- sb.AppendLine($"0x{native:X16}");
- }
- }
- File.WriteAllText(filename, sb.ToString());
- }
- }
- internal class HeaderTableParser
- {
- private Dictionary<ulong, ulong> Table { get; }
- public HeaderTableParser(string[] header)
- {
- Table = new Dictionary<ulong, ulong>();
- foreach (var line in header)
- {
- var raw = line.Replace(" ", string.Empty).Replace("{", string.Empty).Replace("}", string.Empty).TrimEnd(',')
- .Split(',');
- if (raw.Length != 2)
- continue;
- if (raw[0].StartsWith("//"))
- continue;
- Table.Add(Convert.ToUInt64(raw[0].Replace("0x", string.Empty), 16),
- Convert.ToUInt64(raw[1].Replace("0x", string.Empty), 16));
- }
- }
- public Dictionary<ulong, ulong> GetTable() => Table;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement