Files
TheriapolisV3/Theriapolis.Godot/Platform/ContentLoader.cs
T
Christopher Wiebe a23cf8bd97 M3: Asset pipeline — ContentPaths + ContentLoader + asset-test
Formalises Content/ access from the Godot host. Content lives at the
repo root (sibling of Theriapolis.Godot/), not duplicated under res://,
so the MonoGame branch and headless Tools keep reading from the same
single source of truth.

ContentPaths.cs:
  Static ContentRoot/DataDir/GfxDir resolved once via res:// walk-up
  and cached. Replaces two inline ResolveDataDir copies in SmokeTest
  and WorldMapView.

ContentLoader.cs:
  LoadGfx(relativePath) -> ImageTexture, cached by relative path.
  Bypasses the res:// import pipeline because Content/ lives outside
  the project — fine for static pixel-art assets at native size, and
  the project default texture filter is already Nearest. Cache is
  per-process, never evicted (full atlas <1 MB).

AssetTest.cs + Main.cs --asset-test flag:
  Smoke-tests the pipeline. Walks Content/Data and Content/Gfx,
  reports counts, attempts to load every PNG, prints per-subdir
  breakdown. Quits with non-zero on any failure.

Verified post-refactor (--asset-test):
  53 JSON files in Data, 50 PNG files in Gfx (8 tactical/deco +
  42 tactical/surface), 50/50 loaded, 0 failures.

Verified no regressions:
  --smoke-test (M1) still produces canonical FNV hashes.
  --world-map 12345 (M2) still produces 3 rivers / 91 roads /
  226 settlements / 0 rails / 0 bridges.

Scope note: plan mentioned "tile/NPC/CodexUI atlases — three
separate themes". Only tactical/ exists in Content/Gfx today; NPC
and CodexUI atlases never landed during MonoGame development. M3
ships what's actually present. ContentLoader.LoadGfx works for any
future sub-directories without changes.

Closes M3 of theriapolis-rpg-implementation-plan-godot-port.md.
Next: M4 (tactical render).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 19:13:51 -07:00

56 lines
1.8 KiB
C#

using Godot;
using System.Collections.Generic;
using System.IO;
namespace Theriapolis.GodotHost.Platform;
/// <summary>
/// Loads PNGs from Content/Gfx as Godot ImageTextures and caches them by
/// relative path. Texture filter is set to Nearest (project default for
/// pixel art).
///
/// This bypasses Godot's res:// import pipeline because Content/ lives
/// outside the project — but for static pixel-art assets at native size
/// the import pipeline doesn't add anything we need.
///
/// Tactical-view tile rendering (M4) will hit this from chunk streamers,
/// so the cache is per-process and never evicted; the full atlas
/// (~50 PNGs at 32x32) is well under 1 MB.
/// </summary>
public static class ContentLoader
{
private static readonly Dictionary<string, ImageTexture> _cache = new();
/// <summary>
/// Loads a PNG from <c>Content/Gfx/&lt;relativePath&gt;</c>. Returns
/// null and logs an error if the file is missing or unreadable.
/// </summary>
public static ImageTexture? LoadGfx(string relativePath)
{
if (_cache.TryGetValue(relativePath, out var cached)) return cached;
string absolute = Path.Combine(ContentPaths.GfxDir, relativePath);
if (!File.Exists(absolute))
{
GD.PrintErr($"[ContentLoader] Missing texture: {absolute}");
return null;
}
var image = Image.LoadFromFile(absolute);
if (image is null)
{
GD.PrintErr($"[ContentLoader] Image.LoadFromFile failed: {absolute}");
return null;
}
var tex = ImageTexture.CreateFromImage(image);
_cache[relativePath] = tex;
return tex;
}
public static int CacheCount => _cache.Count;
/// <summary>Drops every cached texture. Useful for hot-reload tests.</summary>
public static void ClearCache() => _cache.Clear();
}