Aetheria/Assets/Scripts/EngineAssets.cs

50 lines
2.1 KiB
C#
Raw Normal View History

Switch engine asset references from Resources paths to Addressables GUIDs Cut 2 of docs/addressables-cut.md. The asset move (git mv Resources/{Prefabs,Schematics,Sprites/Icons} -> Content/...) and the UnityHelpers.cs deletion already landed in 1fdf8151, picked up by a concurrent session sharing this working tree; this commit is the rest of Cut 2 landing on top of that shared state, atomically: - Bump CultLib to cultlib-unity-v1.0.60 / caching-unity-v1.4.0 (both 45c2f40) in Packages/manifest.json, packages-lock.json and Directory.Build.props, together with the 8 ServerShared [CultInspectorAssetPath] usages becoming [CultInspectorAssetGuid] - the .NET and Unity sides move together or nothing compiles. - Assets/Scripts/EngineAssets.cs replaces UnityHelpers: loads an Addressables key (bare GUID or guid[subAsset]) synchronously, loading the GameObject and taking the component for the four sites that load a component. ActionBarSlot, EntityInstance, ShipInstance and ZoneRenderer call it instead of Resources.Load/UnityHelpers; the inline "Assets/Resources/" prefix strips are gone with them. CultCacheDrawers' schematic preview goes through AssetDatabase.GUIDToAssetPath first. - Assets/Scripts/Editor/EngineAssetCheck.cs validates every stored [CultInspectorAssetGuid] value: 32 lowercase hex, resolves to a file, is addressable, and satisfies its member's consumer-type contract (Texture2D for icons/logos/schematics, a GameObject with the right component for prefabs) - table-driven, so an attributed member with no row fails loudly instead of being skipped. - GameData/Aetheria.cc: the scratch migration (Aetheria.Shared + the pinned CultLib revision, a throwaway console app, never landed) walked all 138 attributed field slots across 180 documents, rewriting the 30 non-empty Resources paths to their .meta GUIDs - 108 stayed empty, 0 unresolved, matching the map's census exactly. Verification: dotnet test tests/Aetheria.Shared.Tests 61/61 green against CultLib 45c2f40; Unity 6000.3.24f1 batchmode compiles clean; EngineAssetCheck.Run passes all 30 stored references; the map's negative greps are clean; tests/mutation_tests.py's 5 mutations (wrong type, non-addressable, random guid, an old path, and an untabled attributed member) are all caught and named, the control case and the post-revert recompile stay clean, and the catalog/source are restored byte-identical afterward. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 05:04:50 +02:00
using System;
using System.Collections.Generic;
using GameCult.Caching;
using UnityEngine;
using UnityEngine.AddressableAssets;
using Object = UnityEngine.Object;
// Turns a CultCache-stored Addressables key (bare GUID, or "guid[subAssetName]" for a sub-asset;
// see CultAssetGuidKey and docs/addressables-cut.md Ruling S) into a loaded asset. Replaces
// UnityHelpers.LoadAsset, which stripped an "Assets/Resources/" path prefix; there is no path to
// strip any more; the catalog names the asset by its stable .meta GUID and Addressables resolves
// it, in editor play mode and in a built player alike. Owns one Addressables handle per (key,
// type) for the process lifetime; nothing here releases a handle, which is the lifetime
// Resources.Load had. All call sites are synchronous construction paths that Instantiate at once,
// so loading stays synchronous (WaitForCompletion) rather than reordering entity construction.
public static class EngineAssets
{
private static readonly Dictionary<(string Key, Type Type), Object> Cache = new Dictionary<(string, Type), Object>();
public static T Load<T>(string key) where T : Object
{
if (string.IsNullOrEmpty(key)) return null;
var cacheKey = (key, typeof(T));
if (Cache.TryGetValue(cacheKey, out var cached)) return cached as T;
T asset;
if (typeof(Component).IsAssignableFrom(typeof(T)))
{
// Addressables cannot load a component directly: load the GameObject and take it from there.
var handle = Addressables.LoadAssetAsync<GameObject>(key);
var gameObject = handle.WaitForCompletion();
asset = gameObject == null ? null : gameObject.GetComponent<T>();
}
else
{
var handle = Addressables.LoadAssetAsync<T>(key);
asset = handle.WaitForCompletion();
}
if (asset == null)
{
Debug.LogError($"EngineAssets: failed to load {typeof(T).Name} for Addressables key \"{key}\"");
return null;
}
Cache[cacheKey] = asset;
return asset;
}
}