using System; using System.Collections.Generic; using UnityEditor.Android; using UnityEditor.Build; using UnityEditor.Build.Reporting; using UnityEngine; using UnityEngine.XR.OpenXR; using UnityEngine.XR.OpenXR.Features; #if XR_MGMT_4_4_0_OR_NEWER using Unity.XR.Management.AndroidManifest.Editor; #endif namespace UnityEditor.XR.OpenXR.Features { /// /// Inherit from this class to get callbacks to hook into the build process when your OpenXR Extension is enabled. /// #pragma warning disable 0618 public abstract class OpenXRFeatureBuildHooks : IPostGenerateGradleAndroidProject, IPostprocessBuildWithReport, IPreprocessBuildWithReport #pragma warning restore 0618 #if XR_MGMT_4_4_0_OR_NEWER , IAndroidManifestRequirementProvider #endif { private OpenXRFeature _ext; private readonly BootConfigBuilder _bootConfigBuilder = new BootConfigBuilder(); private bool IsExtensionEnabled(BuildTarget target, BuildTargetGroup group) { if (!BuildHelperUtils.HasLoader(group, typeof(OpenXRLoaderBase))) return false; OpenXRSettings buildTargetOpenXRSettings = OpenXRSettings.GetSettingsForBuildTargetGroup(BuildPipeline.GetBuildTargetGroup(target)); if (buildTargetOpenXRSettings == null || buildTargetOpenXRSettings.features == null) { UnityEngine.Debug.LogWarning($"Could not find valid OpenXR settings for build target {target}."); return false; } if (_ext == null || _ext.GetType() != featureType) { foreach (var ext in buildTargetOpenXRSettings.features) { if (featureType == ext.GetType()) { _ext = ext; } } } return _ext != null && _ext.enabled; } /// /// Returns the current callback order for build processing. /// /// Int value denoting the callback order. public abstract int callbackOrder { get; } /// /// Post process build step for checking if a feature is enabled. If so will call to the feature to run their build pre processing. /// /// Build report. public virtual void OnPreprocessBuild(BuildReport report) { if (!IsExtensionEnabled(report.summary.platform, report.summary.platformGroup)) return; _bootConfigBuilder.ReadBootConfig(report); OnProcessBootConfigExt(report, _bootConfigBuilder); OnPreprocessBuildExt(report); _bootConfigBuilder.WriteBootConfig(report); } /// /// Post process build step for checking if a feature is enabled for android builds. If so will call to the feature to run their build post processing for android builds. /// /// Path to gradle project. public virtual void OnPostGenerateGradleAndroidProject(string path) { if (!IsExtensionEnabled(BuildTarget.Android, BuildTargetGroup.Android)) return; OnPostGenerateGradleAndroidProjectExt(path); } /// /// Post-process build step for any necessary clean-up. This will also call to the feature to run their build post processing. /// /// Build report. public virtual void OnPostprocessBuild(BuildReport report) { if (!IsExtensionEnabled(report.summary.platform, report.summary.platformGroup)) return; OnPostprocessBuildExt(report); _bootConfigBuilder.ClearAndWriteBootConfig(report); } /// /// System.Type of the class that implements OpenXRFeature. /// public abstract Type featureType { get; } /// /// Called during the build process when the feature is enabled. Implement this function to receive a callback before the build starts. /// /// Report that contains information about the build, such as its target platform and output path. protected abstract void OnPreprocessBuildExt(BuildReport report); /// /// Called during build process when extension is enabled. Implement this function to receive a callback after the Android Gradle project is generated and before building begins. Function is not called for Internal builds. /// /// The path to the root of the Gradle project. Note: When exporting the project, this parameter holds the path to the folder specified for export. protected abstract void OnPostGenerateGradleAndroidProjectExt(string path); /// /// Called during the build process when extension is enabled. Implement this function to receive a callback after the build is complete. /// /// BuildReport that contains information about the build, such as the target platform and output path. protected abstract void OnPostprocessBuildExt(BuildReport report); /// /// Called during the build process when extension is enabled. Implement this function to add Boot Config Settings. /// /// BuildReport that contains information about the build, such as the target platform and output path. /// This is the Boot Config interface tha can be used to write boot configs protected virtual void OnProcessBootConfigExt(BuildReport report, BootConfigBuilder builder) { } #if XR_MGMT_4_4_0_OR_NEWER /// /// Post process build step for checking if the hooks' related feature is enabled for Android builds If so, the hook can safely provide its Android manifest requirements. /// public virtual ManifestRequirement ProvideManifestRequirement() { if (!IsExtensionEnabled(BuildTarget.Android, BuildTargetGroup.Android)) return null; return ProvideManifestRequirementExt(); } /// /// Called during build process when collecting requirements for Android Manifest. Implement this function to add, override or remove Android manifest entries. /// protected virtual ManifestRequirement ProvideManifestRequirementExt() { return null; } #endif } }