DllHelper.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace Model
  7. {
  8. public static class DllHelper
  9. {
  10. public static Assembly LoadHotfixAssembly()
  11. {
  12. GameObject code = (GameObject)Resources.Load("Code");
  13. byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
  14. byte[] mdbBytes = code.Get<TextAsset>("Hotfix.dll.mdb").bytes;
  15. Assembly assembly = Assembly.Load(assBytes, mdbBytes);
  16. return assembly;
  17. }
  18. public static Type[] GetMonoTypes()
  19. {
  20. List<Type> types = new List<Type>();
  21. Assembly[] assemblies = ObjectEvents.Instance.GetAssemblies();
  22. foreach (Assembly assembly in assemblies)
  23. {
  24. Type[] t = assembly.GetTypes();
  25. types.AddRange(t);
  26. }
  27. return types.ToArray();
  28. }
  29. public static Type[] GetHotfixTypes()
  30. {
  31. ILRuntime.Runtime.Enviorment.AppDomain appDomain = ObjectEvents.Instance.AppDomain;
  32. if (appDomain == null)
  33. {
  34. return new Type[0];
  35. }
  36. return appDomain.LoadedTypes.Values.Select(x => x.ReflectionType).ToArray();
  37. }
  38. public static object CreateHotfixObject(Type type)
  39. {
  40. object obj = ObjectEvents.Instance.AppDomain.Instantiate(type.FullName);
  41. return obj;
  42. }
  43. public static T CreateHotfixObject<T>(Type type) where T: class
  44. {
  45. T obj = ObjectEvents.Instance.AppDomain.Instantiate<T>(type.FullName);
  46. return obj;
  47. }
  48. }
  49. }