acdream/src/AcDream.App/RuntimeDatCollectionFactory.cs
Erik 749e8ceeb1 fix(rendering): bound portal resource lifetime
Separate logical ownership, render publication, and GPU retirement across live entities, landblocks, particles, textures, mesh arenas, portal/UI teardown, and per-frame scratch storage. Add bounded DAT/texture caches, upload budgets, three-frame fence retirement, exact-incarnation appearance reconciliation, frame pacing, and extensive lifetime conformance coverage.\n\nThe seven-destination connected route now cuts peak working/private memory roughly in half, returns Caul to 125-153 FPS locally, and produces no WER or AMD reset.\n\nCo-authored-by: OpenAI Codex <codex@openai.com>
2026-07-18 21:35:16 +02:00

101 lines
3.7 KiB
C#

using DatReaderWriter;
using DatReaderWriter.Options;
using AcDream.Content;
using DatReaderWriter.Lib.IO;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
namespace AcDream.App;
/// <summary>
/// Opens the read-only DAT collection owned for the lifetime of an App process.
/// </summary>
/// <remarks>
/// DatReaderWriter's default <see cref="FileCachingStrategy.OnDemand"/> retains
/// every unpacked file entry for the lifetime of the collection. The runtime has
/// bounded decoded-content caches of its own, so retaining that second,
/// unbounded copy makes memory depend on every landblock visited. Keep the small
/// B-tree lookup cache, but read file payloads on demand and let the owning
/// runtime caches decide their residency.
/// </remarks>
internal static class RuntimeDatCollectionFactory
{
internal static IDatReaderWriter OpenReadOnly(string datDirectory) =>
new RuntimeDatCollection(new DatCollection(CreateReadOnlyOptions(datDirectory)));
internal static DatCollectionOptions CreateReadOnlyOptions(string datDirectory)
{
ArgumentException.ThrowIfNullOrWhiteSpace(datDirectory);
return new DatCollectionOptions
{
DatDirectory = datDirectory,
AccessType = DatAccessType.Read,
IndexCachingStrategy = IndexCachingStrategy.OnDemand,
FileCachingStrategy = FileCachingStrategy.Never,
};
}
}
/// <summary>
/// Owns the runtime's one raw DAT handle set and its one shared bounded typed
/// object facade. Disposing this owner closes both layers exactly once.
/// </summary>
internal sealed class RuntimeDatCollection : IDatReaderWriter
{
private readonly DatCollection _raw;
private readonly DatCollectionAdapter _bounded;
private bool _disposed;
internal RuntimeDatCollection(DatCollection raw)
{
ArgumentNullException.ThrowIfNull(raw);
_raw = raw;
_bounded = new DatCollectionAdapter(raw);
}
public string SourceDirectory => _bounded.SourceDirectory;
public IDatDatabase Portal => _bounded.Portal;
public IDatDatabase Cell => _bounded.Cell;
public ReadOnlyDictionary<uint, IDatDatabase> CellRegions => _bounded.CellRegions;
public IDatDatabase HighRes => _bounded.HighRes;
public IDatDatabase Language => _bounded.Language;
public IDatDatabase Local => _bounded.Local;
public ReadOnlyDictionary<uint, uint> RegionFileMap => _bounded.RegionFileMap;
public int PortalIteration => _bounded.PortalIteration;
public int CellIteration => _bounded.CellIteration;
public int HighResIteration => _bounded.HighResIteration;
public int LanguageIteration => _bounded.LanguageIteration;
[return: MaybeNull]
public T Get<T>(uint fileId) where T : IDBObj => _bounded.Get<T>(fileId);
public bool TryGet<T>(uint fileId, [MaybeNullWhen(false)] out T value)
where T : IDBObj =>
_bounded.TryGet(fileId, out value);
public IEnumerable<uint> GetAllIdsOfType<T>() where T : IDBObj =>
_bounded.GetAllIdsOfType<T>();
public bool TryGetFileBytes(uint regionId, uint fileId, ref byte[] bytes, out int bytesRead) =>
_bounded.TryGetFileBytes(regionId, fileId, ref bytes, out bytesRead);
public IEnumerable<IDatReaderWriter.IdResolution> ResolveId(uint id) =>
_bounded.ResolveId(id);
public bool TrySave<T>(T obj, int iteration = 0) where T : IDBObj =>
_bounded.TrySave(obj, iteration);
public bool TrySave<T>(uint regionId, T obj, int iteration = 0) where T : IDBObj =>
_bounded.TrySave(regionId, obj, iteration);
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
_bounded.Dispose();
_raw.Dispose();
}
}