using DatReaderWriter;
using DatReaderWriter.Enums;
using DatReaderWriter.Lib.IO;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace AcDream.Content;
///
/// THE → adapter,
/// shared by runtime DAT-backed gameplay/content access,
/// acdream-bake (AcDream.Bake.BakeRunner), and the pak equivalence suite
/// (AcDream.Content.Tests). MP1b's review found the
/// original App-internal copy had silently forked into three near-identical
/// versions — drift between them is exactly what the live-vs-pak equivalence
/// suite CANNOT detect (both sides would drift together only if they share
/// one implementation), so unification here is load-bearing, not cosmetic.
/// GL-free (DatReaderWriter only), hence Content is the right home
/// (established in MP1a alongside ).
///
/// History: O-D7 originally introduced this adapter (App-internal)
/// because ObjectMeshManager had 26 _dats.X call sites, letting it
/// stay byte-identical to the WB original while routing all DAT I/O through
/// the single DatCollection.
///
/// Iteration properties return the REAL dat iterations (the App copy
/// hardcoded 0 — a stub nothing read; the bake tool stamps iterations into
/// the pak header from DatCollection directly, and any future consumer of
/// these properties should get truth, so the unification intentionally
/// keeps the real values).
///
public sealed class DatCollectionAdapter : IDatReaderWriter {
private readonly DatCollection _dats;
private readonly DatDatabaseWrapper _portal;
private readonly DatDatabaseWrapper _cell;
private readonly DatDatabaseWrapper _highRes;
private readonly DatDatabaseWrapper _language;
private readonly ReadOnlyDictionary _cellRegions;
public DatCollectionAdapter(DatCollection dats) {
ArgumentNullException.ThrowIfNull(dats);
_dats = dats;
_portal = new DatDatabaseWrapper(dats.Portal);
_cell = new DatDatabaseWrapper(dats.Cell);
_highRes = new DatDatabaseWrapper(dats.HighRes);
_language = new DatDatabaseWrapper(dats.Local);
// DatCollection has a single Cell, not multiple cell regions.
// Expose it as region 0 to satisfy callers that iterate CellRegions.
var regions = new Dictionary { [0u] = _cell };
_cellRegions = new ReadOnlyDictionary(regions);
}
/// Source directory of the underlying DatCollection.
public string SourceDirectory => _dats.Options.DatDirectory ?? string.Empty;
///
/// Aggregate hit/miss/eviction counts across the four bounded per-database
/// object caches (portal/cell/highRes/language). 2026-07-24
/// measurement-tooling review: answers "do revisit portals hit or miss
/// the caches" for the DAT-object layer specifically.
///
public CacheStats ObjectCacheStats =>
_portal.ObjectCacheStats + _cell.ObjectCacheStats
+ _highRes.ObjectCacheStats + _language.ObjectCacheStats;
public IDatDatabase Portal => _portal;
public IDatDatabase Cell => _cell;
public ReadOnlyDictionary CellRegions => _cellRegions;
public IDatDatabase HighRes => _highRes;
public IDatDatabase Language => _language;
public IDatDatabase Local => _language;
[return: MaybeNull]
public T Get(uint fileId) where T : IDBObj =>
TryGet(fileId, out var value) ? value : default;
public bool TryGet(uint fileId, [MaybeNullWhen(false)] out T value) where T : IDBObj {
if (typeof(T) == typeof(DatReaderWriter.DBObjs.Iteration)) {
throw new Exception(
"Iteration is not a valid type to get from a dat file collection since it is used in all dat files. Use a specific dat like datCollection.Portal.Get()");
}
switch (_dats.TypeToDatFileType()) {
case DatFileType.Cell:
return _cell.TryGet(fileId, out value);
case DatFileType.Portal:
return _portal.TryGet(fileId, out value)
|| _highRes.TryGet(fileId, out value);
case DatFileType.Local:
return _language.TryGet(fileId, out value);
default:
value = default;
return false;
}
}
public IEnumerable GetAllIdsOfType() where T : IDBObj =>
_dats.TypeToDatFileType() switch {
DatFileType.Cell => _cell.GetAllIdsOfType(),
DatFileType.Portal => _portal.GetAllIdsOfType()
.Concat(_highRes.GetAllIdsOfType()),
DatFileType.Local => _language.GetAllIdsOfType(),
_ => Array.Empty(),
};
// RegionFileMap is used by some WB internals but not by any acdream consumer.
public ReadOnlyDictionary RegionFileMap =>
new ReadOnlyDictionary(new Dictionary());
public int PortalIteration => _portal.Iteration;
public int CellIteration => _cell.Iteration;
public int HighResIteration => _highRes.Iteration;
public int LanguageIteration => _language.Iteration;
public bool TryGetFileBytes(uint regionId, uint fileId, ref byte[] bytes, out int bytesRead) {
// Route through the wrapper's serialized raw-read path. The process
// content owner is shared by headless sessions, while DatDatabase's
// seek/read cursor is not a per-caller resource.
return _cell.TryGetFileBytes(fileId, ref bytes, out bytesRead);
}
///
/// Resolves a DAT id to all databases that contain it, along with the DBObjType.
/// Mirrors DefaultDatReaderWriter.ResolveId — checks each underlying DatDatabase
/// via DatDatabase.TypeFromId (which reads the type range tables).
///
public IEnumerable ResolveId(uint id) {
var results = new List();
void CheckDb(DatDatabaseWrapper wrapper) {
var rawDb = wrapper.RawDatabase;
if (rawDb.Tree.TryGetFile(id, out _)) {
var type = rawDb.TypeFromId(id);
if (type != DBObjType.Unknown)
results.Add(new IDatReaderWriter.IdResolution(wrapper, type));
}
}
// Match DefaultDatReaderWriter ordering: HighRes → Portal → Language → Cell
CheckDb(_highRes);
CheckDb(_portal);
CheckDb(_language);
CheckDb(_cell);
return results;
}
///
/// Direct, allocation-free production lookup. Portal precedence is
/// load-bearing: the legacy ResolveId ordering is HighRes-first and its
/// callers historically re-sorted the materialized results to make Portal
/// win. Encode that rule once instead.
///
public bool TryResolvePreferred(
uint id,
[NotNullWhen(true)] out IDatDatabase? database,
out DBObjType type) {
if (TryResolve(_portal, id, out type)) {
database = _portal;
return true;
}
if (TryResolve(_highRes, id, out type)) {
database = _highRes;
return true;
}
if (TryResolve(_language, id, out type)) {
database = _language;
return true;
}
if (TryResolve(_cell, id, out type)) {
database = _cell;
return true;
}
database = null;
type = DBObjType.Unknown;
return false;
}
private static bool TryResolve(
DatDatabaseWrapper database,
uint id,
out DBObjType type) {
DatDatabase raw = database.RawDatabase;
if (raw.Tree.TryGetFile(id, out _)) {
type = raw.TypeFromId(id);
if (type != DBObjType.Unknown)
return true;
}
type = DBObjType.Unknown;
return false;
}
public bool TrySave(T obj, int iteration = 0) where T : IDBObj =>
throw new NotSupportedException("DatCollectionAdapter is read-only.");
public bool TrySave(uint regionId, T obj, int iteration = 0) where T : IDBObj =>
throw new NotSupportedException("DatCollectionAdapter is read-only.");
public void Dispose() {
// The underlying DatCollection is owned by the caller — do not dispose it here.
// Individual wrapper objects hold no unmanaged resources.
}
}
///
/// Wraps a as .
/// Mirrors WorldBuilder.Shared.Services.DefaultDatDatabase (taken into our
/// tree in Phase O; moved here from AcDream.App in the MP1b adapter
/// unification).
///
public sealed class DatDatabaseWrapper : IDatDatabase {
private readonly DatDatabase _db;
// One cache per database: a DatCollectionAdapter therefore retains at
// most 4 * 256 decoded entries and 4 * 64 MiB of estimated payload. The
// cache itself documents why estimated bytes are not a hard heap bound.
private readonly BoundedDatObjectCache _cache = new();
private readonly object _databaseLock = new();
private int _malformedRecordDiagnosticEmitted;
public DatDatabaseWrapper(DatDatabase db) {
ArgumentNullException.ThrowIfNull(db);
_db = db;
}
/// Exposes the raw DatDatabase for ResolveId's Tree.TryGetFile + TypeFromId calls.
internal DatDatabase RawDatabase => _db;
public DatDatabase Db => _db;
public int Iteration => _db.Iteration?.CurrentIteration ?? 0;
/// This database's bounded DAT-object cache hit/miss/eviction counts (2026-07-24 measurement-tooling review).
public CacheStats ObjectCacheStats => _cache.Stats;
public IEnumerable GetAllIdsOfType() where T : IDBObj =>
_db.GetAllIdsOfType();
public bool TryGet(uint fileId, [MaybeNullWhen(false)] out T value) where T : IDBObj {
if (_cache.TryGet(fileId, out value)) {
return true;
}
bool existingRecordFailed;
lock (_databaseLock) {
// A different reader may have populated the cache while this
// caller waited for the serialized DatDatabase read path.
if (_cache.TryGet(fileId, out value)) {
return true;
}
if (_db.TryGet(fileId, out value)) {
value = _cache.GetOrAdd(fileId, value);
return true;
}
// A miss for an id whose BTree entry exists is a typed-access
// anomaly (wrong requested type, malformed content, or an unpacker
// failure). Capture only the fact while serialized DAT access is
// held; diagnostics must never perform console I/O under this
// shared lock.
existingRecordFailed = _db.Tree.TryGetFile(fileId, out _);
}
// One actionable report per database keeps a repeatedly requested
// malformed record from becoming another exception/logging-style hot
// path. Missing keys stay silent.
if (existingRecordFailed
&& Interlocked.Exchange(
ref _malformedRecordDiagnosticEmitted,
1) == 0)
{
Console.Error.WriteLine(
$"[dat-miss] {typeof(T).Name} 0x{fileId:X8} entry EXISTS but TryGet failed " +
$"(thread={Environment.CurrentManagedThreadId}; further reports suppressed)");
}
return false;
}
public bool TryGetFileBytes(uint fileId, [MaybeNullWhen(false)] out byte[] value) {
lock (_databaseLock) {
return _db.TryGetFileBytes(fileId, out value);
}
}
public bool TryGetFileBytes(uint fileId, ref byte[] bytes, out int bytesRead) {
lock (_databaseLock) {
return _db.TryGetFileBytes(fileId, ref bytes, out bytesRead);
}
}
public bool TrySave(T obj, int iteration = 0) where T : IDBObj =>
throw new NotSupportedException("DatDatabaseWrapper is read-only.");
public void Dispose() {
// The underlying DatDatabase is owned by DatCollection — do not dispose here.
}
}