Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7. using System.Diagnostics;
  8. using System.Threading.Tasks;
  9. using System.Runtime.InteropServices;
  10. using System.Security.Cryptography;
  11. using System.Reflection;
  12. using System.Text.RegularExpressions;
  13. using Newtonsoft.Json;
  14.  
  15. namespace FNV1Matcher
  16. {
  17. class Program
  18. {
  19. public const string PrefixesRes = "Prefixes.txt";
  20. public const string RelatedKeywordsRes = "RelatedKeywords.txt";
  21. public static string[] gPrefixes;
  22. public static Dictionary<string, string[]> gRelatedKeywords;
  23. public static Dictionary<int, List<string>> gResult;
  24. public static HashSet<int> gPossibleHashes;
  25. public static string[] gPossibleKeywords;
  26. public static int totalFound = 0;
  27. static string uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ._: ";
  28.  
  29. static void Main(string[] args)
  30. {
  31. Init();
  32.  
  33. foreach (string s in gPossibleKeywords)
  34. {
  35. AddTest(s);
  36. }
  37.  
  38. foreach (string s in gPossibleKeywords)
  39. {
  40. CreateSourceCombination(s);
  41. }
  42.  
  43. string json = JsonConvert.SerializeObject(gResult, Formatting.Indented);
  44. using (TextWriter writer = new StreamWriter(new FileStream("keywordhashmap.json", FileMode.Create)))
  45. {
  46. writer.Write(json);
  47. }
  48.  
  49. Console.ReadLine();
  50. }
  51.  
  52. public static void Init()
  53. {
  54. gPossibleHashes = new HashSet<int>();
  55. gResult = new Dictionary<int, List<string>>();
  56. gRelatedKeywords = new Dictionary<string, string[]>();
  57.  
  58. //Build prefix list
  59. using (StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + PrefixesRes)))
  60. {
  61. var list = new List<string>();
  62. while (!sr.EndOfStream)
  63. list.Add(sr.ReadLine());
  64. gPrefixes = list.ToArray();
  65. }
  66.  
  67. //Build related keywords map
  68. using (StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + RelatedKeywordsRes)))
  69. {
  70. while (!sr.EndOfStream)
  71. {
  72. string[] currentSplits = sr.ReadLine().Split(' ');
  73. for (int i = 0; i < currentSplits.Length; i++)
  74. {
  75. List<string> relatedList;
  76.  
  77. //If keyword already has a relation list, we must add to that list
  78. if (gRelatedKeywords.ContainsKey(currentSplits[i]))
  79. relatedList = gRelatedKeywords[currentSplits[i]].ToList();
  80. else
  81. relatedList = new List<string>();
  82.  
  83. //Add each related keyword on the same line to the list, except if it's the same one
  84. for (int j = 0; j < currentSplits.Length; j++)
  85. {
  86. if (i == j)
  87. continue;
  88.  
  89. if (!relatedList.Contains(currentSplits[j]))
  90. relatedList.Add(currentSplits[j]);
  91. }
  92.  
  93. //Add dictionary entry
  94. if (gRelatedKeywords.ContainsKey(currentSplits[i]))
  95. gRelatedKeywords[currentSplits[i]] = relatedList.ToArray();
  96. else
  97. gRelatedKeywords.Add(currentSplits[i], relatedList.ToArray());
  98. }
  99. }
  100.  
  101. }
  102.  
  103.  
  104. //Build the possible hashes list
  105. //Hashes are contained in a .txt where each hash is delimited by a newline
  106. Console.WriteLine("Building hash list");
  107. using (FileStream fstream = new FileStream(@"C:\Users\asdf\Documents\DAIeventhashes.txt", FileMode.Open, FileAccess.Read))
  108. {
  109. while (fstream.Position < fstream.Length)
  110. {
  111. string s = ReadString(fstream, 0x0A); //0x0A = newline
  112. int a = Int32.Parse(s);
  113. gPossibleHashes.Add(a);
  114. }
  115. }
  116.  
  117. //Build the possible keywords list
  118. //Keywords are contained in a .txt where each keyword is delimited by a newline
  119. Console.WriteLine("Building keyword list");
  120. using (FileStream fstream = new FileStream(@"C:\Users\asdf\Documents\DAIstrings.txt", FileMode.Open, FileAccess.Read))
  121. {
  122. var list = new List<string>();
  123. while (fstream.Position < fstream.Length)
  124. {
  125. list.Add(ReadString(fstream, 0x0A)); //0x0A = newline
  126.  
  127. }
  128. gPossibleKeywords = list.ToArray();
  129. }
  130. }
  131.  
  132. public static IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
  133. {
  134. return from m in Enumerable.Range(0, 1 << list.Count)
  135. select
  136. from i in Enumerable.Range(0, list.Count)
  137. where (m & (1 << i)) != 0
  138. select list[i];
  139. }
  140.  
  141. public static void CreateSourceCombination(string s = "")
  142. {
  143. // s = "GamePlatform_Win32";
  144. // Quickly validate if the string would be of any use in a permutated manner
  145. if (s.Contains(' '))
  146. return;
  147.  
  148. if (s == s.ToUpper())
  149. return;
  150.  
  151. string[] splits = GetSplits(s);
  152.  
  153. List<int> a = new List<int>();
  154. for (int i = 0; i < splits.Length; i++) a.Add(i);
  155.  
  156. var powerset = GetPowerSet<int>(a);
  157.  
  158. List<string> aSourceCombination;
  159. foreach (var set in powerset)
  160. {
  161. aSourceCombination = new List<string>();
  162. foreach (int splitIndex in set)
  163. {
  164. aSourceCombination.Add(splits[splitIndex]);
  165. }
  166.  
  167. //Cleanup
  168. if (IsSourceCombinationValid(aSourceCombination.ToArray()))
  169. GenerateVariableCombinationsFromSource("", aSourceCombination.ToArray());
  170. }
  171.  
  172. //Console.ReadKey();
  173. }
  174.  
  175. public static bool IsSourceCombinationValid(string[] splits)
  176. {
  177. if (splits.Length == 0)
  178. return false;
  179. if (splits.Length > 10)
  180. return false;
  181. if (splits[0] == "_")
  182. return false;
  183. if (splits[splits.Length - 1] == "_")
  184. return false;
  185.  
  186. return true;
  187. }
  188.  
  189. public static void GenerateVariableCombinationsFromSource(string prefix, string[] splits)
  190. {
  191. if (splits.Length == 1)
  192. {
  193. //Source combination
  194. PrependPrefix(prefix + splits[0]);
  195.  
  196. foreach (KeyValuePair<string, string[]> RelatedKeywordsPair in gRelatedKeywords)
  197. {
  198. if (splits[0] == RelatedKeywordsPair.Key)
  199. {
  200. foreach (string relatedKeyword in RelatedKeywordsPair.Value)
  201. {
  202. PrependPrefix(prefix + relatedKeyword);
  203. }
  204. break;
  205. }
  206. }
  207.  
  208. }
  209. else
  210. {
  211. List<string> splitsMinusFirst = splits.ToList();
  212. splitsMinusFirst.RemoveAt(0);
  213.  
  214. //Source combination
  215. GenerateVariableCombinationsFromSource(prefix + splits[0], splitsMinusFirst.ToArray());
  216.  
  217. //Variate combinations from source
  218. foreach (KeyValuePair<string, string[]> RelatedKeywordsPair in gRelatedKeywords)
  219. {
  220. if (splits[0] == RelatedKeywordsPair.Key)
  221. {
  222. foreach (string relatedKeyword in RelatedKeywordsPair.Value)
  223. {
  224. GenerateVariableCombinationsFromSource(prefix + relatedKeyword, splitsMinusFirst.ToArray());
  225. }
  226. break;
  227. }
  228. }
  229. }
  230. }
  231.  
  232. public static void PrependPrefix(string s)
  233. {
  234. foreach (string prefix in gPrefixes)
  235. {
  236. AddTest(prefix + s);
  237. AddTest(prefix.ToLower() + s);
  238. }
  239. }
  240.  
  241. public static void AddTest(string s)
  242. {
  243. //Console.WriteLine("AddTest: " + s);
  244. if (gPossibleHashes.Contains(HashString(s)))
  245. {
  246. if (!gResult.ContainsKey(HashString(s)))
  247. {
  248. gResult.Add(HashString(s), new List<string>() { s });
  249. PrintFound(s, false);
  250. }
  251. else
  252. {
  253. if (!gResult[HashString(s)].Contains(s))
  254. {
  255. if (s.Length < gResult[HashString(s)].ElementAt(0).Length)
  256. gResult[HashString(s)].Insert(0, s);
  257. else
  258. gResult[HashString(s)].Add(s);
  259. PrintFound(s, true);
  260. }
  261. }
  262. }
  263. }
  264.  
  265.  
  266. public static string[] GetSplits(string s)
  267. {
  268. if (s.Contains(' '))
  269. {
  270. return s.Split(' ');
  271. }
  272. var r = new Regex(@"
  273. (?<=[A-Z])(?=[A-Z][a-z]) |
  274. (?<=[^A-Z])(?=[A-Z]) |
  275. (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
  276. string a = r.Replace(s, " ");
  277. return a.Split(' ');
  278. }
  279.  
  280. public static int HashString(string StrToHash)
  281. {
  282. int Hash = 0x1505;
  283. for (int i = 0; i < StrToHash.Length; i++)
  284. {
  285. byte b = (byte)StrToHash[i];
  286. Hash = (int)(Hash * 33) ^ b;
  287. }
  288. return Hash;
  289. }
  290. public static string ReadString(Stream s, byte endByte)
  291. {
  292. string str = "";
  293. byte b;
  294. while ((b = (byte)s.ReadByte()) != endByte && s.Position < s.Length)
  295. str += (char)b;
  296. return str;
  297. }
  298. public static void PrintFound(string s, bool collide)
  299. {
  300. if (!collide)
  301. {
  302. totalFound++;
  303. Console.WriteLine("\t{0, 5} : {1}", totalFound, s);
  304. //Trace.WriteLine(totalFound + " : " + s);
  305. }
  306. else
  307. {
  308. Console.WriteLine("\tcollision : {0}", s);
  309. }
  310. }
  311. }
  312. }
  313.  
  314. //relatedkeywords
  315.  
  316. Start Stop Begin End Reset Play
  317. start stop begin end reset play
  318. Started Stopped Ended Resetted
  319. started stopped ended resetted
  320. Enable Disable Reset Allow
  321. Enabled Disabled Resetted Allowed
  322. enable disable reset allow
  323. enabled disabled resetted allowed
  324. Do Undo
  325. do undo
  326. On Off
  327. on off
  328. In Out
  329. in out
  330. True False
  331. true False
  332. Max Min
  333. Get Set
  334. get set
  335. Is IsNot
  336. Show Hide Unhide UnHide
  337. show hide unhide
  338. Enter Exit
  339. enter exit
  340. Select Unselect
  341. select unselect
  342. Activate Deactivate
  343. activate deactivate
  344. Input Output
  345. input output
  346.  
  347. //prefixes
  348.  
  349. On
  350. Do
  351. If
  352. Use
  353. Start
  354. Stop
  355. Enter
  356. Exit
  357. Get
  358. Set
  359. Is
  360. Has
  361. In
  362. Can
  363. Not
  364. Begin
  365. End
  366. Enable
  367. Disable
  368. Select
  369. Allow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement