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.

63 lines
2.0 KiB

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