Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Diagnostics;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Reflection;
- using System.Text.RegularExpressions;
- using Newtonsoft.Json;
- namespace FNV1Matcher
- {
- class Program
- {
- public const string PrefixesRes = "Prefixes.txt";
- public const string RelatedKeywordsRes = "RelatedKeywords.txt";
- public static string[] gPrefixes;
- public static Dictionary<string, string[]> gRelatedKeywords;
- public static Dictionary<int, List<string>> gResult;
- public static HashSet<int> gPossibleHashes;
- public static string[] gPossibleKeywords;
- public static int totalFound = 0;
- static string uppercaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ._: ";
- static void Main(string[] args)
- {
- Init();
- foreach (string s in gPossibleKeywords)
- {
- AddTest(s);
- }
- foreach (string s in gPossibleKeywords)
- {
- CreateSourceCombination(s);
- }
- string json = JsonConvert.SerializeObject(gResult, Formatting.Indented);
- using (TextWriter writer = new StreamWriter(new FileStream("keywordhashmap.json", FileMode.Create)))
- {
- writer.Write(json);
- }
- Console.ReadLine();
- }
- public static void Init()
- {
- gPossibleHashes = new HashSet<int>();
- gResult = new Dictionary<int, List<string>>();
- gRelatedKeywords = new Dictionary<string, string[]>();
- //Build prefix list
- using (StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + PrefixesRes)))
- {
- var list = new List<string>();
- while (!sr.EndOfStream)
- list.Add(sr.ReadLine());
- gPrefixes = list.ToArray();
- }
- //Build related keywords map
- using (StreamReader sr = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + RelatedKeywordsRes)))
- {
- while (!sr.EndOfStream)
- {
- string[] currentSplits = sr.ReadLine().Split(' ');
- for (int i = 0; i < currentSplits.Length; i++)
- {
- List<string> relatedList;
- //If keyword already has a relation list, we must add to that list
- if (gRelatedKeywords.ContainsKey(currentSplits[i]))
- relatedList = gRelatedKeywords[currentSplits[i]].ToList();
- else
- relatedList = new List<string>();
- //Add each related keyword on the same line to the list, except if it's the same one
- for (int j = 0; j < currentSplits.Length; j++)
- {
- if (i == j)
- continue;
- if (!relatedList.Contains(currentSplits[j]))
- relatedList.Add(currentSplits[j]);
- }
- //Add dictionary entry
- if (gRelatedKeywords.ContainsKey(currentSplits[i]))
- gRelatedKeywords[currentSplits[i]] = relatedList.ToArray();
- else
- gRelatedKeywords.Add(currentSplits[i], relatedList.ToArray());
- }
- }
- }
- //Build the possible hashes list
- //Hashes are contained in a .txt where each hash is delimited by a newline
- Console.WriteLine("Building hash list");
- using (FileStream fstream = new FileStream(@"C:\Users\asdf\Documents\DAIeventhashes.txt", FileMode.Open, FileAccess.Read))
- {
- while (fstream.Position < fstream.Length)
- {
- string s = ReadString(fstream, 0x0A); //0x0A = newline
- int a = Int32.Parse(s);
- gPossibleHashes.Add(a);
- }
- }
- //Build the possible keywords list
- //Keywords are contained in a .txt where each keyword is delimited by a newline
- Console.WriteLine("Building keyword list");
- using (FileStream fstream = new FileStream(@"C:\Users\asdf\Documents\DAIstrings.txt", FileMode.Open, FileAccess.Read))
- {
- var list = new List<string>();
- while (fstream.Position < fstream.Length)
- {
- list.Add(ReadString(fstream, 0x0A)); //0x0A = newline
- }
- gPossibleKeywords = list.ToArray();
- }
- }
- public static IEnumerable<IEnumerable<T>> GetPowerSet<T>(List<T> list)
- {
- return from m in Enumerable.Range(0, 1 << list.Count)
- select
- from i in Enumerable.Range(0, list.Count)
- where (m & (1 << i)) != 0
- select list[i];
- }
- public static void CreateSourceCombination(string s = "")
- {
- // s = "GamePlatform_Win32";
- // Quickly validate if the string would be of any use in a permutated manner
- if (s.Contains(' '))
- return;
- if (s == s.ToUpper())
- return;
- string[] splits = GetSplits(s);
- List<int> a = new List<int>();
- for (int i = 0; i < splits.Length; i++) a.Add(i);
- var powerset = GetPowerSet<int>(a);
- List<string> aSourceCombination;
- foreach (var set in powerset)
- {
- aSourceCombination = new List<string>();
- foreach (int splitIndex in set)
- {
- aSourceCombination.Add(splits[splitIndex]);
- }
- //Cleanup
- if (IsSourceCombinationValid(aSourceCombination.ToArray()))
- GenerateVariableCombinationsFromSource("", aSourceCombination.ToArray());
- }
- //Console.ReadKey();
- }
- public static bool IsSourceCombinationValid(string[] splits)
- {
- if (splits.Length == 0)
- return false;
- if (splits.Length > 10)
- return false;
- if (splits[0] == "_")
- return false;
- if (splits[splits.Length - 1] == "_")
- return false;
- return true;
- }
- public static void GenerateVariableCombinationsFromSource(string prefix, string[] splits)
- {
- if (splits.Length == 1)
- {
- //Source combination
- PrependPrefix(prefix + splits[0]);
- foreach (KeyValuePair<string, string[]> RelatedKeywordsPair in gRelatedKeywords)
- {
- if (splits[0] == RelatedKeywordsPair.Key)
- {
- foreach (string relatedKeyword in RelatedKeywordsPair.Value)
- {
- PrependPrefix(prefix + relatedKeyword);
- }
- break;
- }
- }
- }
- else
- {
- List<string> splitsMinusFirst = splits.ToList();
- splitsMinusFirst.RemoveAt(0);
- //Source combination
- GenerateVariableCombinationsFromSource(prefix + splits[0], splitsMinusFirst.ToArray());
- //Variate combinations from source
- foreach (KeyValuePair<string, string[]> RelatedKeywordsPair in gRelatedKeywords)
- {
- if (splits[0] == RelatedKeywordsPair.Key)
- {
- foreach (string relatedKeyword in RelatedKeywordsPair.Value)
- {
- GenerateVariableCombinationsFromSource(prefix + relatedKeyword, splitsMinusFirst.ToArray());
- }
- break;
- }
- }
- }
- }
- public static void PrependPrefix(string s)
- {
- foreach (string prefix in gPrefixes)
- {
- AddTest(prefix + s);
- AddTest(prefix.ToLower() + s);
- }
- }
- public static void AddTest(string s)
- {
- //Console.WriteLine("AddTest: " + s);
- if (gPossibleHashes.Contains(HashString(s)))
- {
- if (!gResult.ContainsKey(HashString(s)))
- {
- gResult.Add(HashString(s), new List<string>() { s });
- PrintFound(s, false);
- }
- else
- {
- if (!gResult[HashString(s)].Contains(s))
- {
- if (s.Length < gResult[HashString(s)].ElementAt(0).Length)
- gResult[HashString(s)].Insert(0, s);
- else
- gResult[HashString(s)].Add(s);
- PrintFound(s, true);
- }
- }
- }
- }
- public static string[] GetSplits(string s)
- {
- if (s.Contains(' '))
- {
- return s.Split(' ');
- }
- var r = new Regex(@"
- (?<=[A-Z])(?=[A-Z][a-z]) |
- (?<=[^A-Z])(?=[A-Z]) |
- (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
- string a = r.Replace(s, " ");
- return a.Split(' ');
- }
- public static int HashString(string StrToHash)
- {
- int Hash = 0x1505;
- for (int i = 0; i < StrToHash.Length; i++)
- {
- byte b = (byte)StrToHash[i];
- Hash = (int)(Hash * 33) ^ b;
- }
- return Hash;
- }
- public static string ReadString(Stream s, byte endByte)
- {
- string str = "";
- byte b;
- while ((b = (byte)s.ReadByte()) != endByte && s.Position < s.Length)
- str += (char)b;
- return str;
- }
- public static void PrintFound(string s, bool collide)
- {
- if (!collide)
- {
- totalFound++;
- Console.WriteLine("\t{0, 5} : {1}", totalFound, s);
- //Trace.WriteLine(totalFound + " : " + s);
- }
- else
- {
- Console.WriteLine("\tcollision : {0}", s);
- }
- }
- }
- }
- //relatedkeywords
- Start Stop Begin End Reset Play
- start stop begin end reset play
- Started Stopped Ended Resetted
- started stopped ended resetted
- Enable Disable Reset Allow
- Enabled Disabled Resetted Allowed
- enable disable reset allow
- enabled disabled resetted allowed
- Do Undo
- do undo
- On Off
- on off
- In Out
- in out
- True False
- true False
- Max Min
- Get Set
- get set
- Is IsNot
- Show Hide Unhide UnHide
- show hide unhide
- Enter Exit
- enter exit
- Select Unselect
- select unselect
- Activate Deactivate
- activate deactivate
- Input Output
- input output
- //prefixes
- On
- Do
- If
- Use
- Start
- Stop
- Enter
- Exit
- Get
- Set
- Is
- Has
- In
- Can
- Not
- Begin
- End
- Enable
- Disable
- Select
- Allow
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement