AssemblyNameProvider.cs 8.9 KB

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