12#include <unordered_map>
20 std::format(
"{}: {} bytes is too small for a WDBC header", info.
name, data.size()));
22 std::memcpy(&header, data.data(),
sizeof header);
27 std::format(
"{}: file record_size {} disagrees with the generated schema stride {}", info.
name,
30 const std::size_t expected =
sizeof header + std::size_t{header.
recordCount} * header.
recordSize + header.
32 if (data.size() != expected)
35 "{}: {} bytes on disk, but the header describes {} ({} records of {} bytes "
36 "+ {} string bytes)", info.
name, data.size(), expected, header.
recordCount,
41 if (
auto r = state.
strings.
read(data.subspan(
sizeof header + std::size_t{header.recordCount} * header.
recordSize,
46 for (std::uint32_t i = 0; i < header.
recordCount; ++i) {
47 const std::size_t rec = sink.
add();
48 const auto image = data.subspan(
sizeof header + std::size_t{i} * stride, stride);
67 out.reserve(
sizeof header + source.
size() * stride + pool.
block.
size());
68 out.resize(
sizeof header);
69 std::size_t cursor = 0;
70 for (std::size_t r = 0; r < source.
size(); ++r)
74 if (
auto w = pool.
block.
write(out); !w)
return std::unexpected{w.error()};
75 std::memcpy(out.data(), &header,
sizeof header);
The decode target: the codecs build the record vector through this, one field at a time.
virtual std::size_t add()=0
virtual void reserve(std::size_t n)=0
The encode source: the codecs read the record vector through this.
virtual std::size_t size() const =0
Shared codec internals for the fixed-stride formats (WDBC, WDB2): the string pool that reproduces byt...
std::size_t stringSlotCount(std::span< const Column > schema)
The per-record string-reference count of schema (the offset journal width).
void decodeInlineRecord(std::span< const Column > schema, std::size_t record, std::span< const std::byte > image, RecordSink &sink, TableState &state)
Decode one fixed-stride record's inline fields into sink and journal its string offsets (record then ...
void encodeInlineRecord(std::span< const Column > schema, const RecordSource &source, std::size_t record, FileBuffer &out, StringPool &pool, const TableState &state, std::size_t &cursor)
Encode one fixed-stride record's inline fields from source, appending to out and resolving strings th...
std::size_t recordStride(std::span< const Column > schema)
The fixed record stride (WDBC/WDB2) of schema.
std::uint32_t fieldSlotCount(std::span< const Column > schema)
The expanded on-disk field-slot count of schema (WDBC/WDB2 fieldCount).
Result< FileBuffer > writeWdbc(const TableInfo &info, const RecordSource &source, const TableState &state)
Encode a WDBC image; the string block preserves decoded offsets and appends new/changed strings (byte...
Result< void > readWdbc(const TableInfo &info, std::span< const std::byte > data, RecordSink &sink, TableState &state)
Decode a WDBC image: 20-byte header, fixed-stride records, string block.
std::expected< T, Error > Result
Every fallible wowlib operation returns Result<T>; bindings translate the error branch into a target-...
std::unexpected< Error > makeError(ErrorCode code, std::string message, std::uint32_t nativeError=0)
Shorthand for constructing the error branch of a Result.
@ SchemaMismatch
A client-database record layout disagrees with the generated WoWDBDefs schema.
@ TableTruncated
A client-database header, record block or satellite block overruns the file.
std::vector< std::byte > FileBuffer
Owning byte buffer for file contents read out of a client storage.
The per-table identity a codec needs beyond the record data: the client version (format selection,...
std::span< const Column > schema
The record schema (schemaOf<Record>()).
std::string_view name
The WoWDBDefs table name.
The preserved decode state a table carries between read() and write() so a WDBC/WDB2 write is byte-pe...
formats::StringBlock strings
Preserved string block; offsets never move.
void reset()
Reset the per-read state before decoding a fresh image.
std::vector< std::uint32_t > stringOffsets
The original string-block offset of every string field, row-major in record then schema order — the b...
std::uint32_t recordSize
Preserved header recordSize.
std::uint32_t fieldCount
Preserved header fieldCount (not always derivable).
std::uint32_t sourceMagic
The magic read() sniffed; 0 for a fresh table.
A writable string block resolving values to byte-perfect offsets: the journaled original offset while...
formats::StringBlock block
WDBC — the client-database format of every pre-Cataclysm .dbc (and the .dbc leftovers of Cataclysm....