暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AssemblyNameProvider.cs 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.Compilation;
  6. using UnityEditor.PackageManager;
  7. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  8. namespace Packages.Rider.Editor.ProjectGeneration
  9. {
  10. internal class AssemblyNameProvider : IAssemblyNameProvider
  11. {
  12. private readonly Dictionary<string, PackageInfo> m_PackageInfoCache = new Dictionary<string, PackageInfo>();
  13. private readonly Dictionary<string, ResponseFileData> m_ResponseFilesCache = new Dictionary<string, ResponseFileData>();
  14. ProjectGenerationFlag m_ProjectGenerationFlag = (ProjectGenerationFlag)EditorPrefs.GetInt("unity_project_generation_flag", 3);
  15. public string[] ProjectSupportedExtensions => EditorSettings.projectGenerationUserExtensions;
  16. public string ProjectGenerationRootNamespace => EditorSettings.projectGenerationRootNamespace;
  17. private Assembly[] m_AllEditorAssemblies;
  18. private Assembly[] m_AllPlayerAssemblies;
  19. private Assembly[] m_AllAssemblies;
  20. public ProjectGenerationFlag ProjectGenerationFlag
  21. {
  22. get => m_ProjectGenerationFlag;
  23. private set
  24. {
  25. EditorPrefs.SetInt("unity_project_generation_flag", (int)value);
  26. m_ProjectGenerationFlag = value;
  27. }
  28. }
  29. public string GetAssemblyNameFromScriptPath(string path)
  30. {
  31. return CompilationPipeline.GetAssemblyNameFromScriptPath(path);
  32. }
  33. public Assembly[] GetAllAssemblies()
  34. {
  35. if (m_AllEditorAssemblies == null)
  36. {
  37. m_AllEditorAssemblies = GetAssembliesByType(AssembliesType.Editor);
  38. m_AllAssemblies = m_AllEditorAssemblies;
  39. }
  40. if (ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.PlayerAssemblies))
  41. {
  42. if (m_AllPlayerAssemblies == null)
  43. {
  44. m_AllPlayerAssemblies = GetAssembliesByType(AssembliesType.Player);
  45. m_AllAssemblies = new Assembly[m_AllEditorAssemblies.Length + m_AllPlayerAssemblies.Length];
  46. Array.Copy(m_AllEditorAssemblies, m_AllAssemblies, m_AllEditorAssemblies.Length);
  47. Array.Copy(m_AllPlayerAssemblies, 0, m_AllAssemblies, m_AllEditorAssemblies.Length, m_AllPlayerAssemblies.Length);
  48. }
  49. }
  50. return m_AllAssemblies;
  51. }
  52. private static Assembly[] GetAssembliesByType(AssembliesType type)
  53. {
  54. // This is a very expensive Unity call...
  55. var compilationPipelineAssemblies = CompilationPipeline.GetAssemblies(type);
  56. var assemblies = new Assembly[compilationPipelineAssemblies.Length];
  57. var i = 0;
  58. foreach (var compilationPipelineAssembly in compilationPipelineAssemblies)
  59. {
  60. // The CompilationPipeline's assemblies have an output path of Libraries/ScriptAssemblies
  61. // TODO: It might be worth using the app's copy of Assembly and updating output path when we need it
  62. // But that requires tracking editor and player assemblies separately
  63. var outputPath = type == AssembliesType.Editor
  64. ? $@"Temp\Bin\Debug\{compilationPipelineAssembly.name}\"
  65. : $@"Temp\Bin\Debug\{compilationPipelineAssembly.name}\Player\";
  66. assemblies[i] = new Assembly(
  67. compilationPipelineAssembly.name,
  68. outputPath,
  69. compilationPipelineAssembly.sourceFiles,
  70. compilationPipelineAssembly.defines,
  71. compilationPipelineAssembly.assemblyReferences,
  72. compilationPipelineAssembly.compiledAssemblyReferences,
  73. compilationPipelineAssembly.flags,
  74. compilationPipelineAssembly.compilerOptions
  75. #if UNITY_2020_2_OR_NEWER
  76. , compilationPipelineAssembly.rootNamespace
  77. #endif
  78. );
  79. i++;
  80. }
  81. return assemblies;
  82. }
  83. public Assembly GetNamedAssembly(string name)
  84. {
  85. foreach (var assembly in GetAllAssemblies())
  86. {
  87. if (assembly.name == name)
  88. return assembly;
  89. }
  90. return null;
  91. }
  92. public string GetProjectName(string name, string[] defines)
  93. {
  94. if (!ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.PlayerAssemblies))
  95. return name;
  96. return !defines.Contains("UNITY_EDITOR") ? name + ".Player" : name;
  97. }
  98. public IEnumerable<string> GetAllAssetPaths()
  99. {
  100. return AssetDatabase.GetAllAssetPaths();
  101. }
  102. private static string GetPackageRootDirectoryName(string assetPath)
  103. {
  104. const string packagesPrefix = "packages/";
  105. if (!assetPath.StartsWith(packagesPrefix, StringComparison.OrdinalIgnoreCase))
  106. {
  107. return null;
  108. }
  109. var followupSeparator = assetPath.IndexOf('/', packagesPrefix.Length);
  110. // Note that we return the first path segment without modifying/normalising case!
  111. return followupSeparator == -1 ? assetPath : assetPath.Substring(0, followupSeparator);
  112. }
  113. public PackageInfo GetPackageInfoForAssetPath(string assetPath)
  114. {
  115. var packageName = GetPackageRootDirectoryName(assetPath);
  116. if (packageName == null)
  117. {
  118. return null;
  119. }
  120. // Assume the package name casing is consistent. If it's not, we'll fall back to an uppercase variant that's
  121. // saved in the same dictionary. This gives us cheaper case sensitive matching, with a fallback if our assumption
  122. // is incorrect
  123. if (m_PackageInfoCache.TryGetValue(packageName, out var cachedPackageInfo))
  124. return cachedPackageInfo;
  125. var packageNameUpper = packageName.ToUpperInvariant();
  126. if (m_PackageInfoCache.TryGetValue(packageNameUpper, out cachedPackageInfo))
  127. return cachedPackageInfo;
  128. var result = PackageInfo.FindForAssetPath(packageName);
  129. m_PackageInfoCache[packageName] = result;
  130. m_PackageInfoCache[packageNameUpper] = result;
  131. return result;
  132. }
  133. public void ResetCaches()
  134. {
  135. m_PackageInfoCache.Clear();
  136. m_ResponseFilesCache.Clear();
  137. m_AllEditorAssemblies = null;
  138. m_AllPlayerAssemblies = null;
  139. }
  140. public bool IsInternalizedPackagePath(string path)
  141. {
  142. if (string.IsNullOrEmpty(path))
  143. {
  144. return false;
  145. }
  146. var packageInfo = GetPackageInfoForAssetPath(path);
  147. if (packageInfo == null)
  148. {
  149. return false;
  150. }
  151. var packageSource = packageInfo.source;
  152. switch (packageSource)
  153. {
  154. case PackageSource.Embedded:
  155. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Embedded);
  156. case PackageSource.Registry:
  157. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Registry);
  158. case PackageSource.BuiltIn:
  159. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.BuiltIn);
  160. case PackageSource.Unknown:
  161. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Unknown);
  162. case PackageSource.Local:
  163. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Local);
  164. case PackageSource.Git:
  165. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Git);
  166. #if UNITY_2019_3_OR_NEWER
  167. case PackageSource.LocalTarball:
  168. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.LocalTarBall);
  169. #endif
  170. }
  171. return false;
  172. }
  173. public ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory,
  174. ApiCompatibilityLevel apiCompatibilityLevel)
  175. {
  176. var key = responseFilePath + ":" + (int) apiCompatibilityLevel;
  177. if (!m_ResponseFilesCache.TryGetValue(key, out var responseFileData))
  178. {
  179. var systemReferenceDirectories =
  180. CompilationPipeline.GetSystemAssemblyDirectories(apiCompatibilityLevel);
  181. responseFileData = CompilationPipeline.ParseResponseFile(
  182. responseFilePath,
  183. projectDirectory,
  184. systemReferenceDirectories
  185. );
  186. m_ResponseFilesCache.Add(key, responseFileData);
  187. }
  188. return responseFileData;
  189. }
  190. public IEnumerable<string> GetRoslynAnalyzerPaths()
  191. {
  192. return PluginImporter.GetAllImporters()
  193. .Where(i => !i.isNativePlugin && AssetDatabase.GetLabels(i).SingleOrDefault(l => l == "RoslynAnalyzer") != null)
  194. .Select(i => i.assetPath);
  195. }
  196. public void ToggleProjectGeneration(ProjectGenerationFlag preference)
  197. {
  198. if (ProjectGenerationFlag.HasFlag(preference))
  199. {
  200. ProjectGenerationFlag ^= preference;
  201. }
  202. else
  203. {
  204. ProjectGenerationFlag |= preference;
  205. }
  206. }
  207. public void ResetProjectGenerationFlag()
  208. {
  209. ProjectGenerationFlag = ProjectGenerationFlag.None;
  210. }
  211. }
  212. }