AssemblyNameProvider.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Unity Technologies.
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. * Licensed under the MIT License. See License.txt in the project root for license information.
  5. *--------------------------------------------------------------------------------------------*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using UnityEditor;
  10. using UnityEditor.Compilation;
  11. using UnityEditor.PackageManager;
  12. namespace Microsoft.Unity.VisualStudio.Editor
  13. {
  14. public interface IAssemblyNameProvider
  15. {
  16. string[] ProjectSupportedExtensions { get; }
  17. string ProjectGenerationRootNamespace { get; }
  18. ProjectGenerationFlag ProjectGenerationFlag { get; }
  19. string GetAssemblyNameFromScriptPath(string path);
  20. string GetAssemblyName(string assemblyOutputPath, string assemblyName);
  21. bool IsInternalizedPackagePath(string path);
  22. IEnumerable<Assembly> GetAssemblies(Func<string, bool> shouldFileBePartOfSolution);
  23. IEnumerable<string> GetAllAssetPaths();
  24. UnityEditor.PackageManager.PackageInfo FindForAssetPath(string assetPath);
  25. ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory, string[] systemReferenceDirectories);
  26. void ToggleProjectGeneration(ProjectGenerationFlag preference);
  27. }
  28. public class AssemblyNameProvider : IAssemblyNameProvider
  29. {
  30. private readonly Dictionary<string, UnityEditor.PackageManager.PackageInfo> m_PackageInfoCache = new Dictionary<string, UnityEditor.PackageManager.PackageInfo>();
  31. ProjectGenerationFlag m_ProjectGenerationFlag = (ProjectGenerationFlag)EditorPrefs.GetInt(
  32. "unity_project_generation_flag",
  33. (int)(ProjectGenerationFlag.Local | ProjectGenerationFlag.Embedded));
  34. public string[] ProjectSupportedExtensions => EditorSettings.projectGenerationUserExtensions;
  35. public string ProjectGenerationRootNamespace => EditorSettings.projectGenerationRootNamespace;
  36. public ProjectGenerationFlag ProjectGenerationFlag
  37. {
  38. get => m_ProjectGenerationFlag;
  39. private set
  40. {
  41. EditorPrefs.SetInt("unity_project_generation_flag", (int)value);
  42. m_ProjectGenerationFlag = value;
  43. }
  44. }
  45. public string GetAssemblyNameFromScriptPath(string path)
  46. {
  47. return CompilationPipeline.GetAssemblyNameFromScriptPath(path);
  48. }
  49. public IEnumerable<Assembly> GetAssemblies(Func<string, bool> shouldFileBePartOfSolution)
  50. {
  51. IEnumerable<Assembly> assemblies = GetAssembliesByType(AssembliesType.Editor, shouldFileBePartOfSolution, @"Temp\Bin\Debug\");
  52. if (!ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.PlayerAssemblies))
  53. {
  54. return assemblies;
  55. }
  56. var playerAssemblies = GetAssembliesByType(AssembliesType.Player, shouldFileBePartOfSolution, @"Temp\Bin\Debug\Player\");
  57. return assemblies.Concat(playerAssemblies);
  58. }
  59. private static IEnumerable<Assembly> GetAssembliesByType(AssembliesType type, Func<string, bool> shouldFileBePartOfSolution, string outputPath)
  60. {
  61. foreach (var assembly in CompilationPipeline.GetAssemblies(type))
  62. {
  63. if (assembly.sourceFiles.Any(shouldFileBePartOfSolution))
  64. {
  65. yield return new Assembly(
  66. assembly.name,
  67. outputPath,
  68. assembly.sourceFiles,
  69. assembly.defines,
  70. assembly.assemblyReferences,
  71. assembly.compiledAssemblyReferences,
  72. assembly.flags,
  73. assembly.compilerOptions
  74. #if UNITY_2020_2_OR_NEWER
  75. , assembly.rootNamespace
  76. #endif
  77. );
  78. }
  79. }
  80. }
  81. public string GetCompileOutputPath(string assemblyName)
  82. {
  83. return assemblyName.EndsWith(".Player", StringComparison.Ordinal) ? @"Temp\Bin\Debug\Player\" : @"Temp\Bin\Debug\";
  84. }
  85. public IEnumerable<string> GetAllAssetPaths()
  86. {
  87. return AssetDatabase.GetAllAssetPaths();
  88. }
  89. private static string ResolvePotentialParentPackageAssetPath(string assetPath)
  90. {
  91. const string packagesPrefix = "packages/";
  92. if (!assetPath.StartsWith(packagesPrefix, StringComparison.OrdinalIgnoreCase))
  93. {
  94. return null;
  95. }
  96. var followupSeparator = assetPath.IndexOf('/', packagesPrefix.Length);
  97. if (followupSeparator == -1)
  98. {
  99. return assetPath.ToLowerInvariant();
  100. }
  101. return assetPath.Substring(0, followupSeparator).ToLowerInvariant();
  102. }
  103. public UnityEditor.PackageManager.PackageInfo FindForAssetPath(string assetPath)
  104. {
  105. var parentPackageAssetPath = ResolvePotentialParentPackageAssetPath(assetPath);
  106. if (parentPackageAssetPath == null)
  107. {
  108. return null;
  109. }
  110. if (m_PackageInfoCache.TryGetValue(parentPackageAssetPath, out var cachedPackageInfo))
  111. {
  112. return cachedPackageInfo;
  113. }
  114. var result = UnityEditor.PackageManager.PackageInfo.FindForAssetPath(parentPackageAssetPath);
  115. m_PackageInfoCache[parentPackageAssetPath] = result;
  116. return result;
  117. }
  118. public bool IsInternalizedPackagePath(string path)
  119. {
  120. if (string.IsNullOrEmpty(path.Trim()))
  121. {
  122. return false;
  123. }
  124. var packageInfo = FindForAssetPath(path);
  125. if (packageInfo == null)
  126. {
  127. return false;
  128. }
  129. var packageSource = packageInfo.source;
  130. switch (packageSource)
  131. {
  132. case PackageSource.Embedded:
  133. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Embedded);
  134. case PackageSource.Registry:
  135. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Registry);
  136. case PackageSource.BuiltIn:
  137. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.BuiltIn);
  138. case PackageSource.Unknown:
  139. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Unknown);
  140. case PackageSource.Local:
  141. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Local);
  142. case PackageSource.Git:
  143. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.Git);
  144. case PackageSource.LocalTarball:
  145. return !ProjectGenerationFlag.HasFlag(ProjectGenerationFlag.LocalTarBall);
  146. }
  147. return false;
  148. }
  149. public ResponseFileData ParseResponseFile(string responseFilePath, string projectDirectory, string[] systemReferenceDirectories)
  150. {
  151. return CompilationPipeline.ParseResponseFile(
  152. responseFilePath,
  153. projectDirectory,
  154. systemReferenceDirectories
  155. );
  156. }
  157. public void ToggleProjectGeneration(ProjectGenerationFlag preference)
  158. {
  159. if (ProjectGenerationFlag.HasFlag(preference))
  160. {
  161. ProjectGenerationFlag ^= preference;
  162. }
  163. else
  164. {
  165. ProjectGenerationFlag |= preference;
  166. }
  167. }
  168. internal void ResetPackageInfoCache()
  169. {
  170. m_PackageInfoCache.Clear();
  171. }
  172. public void ResetProjectGenerationFlag()
  173. {
  174. ProjectGenerationFlag = ProjectGenerationFlag.None;
  175. }
  176. public string GetAssemblyName(string assemblyOutputPath, string assemblyName)
  177. {
  178. return assemblyOutputPath.EndsWith(@"\Player\", StringComparison.Ordinal) ? assemblyName + ".Player" : assemblyName;
  179. }
  180. }
  181. }