Package provider-owned Unity presentation prefabs

This commit is contained in:
Meta Vens 2026-07-13 21:00:20 +01:00
commit 05a31a89ae
4 changed files with 141 additions and 15 deletions

View file

@ -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,

View file

@ -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<GameObject>(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<GameObject>(outputPath);
VerifyPrefab(savedRoot, outputPath);
}
private static void StripNonPresentationScripts(GameObject root)
{
foreach (var transform in root.GetComponentsInChildren<Transform>(true))
{
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(transform.gameObject);
foreach (var behaviour in transform.GetComponents<MonoBehaviour>())
{
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<string>();
foreach (var transform in root.GetComponentsInChildren<Transform>(true))
{
var missing = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(transform.gameObject);
if (missing > 0)
violations.Add($"{transform.name}: {missing} missing script(s)");
foreach (var behaviour in transform.GetComponents<MonoBehaviour>())
{
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<GameObject>(assetPath), assetPath);
}
finally
{
bundle.Unload(true);
}
}
}
}

View file

@ -48,7 +48,7 @@
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
"AETHERIA_FOSSIL_TESTS"
],
"versionDefines": [],
"noEngineReferences": false

View file

@ -184,7 +184,11 @@ namespace GameCult.Aetheria.State.Verse
{
var asset = Prefab(key, resourcePath);
asset.Metadata = asset.Metadata
.Concat(new[] { new KeyValuePair<string, string>("presentationRole", presentationRole ?? "") })
.Concat(new[]
{
new KeyValuePair<string, string>("presentationRole", presentationRole ?? ""),
new KeyValuePair<string, string>("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<string, string>(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 ??= "";