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.

64 lines
2.0 KiB

6 years ago
6 years ago
6 years ago
  1. using IllusionInjector;
  2. using IPA.Loader;
  3. using IPA.Logging;
  4. using System;
  5. using System.IO;
  6. using System.Reflection;
  7. using UnityEngine;
  8. using static IPA.Logging.Logger;
  9. using Logger = IPA.Logging.Logger;
  10. namespace IPA.Injector
  11. {
  12. public static class Injector
  13. {
  14. private static bool injected = false;
  15. public static void Inject()
  16. {
  17. if (!injected)
  18. {
  19. injected = true;
  20. AppDomain.CurrentDomain.AssemblyResolve += AssemblyLibLoader;
  21. var bootstrapper = new GameObject("Bootstrapper").AddComponent<Bootstrapper>();
  22. bootstrapper.Destroyed += Bootstrapper_Destroyed;
  23. }
  24. }
  25. private static string libsDir;
  26. private static Assembly AssemblyLibLoader(object source, ResolveEventArgs e)
  27. {
  28. if (libsDir == null)
  29. libsDir = Path.Combine(Environment.CurrentDirectory, "Libs");
  30. var asmName = new AssemblyName(e.Name);
  31. Log(Level.Debug, $"Resolving library {asmName}");
  32. var testFilen = Path.Combine(libsDir, $"{asmName.Name}.{asmName.Version}.dll");
  33. Log(Level.Debug, $"Looking for file {testFilen}");
  34. if (File.Exists(testFilen))
  35. return Assembly.LoadFile(testFilen);
  36. Log(Level.Critical, $"Could not load library {asmName}");
  37. return null;
  38. }
  39. private static void Log(Level lvl, string message)
  40. { // multiple proxy methods to delay loading of assemblies until it's done
  41. if (Logger.LogCreated)
  42. AssemblyLibLoaderCallLogger(lvl, message);
  43. else
  44. if (((byte)lvl & (byte)StandardLogger.PrintFilter) != 0)
  45. Console.WriteLine($"[{lvl}] {message}");
  46. }
  47. private static void AssemblyLibLoaderCallLogger(Level lvl, string message)
  48. {
  49. Logger.log.Log(lvl, message);
  50. }
  51. private static void Bootstrapper_Destroyed()
  52. {
  53. PluginComponent.Create();
  54. }
  55. }
  56. }