Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32;
- using Mono.Cecil;
- using Mono.Cecil.Cil;
- using System;
- using System.IO;
- namespace ESStaticInjectorWin
- {
- class MainClass
- {
- public static void expose(AssemblyDefinition assembly, string className, string methodName)
- {
- foreach (TypeDefinition type in assembly.MainModule.Types)
- {
- if (type.FullName == className)
- {
- foreach (MethodDefinition method in type.Methods)
- {
- if (method.Name == methodName)
- {
- method.IsPublic = true;
- method.IsVirtual = true;
- return;
- }
- }
- }
- }
- throw new Exception("Problem with finding method" + methodName);
- }
- static void Main(string[] args)
- {
- var resolver = new DefaultAssemblyResolver();
- string path = ESPath.GameDirectory.Root + @"/EndlessSpace_Data/Managed/";
- resolver.AddSearchDirectory(path);
- var parameters = new ReaderParameters
- {
- AssemblyResolver = resolver,
- };
- // Environment.CurrentDirectory+ " /../../Assembly-CSharp.dll",parameters
- string target = (Environment.CurrentDirectory + "\\Assembly-CSharp.dll"); //where?
- AssemblyDefinition myLibrary = AssemblyDefinition.ReadAssembly(target, parameters);
- Console.WriteLine("Definition Target: " + target + "\n" + myLibrary.FullName);
- var trampolineType = typeof(AITrampoline.AITrampoline);
- expose(myLibrary, "AIPlayerController", "Bind");
- expose(myLibrary, "AIPlayerController", "CreateAIEmpire");
- foreach (TypeDefinition type in myLibrary.MainModule.Types)
- {
- if (type.FullName == "ApplicationState_Lobby")
- {
- Console.WriteLine("Searching Type 'ApplicationState_Lobby' for 'Session_PlayerSlotOpening()'..");
- foreach (MethodDefinition method in type.Methods)
- {
- Console.WriteLine("ApplicationState_Lobby." + method.Name + "()"); //todo debug method names
- if (method.Name == "Session_PlayerSlotOpening")
- {
- Console.WriteLine("Found " + method.Name);
- var processor = method.Body.GetILProcessor();
- Console.WriteLine("Session_PlayerSlotOpening, " + typeof(MainClass).GetMethods());
- var newType = method.Module.Import(trampolineType);
- Console.WriteLine("\nImporting 'trampolineType' " + trampolineType.Assembly.Location);
- foreach (Instruction i in method.Body.Instructions)
- {
- Console.WriteLine("\nInstruction: " + i.ToString());
- if (i.OpCode == OpCodes.Ldtoken)
- {
- TypeDefinition td = (TypeDefinition)i.Operand;
- if (td.Name == "AIPlayerController")
- {
- processor.Replace(i, processor.Create(OpCodes.Ldtoken, newType));
- Console.WriteLine("\nInstruction: " + i.ToString());
- Console.WriteLine("Patch successful. Replaced '" + td.Name + "' with '" + newType.Name + "'");
- break;
- }
- }
- }
- Console.WriteLine("Inject success!");
- }
- }
- }
- }
- Console.WriteLine("Press any key to copy assembly.");
- Console.ReadKey();
- if (Directory.Exists(path))
- {
- myLibrary.Write(path + "Assembly-CSharp.dll");
- System.IO.File.Copy(Environment.CurrentDirectory + @"/../../../AITrampoline/bin/Debug/AITrampoline.dll", path + "AITrampoline.dll", true);
- System.IO.File.Copy(Environment.CurrentDirectory + @"/../../../DrunkenWalkAI/bin/Debug/DrunkenWalkAI.dll", path + "DrunkenWalkAI.dll", true);
- Console.WriteLine("Writing assembly to..\n" + path + "Assembly-CSharp.dll");
- Console.WriteLine(path + "AITrampoline.dll");
- Console.WriteLine(path + "DrunkenWalkAI.dll");
- }
- else
- {
- Console.WriteLine("Cannot write to assembly: " + path + "Assembly-CSharp.dll");
- }
- Console.ReadKey();
- }
- }
- public static class ESPath
- {
- public struct GameDirectory
- {
- public static string Root { get { return GetGamePath(); } }
- public static string Data { get { return GetGamePath() + "\\Public"; } }
- public static string XpData { get { return GetGamePath() + "\\Public_xp1"; } }
- }
- public struct UserMods
- {
- public static string Root { get { return GetModdingPath(); } }
- public static string ModPath { get { return GetModdingPath() + "\\Modding"; } }
- public static string XpPath { get { return GetModdingPath() + "\\Disharmony\\Modding"; } }
- }
- public static string GetGamePath()
- {
- string s = GetSteamPath();
- if (Directory.Exists(s))
- {
- string assembly = "\\EndlessSpace.exe";
- string es = s + "\\SteamApps\\common\\Endless Space";
- if (File.Exists(es + assembly))
- {
- return es;
- }
- else
- {
- return null;
- }
- }
- else
- {
- return null;
- }
- }
- public static string GetModdingPath()
- {
- string doc = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
- string es = doc + "\\Endless Space";
- if (Directory.Exists(es))
- {
- return es;
- }
- return null;
- }
- private static string GetSteamPath()
- {
- RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser;
- regKey = regKey.OpenSubKey("Software\\Valve\\Steam");
- if (regKey != null)
- {
- string steam = regKey.GetValue("SteamPath").ToString();
- if (Directory.Exists(steam))
- {
- return Path.GetFullPath(steam);
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement