first commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace OSM.Client.Autoload;
|
||||
|
||||
public partial class AssetCatalog : Node
|
||||
{
|
||||
private const string ManifestPath = "res://src/Resources/asset_catalog.json";
|
||||
private readonly Dictionary<int, string> _characterPortraits = [];
|
||||
private string _fallbackPortrait = string.Empty;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
LoadManifest();
|
||||
}
|
||||
|
||||
public string GetCharacterPortraitPath(int characterId)
|
||||
{
|
||||
return _characterPortraits.GetValueOrDefault(characterId, _fallbackPortrait);
|
||||
}
|
||||
|
||||
public Texture2D? LoadCharacterPortrait(int characterId)
|
||||
{
|
||||
var path = GetCharacterPortraitPath(characterId);
|
||||
return string.IsNullOrWhiteSpace(path) ? null : GD.Load<Texture2D>(path);
|
||||
}
|
||||
|
||||
private void LoadManifest()
|
||||
{
|
||||
if (!Godot.FileAccess.FileExists(ManifestPath))
|
||||
{
|
||||
GD.PushWarning($"Asset manifest not found: {ManifestPath}");
|
||||
return;
|
||||
}
|
||||
|
||||
var json = Godot.FileAccess.GetFileAsString(ManifestPath);
|
||||
var manifest = JsonSerializer.Deserialize<AssetManifest>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
||||
if (manifest is null)
|
||||
{
|
||||
GD.PushWarning("Asset manifest could not be parsed.");
|
||||
return;
|
||||
}
|
||||
|
||||
_fallbackPortrait = manifest.FallbackPortrait ?? string.Empty;
|
||||
foreach (var pair in manifest.CharacterPortraits)
|
||||
{
|
||||
if (int.TryParse(pair.Key, out var id))
|
||||
{
|
||||
_characterPortraits[id] = pair.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record AssetManifest
|
||||
{
|
||||
public Dictionary<string, string> CharacterPortraits { get; init; } = [];
|
||||
public string? FallbackPortrait { get; init; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user