Use typed runtime item catalog

This commit is contained in:
Meta Vens 2026-06-13 14:36:54 +01:00
commit 949907de65
11 changed files with 415 additions and 245 deletions

View file

@ -68,7 +68,7 @@ public abstract class DatabaseEntry
public class DatabaseLink<T> : DatabaseLinkBase where T : ItemData
{
[IgnoreMember]
public T Value => ResolveLegacyItemCatalog<T>(LinkID);
public T Value => ResolveRuntimeItemCatalog<T>(LinkID);
}
[MessagePackObject]
@ -78,17 +78,17 @@ public class DatabaseLinkBase
public Guid LinkID;
[IgnoreMember]
private static ILegacyItemCatalogReader Catalog { get; set; }
private static IRuntimeItemCatalogReader Catalog { get; set; }
protected static T ResolveLegacyItemCatalog<T>(Guid linkId) where T : ItemData
protected static T ResolveRuntimeItemCatalog<T>(Guid linkId) where T : ItemData
{
if (Catalog == null)
throw new InvalidOperationException("Legacy item link resolution is not bound. Open LegacyItemCatalogBoundary before reading legacy item object graphs.");
throw new InvalidOperationException("Runtime item catalog resolution is not bound. Open typed Aetheria runtime state before reading item object graphs.");
return Catalog.Get<T>(linkId);
}
public static void BindLegacyItemCatalog(ILegacyItemCatalogReader catalog)
public static void BindRuntimeItemCatalog(IRuntimeItemCatalogReader catalog)
{
if (catalog == null) throw new ArgumentNullException(nameof(catalog));

View file

@ -1,114 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
using System;
using System.Collections.Generic;
using System.IO;
using MessagePack;
using UniRx;
public interface ILegacyItemCatalogReader
{
ItemData Get(Guid guid);
T Get<T>(Guid guid) where T : ItemData;
}
public class LegacyItemCatalogCache : ILegacyItemCatalogReader
{
private readonly object addLock = new object();
private List<CacheBackingStore> _backingStores = new List<CacheBackingStore>();
private readonly Dictionary<Guid, ItemData> _entries = new Dictionary<Guid, ItemData>();
public void AddBackingStore(CacheBackingStore store)
{
_backingStores.Add(store);
store.EntryAdded.Subscribe(entry =>
{
AddInternal(entry, store);
});
}
public void PullAllBackingStores()
{
foreach(var store in _backingStores) store.PullAll();
}
private void AddInternal(DatabaseEntry entry, CacheBackingStore source = null)
{
lock(addLock)
{
if (entry is ItemData item)
{
_entries[item.ID] = item;
}
}
}
public ItemData Get(Guid guid)
{
ItemData entry;
_entries.TryGetValue(guid, out entry);
return entry;
}
public T Get<T>(Guid guid) where T : ItemData
{
return Get(guid) as T;
}
}
public abstract class CacheBackingStore
{
protected CacheBackingStore()
{
EntryAdded = new Subject<DatabaseEntry>();
}
public abstract void PullAll();
public Subject<DatabaseEntry> EntryAdded { get; }
protected Dictionary<Guid, DatabaseEntry> Entries = new Dictionary<Guid, DatabaseEntry>();
}
public abstract class SingleFileBackingStore : CacheBackingStore
{
public FileInfo FileInfo { get; }
public abstract DatabaseEntry[] Deserialize(byte[] data);
public SingleFileBackingStore(string filePath)
{
FileInfo = new FileInfo(filePath);
}
public override void PullAll()
{
if (!FileInfo.Exists) return;
foreach (var entry in Deserialize(File.ReadAllBytes(FileInfo.FullName)))
{
Entries[entry.ID] = entry;
EntryAdded.OnNext(entry);
}
}
}
public class SingleFileMessagePackBackingStore : SingleFileBackingStore
{
public SingleFileMessagePackBackingStore(string filePath) : base(filePath)
{
RegisterResolver.Register();
}
public override DatabaseEntry[] Deserialize(byte[] data)
{
return MessagePackSerializer.Deserialize<DatabaseEntry[]>(data);
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a43b39c6991618b43aed4a24ffcdc0ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: