|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +using Mono.Cecil; |
| 6 | +using Mono.Cecil.Cil; |
| 7 | + |
| 8 | +namespace PluginManager.Installer |
| 9 | +{ |
| 10 | + internal class Program |
| 11 | + { |
| 12 | + /// <summary>The program entry point.</summary> |
| 13 | + private static void Main() |
| 14 | + { |
| 15 | + string enginePath, managedPath; |
| 16 | + var resolver = new DefaultAssemblyResolver(); |
| 17 | + |
| 18 | + var enginePaths = Directory.EnumerateFiles( |
| 19 | + "./", "UnityEngine.dll", SearchOption.AllDirectories).ToArray(); |
| 20 | + |
| 21 | + if (enginePaths.Length < 1) |
| 22 | + throw new FileNotFoundException("UnityEngine.dll"); |
| 23 | + else |
| 24 | + { |
| 25 | + // use the first instance of UnityEngine.dll found |
| 26 | + enginePath = enginePaths.First(); |
| 27 | + managedPath = Path.GetDirectoryName(enginePath) ?? string.Empty; |
| 28 | + |
| 29 | + // add the Managed directory as a search path |
| 30 | + resolver.AddSearchDirectory(managedPath); |
| 31 | + |
| 32 | + // copy the plugin dlls to the managed path |
| 33 | + if (File.Exists("PluginManager.dll")) |
| 34 | + File.Copy("PluginManager.dll", Path.Combine(managedPath, "PluginManager.dll"), true); |
| 35 | + } |
| 36 | + |
| 37 | + // load the UnityEngine module |
| 38 | + var engine = ModuleDefinition.ReadModule( |
| 39 | + new MemoryStream(File.ReadAllBytes(enginePath)), |
| 40 | + new ReaderParameters { AssemblyResolver = resolver }); |
| 41 | + var gameObject = engine.GetType("UnityEngine", "GameObject"); |
| 42 | + |
| 43 | + // load the PluginManager module |
| 44 | + var plugin = ModuleDefinition.ReadModule(Path.Combine(managedPath, "PluginManager.dll")); |
| 45 | + var manager = plugin.GetType("PluginManager", "PluginManager"); |
| 46 | + |
| 47 | + // has UnityEngine already been patched? |
| 48 | + if (gameObject.Methods.Any(m => m.Name == ".cctor")) |
| 49 | + { |
| 50 | + // remove the static constructor |
| 51 | + gameObject.Methods.Remove( |
| 52 | + gameObject.Methods.Single(m => m.Name == ".cctor")); |
| 53 | + |
| 54 | + // save changes |
| 55 | + engine.Write(enginePath); |
| 56 | + |
| 57 | + Console.WriteLine("Plugin manager uninstalled."); |
| 58 | + Console.WriteLine("Press any key to exit."); |
| 59 | + Console.ReadKey(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // create a static constructor |
| 64 | + var cctor = new MethodDefinition(".cctor", |
| 65 | + MethodAttributes.Private | MethodAttributes.Static | |
| 66 | + MethodAttributes.HideBySig | MethodAttributes.SpecialName | |
| 67 | + MethodAttributes.RTSpecialName | MethodAttributes.ReuseSlot, engine.TypeSystem.Void); |
| 68 | + |
| 69 | + var il = cctor.Body.GetILProcessor(); |
| 70 | + cctor.Body.Instructions.Add(il.Create(OpCodes.Call, |
| 71 | + engine.Import(manager.Methods.Single(m => m.Name == "Initialize")))); |
| 72 | + cctor.Body.Instructions.Add(il.Create(OpCodes.Ret)); |
| 73 | + |
| 74 | + // add the static constructor |
| 75 | + gameObject.Methods.Add(cctor); |
| 76 | + |
| 77 | + // save changes |
| 78 | + engine.Write(enginePath); |
| 79 | + Console.WriteLine("Plugin manager installed."); |
| 80 | + Console.WriteLine("Press any key to exit."); |
| 81 | + Console.ReadKey(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments