A game about forced loneliness, made by TACStudios
at master 3.3 kB view raw
1using System; 2using System.IO; 3using System.Linq; 4using UnityEditor.Scripting.ScriptCompilation; 5 6namespace UnityEditor.TestTools.TestRunner.GUI.TestAssets 7{ 8 /// <inheritdoc /> 9 internal class CustomScriptAssemblyMappingFinder : ICustomScriptAssemblyMappingFinder 10 { 11 /// <inheritdoc /> 12 /// <exception cref="ArgumentNullException">The provided <paramref name="folderPath" /> string argument is null.</exception> 13 public ICustomScriptAssembly FindCustomScriptAssemblyFromFolderPath(string folderPath) 14 { 15 if (folderPath == null) 16 { 17 throw new ArgumentNullException(nameof(folderPath)); 18 } 19 20 var scriptInFolderPath = Path.Combine(folderPath, "Foo.cs"); 21 var customScriptAssembly = FindCustomScriptAssemblyFromScriptPath(scriptInFolderPath); 22 return customScriptAssembly; 23 } 24 25 /// <summary> 26 /// Finds the Custom Script Assembly associated with the provided script asset path. 27 /// </summary> 28 /// <param name="scriptPath">The script path to check.</param> 29 /// <returns>The associated <see cref="ICustomScriptAssembly" />; null if none.</returns> 30 private static ICustomScriptAssembly FindCustomScriptAssemblyFromScriptPath(string scriptPath) 31 { 32 try 33 { 34 var customScriptAssembly = EditorCompilationInterface.Instance.FindCustomScriptAssemblyFromScriptPath(scriptPath); 35 return new CustomScriptAssemblyWrapper(customScriptAssembly); 36 } 37 catch (Exception) 38 { 39 return null; 40 } 41 } 42 43 /// <summary> 44 /// Custom Script Assembly wrapper. 45 /// </summary> 46 internal class CustomScriptAssemblyWrapper : ICustomScriptAssembly 47 { 48 internal readonly CustomScriptAssembly targetAssembly; 49 50 /// <summary> 51 /// Creates a new instance of the <see cref="CustomScriptAssemblyWrapper" /> class. 52 /// </summary> 53 /// <param name="assembly">The <see cref="CustomScriptAssembly" /> to be represented by the wrapper.</param> 54 /// <exception cref="ArgumentNullException">The provided <paramref name="assembly" /> argument is null.</exception> 55 internal CustomScriptAssemblyWrapper(CustomScriptAssembly assembly) 56 { 57 targetAssembly = assembly 58 ?? throw new ArgumentNullException(nameof(assembly), "The provided assembly must not be null."); 59 } 60 61 /// <inheritdoc /> 62 public bool HasPrecompiledReference(string libraryFilename) 63 { 64 var precompiledReferences = targetAssembly.PrecompiledReferences; 65 var libraryReferenceExists = precompiledReferences != null 66 && precompiledReferences.Any(r => Path.GetFileName(r) == libraryFilename); 67 return libraryReferenceExists; 68 } 69 70 /// <inheritdoc /> 71 public bool HasAssemblyFlag(AssemblyFlags flag) 72 { 73 var hasAssemblyFlag = (targetAssembly.AssemblyFlags & flag) == flag; 74 return hasAssemblyFlag; 75 } 76 } 77 } 78}