diff --git a/Assets/Scripts/Editor/RockMeshRadius.cs b/Assets/Scripts/Editor/RockMeshRadius.cs index cdc7c663f..cefb3b1b1 100644 --- a/Assets/Scripts/Editor/RockMeshRadius.cs +++ b/Assets/Scripts/Editor/RockMeshRadius.cs @@ -1,6 +1,8 @@ using System; using System.Linq; using UnityEditor; +using UnityEditor.Build; +using UnityEditor.Build.Reporting; using UnityEditor.SceneManagement; using UnityEngine; @@ -12,6 +14,9 @@ using UnityEngine; // Unity -batchmode -nographics -projectPath -executeMethod Aetheria.EditorTools.RockMeshRadius.Verify -logFile // // Fill measures every AsteroidMeshes entry and saves the scene; Verify fails (exit 1) when any stored radius is not the measured one. +// RockMeshRadiusBuildGate runs the same check before every player build and fails the build, so a mesh with no filled radius cannot ship; +// Gate runs that gate in batch mode. +// Unity -batchmode -nographics -projectPath -executeMethod Aetheria.EditorTools.RockMeshRadius.Gate -logFile namespace Aetheria.EditorTools { public static class RockMeshRadius @@ -21,35 +26,68 @@ namespace Aetheria.EditorTools public static float Measure(Mesh mesh) => Mathf.Sqrt(mesh.vertices.Max(v => v.sqrMagnitude)); - public static void Fill() => Run(fill: true); + public static void Fill() => Batch(() => + { + var scene = EditorSceneManager.OpenScene(Scene, OpenSceneMode.Single); + var (renderer, meshes, measured) = Load(scene); + for (var i = 0; i < meshes.Length; i++) meshes[i].Radius = measured[i]; + EditorUtility.SetDirty(renderer); + EditorSceneManager.MarkSceneDirty(scene); + if (!EditorSceneManager.SaveScene(scene)) throw new InvalidOperationException("The scene did not save."); + return Summary("filled", measured); + }); - public static void Verify() => Run(fill: false); + public static void Verify() => Batch(() => Check(OpenSceneMode.Single)); - private static void Run(bool fill) + // The build gate's own entry in batch mode: exit 1 exactly when a build would fail (RockMeshRadiusBuildGate). + public static void Gate() => Batch(() => + { + try { new RockMeshRadiusBuildGate().OnPreprocessBuild(null); } + catch (BuildFailedException failure) { throw new InvalidOperationException(failure.Message); } + return "the build gate passes"; + }); + + // Throws unless every AsteroidMeshes entry of ARPG.unity has a stored radius equal to its mesh's measured one (a zero radius never does). + public static string Check(OpenSceneMode mode) + { + var scene = EditorSceneManager.OpenScene(Scene, mode); + try + { + var (_, meshes, measured) = Load(scene); + var failures = meshes.Select((m, i) => (m, i)).Where(p => !(measured[p.i] > 0f) || Mathf.Abs(p.m.Radius - measured[p.i]) > Tolerance).ToArray(); + if (failures.Length > 0) + throw new InvalidOperationException("InstancedMesh.Radius differs from the measured one (run RockMeshRadius.Fill): " + string.Join(", ", + failures.Select(f => $"{f.m.Mesh.name} stored {f.m.Radius} measured {measured[f.i]}"))); + return Summary("ok", measured); + } + finally + { + if (mode == OpenSceneMode.Additive) EditorSceneManager.CloseScene(scene, true); + } + } + + private static (ZoneRenderer renderer, InstancedMesh[] meshes, float[] measured) Load(UnityEngine.SceneManagement.Scene scene) + { + var renderer = UnityEngine.Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None) + .Single(r => r.gameObject.scene == scene); + var meshes = renderer.AsteroidMeshes; + if (meshes == null || meshes.Length == 0) throw new InvalidOperationException("ARPG.unity's ZoneRenderer has no AsteroidMeshes."); + var bad = meshes.Where(m => m.Mesh == null).ToArray(); + if (bad.Length > 0) throw new InvalidOperationException($"{bad.Length} AsteroidMeshes entries have no mesh."); + return (renderer, meshes, meshes.Select(m => Measure(m.Mesh)).ToArray()); + } + + private static string Summary(string what, float[] measured) + { + var sorted = measured.OrderBy(r => r).ToArray(); + return $"{what}: {sorted.Length} meshes, radius min {sorted[0]:F3} median {sorted[sorted.Length / 2]:F3} max {sorted[^1]:F3}"; + } + + private static void Batch(Func run) { try { - var scene = EditorSceneManager.OpenScene(Scene, OpenSceneMode.Single); - var renderer = UnityEngine.Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None) - .Single(r => r.gameObject.scene == scene); - var meshes = renderer.AsteroidMeshes; - if (meshes == null || meshes.Length == 0) throw new InvalidOperationException("ARPG.unity's ZoneRenderer has no AsteroidMeshes."); - var bad = meshes.Where(m => m.Mesh == null).ToArray(); - if (bad.Length > 0) throw new InvalidOperationException($"{bad.Length} AsteroidMeshes entries have no mesh."); - var measured = meshes.Select(m => Measure(m.Mesh)).ToArray(); - var failures = meshes.Select((m, i) => (m, i)).Where(p => !(measured[p.i] > 0f) || Mathf.Abs(p.m.Radius - measured[p.i]) > Tolerance).ToArray(); - if (fill) - { - for (var i = 0; i < meshes.Length; i++) meshes[i].Radius = measured[i]; - EditorUtility.SetDirty(renderer); - EditorSceneManager.MarkSceneDirty(scene); - if (!EditorSceneManager.SaveScene(scene)) throw new InvalidOperationException("The scene did not save."); - } - else if (failures.Length > 0) - throw new InvalidOperationException("Stored radius differs from measured: " + string.Join(", ", - failures.Select(f => $"{f.m.Mesh.name} stored {f.m.Radius} measured {measured[f.i]}"))); - var sorted = measured.OrderBy(r => r).ToArray(); - Debug.Log($"[RockMeshRadius] {(fill ? "filled" : "ok")}: {sorted.Length} meshes, radius min {sorted[0]:F3} median {sorted[sorted.Length / 2]:F3} max {sorted[^1]:F3}"); + Debug.Log($"[RockMeshRadius] {run()}"); EditorApplication.Exit(0); } catch (Exception exception) @@ -59,4 +97,16 @@ namespace Aetheria.EditorTools } } } + + // A mesh in AsteroidMeshes with no filled radius must not ship: the build fails, naming the field. + public class RockMeshRadiusBuildGate : IPreprocessBuildWithReport + { + public int callbackOrder => 0; + + public void OnPreprocessBuild(BuildReport report) + { + try { RockMeshRadius.Check(OpenSceneMode.Additive); } + catch (InvalidOperationException failure) { throw new BuildFailedException(failure.Message); } + } + } }