ProjectPart.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor.Compilation;
  4. using UnityEngine;
  5. namespace SingularityGroup.HotReload.Editor.ProjectGeneration
  6. {
  7. internal class ProjectPart
  8. {
  9. public string Name { get; }
  10. public string OutputPath { get; }
  11. public Assembly Assembly { get; }
  12. public string AssetsProjectPart { get; }
  13. public string[] SourceFiles { get; }
  14. public string RootNamespace { get; }
  15. public Assembly[] AssemblyReferences { get; }
  16. public string[] CompiledAssemblyReferences { get; }
  17. public string[] Defines { get; }
  18. public ScriptCompilerOptions CompilerOptions { get; }
  19. public ProjectPart(string name, Assembly assembly, string assetsProjectPart, string fallbackRootNamespace, HashSet<string> additionalSourceFiles)
  20. {
  21. Name = name;
  22. Assembly = assembly;
  23. AssetsProjectPart = assetsProjectPart;
  24. OutputPath = assembly != null ? assembly.outputPath : "Temp/Bin/Debug";
  25. SourceFiles = assembly != null ? assembly.sourceFiles.Union(additionalSourceFiles).ToArray() : new string[0];
  26. #if UNITY_2020_2_OR_NEWER
  27. RootNamespace = assembly != null ? assembly.rootNamespace : string.Empty;
  28. #else
  29. RootNamespace = fallbackRootNamespace;
  30. #endif
  31. AssemblyReferences = assembly != null ? assembly.assemblyReferences : new Assembly[0];
  32. CompiledAssemblyReferences = assembly!=null? assembly.compiledAssemblyReferences:new string[0];
  33. Defines = assembly != null ? assembly.defines : new string[0];
  34. CompilerOptions = assembly != null ? assembly.compilerOptions : new ScriptCompilerOptions();
  35. }
  36. public IEnumerable<ResponseFileData> ParseResponseFileData(string projectDirectory)
  37. {
  38. #if UNITY_2019_1_OR_NEWER
  39. if (Assembly == null)
  40. return new ResponseFileData[0];
  41. var systemReferenceDirectories =
  42. CompilationPipeline.GetSystemAssemblyDirectories(Assembly.compilerOptions.ApiCompatibilityLevel);
  43. var responseFilesData = Assembly.compilerOptions.ResponseFiles.ToDictionary(
  44. x => x, x => CompilationPipeline.ParseResponseFile(
  45. x,
  46. projectDirectory,
  47. systemReferenceDirectories
  48. ));
  49. var responseFilesWithErrors = responseFilesData.Where(x => x.Value.Errors.Any())
  50. .ToDictionary(x => x.Key, x => x.Value);
  51. if (responseFilesWithErrors.Any())
  52. {
  53. foreach (var error in responseFilesWithErrors)
  54. foreach (var valueError in error.Value.Errors)
  55. {
  56. Log.Error("{0} Parse Error : {1}", error.Key, valueError);
  57. }
  58. }
  59. return responseFilesData.Select(x => x.Value);
  60. #else
  61. return new ResponseFileData[0];
  62. #endif
  63. }
  64. }
  65. }
  66. #if !UNITY_2019_1_OR_NEWER
  67. namespace UnityEditor.Compilation {
  68. internal class ResponseFileData {
  69. public string[] Defines;
  70. public string[] FullPathReferences;
  71. public string[] Errors;
  72. public string[] OtherArguments;
  73. public bool Unsafe;
  74. }
  75. }
  76. #endif