Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.27 KB | None | 0 0
  1. using Microsoft.Win32;
  2. using Mono.Cecil;
  3. using Mono.Cecil.Cil;
  4. using System;
  5. using System.IO;
  6.  
  7.  
  8. namespace ESStaticInjectorWin
  9. {
  10.     class MainClass
  11.     {
  12.         public static void expose(AssemblyDefinition assembly, string className, string methodName)
  13.         {
  14.             foreach (TypeDefinition type in assembly.MainModule.Types)
  15.             {
  16.                 if (type.FullName == className)
  17.                 {
  18.                     foreach (MethodDefinition method in type.Methods)
  19.                     {
  20.                         if (method.Name == methodName)
  21.                         {
  22.                             method.IsPublic = true;
  23.                             method.IsVirtual = true;
  24.                             return;
  25.                         }
  26.                     }
  27.                 }
  28.             }
  29.             throw new Exception("Problem with finding method" + methodName);
  30.         }
  31.  
  32.  
  33.         static void Main(string[] args)
  34.         {
  35.             var resolver = new DefaultAssemblyResolver();
  36.             string path = ESPath.GameDirectory.Root + @"/EndlessSpace_Data/Managed/";
  37.             resolver.AddSearchDirectory(path);
  38.             var parameters = new ReaderParameters
  39.             {
  40.                 AssemblyResolver = resolver,
  41.             };
  42.  
  43.             //      Environment.CurrentDirectory+ " /../../Assembly-CSharp.dll",parameters
  44.             string target = (Environment.CurrentDirectory + "\\Assembly-CSharp.dll"); //where?
  45.             AssemblyDefinition myLibrary = AssemblyDefinition.ReadAssembly(target, parameters);
  46.             Console.WriteLine("Definition Target: " + target + "\n" + myLibrary.FullName);
  47.  
  48.            
  49.  
  50.             var trampolineType = typeof(AITrampoline.AITrampoline);
  51.  
  52.             expose(myLibrary, "AIPlayerController", "Bind");
  53.             expose(myLibrary, "AIPlayerController", "CreateAIEmpire");
  54.  
  55.             foreach (TypeDefinition type in myLibrary.MainModule.Types)
  56.             {
  57.                 if (type.FullName == "ApplicationState_Lobby")
  58.                 {
  59.                     Console.WriteLine("Searching Type 'ApplicationState_Lobby' for 'Session_PlayerSlotOpening()'..");
  60.                     foreach (MethodDefinition method in type.Methods)
  61.                     {
  62.                         Console.WriteLine("ApplicationState_Lobby." + method.Name + "()"); //todo debug method names
  63.                         if (method.Name == "Session_PlayerSlotOpening")
  64.                         {
  65.                             Console.WriteLine("Found " + method.Name);
  66.                             var processor = method.Body.GetILProcessor();
  67.                             Console.WriteLine("Session_PlayerSlotOpening, " + typeof(MainClass).GetMethods());
  68.  
  69.                             var newType = method.Module.Import(trampolineType);
  70.                             Console.WriteLine("\nImporting 'trampolineType' " + trampolineType.Assembly.Location);
  71.  
  72.                             foreach (Instruction i in method.Body.Instructions)
  73.                             {
  74.                                 Console.WriteLine("\nInstruction: " + i.ToString());
  75.  
  76.                                 if (i.OpCode == OpCodes.Ldtoken)
  77.                                 {
  78.                                     TypeDefinition td = (TypeDefinition)i.Operand;
  79.                                     if (td.Name == "AIPlayerController")
  80.                                     {
  81.                                         processor.Replace(i, processor.Create(OpCodes.Ldtoken, newType));
  82.                                         Console.WriteLine("\nInstruction: " + i.ToString());
  83.                                         Console.WriteLine("Patch successful. Replaced '" + td.Name + "' with '" + newType.Name + "'");
  84.                                         break;
  85.                                     }
  86.  
  87.                                 }
  88.                             }
  89.                             Console.WriteLine("Inject success!");
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.  
  95.  
  96.             Console.WriteLine("Press any key to copy assembly.");
  97.             Console.ReadKey();
  98.  
  99.             if (Directory.Exists(path))
  100.             {
  101.                 myLibrary.Write(path + "Assembly-CSharp.dll");
  102.                 System.IO.File.Copy(Environment.CurrentDirectory + @"/../../../AITrampoline/bin/Debug/AITrampoline.dll", path + "AITrampoline.dll", true);
  103.                 System.IO.File.Copy(Environment.CurrentDirectory + @"/../../../DrunkenWalkAI/bin/Debug/DrunkenWalkAI.dll", path + "DrunkenWalkAI.dll", true);
  104.                 Console.WriteLine("Writing assembly to..\n" + path + "Assembly-CSharp.dll");
  105.                 Console.WriteLine(path + "AITrampoline.dll");
  106.                 Console.WriteLine(path + "DrunkenWalkAI.dll");
  107.             }
  108.             else
  109.             {
  110.                 Console.WriteLine("Cannot write to assembly: " + path + "Assembly-CSharp.dll");
  111.             }
  112.  
  113.             Console.ReadKey();
  114.  
  115.         }
  116.     }
  117.  
  118.  
  119.     public static class ESPath
  120.     {
  121.         public struct GameDirectory
  122.         {
  123.             public static string Root { get { return GetGamePath(); } }
  124.             public static string Data { get { return GetGamePath() + "\\Public"; } }
  125.             public static string XpData { get { return GetGamePath() + "\\Public_xp1"; } }
  126.         }
  127.  
  128.         public struct UserMods
  129.         {
  130.             public static string Root { get { return GetModdingPath(); } }
  131.             public static string ModPath { get { return GetModdingPath() + "\\Modding"; } }
  132.             public static string XpPath { get { return GetModdingPath() + "\\Disharmony\\Modding"; } }
  133.         }
  134.  
  135.         public static string GetGamePath()
  136.         {
  137.             string s = GetSteamPath();
  138.  
  139.             if (Directory.Exists(s))
  140.             {
  141.                 string assembly = "\\EndlessSpace.exe";
  142.                 string es = s + "\\SteamApps\\common\\Endless Space";
  143.                 if (File.Exists(es + assembly))
  144.                 {
  145.                     return es;
  146.                 }
  147.                 else
  148.                 {
  149.                     return null;
  150.                 }
  151.             }
  152.             else
  153.             {
  154.                 return null;
  155.             }
  156.         }
  157.  
  158.         public static string GetModdingPath()
  159.         {
  160.             string doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  161.             string es = doc + "\\Endless Space";
  162.             if (Directory.Exists(es))
  163.             {
  164.                 return es;
  165.             }
  166.             return null;
  167.         }
  168.  
  169.         private static string GetSteamPath()
  170.         {
  171.             RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser;
  172.             regKey = regKey.OpenSubKey("Software\\Valve\\Steam");
  173.             if (regKey != null)
  174.             {
  175.                 string steam = regKey.GetValue("SteamPath").ToString();
  176.                 if (Directory.Exists(steam))
  177.                 {
  178.                     return Path.GetFullPath(steam);
  179.                 }
  180.             }
  181.             return null;
  182.         }
  183.  
  184.     }
  185.  
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement