Adversarially-verified review findings 7 and 8: (7) The DatCollection->IDatReaderWriter adapter existed as THREE near-identical copies (App-internal original, Bake's copy, Content. Tests' copy) — a structure where adapter drift is exactly what the live-vs-pak equivalence suite cannot detect (both sides would only drift together if they shared one implementation). Now ONE public AcDream.Content.DatCollectionAdapter next to IDatReaderWriter (GL-free home established in MP1a), carrying App's FULL behavior including the [dat-miss] TryGet tripwire log (which now also covers the bake tool and the equivalence suite) and the caching/locking. All three copies deleted; WbMeshAdapter (App), BakeRunner (Bake), and PakEquivalenceTests (Content.Tests) resolve the shared class. Iteration properties return the REAL dat iterations — the App copy's hardcoded 0 was a stub nothing read; the unification intentionally keeps truth (noted in the doc comment). Verified post-move: no Silk.NET anywhere in Content / Bake / Content.Tests / Bake.Tests resolved dependency graphs. (8) Two test gaps closed in PakRoundTripTests: (a) direct on-disk TOC sortedness — blobs added in DESCENDING key order, then the raw file bytes parsed (not through the reader) and every TOC entry asserted strictly ascending; (b) corrupt-blob logging — five repeated reads through both public paths (TryReadObjectMeshData + ContainsKey) with stderr captured, asserting exactly ONE [pak-corrupt] line for the victim key. Full suite: 4120 tests, 0 failures (Content.Tests 56, Bake.Tests 1, plus the pre-existing 4 skips).
188 lines
7.9 KiB
C#
188 lines
7.9 KiB
C#
using DatReaderWriter;
|
|
using DatReaderWriter.Enums;
|
|
using DatReaderWriter.Lib.IO;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace AcDream.Content;
|
|
|
|
/// <summary>
|
|
/// THE <see cref="DatCollection"/> → <see cref="IDatReaderWriter"/> adapter,
|
|
/// shared by all three consumers: ObjectMeshManager (AcDream.App, via
|
|
/// WbMeshAdapter), 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 <see cref="IDatReaderWriter"/>).
|
|
///
|
|
/// <para>History: O-D7 originally introduced this adapter (App-internal)
|
|
/// because ObjectMeshManager had 26 <c>_dats.X</c> call sites, letting it
|
|
/// stay byte-identical to the WB original while routing all DAT I/O through
|
|
/// the single DatCollection.</para>
|
|
///
|
|
/// <para>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).</para>
|
|
/// </summary>
|
|
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<uint, IDatDatabase> _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<uint, IDatDatabase> { [0u] = _cell };
|
|
_cellRegions = new ReadOnlyDictionary<uint, IDatDatabase>(regions);
|
|
}
|
|
|
|
/// <summary>Source directory of the underlying DatCollection.</summary>
|
|
public string SourceDirectory => _dats.Options.DatDirectory ?? string.Empty;
|
|
|
|
public IDatDatabase Portal => _portal;
|
|
public ReadOnlyDictionary<uint, IDatDatabase> CellRegions => _cellRegions;
|
|
public IDatDatabase HighRes => _highRes;
|
|
public IDatDatabase Language => _language;
|
|
|
|
// RegionFileMap is used by some WB internals but not by any acdream consumer.
|
|
public ReadOnlyDictionary<uint, uint> RegionFileMap =>
|
|
new ReadOnlyDictionary<uint, uint>(new Dictionary<uint, uint>());
|
|
|
|
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 to cell db (the only region we expose)
|
|
return _dats.Cell.TryGetFileBytes(fileId, ref bytes, out bytesRead);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
public IEnumerable<IDatReaderWriter.IdResolution> ResolveId(uint id) {
|
|
var results = new List<IDatReaderWriter.IdResolution>();
|
|
|
|
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;
|
|
}
|
|
|
|
public bool TrySave<T>(T obj, int iteration = 0) where T : IDBObj =>
|
|
throw new NotSupportedException("DatCollectionAdapter is read-only.");
|
|
|
|
public bool TrySave<T>(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.
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wraps a <see cref="DatDatabase"/> as <see cref="IDatDatabase"/>.
|
|
/// Mirrors WorldBuilder.Shared.Services.DefaultDatDatabase (taken into our
|
|
/// tree in Phase O; moved here from AcDream.App in the MP1b adapter
|
|
/// unification).
|
|
/// </summary>
|
|
public sealed class DatDatabaseWrapper : IDatDatabase {
|
|
private readonly DatDatabase _db;
|
|
private readonly ConcurrentDictionary<(Type, uint), IDBObj> _cache = new();
|
|
private readonly object _lock = new();
|
|
|
|
public DatDatabaseWrapper(DatDatabase db) {
|
|
ArgumentNullException.ThrowIfNull(db);
|
|
_db = db;
|
|
}
|
|
|
|
/// <summary>Exposes the raw DatDatabase for ResolveId's Tree.TryGetFile + TypeFromId calls.</summary>
|
|
internal DatDatabase RawDatabase => _db;
|
|
|
|
public DatDatabase Db => _db;
|
|
public int Iteration => _db.Iteration?.CurrentIteration ?? 0;
|
|
|
|
public IEnumerable<uint> GetAllIdsOfType<T>() where T : IDBObj =>
|
|
_db.GetAllIdsOfType<T>();
|
|
|
|
public bool TryGet<T>(uint fileId, [MaybeNullWhen(false)] out T value) where T : IDBObj {
|
|
if (_cache.TryGetValue((typeof(T), fileId), out var cached)) {
|
|
value = (T)cached;
|
|
return true;
|
|
}
|
|
|
|
lock (_lock) {
|
|
if (_db.TryGet<T>(fileId, out value)) {
|
|
_cache.TryAdd((typeof(T), fileId), value);
|
|
return true;
|
|
}
|
|
|
|
// TEMP diagnostic (dat-race investigation 2026-06-09, strip with fix):
|
|
// a miss for an id whose BTree entry EXISTS is always an anomaly —
|
|
// either Unpack returned false or the lookup flickered transiently.
|
|
// Legit not-found probes (e.g. Portal→HighRes fallback) stay silent.
|
|
// Kept through the MP1b unification — the tripwire now covers the
|
|
// bake tool and the equivalence suite too, not just the client.
|
|
if (_db.Tree.TryGetFile(fileId, out _)) {
|
|
Console.WriteLine(
|
|
$"[dat-miss] {typeof(T).Name} 0x{fileId:X8} entry EXISTS but TryGet failed " +
|
|
$"(thread={Environment.CurrentManagedThreadId})");
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool TryGetFileBytes(uint fileId, [MaybeNullWhen(false)] out byte[] value) {
|
|
lock (_lock) {
|
|
return _db.TryGetFileBytes(fileId, out value);
|
|
}
|
|
}
|
|
|
|
public bool TryGetFileBytes(uint fileId, ref byte[] bytes, out int bytesRead) {
|
|
lock (_lock) {
|
|
return _db.TryGetFileBytes(fileId, ref bytes, out bytesRead);
|
|
}
|
|
}
|
|
|
|
public bool TrySave<T>(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.
|
|
}
|
|
}
|