Advertisement
MMMonster

Native CrossMap Patcher by Mike Rohsoft

Jan 11th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Native CrossMap Patcher by Mike Rohsoft */
  2. /* Paste in folder Crossmap.txt with new data */
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <sstream>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <map>
  12. #include <regex>
  13. #include <inttypes.h>
  14. #include <Windows.h>
  15. using namespace std;
  16.  
  17. enum FILEMODE
  18. {
  19.     NEW_CROSS_MAP = 0,
  20.     CPP_CROSS_MAP,
  21.     //NATIVES_H
  22. };
  23.  
  24. static regex new_cross_map_regexp = regex("\\{\\s*0x0*([1-9a-fA-F][0-9a-fA-F]+)\\s*,\\s*0x0*([1-9a-fA-F][0-9a-fA-F]+)\\s*\\},");
  25. static regex cpp_cross_map_regexp = regex("\\s*0x0*([1-9a-fA-F][0-9a-fA-F]+)\\s*,\\s*0x0*([1-9a-fA-F][0-9a-fA-F]+)\\s*,");
  26. //static regex natives_h_map_regexp = regex("^\\s*static\\s*[a-zA-Z0-9\\*]+\\s*([A-Z0-9_-x]+)\\(.*?(0x[0-9a-fA-F]+).*$");
  27. static vector<string> crossMap;
  28. map<string, string> newkeys;
  29. map<string, string> getMap(FILEMODE mode, string fileName)
  30. {
  31.     map<string, string> ret;
  32.     regex e;
  33.     switch (mode)
  34.     {
  35.     case NEW_CROSS_MAP: e = new_cross_map_regexp; break;
  36.     case CPP_CROSS_MAP: e = cpp_cross_map_regexp; break;
  37.     //case NATIVES_H: e = natives_h_map_regexp; break;
  38.     default: cout << "Error: Unknown Filemode" << endl; exit(1337);
  39.     }  
  40.     ifstream infile(fileName);
  41.     if (infile.good())
  42.     {
  43.         string line;
  44.         string key = "0x";
  45.         string value = "0x";
  46.         cmatch cm;
  47.         int i = 0;
  48.         while (getline(infile, line))
  49.         {
  50.             if (mode == CPP_CROSS_MAP)
  51.                 crossMap.push_back(line);
  52.             if (regex_match(&line[0], cm, e, regex_constants::match_default))
  53.             {
  54.                 if (mode == NEW_CROSS_MAP)
  55.                 {
  56.                     key += cm[1];
  57.                     value += cm[2];
  58.                 }
  59.                 else
  60.                 {
  61.                     key += cm[2];
  62.                     value += cm[1];
  63.                 }
  64.                 transform(key.begin(), key.end(), key.begin(), ::tolower);
  65.                 transform(value.begin(), value.end(), value.begin(), ::tolower);
  66.                 if (ret.find(key) != ret.end())
  67.                 {
  68.                     newkeys[value] = value;
  69.                 }
  70.                 else
  71.                     ret[key] = value;
  72.                 key = "0x";
  73.                 value = "0x";
  74.                 i++;
  75.             }
  76.         }
  77.         printf("found %d pairs\n", i);
  78.         infile.close();
  79.     }
  80.     else //if (mode != NATIVES_H)
  81.     {
  82.         cout << "Error: Map File not found: " << fileName << endl;
  83.         exit(1337);
  84.     }
  85.     return ret;
  86. }
  87.  
  88. string getCurrentPath()
  89. {
  90.     char buffer[MAX_PATH];
  91.     GetModuleFileNameA(NULL, &buffer[0], MAX_PATH);
  92.     string b = string(&buffer[0]);
  93.     string::size_type pos = b.find_last_of("\\/");
  94.     return b.substr(0, pos) + "\\";
  95. }
  96.  
  97. const string ALEXANDER_BLADES_PASTBIN_POST_FILE = "crossmap.txt";
  98. const string CODE_CROSSMAP = "CrossMapping.cpp";
  99.  
  100. int main()
  101. {
  102.     cout << "open file: " << "crossmap.txt" << endl;
  103.     map<string, string> updateMap = getMap(NEW_CROSS_MAP, getCurrentPath() + ALEXANDER_BLADES_PASTBIN_POST_FILE);
  104.     cout << "open file: " << "CrossMapping.cpp" << endl;
  105.     map<string, string> currentMap = getMap(CPP_CROSS_MAP, getCurrentPath() + CODE_CROSSMAP);
  106.     //map<string, string> nativeMap = getMap(NATIVES_H, getCurrentPath() + "natives.h");   
  107.     map<string, string> newMap;
  108.     int patched = 0;
  109.     cout << "Comparing ... ";
  110.     for (map<string, string>::iterator it = updateMap.begin(); it != updateMap.end(); ++it)
  111.     {
  112.         string key = it->first;
  113.         string value = it->second;
  114.         if (currentMap.find(key) != currentMap.end())
  115.         {
  116.             patched++;
  117.             string basekey = currentMap[key];
  118.             newMap[basekey] = value;
  119.         }
  120.     }
  121.     cout << patched << " Natives can be patched..." << endl;
  122.     cmatch cm;
  123.     ofstream out("CrossMapping_new.cpp");
  124.     patched = 0;
  125.     for (int i = 0, l = crossMap.size(); i < l; i++)
  126.     {
  127.         if (regex_match(&crossMap[i][0], cm, cpp_cross_map_regexp, regex_constants::match_default))
  128.         {
  129.             string key = "0x";
  130.             key += cm[1];
  131.             string found = cm[2];
  132.             size_t pos = crossMap[i].find(key) + key.size();
  133.             string value = newMap[key];
  134.             if (value.size() > 0)
  135.             {
  136.                 crossMap[i].replace(pos + 2, found.size() + 2, value);
  137.                 patched++;
  138.             }
  139.         }
  140.         out << crossMap[i] << endl;
  141.     }
  142.     out.close();
  143.     printf("Done ... patched %d Natives\n", patched);
  144.     string bla;
  145.     getline(cin, bla);
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement