You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.5 KiB

  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace IllusionInjector
  6. {
  7. public static class Injector
  8. {
  9. private static bool injected = false;
  10. public static void Inject()
  11. {
  12. if (!injected)
  13. {
  14. injected = true;
  15. AppDomain.CurrentDomain.AssemblyResolve += AssemblyLibLoader;
  16. var bootstrapper = new GameObject("Bootstrapper").AddComponent<Bootstrapper>();
  17. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  18. }
  19. }
  20. private static string libsDir;
  21. private static Assembly AssemblyLibLoader(object source, ResolveEventArgs e)
  22. {
  23. if (libsDir == null)
  24. libsDir = Path.Combine(Environment.CurrentDirectory, "Libs");
  25. var asmName = new AssemblyName(e.Name);
  26. //Logger.log.Debug($"Resolving library {asmName}");
  27. var testFilen = Path.Combine(libsDir, $"{asmName.Name}.{asmName.Version}.dll");
  28. //Logger.log.Debug($"Looking for file {testFilen}");
  29. if (File.Exists(testFilen))
  30. {
  31. return Assembly.LoadFile(testFilen);
  32. }
  33. //Logger.log.Error($"Could not load library {asmName}");
  34. return null;
  35. }
  36. private static void Bootstrapper_Destroyed()
  37. {
  38. PluginComponent.Create();
  39. }
  40. }
  41. }