diff --git a/Aetheria.State.Daemon/Program.cs b/Aetheria.State.Daemon/Program.cs index 28660dd14..094e6acbf 100644 --- a/Aetheria.State.Daemon/Program.cs +++ b/Aetheria.State.Daemon/Program.cs @@ -2132,9 +2132,11 @@ static EveAssetCatalogDocument BuildCoreAssetCatalog( entry.Ref.Metadata.TryGetValue("resourcesPath", out _))) .Select(entry => { - var unityAssetPath = entry.Ref.Metadata.TryGetValue("unityAssetPath", out var explicitPath) - ? explicitPath - : $"Assets/Resources/{entry.Ref.Metadata["resourcesPath"]}.prefab"; + var unityAssetPath = entry.Ref.Metadata.TryGetValue("bundleAssetPath", out var bundleAssetPath) + ? bundleAssetPath + : entry.Ref.Metadata.TryGetValue("unityAssetPath", out var explicitPath) + ? explicitPath + : $"Assets/Resources/{entry.Ref.Metadata["resourcesPath"]}.prefab"; return new EveAssetCatalogEntry( entry.Ref.AssetKey, entry.Ref.Kind, diff --git a/Assets/Editor/EveAssetBundleBuilder.cs b/Assets/Editor/EveAssetBundleBuilder.cs index 7d88bb16d..58c6e9f15 100644 --- a/Assets/Editor/EveAssetBundleBuilder.cs +++ b/Assets/Editor/EveAssetBundleBuilder.cs @@ -1,8 +1,10 @@ using System; using System.IO; using System.Linq; +using System.Collections.Generic; using GameCult.Aetheria.State.Verse; using UnityEditor; +using UnityEngine; namespace Aetheria.Editor { @@ -15,20 +17,28 @@ namespace Aetheria.Editor Build(BuildTarget.StandaloneWindows64); } + public static void VerifyWindows() + { + var output = ResolveOutput(BuildTarget.StandaloneWindows64); + VerifyBundle(Path.Combine(output, BundleName)); + } + private static void Build(BuildTarget target) { EveThermalProfileMigrator.EnsureGenerated(); - var output = Environment.GetEnvironmentVariable("AETHERIA_EVE_BUNDLE_OUTPUT"); - if (string.IsNullOrWhiteSpace(output)) - output = Path.GetFullPath(Path.Combine("Build", "EveAssets", target.ToString())); + var output = ResolveOutput(target); Directory.CreateDirectory(output); - var assetNames = AetheriaRuntimeAssets.ProjectManifest(null).Assets - .Select(entry => entry.Ref.Metadata.TryGetValue("unityAssetPath", out var explicitPath) - ? explicitPath - : entry.Ref.Metadata.TryGetValue("resourcesPath", out var path) && - string.Equals(entry.Ref.Kind, AetheriaRuntimeAssetKinds.Prefab, StringComparison.Ordinal) - ? $"Assets/Resources/{path}.prefab" + var assets = AetheriaRuntimeAssets.ProjectManifest(null).Assets; + foreach (var entry in assets.Where(entry => + string.Equals(entry.Ref.Kind, AetheriaRuntimeAssetKinds.Prefab, StringComparison.Ordinal))) + BuildPresentationPrefab(entry); + + var assetNames = assets + .Select(entry => entry.Ref.Metadata.TryGetValue("bundleAssetPath", out var presentationPath) + ? presentationPath + : entry.Ref.Metadata.TryGetValue("unityAssetPath", out var explicitPath) + ? explicitPath : "") .Where(path => !string.IsNullOrWhiteSpace(path) && File.Exists(path)) .Distinct(StringComparer.Ordinal) @@ -52,7 +62,107 @@ namespace Aetheria.Editor if (manifest == null || !manifest.GetAllAssetBundles().Contains(BundleName, StringComparer.Ordinal)) throw new InvalidOperationException("Unity did not emit the Aetheria Eve world bundle."); + VerifyBundle(Path.Combine(output, BundleName)); Console.WriteLine($"Aetheria Eve AssetBundle: {Path.Combine(output, BundleName)}"); } + + private static string ResolveOutput(BuildTarget target) + { + var output = Environment.GetEnvironmentVariable("AETHERIA_EVE_BUNDLE_OUTPUT"); + return string.IsNullOrWhiteSpace(output) + ? Path.GetFullPath(Path.Combine("Build", "EveAssets", target.ToString())) + : output; + } + + private static void BuildPresentationPrefab(AetheriaRuntimeAssetManifestEntry entry) + { + if (!entry.Ref.Metadata.TryGetValue("bundleAssetPath", out var outputPath)) + throw new InvalidOperationException($"Prefab {entry.Ref.AssetKey} has no presentation bundle path."); + + var sourcePath = entry.Ref.Metadata.TryGetValue("unityAssetPath", out var explicitPath) + ? explicitPath + : entry.Ref.Metadata.TryGetValue("resourcesPath", out var resourcesPath) + ? $"Assets/Resources/{resourcesPath}.prefab" + : ""; + if (string.IsNullOrWhiteSpace(sourcePath) || AssetDatabase.LoadAssetAtPath(sourcePath) == null) + throw new InvalidOperationException($"Prefab {entry.Ref.AssetKey} source does not exist: {sourcePath}"); + + Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? "Assets/Generated/Eve/ProviderPrefabs"); + var root = PrefabUtility.LoadPrefabContents(sourcePath); + try + { + StripNonPresentationScripts(root); + VerifyPrefab(root, outputPath); + PrefabUtility.SaveAsPrefabAsset(root, outputPath, out var saved); + if (!saved) + throw new InvalidOperationException($"Could not save presentation prefab {outputPath}."); + } + finally + { + PrefabUtility.UnloadPrefabContents(root); + } + + var savedRoot = AssetDatabase.LoadAssetAtPath(outputPath); + VerifyPrefab(savedRoot, outputPath); + } + + private static void StripNonPresentationScripts(GameObject root) + { + foreach (var transform in root.GetComponentsInChildren(true)) + { + GameObjectUtility.RemoveMonoBehavioursWithMissingScript(transform.gameObject); + foreach (var behaviour in transform.GetComponents()) + { + if (behaviour != null && !IsPresentationAssembly(behaviour.GetType().Assembly.GetName().Name)) + UnityEngine.Object.DestroyImmediate(behaviour, true); + } + } + } + + private static bool IsPresentationAssembly(string assemblyName) + { + return assemblyName.StartsWith("Unity", StringComparison.Ordinal) || + assemblyName.StartsWith("GameCult.Eve", StringComparison.Ordinal); + } + + private static void VerifyPrefab(GameObject root, string assetPath) + { + if (root == null) + throw new InvalidOperationException($"Presentation prefab could not be loaded: {assetPath}"); + + var violations = new List(); + foreach (var transform in root.GetComponentsInChildren(true)) + { + var missing = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(transform.gameObject); + if (missing > 0) + violations.Add($"{transform.name}: {missing} missing script(s)"); + foreach (var behaviour in transform.GetComponents()) + { + if (behaviour != null && !IsPresentationAssembly(behaviour.GetType().Assembly.GetName().Name)) + violations.Add($"{transform.name}: {behaviour.GetType().FullName}"); + } + } + if (violations.Count > 0) + throw new InvalidOperationException( + $"Presentation prefab {assetPath} contains forbidden scripts:\n{string.Join("\n", violations)}"); + } + + private static void VerifyBundle(string bundlePath) + { + if (!File.Exists(bundlePath)) + throw new InvalidOperationException($"Aetheria Eve bundle does not exist: {bundlePath}"); + var bundle = AssetBundle.LoadFromFile(bundlePath); + if (bundle == null) + throw new InvalidOperationException($"Aetheria Eve bundle could not be loaded: {bundlePath}"); + try + { + foreach (var assetPath in bundle.GetAllAssetNames().Where(path => path.EndsWith(".prefab", StringComparison.OrdinalIgnoreCase))) + VerifyPrefab(bundle.LoadAsset(assetPath), assetPath); + } + finally + { + bundle.Unload(true); + } + } } } diff --git a/Assets/Scripts/Tests/Tests.asmdef b/Assets/Scripts/Tests/Tests.asmdef index 09663af15..037507d4e 100644 --- a/Assets/Scripts/Tests/Tests.asmdef +++ b/Assets/Scripts/Tests/Tests.asmdef @@ -48,7 +48,7 @@ ], "autoReferenced": false, "defineConstraints": [ - "UNITY_INCLUDE_TESTS" + "AETHERIA_FOSSIL_TESTS" ], "versionDefines": [], "noEngineReferences": false diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeAssets.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeAssets.cs index 7c4509ee5..2a7c08d0f 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeAssets.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaRuntimeAssets.cs @@ -184,7 +184,11 @@ namespace GameCult.Aetheria.State.Verse { var asset = Prefab(key, resourcePath); asset.Metadata = asset.Metadata - .Concat(new[] { new KeyValuePair("presentationRole", presentationRole ?? "") }) + .Concat(new[] + { + new KeyValuePair("presentationRole", presentationRole ?? ""), + new KeyValuePair("bundleAssetPath", PresentationPrefabPath(key)) + }) .ToDictionary(pair => pair.Key, pair => pair.Value, StringComparer.Ordinal); return new AetheriaRuntimeAssetManifestEntry { @@ -208,7 +212,8 @@ namespace GameCult.Aetheria.State.Verse MimeType = "application/vnd.unity.prefab", Metadata = new Dictionary(StringComparer.Ordinal) { - ["unityAssetPath"] = (unityAssetPath ?? "").Replace('\\', '/') + ["unityAssetPath"] = (unityAssetPath ?? "").Replace('\\', '/'), + ["bundleAssetPath"] = PresentationPrefabPath(key) } }, Tags = new[] { "world", "prefab", label } @@ -339,6 +344,15 @@ namespace GameCult.Aetheria.State.Verse return $"cultmesh://aetheria/assets/{path}"; } + private static string PresentationPrefabPath(string key) + { + var fileName = string.Concat((key ?? "").Select(character => + char.IsLetterOrDigit(character) || character == '.' || character == '-' + ? character + : '_')); + return $"Assets/Generated/Eve/ProviderPrefabs/{fileName}.prefab"; + } + private static string NormalizeResourcePath(string path) { path ??= "";