ScriptsReferencesHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace YIUIFramework.Editor
  8. {
  9. public class AssemblyDefinitionAsset
  10. {
  11. public string name;
  12. public string rootNamespace;
  13. public string[] references;
  14. public string[] includePlatforms;
  15. public string[] excludePlatforms;
  16. public bool allowUnsafeCode;
  17. public bool overrideReferences;
  18. public string[] precompiledReferences;
  19. public bool autoReferenced;
  20. public string[] defineConstraints;
  21. public string[] versionDefines;
  22. public bool noEngineReferences;
  23. public string[] optionalUnityReferences;
  24. public string[] additionalReferences;
  25. public string compilerOptions;
  26. }
  27. public static class ScriptsReferencesHelper
  28. {
  29. public static readonly string[] AssNames = { "Model", "ModelView", "Hotfix", "HotfixView" };
  30. public static readonly string[] PackagePaths = { "Packages", "Library/PackageCache" };
  31. public static void Run(bool refresh = false)
  32. {
  33. Dictionary<string, HashSet<string>> refs = new()
  34. {
  35. { "Model", new HashSet<string>() },
  36. { "Hotfix", new HashSet<string>() },
  37. { "ModelView", new HashSet<string>() },
  38. { "HotfixView", new HashSet<string>() }
  39. };
  40. foreach (string directory in Directory.GetDirectories("Packages", "cn.etetet.*"))
  41. {
  42. PackageGit packageGit = PackageGitHelper.Load(Path.Combine(directory, "packagegit.json"));
  43. if (packageGit?.ScriptsReferences == null)
  44. {
  45. continue;
  46. }
  47. foreach ((string assName, string[] references) in packageGit.ScriptsReferences)
  48. {
  49. foreach (string s in references)
  50. {
  51. refs[assName].Add(s);
  52. }
  53. }
  54. }
  55. string fourAssemblyDir = null;
  56. foreach (string directory in Directory.GetDirectories("Packages", "cn.etetet.*"))
  57. {
  58. if (File.Exists(Path.Combine(directory, "Runtime/Model/ET.Model.asmdef")))
  59. {
  60. fourAssemblyDir = directory;
  61. break;
  62. }
  63. }
  64. List<string> findRet = new List<string>();
  65. foreach ((string assName, HashSet<string> refAss) in refs)
  66. {
  67. findRet.Clear();
  68. FileHelper.GetAllFiles(findRet, fourAssemblyDir, $"ET.{assName}.asmdef");
  69. string p = findRet[0];
  70. if (!File.Exists(p))
  71. {
  72. throw new Exception($"not found: {p}");
  73. }
  74. string json = File.ReadAllText(p);
  75. AssemblyDefinitionAsset assemblyDefinitionAsset = JsonUtility.FromJson<AssemblyDefinitionAsset>(json);
  76. List<string> list = refAss.ToList();
  77. list.Sort();
  78. assemblyDefinitionAsset.references = list.ToArray();
  79. File.WriteAllText(p, JsonUtility.ToJson(assemblyDefinitionAsset, true));
  80. }
  81. if (refresh)
  82. {
  83. AssetDatabase.SaveAssets();
  84. AssetDatabase.Refresh();
  85. }
  86. }
  87. }
  88. }