ScriptsReferencesHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace ET
  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. // 自动把各个包中的引用加到Assets对应的包中去,后面搞个编辑器来编辑每个包的引用
  32. //[MenuItem("ET/UpdateScriptsReferences")]
  33. public static void Run()
  34. {
  35. Dictionary<string, HashSet<string>> refs = new ()
  36. {
  37. {"Model", new HashSet<string>()},
  38. {"Hotfix", new HashSet<string>()},
  39. {"ModelView", new HashSet<string>()},
  40. {"HotfixView", new HashSet<string>()}
  41. };
  42. foreach (string directory in Directory.GetDirectories("Packages", "cn.etetet.*"))
  43. {
  44. PackageGit packageGit = PackageGitHelper.Load(Path.Combine(directory, "packagegit.json"));
  45. if (packageGit.ScriptsReferences == null)
  46. {
  47. continue;
  48. }
  49. foreach ((string assName, string[] references) in packageGit.ScriptsReferences)
  50. {
  51. foreach (string s in references)
  52. {
  53. refs[assName].Add(s);
  54. }
  55. }
  56. }
  57. string fourAssemblyDir = null;
  58. foreach (string directory in Directory.GetDirectories("Packages", "cn.etetet.*"))
  59. {
  60. if (File.Exists(Path.Combine(directory, "Runtime/Model/ET.Model.asmdef")))
  61. {
  62. fourAssemblyDir = directory;
  63. break;
  64. }
  65. }
  66. List<string> findRet = new List<string>();
  67. foreach ((string assName, HashSet<string> refAss) in refs)
  68. {
  69. findRet.Clear();
  70. FileHelper.GetAllFiles(findRet, fourAssemblyDir, $"ET.{assName}.asmdef");
  71. string p = findRet[0];
  72. if (!File.Exists(p))
  73. {
  74. throw new Exception($"not found: {p}");
  75. }
  76. string json = File.ReadAllText(p);
  77. AssemblyDefinitionAsset assemblyDefinitionAsset = JsonUtility.FromJson<AssemblyDefinitionAsset>(json);
  78. List<string> list = refAss.ToList();
  79. list.Sort();
  80. assemblyDefinitionAsset.references = list.ToArray();
  81. File.WriteAllText(p, JsonUtility.ToJson(assemblyDefinitionAsset, true));
  82. }
  83. AssetDatabase.SaveAssets();
  84. AssetDatabase.Refresh();
  85. }
  86. }
  87. }