InjectionSettings.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEditorInternal;
  7. using UnityEngine;
  8. using System.Collections.Generic;
  9. namespace com.bbbirder.injection.editor
  10. {
  11. [FilePath(savePath, FilePathAttribute.Location.ProjectFolder)]
  12. public class InjectionSettings : ScriptableSingleton<InjectionSettings>
  13. {
  14. const string savePath = "Library/InjectionSettings.asset";
  15. [Serializable]
  16. public struct AssemblyRecord
  17. {
  18. public string path;
  19. public long lastModifyTime;
  20. }
  21. [SerializeField]
  22. public bool enabled = true;
  23. [SerializeField]
  24. bool autoInjectEditor = true;
  25. [SerializeField]
  26. bool autoInjectBuild = true;
  27. [SerializeField]
  28. public List<AssemblyRecord> injectionSources = new();
  29. [SerializeField]
  30. public List<string> compilationErrorAssemblies = new();
  31. public bool ShouldAutoInjectEditor => enabled && autoInjectEditor;
  32. public bool ShouldAutoInjectBuild => enabled && autoInjectBuild;
  33. public void Save(){
  34. base.Save(true);
  35. }
  36. // public Assembly[] GetAssemblies()
  37. // {
  38. // var hashset = injectionSources
  39. // .Select(r => Path.Join(Directory.GetCurrentDirectory(), r.path))
  40. // .Select(p => p.Replace('\\', '/'))
  41. // .ToHashSet();
  42. // return AppDomain.CurrentDomain.GetAssemblies()
  43. // .Where(a => hashset.Contains(a.GetAssemblyPath()))
  44. // .ToArray();
  45. // }
  46. public void SetInjectionSources(IEnumerable<string> sources)
  47. {
  48. injectionSources.Clear();
  49. foreach (var path in sources)
  50. {
  51. var lwts = File.GetLastWriteTimeUtc(path).ToFileTimeUtc();
  52. injectionSources.Add(new()
  53. {
  54. path = path,
  55. lastModifyTime = lwts
  56. });
  57. }
  58. Save();
  59. }
  60. public void GetOutdatedSources(IEnumerable<string> sources, List<string> outdated)
  61. {
  62. var srcDict = injectionSources.ToDictionary(r => r.path, r => r.lastModifyTime);
  63. outdated.AddRange(
  64. sources.Where(s =>
  65. !srcDict.TryGetValue(s, out var lwts) || lwts < File.GetLastWriteTimeUtc(s).ToFileTimeUtc()
  66. )
  67. );
  68. }
  69. public bool CheckShouldUpdate(string path)
  70. {
  71. var srcDict = injectionSources.ToDictionary(r => r.path, r => r.lastModifyTime);
  72. if (!srcDict.TryGetValue(path, out var lwts)) return true;
  73. if (lwts < File.GetLastWriteTimeUtc(path).ToFileTimeUtc()) return true;
  74. return false;
  75. }
  76. }
  77. }