Advertisement
Guest User

Untitled

a guest
Dec 29th, 2018
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace NativeTableMerger
  9. {
  10.     class Program
  11.     {
  12.         static Dictionary<int, List<ulong>> ParseDump(string filename)
  13.         {
  14.             var result = new Dictionary<int, List<ulong>>();
  15.             var raw = File.ReadAllLines(filename);
  16.  
  17.             for (int i = 0; i < raw.Length; i++)
  18.             {
  19.                 var str = raw[i];
  20.  
  21.                 if (!str.StartsWith("["))
  22.                     continue;
  23.  
  24.                 var namespaceIndex = int.Parse(str.Replace("[", string.Empty).Replace("]:", string.Empty));
  25.                 var natives = new List<ulong>();
  26.  
  27.                 while (true)
  28.                 {
  29.                     i++;
  30.  
  31.                     if (i >= raw.Length)
  32.                         break;
  33.  
  34.                     str = raw[i];
  35.  
  36.                     if (str.StartsWith("["))
  37.                     {
  38.                         i--;
  39.                         break;
  40.                     }
  41.  
  42.                     var hash = ulong.Parse(str.Replace("0x", string.Empty), System.Globalization.NumberStyles.HexNumber);
  43.  
  44.                     natives.Add(hash);
  45.                 }
  46.  
  47.                 result.Add(namespaceIndex, natives);
  48.             }
  49.  
  50.             return result;
  51.         }
  52.  
  53.         static ulong GetMappedHash(ulong hash, Dictionary<ulong, ulong> table, int index)
  54.         {
  55.             if (table.TryGetValue(hash, out var result))
  56.                 return result;
  57.  
  58.             Console.WriteLine($"[{index}]Cannot find translation from 0x{hash:X16} searching in files...");
  59.             string line;
  60.             bool found = false;
  61.  
  62.             var files = Directory.EnumerateFiles("old", "*.ysc.c", SearchOption.AllDirectories);
  63.  
  64.             foreach (var file in files)
  65.             {
  66.                 var lineNumber = 0;
  67.                 using (var reader = File.OpenText(file))
  68.                 {
  69.                     while ((line = reader.ReadLine()) != null)
  70.                     {
  71.                         lineNumber++;
  72.                         if (!line.Contains($"unk_0x{hash:X16}"))
  73.                             continue;
  74.  
  75.                         Console.WriteLine($"Found hash 0x{hash:X16} in file {file}:{lineNumber}");
  76.  
  77.                         {
  78.                             var process = new Process();
  79.                             var startInfo = new ProcessStartInfo
  80.                             {
  81.                                 FileName = "cmd.exe",
  82.                                 Verb = "runas",
  83.                                 Arguments = $"/c notepad++ \"{Path.GetFullPath(file)}\" -n{lineNumber}",
  84.                                 UseShellExecute = true
  85.                             };
  86.                             process.StartInfo = startInfo;
  87.                             process.Start();
  88.                         }
  89.  
  90.                         {
  91.                             var process = new Process();
  92.                             var startInfo = new ProcessStartInfo
  93.                             {
  94.                                 FileName = "cmd.exe",
  95.                                 Verb = "runas",
  96.                                 Arguments = $"/c notepad++ \"{Path.GetFullPath(file.Replace("old", "new"))}\"",
  97.                                 UseShellExecute = true
  98.                             };
  99.                             process.StartInfo = startInfo;
  100.                             process.Start();
  101.                         }
  102.  
  103.                         found = true;
  104.                         break;
  105.                     }
  106.                 }
  107.  
  108.                 if (found)
  109.                     break;
  110.             }
  111.  
  112.             Console.WriteLine("Enter translation:");
  113.  
  114.             ulong newHash = hash;
  115.             bool skipped = false;
  116.  
  117.             while (true)
  118.             {
  119.                 var str = Console.ReadLine().Replace("0x", string.Empty);
  120.  
  121.                 if (string.IsNullOrEmpty(str))
  122.                 {
  123.                     skipped = true;
  124.                     break;
  125.                 }
  126.  
  127.                 while (!ulong.TryParse(str, System.Globalization.NumberStyles.HexNumber, null, out newHash))
  128.                 {
  129.                     Console.WriteLine("You've entered wrong data!");
  130.                 }
  131.  
  132.                 break;
  133.             }
  134.  
  135.             if (skipped && newHash == hash)
  136.                 return hash;
  137.  
  138.             Console.WriteLine($"Applied new translation 0x{hash:X16} -> 0x{newHash:X16}");
  139.  
  140.             table[hash] = newHash;
  141.  
  142.             SaveTo(table, "cache_table.h");
  143.  
  144.             return newHash;
  145.         }
  146.  
  147.         static void FixDumps()
  148.         {
  149.             const string oldfile = "natives1493.log";
  150.             const string newfile = "natives1604.log";
  151.  
  152.             if (!File.Exists("fixed_" + newfile))
  153.                 File.Copy(newfile, "fixed_" + newfile);
  154.  
  155.             var table = new HeaderTableParser(File.ReadAllLines("cache_table.h")).GetTable();
  156.             var oldDump = ParseDump(oldfile);
  157.             var newDump = ParseDump("fixed_" + newfile);
  158.  
  159.             foreach (var kvp in oldDump)
  160.             {
  161.                 if (kvp.Value.Count == newDump[kvp.Key].Count)
  162.                 {
  163.                     Console.WriteLine($"Namespaces {kvp.Key} are equal! Natives: {kvp.Value.Count}");
  164.                     continue;
  165.                 }
  166.  
  167.                 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...");
  168.  
  169.                 for (int i = 0; i < kvp.Value.Count; i += (kvp.Value.Count - i - 10 > 0 ? 10 : 1))
  170.                 {
  171.                     var mappedHash = GetMappedHash(kvp.Value[i], table, i);
  172.                     var hashIndex = newDump[kvp.Key].IndexOf(mappedHash);
  173.  
  174.                     if (hashIndex == i)
  175.                     {
  176.                         Console.WriteLine($"{i} [GOOD] 0x{kvp.Value[i]:X16} -> 0x{mappedHash:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
  177.  
  178.                         if (kvp.Value.Count - i < 100)
  179.                             continue;
  180.  
  181.                         var ti = i + 100;
  182.  
  183.                         if (kvp.Value.Count > ti && newDump[kvp.Key].IndexOf(GetMappedHash(kvp.Value[ti], table, ti)) == ti)
  184.                         {
  185.                             i = ti;
  186.                         }
  187.  
  188.                         continue;
  189.                     }
  190.  
  191.                     if (newDump[kvp.Key].IndexOf(mappedHash) < i)
  192.                     {
  193.                         Console.WriteLine($"!!!DANGER!!! {newDump[kvp.Key].IndexOf(mappedHash)} < {i}");
  194.                     }
  195.  
  196.                     for (int j = i - Math.Min(10, kvp.Value.Count - i); j <= i; j++)
  197.                     {
  198.                         var mappedHashTemp = GetMappedHash(kvp.Value[j], table, j);
  199.  
  200.                         if (newDump[kvp.Key].IndexOf(mappedHashTemp) == j)
  201.                         {
  202.                             Console.WriteLine($"{j} [GOOD] 0x{kvp.Value[j]:X16} -> 0x{mappedHashTemp:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
  203.                             continue;
  204.                         }
  205.  
  206.                         Console.WriteLine($"{j} [BAD] 0x{kvp.Value[j]:X16} -> 0x{mappedHashTemp:X16} trying to find... Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
  207.  
  208.                         while (GetMappedHash(kvp.Value[j], table, j) != newDump[kvp.Key][j])
  209.                         {
  210.                             Console.WriteLine($"Removing 0x{newDump[kvp.Key][j]:X16} Natives: {kvp.Value.Count}/{newDump[kvp.Key].Count}");
  211.                             Console.ReadLine();
  212.                             newDump[kvp.Key].RemoveAt(j);
  213.                             SaveTo(newDump, "fixed_" + newfile);
  214.                         }
  215.  
  216.                         if (newDump[kvp.Key].IndexOf(mappedHash) == i)
  217.                         {
  218.                             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}");
  219.                             SaveTo(newDump, "fixed_" + newfile);
  220.                             break;
  221.                         }
  222.                     }
  223.  
  224.                     if (kvp.Value.Count == newDump[kvp.Key].Count)
  225.                         break;
  226.  
  227.                     i -= Math.Min(10, kvp.Value.Count - i);
  228.                 }
  229.  
  230.                 if (kvp.Value.Count != newDump[kvp.Key].Count)
  231.                     if (newDump[kvp.Key][kvp.Value.Count - 1] == GetMappedHash(kvp.Value.Last(), table, kvp.Value.Count - 1))
  232.                     {
  233.                         newDump[kvp.Key].RemoveRange(kvp.Value.Count, newDump[kvp.Key].Count - kvp.Value.Count);
  234.                     }
  235.                     else
  236.                         throw new Exception("WTF?");
  237.  
  238.                 SaveTo(newDump, "fixed_" + newfile);
  239.             }
  240.  
  241.             Console.WriteLine("Dumps are equal! Dobby is free now! Congratz!");
  242.             Console.ReadLine();
  243.         }
  244.  
  245.         static void Main(string[] args)
  246.         {
  247.             const string oldfile = "natives1493.log";
  248.             const string newfile = "natives1604.log";
  249.  
  250.             FixDumps();
  251.  
  252.             var oldDump = ParseDump(oldfile);
  253.             var newDump = ParseDump("fixed_" + newfile);
  254.  
  255.             var table = new Dictionary<ulong, ulong>();
  256.  
  257.             foreach (var @namespace in oldDump)
  258.             {
  259.                 for (int i = 0; i < @namespace.Value.Count; i++)
  260.                 {
  261.                     table[@namespace.Value[i]] = newDump[@namespace.Key][i];
  262.                 }
  263.             }
  264.  
  265.             SaveTo(table, "1493to1604.h");
  266.         }
  267.  
  268.         static void SaveTo(Dictionary<ulong, ulong> table, string filename)
  269.         {
  270.             if (File.Exists(filename))
  271.                 File.Delete(filename);
  272.  
  273.             var sb = new StringBuilder();
  274.  
  275.             foreach (var item in table)
  276.                 sb.AppendLine("{0x" + item.Key.ToString("X16") + ",0x" + item.Value.ToString("X16") + "},");
  277.  
  278.             File.WriteAllText(filename, sb.ToString());
  279.         }
  280.  
  281.         static void SaveTo(Dictionary<int, List<ulong>> dump, string filename)
  282.         {
  283.             var sb = new StringBuilder();
  284.  
  285.             foreach (var @namespace in dump)
  286.             {
  287.                 sb.AppendLine($"[{@namespace.Key}]:");
  288.  
  289.                 foreach (var native in @namespace.Value)
  290.                 {
  291.                     sb.AppendLine($"0x{native:X16}");
  292.                 }
  293.             }
  294.  
  295.             File.WriteAllText(filename, sb.ToString());
  296.         }
  297.     }
  298.  
  299.     internal class HeaderTableParser
  300.     {
  301.         private Dictionary<ulong, ulong> Table { get; }
  302.  
  303.         public HeaderTableParser(string[] header)
  304.         {
  305.             Table = new Dictionary<ulong, ulong>();
  306.  
  307.             foreach (var line in header)
  308.             {
  309.                 var raw = line.Replace(" ", string.Empty).Replace("{", string.Empty).Replace("}", string.Empty).TrimEnd(',')
  310.                     .Split(',');
  311.  
  312.                 if (raw.Length != 2)
  313.                     continue;
  314.  
  315.                 if (raw[0].StartsWith("//"))
  316.                     continue;
  317.  
  318.                 Table.Add(Convert.ToUInt64(raw[0].Replace("0x", string.Empty), 16),
  319.                     Convert.ToUInt64(raw[1].Replace("0x", string.Empty), 16));
  320.             }
  321.         }
  322.  
  323.         public Dictionary<ulong, ulong> GetTable() => Table;
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement