Skip to content
azerothcorePublic

About

Read and write World of Warcraft DBC and DB2 (WDBC, WDB2) files in pure TypeScript, driven by WoWDBDefs definitions. Works in Node and browsers.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

dbc-ts

Read, edit and write World of Warcraft client database files (.dbc and .db2) in TypeScript, with table layouts from WoWDBDefs.

  • Every client format, read and write: WDBC (every .dbc), WDB2 (Cataclysm to Warlords .db2), WDB3 to WDB6 (Legion) and WDC1 to WDC5 (Battle for Azeroth to today, and modern Classic). Sparse tables, copy tables, ID lists, relationship maps, compressed columns and encrypted sections included.
  • Schema driven: layouts come from WoWDBDefs .dbd files. Bundled, ready-made layouts for one build of every expansion and of modern Classic, found by build or by the layout hash of a DB2 file, and any .dbd text can be loaded at run time, so custom server tables and columns work too.
  • Lossless: an unchanged file is written back byte for byte. WDBC and WDB2 keep the row order and edits only touch the bytes they change; checked against every DBC file of the 3.3.5a and 4.3.4 clients. WDB3 and later files are re-encoded after an edit with every value kept (see Writing WDB3 and later files).
  • Pure TypeScript with a synchronous Uint8Array API. No DOM or Node built-ins, so the same code runs in Node 20+, browsers, workers and Electron. No dependencies.
  • Ships ESM, CommonJS and type declarations.

Install

npm install @helias32/dbc-ts

Usage

Open, edit and save a 3.3.5a table

import { openDbc } from '@helias32/dbc-ts';
import { definitions } from '@helias32/dbc-ts/definitions/3.3.5.12340';

const table = openDbc(bytes, { schema: definitions.Spell });

table.columns; // [{ name: 'ID', type: 'int32', arraySize: 1, isId: true, ... }, ...]
table.rowCount; // 49839

const row = table.findRowById(133);
table.getValue(row, 'SpellIconID'); // 185
table.getValue(row, 'Name_lang'); // { values: ['Fireball', '', ...16 locales], flags: 16712190 }
table.getRow(row); // every column of the row as an object

table.setValue(row, 'ManaCost', 25);
table.setValue(row, 'EffectBasePoints', 13, 0); // one element of an array column
table.setRow(row, { RangeIndex: 4, Name_lang: { values: ['Big fireball'], flags: 0 } });

const copy = table.addRow({ ...table.getRow(row), ID: 90001 }); // appended at the end
table.insertRow(0, { ID: 90002 }); // unset values are 0 or '', IDs default to max + 1
table.deleteRow(copy + 1);

const out = table.toBytes(); // Uint8Array, same format as the input

findSchema(definitions, 'DBFilesClient\\spell.dbc') finds a table by file name, ignoring case, folders and the extension.

Open a modern DB2 file

WDB5 and later files carry the layout hash of their table, so the layout can be picked from a definitions module without knowing the table name:

import { openDbc } from '@helias32/dbc-ts';
import { definitions } from '@helias32/dbc-ts/definitions/11.2.7.65299';

const table = openDbc(bytes, { definitions }); // by layout hash, or add `fileName`
table.format; // 'WDC5'
table.db2.layoutHash; // 8 hex digits, as WoWDBDefs writes them
table.findRowById(133);
table.setValue(row, 'Name_lang', 'Fireball');
const out = table.toBytes(); // WDC5 again

findSchemaByLayoutHash(definitions, hash) does the lookup alone. A file whose build is not bundled opens with schemaFromDbd(dbdText, build, { layoutHash }) and its .dbd file from WoWDBDefs.

Load your own definitions

Servers often add tables or columns. Load a .dbd file (WoWDBDefs format) and resolve it for a build:

import { openDbc, schemaFromDbd } from '@helias32/dbc-ts';

const schema = schemaFromDbd(dbdText, '3.3.5.12340', { name: 'MyCustomTable' });
const table = openDbc(bytes, { schema });

parseDbd(text) and findDbdVersion(dbd, build) give access to the parsed file and its version blocks. A TableSchema is plain data, so it can also be written by hand or stored as JSON.

Without a schema, a WDBC or WDB2 table whose records are all 4-byte fields opens with one uint32 column per field (field_0, field_1, …), so unknown tables can still be viewed and edited. A WDB5 or later table opens with one unsigned column per field, sized and shaped as the file describes it, plus ID and RelationID columns when the file has an ID list or relationship map; strings then show as numbers. Sparse tables (inline strings) need a schema.

Node and the browser

// Node
import { readFile, writeFile } from 'node:fs/promises';
const table = openDbc(await readFile('Spell.dbc'), { schema: definitions.Spell });
await writeFile('Spell.dbc', table.toBytes());

// Browser
const table = openDbc(new Uint8Array(await file.arrayBuffer()), { schema });
const url = URL.createObjectURL(new Blob([table.toBytes()]));

Values

Column type Value
int8 uint8 int16 uint16 int32 uint32 number
int64 uint64 bigint (a safe integer number is accepted when setting)
float number (stored as 32-bit float)
string string
locstring, 1.x to 3.x { values: string[], flags: number }
locstring, 4.x and later string

In WDB3 and later files the ID and the relation column can live outside the records (the ID list and the relationship map; nonInline columns). They are ordinary columns of the table.

WDB6 files keep some fields only in their common data, and WoWDBDefs often leaves these out or lists them as non-inline relations. When the file has no relationship data, such non-inline columns are read from the common data, and the common fields the schema leaves out are added as field_<n> columns, so a write keeps them.

Array columns hold an array of arraySize values. Signed or unsigned comes from the schema, as the files do not store it.

A locstring in 2.x and 3.x is 16 string offsets (enUS, koKR, frFR, deDE, zhCN, zhTW, esES, esMX, ruRU, jaJP, ptPT, itIT and 4 unused) plus a flags field: 17 file fields shown as one column (LOCALES_16 lists the names). In 1.x it is 8 offsets plus flags (LOCALES_8). Setting values with fewer entries than locales fills the rest with ''.

Setting a value checks its type and range and throws BAD_VALUE without changing anything.

Saving and strings

Records keep their raw bytes and are decoded when read, so columns a schema gets wrong, unknown bytes and padding survive a save. toBytes() writes the format the file was read in.

  • toBytes() (strings: 'preserve', the default) keeps the original string block as it is and appends new strings at its end, reusing any string already in the block. Unchanged records keep their offsets, so an unchanged table, or one where values were set to what they already were, is written back byte for byte. Strings no longer used stay in the block.
  • toBytes({ strings: 'compact' }) writes a new block with each string in use once, in record order after a leading empty string. That is how the Blizzard files are laid out: every 3.3.5a DBC file also comes back byte for byte in this mode.

WDB2 files keep their header (table hash, build, timestamp, locale). Files after build 12880 can hold an ID index with the total string length of each row; it is written back as it was when nothing changed, and rebuilt from the ID column after an edit.

Formats

Format Files Read Write
WDBC .dbc, 0.5.3 to 3.3.5a, and some .dbc until 6.x yes yes
WDB2 .db2, 4.x to 6.x yes yes
WDB3, WDB4 .db2 of the first Legion alpha builds yes yes
WDB5, WDB6 .db2, 7.0.3 to 7.3.2 yes yes
WDC1 .db2, 7.3.5 yes yes
WDC2, CLS1 .db2, 8.0.1 and the first Classic 1.13 builds yes yes
WDC3 .db2, 8.1 to 10.0, and modern Classic (1.14 on, 2.5, 3.4, 4.4) yes yes
WDC4, WDC5 .db2, 10.1 and later, and the newest Classic clients yes yes

The DB2 formats are ported from DBCD and were checked against it on the test tables: DBCD reads the files written here with the same values, and WDC files written by DBCD read back the same here. In WDC5 files the pallets of single values come before the pallet arrays, as DBCD reads them since a fix for 12.1.5 files; the wowdev.wiki page and other readers keep column order, so this is worth checking on real WDC5 files. WDB2 files with a copy table are not supported (none are known: WDB2 always writes it empty).

table.db2 holds the header of WDB3 and later files (tableHash, layoutHash, flags, minId, maxId, locale, schemaString of WDC5, …) and their sections.

Encrypted sections

WDC3 and later files can encrypt sections with a TACT key. Extractors that do not have the key write zeros in their place. Such a section is skipped: its records are not in the table, and table.db2 shows it:

table.db2.skippedRecords; // records of skipped sections, 0 when nothing is missing
table.db2.sections; // [{ tactKeyLookup: 'FEDCBA9876543210', recordCount: 2, skipped: true, encryptedIds: [50, 51] }, …]

A section that was decrypted reads like any other (it keeps its tactKeyLookup). WDC4 and later list the IDs of encrypted records (encryptedIds).

Writing WDB3 and later files

  • toBytes() on an unchanged table returns the bytes it was opened from.
  • After an edit, or with toBytes({ rebuild: true }), the file is re-encoded in its own format, keeping its layout: header fields, flags, field sizes, the compression of each column and the sections. What depends on the values is rebuilt: bitpacked widths grow when a value no longer fits, new pallet and common values are added (the original pallets keep their order), string tables keep the original strings and get new ones appended, and the ID list, copy table, offset map and relationship map are written from the rows.
  • Reading the result gives the same rows with the same values. The order is kept, except that a new row joins the first section that is not encrypted, and rows stored through the copy table are listed after the other rows of their section (as on every read).
  • Rows that came from the copy table stay copies while they equal their source; edited ones become records of their own.
  • Skipped encrypted sections are written back zero-filled, with their header and ID range; WDC4+ encrypted ID lists are kept. When every section is encrypted, new rows go to a new section (WDC3 and later; WDC2 throws UNSUPPORTED).
  • A value that does not fit a fixed-size field (a 2-byte WDB5 field, an uncompressed WDC field) throws BAD_VALUE from toBytes().
  • The bytes match the input when the file was laid out the same way: the test files are, and so are the files DBCD writes, apart from padding. Blizzard's files may order or pad things differently, so a re-encoded file can differ in bytes while holding the same values.

Bundled definitions

@helias32/dbc-ts/definitions/<build> exports definitions (table name to TableSchema), build and source (the WoWDBDefs commit). They are separate entry points, so an app only loads the builds it imports. Schemas of WDB5 and later builds carry their layoutHashes.

Build Client Tables
1.12.1.5875 Vanilla 158
2.4.3.8606 The Burning Crusade 185
3.3.5.12340 Wrath of the Lich King 246
4.3.4.15595 Cataclysm 331
5.4.8.18414 Mists of Pandaria 409
6.2.4.21742 Warlords of Draenor 165
7.3.5.26972 Legion 612
8.3.7.35662 Battle for Azeroth 716
9.2.7.45745 Shadowlands 830
10.2.7.55664 Dragonflight 987
11.2.7.65299 The War Within 1087
12.1.7.71087 Midnight 1167
1.15.8.67156 Classic Era 848
2.5.5.68575 Burning Crusade Classic 944
3.4.5.61934 Wrath Classic 839
4.4.2.60895 Cataclysm Classic 815
5.5.4.71089 Mists of Pandaria Classic 944

Layouts of other builds can differ: for those, load the table's .dbd file with schemaFromDbd. WoWDBDefs covers 6.x only partly. The modules store the layouts in a compact text form (packSchemas / unpackSchemas) and expand them on import.

They are generated by npm run definitions from a pinned WoWDBDefs commit (see definitions/README.md); other builds can be added to the script. Column names for old builds are partly guesses (verified: false on a column marks them) and can differ from the names in AzerothCore or TrinityCore; fixes belong upstream in WoWDBDefs. The few local corrections are listed with their reasons in scripts/definition-overrides.ts: in 3.3.5a and 4.3.4, bit masks (such as Spell attributes and race and class masks), ARGB colours and MD5 words are read as uint32, so a value with bit 31 set is a positive number rather than a negative one. Columns whose negative values are meaningful, such as a -1 that means "any", stay signed. The 3.3.5a VehicleSeat flags, which WoWDBDefs types as floats, are read as uint32 too. 1.12.1 and 2.4.3 have no corrections.

API

Function Description
openDbc(bytes, { schema?, definitions?, fileName? }) Opens a DBC or DB2 file of any format as a DbcTable.
readDbcHeader(bytes) Header only: format, counts, sizes, WDB2 or DB2 fields.
schemaFromDbd(dbd, build, { name?, layoutHash? }) Layout of a table for a build, from .dbd text or a parsed file.
parseDbd(text) / findDbdVersion(dbd, build, layoutHash?) Parse a .dbd file and pick its version block.
findSchema(schemas, fileName) Finds a table by file name, ignoring case, folders and extension.
findSchemaByLayoutHash(schemas, hash) Finds the table of a WDB5+ layout hash.
packSchemas(schemas) / unpackSchemas(build, locales, packed) The compact form the bundled definitions use.
parseBuild, compareBuilds, formatBuild, localeCountForBuild Build helpers.

DbcTable has format, columns, schema, rowCount, fieldCount, recordSize, wdb2, db2, idColumn, isModified, and the methods getColumn, getRow, getValue, setValue, setRow, addRow, insertRow, deleteRow, findRowById and toBytes.

Errors

Every failure throws a DbcError with a code.

Code Meaning
BAD_MAGIC Not a DBC or DB2 file.
TRUNCATED The file ends before what its header declares.
BAD_HEADER A header field holds an invalid value.
UNSUPPORTED A feature this library does not handle, such as a WDB2 copy table.
SCHEMA_MISMATCH The schema's field count or record size does not match the file.
SCHEMA_REQUIRED The file cannot be opened without a schema.
NO_DEFINITION The .dbd file has no layout for the build.
BAD_DBD Malformed .dbd text (the message has the line number).
BAD_STRING A string offset points outside the string block.
BAD_VALUE A value of the wrong type or out of range for its column.
BAD_INPUT An invalid argument: unknown column, row out of range, invalid schema.

Development

npm ci
npm run lint && npm run typecheck && npm run test:coverage && npm run build && npm run smoke

Tests need 100% coverage. Committed tests use synthetic data only: game client files are copyrighted and must never be committed. The DB2 test files are built byte by byte in test/samples.ts, independently of the writer. To test against a real 3.3.5a client, point DBC_TS_CLIENT_DBC at a folder of .dbc files (for example the dbc folder of an AzerothCore server). For any other client, point DBC_TS_CLIENT_DB2 at a folder of .db2 and .dbc files and DBC_TS_CLIENT_BUILD at their build, which picks the bundled layouts by file name (WDB5+ files find theirs by layout hash in every bundled build):

DBC_TS_CLIENT_DBC=/path/to/dbc npm test
DBC_TS_CLIENT_DB2=/path/to/DBFilesClient DBC_TS_CLIENT_BUILD=11.2.7.65299 npm test
npm run corpus -- --root /path/to/DBFilesClient --build 11.2.7.65299

The corpus script prints how many files parse, come back byte for byte unchanged, keep their values when re-encoded and read back after edits.

Credits

License

MIT, except the bundled definitions in definitions/ (and their builds in dist/definitions/), which are CC BY-SA 4.0: see definitions/LICENSE.md.

About

Read and write World of Warcraft DBC and DB2 (WDBC, WDB2) files in pure TypeScript, driven by WoWDBDefs definitions. Works in Node and browsers.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages