PluginProcessor.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #if UNITY_2018_1_OR_NEWER || (UNITY_2017_4_OR_NEWER && !UNITY_2017_4_0 && !UNITY_2017_4_1 && !UNITY_2017_4_2 && !UNITY_2017_4_3 && !UNITY_2017_4_4 && !UNITY_2017_4_5 && !UNITY_2017_4_6 && !UNITY_2017_4_7 && !UNITY_2017_4_8 && !UNITY_2017_4_9 && !UNITY_2017_4_10 && !UNITY_2017_4_11 && !UNITY_2017_4_12 && !UNITY_2017_4_13 && !UNITY_2017_4_14 && !UNITY_2017_4_15 && !UNITY_2017_4_15)
  2. // Unity added Android ARM64 support in 2018.1, and backported to 2017.4.16
  3. #define AVPROMC_UNITY_ANDROID_ARM64_SUPPORT
  4. #endif
  5. #if !UNITY_2019_3_OR_NEWER || UNITY_2021_2_OR_NEWER || (UNITY_2020_3_OR_NEWER && !UNITY_2020_3_0 && !UNITY_2020_3_1 && !UNITY_2020_3_2 && !UNITY_2020_3_3 && !UNITY_2020_3_4 && !UNITY_2020_3_5 && !UNITY_2020_3_6 && !UNITY_2020_3_7 && !UNITY_2020_3_8 && !UNITY_2020_3_9 && !UNITY_2020_3_10 && !UNITY_2020_3_11 && !UNITY_2020_3_12 && !UNITY_2020_3_13 && !UNITY_2020_3_14 && !UNITY_2020_3_15 && !UNITY_2020_3_16) || (UNITY_2019_4_OR_NEWER && !UNITY_2019_4_0 && !UNITY_2019_4_1 && !UNITY_2019_4_2 && !UNITY_2019_4_3 && !UNITY_2019_4_4 && !UNITY_2019_4_5 && !UNITY_2019_4_6 && !UNITY_2019_4_7 && !UNITY_2019_4_8 && !UNITY_2019_4_9 && !UNITY_2019_4_10 && !UNITY_2019_4_11 && !UNITY_2019_4_12 && !UNITY_2019_4_13 && !UNITY_2019_4_14 && !UNITY_2019_4_15 && !UNITY_2019_4_16 && !UNITY_2019_4_17 && !UNITY_2019_4_18 && !UNITY_2019_4_19 && !UNITY_2019_4_20 && !UNITY_2019_4_21 && !UNITY_2019_4_22 && !UNITY_2019_4_23 && !UNITY_2019_4_24 && !UNITY_2019_4_25 && !UNITY_2019_4_26 && !UNITY_2019_4_27 && !UNITY_2019_4_28 && !UNITY_2019_4_29 && !UNITY_2019_4_30)
  6. // Unity dropped Android x86 support in 2019, but then added it back in 2021.2.0 and backported to 2020.3.17 and 2019.4.31
  7. #define AVPROMC_UNITY_ANDROID_X86_SUPPORT
  8. #endif
  9. #if UNITY_2021_2_OR_NEWER || (UNITY_2020_3_OR_NEWER && !UNITY_2020_3_0 && !UNITY_2020_3_1 && !UNITY_2020_3_2 && !UNITY_2020_3_3 && !UNITY_2020_3_4 && !UNITY_2020_3_5 && !UNITY_2020_3_6 && !UNITY_2020_3_7 && !UNITY_2020_3_8 && !UNITY_2020_3_9 && !UNITY_2020_3_10 && !UNITY_2020_3_11 && !UNITY_2020_3_12 && !UNITY_2020_3_13 && !UNITY_2020_3_14 && !UNITY_2020_3_15 && !UNITY_2020_3_16) || (UNITY_2019_4_OR_NEWER && !UNITY_2019_4_0 && !UNITY_2019_4_1 && !UNITY_2019_4_2 && !UNITY_2019_4_3 && !UNITY_2019_4_4 && !UNITY_2019_4_5 && !UNITY_2019_4_6 && !UNITY_2019_4_7 && !UNITY_2019_4_8 && !UNITY_2019_4_9 && !UNITY_2019_4_10 && !UNITY_2019_4_11 && !UNITY_2019_4_12 && !UNITY_2019_4_13 && !UNITY_2019_4_14 && !UNITY_2019_4_15 && !UNITY_2019_4_16 && !UNITY_2019_4_17 && !UNITY_2019_4_18 && !UNITY_2019_4_19 && !UNITY_2019_4_20 && !UNITY_2019_4_21 && !UNITY_2019_4_22 && !UNITY_2019_4_23 && !UNITY_2019_4_24 && !UNITY_2019_4_25 && !UNITY_2019_4_26 && !UNITY_2019_4_27 && !UNITY_2019_4_28 && !UNITY_2019_4_29 && !UNITY_2019_4_30)
  10. // Unity added Android x86_64 support in 2021.2.0 and backported to 2020.3.17 and 2019.4.31
  11. #define AVPROMC_UNITY_ANDROID_X8664_SUPPORT
  12. #endif
  13. #if UNITY_2019_1_OR_NEWER
  14. #define AVPROMC_UNITY_UWP_ARM64_SUPPORT
  15. #endif
  16. #if UNITY_2018_1_OR_NEWER
  17. #define AVPROMC_UNITY_BUILDWITHREPORT_SUPPORT
  18. #endif
  19. using UnityEngine;
  20. using UnityEditor;
  21. using UnityEditor.Build;
  22. #if AVPROMC_UNITY_BUILDWITHREPORT_SUPPORT
  23. using UnityEditor.Build.Reporting;
  24. #endif
  25. using System.Collections.Generic;
  26. //-----------------------------------------------------------------------------
  27. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  28. //-----------------------------------------------------------------------------
  29. namespace RenderHeads.Media.AVProMovieCapture.Editor
  30. {
  31. /// <summary>
  32. /// Some versions of Unity do not support specific CPU architectures for plugin files
  33. /// so this Build Preprocessor checks the plugin files for those and either disables
  34. /// them if their arch is not supported, or assigns the correct arch and enables them
  35. /// </summary>
  36. public class PluginProcessor :
  37. #if AVPROMC_UNITY_BUILDWITHREPORT_SUPPORT
  38. IPreprocessBuildWithReport
  39. #else
  40. IPreprocessBuild
  41. #endif
  42. {
  43. internal class CpuArchitecture
  44. {
  45. internal CpuArchitecture(string code, bool isSupportedByThisUnityVersion)
  46. {
  47. _code = code;
  48. _isSupportedByThisUnityVersion = isSupportedByThisUnityVersion;
  49. }
  50. private string _code;
  51. private bool _isSupportedByThisUnityVersion;
  52. internal string Code()
  53. {
  54. return _code;
  55. }
  56. internal bool IsSupportedByThisUnityVersion()
  57. {
  58. return _isSupportedByThisUnityVersion;
  59. }
  60. }
  61. internal class PluginFile
  62. {
  63. internal PluginFile(BuildTarget buildTarget, string relativeFilePath, bool supportsEditor, CpuArchitecture cpuArchitecture)
  64. {
  65. _buildTarget = buildTarget;
  66. _relativeFilePath = relativeFilePath;
  67. _cpuArchitecture = cpuArchitecture;
  68. _supportsEditor = supportsEditor;
  69. }
  70. internal bool IsBuildTarget(BuildTarget buildTarget)
  71. {
  72. return (_buildTarget == buildTarget);
  73. }
  74. internal BuildTarget BuildTarget()
  75. {
  76. return _buildTarget;
  77. }
  78. internal bool IsForFile(string path)
  79. {
  80. return path.Replace("\\", "/").Contains(_relativeFilePath);
  81. }
  82. internal bool IsSupportedByThisUnityVersion()
  83. {
  84. return _cpuArchitecture.IsSupportedByThisUnityVersion();
  85. }
  86. internal string CpuArchitectureCode()
  87. {
  88. return _cpuArchitecture.Code();
  89. }
  90. internal bool SupportsEditor()
  91. {
  92. return _supportsEditor;
  93. }
  94. private BuildTarget _buildTarget;
  95. private string _relativeFilePath;
  96. private CpuArchitecture _cpuArchitecture;
  97. private bool _supportsEditor;
  98. }
  99. private static List<PluginFile> _pluginFiles = new List<PluginFile>(32);
  100. internal static void AddPluginFiles(BuildTarget buildTarget, string[] filenames, string folderPrefix, bool supportsEditor, CpuArchitecture cpuArchitecture)
  101. {
  102. foreach (string filename in filenames)
  103. {
  104. _pluginFiles.Add(new PluginFile(buildTarget, folderPrefix + filename, supportsEditor, cpuArchitecture));
  105. }
  106. }
  107. internal static void AddPlugins_Android()
  108. {
  109. #if AVPROMC_UNITY_ANDROID_ARM64_SUPPORT
  110. const bool IsAndroidArm64Supported = true;
  111. #else
  112. const bool IsAndroidArm64Supported = false;
  113. #endif
  114. #if AVPROMC_UNITY_ANDROID_X86_SUPPORT
  115. const bool IsAndroidX86Supported = true;
  116. #else
  117. const bool IsAndroidX86Supported = false;
  118. #endif
  119. #if AVPROMC_UNITY_ANDROID_X8664_SUPPORT
  120. const bool IsAndroidX8664Supported = true;
  121. #else
  122. const bool IsAndroidX8664Supported = false;
  123. #endif
  124. string[] filenames = {
  125. "libAVProMovieCaptureNative.so",
  126. };
  127. BuildTarget target = BuildTarget.Android;
  128. AddPluginFiles(target, filenames, "Android/libs/armeabi-v7a/", false, new CpuArchitecture("ARMv7", true));
  129. AddPluginFiles(target, filenames, "Android/libs/arm64-v8a/", false, new CpuArchitecture("ARM64", IsAndroidArm64Supported));
  130. AddPluginFiles(target, filenames, "Android/libs/x86/", false, new CpuArchitecture("X86", IsAndroidX86Supported));
  131. AddPluginFiles(target, filenames, "Android/libs/x86_64/", false, new CpuArchitecture("X86_64", IsAndroidX8664Supported));
  132. }
  133. private static void BuildPluginFileList()
  134. {
  135. _pluginFiles.Clear();
  136. AddPlugins_Android();
  137. }
  138. public int callbackOrder { get { return 0; } }
  139. #if AVPROMC_UNITY_BUILDWITHREPORT_SUPPORT
  140. public void OnPreprocessBuild(BuildReport report)
  141. {
  142. BuildPluginFileList();
  143. CheckNativePlugins(report.summary.platform);
  144. }
  145. #else
  146. public void OnPreprocessBuild(BuildTarget target, string path)
  147. {
  148. BuildPluginFileList();
  149. CheckNativePlugins(target);
  150. }
  151. #endif
  152. internal static void CheckNativePlugins(BuildTarget target)
  153. {
  154. PluginImporter[] importers = PluginImporter.GetAllImporters();
  155. foreach (PluginImporter pi in importers)
  156. {
  157. // Currently we're only interested in native plugins
  158. if (!pi.isNativePlugin) continue;
  159. // Skip plugins that aren't in the AVProMovieCapture path
  160. // NOTE: This is commented out for now to allow the case where users have moved the plugin files to another folder.
  161. // Eventually might need a more robust method, perhaps using GUIDS
  162. //if (!pi.assetPath.Contains("AVProMovieCapture")) continue;
  163. foreach (PluginFile pluginFile in _pluginFiles)
  164. {
  165. if (pluginFile.IsBuildTarget(target) &&
  166. pluginFile.IsForFile(pi.assetPath))
  167. {
  168. pi.SetCompatibleWithAnyPlatform(false);
  169. if (pluginFile.IsSupportedByThisUnityVersion())
  170. {
  171. Debug.Log("[AVProMovieCapture] Enabling " + pluginFile.CpuArchitectureCode() + " " + pi.assetPath);
  172. pi.SetCompatibleWithEditor(pluginFile.SupportsEditor());
  173. pi.SetCompatibleWithPlatform(pluginFile.BuildTarget(), true);
  174. pi.SetPlatformData(pluginFile.BuildTarget(), "CPU", pluginFile.CpuArchitectureCode());
  175. }
  176. else
  177. {
  178. pi.SetCompatibleWithEditor(false);
  179. pi.SetCompatibleWithPlatform(pluginFile.BuildTarget(), false);
  180. pi.SetPlatformData(pluginFile.BuildTarget(), "CPU", "");
  181. Debug.Log("[AVProMovieCapture] Disabling " + pluginFile.CpuArchitectureCode() + " " + pi.assetPath);
  182. }
  183. pi.SaveAndReimport();
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }