Skip to content

BDAT Data

Kevin Bonnoron edited this page Nov 21, 2026 · 1 revision

BDAT Data

Almost every number the game uses (stats, probabilities, names of resources, quest rules, conditions) lives in BDAT files: tables that the game loads at start-up and reads by name. This page explains how the executable reads them. It describes tables and columns by name only; their contents are not reproduced here.

Reading guide: see Home. "Inferred" marks a role the repository gave a field from the way the code uses it. Snapshot: main at commit a313ecb66.

Two layers

  1. The Bdat:: library: a small C-style reader of the file format. Matched, in src/application/BF2/plugin/bdat.cpp (header include/bdat.h).
  2. The game layer: fw::BdatManager (loads files), gf::GfDataBdat (names every table with an id) and sixteen gf::GfBdat* classes (typed accessors for one family of tables each).

File and sheet layout (Bdat:: library)

A BDAT file is an archive of sheets (tables). The sheet is the unit callers talk about; the library says FP ("file pointer") for a pointer to one sheet.

  • Bdat::regist(idx, data) stores a loaded archive in slot idx of a global array of 16 slots (BDAT_FILE_MAX, the name comes from the assertion string idx < BDAT_FILE_MAX) and immediately calls Bdat::decodeScramble. unregist clears the slot.
  • The archive header holds a sheet count and a table of sheet offsets (unknown001 in bdat.cpp). The sheet header (unknown002, packed, its fields run up to offset 0x24) has, with roles inferred from the code: a flags byte at 0x04 (bit 1 = the sheet is still scrambled), the offset of the sheet name, the row size, the offset and size of the member hash table, the offset of the row data, the row count, the first row id, a checksum/scramble key, the range of the string data, and the offset and count of the member (column) table.
  • Scrambling. Sheets whose flag bit 1 is set are decoded in place when registered: decodeScrambleSub runs a two-lane rolling XOR (even and odd bytes each with their own running key, seeded from the 16-bit key of the header) over the string data and over the range from the sheet name up to the member hash table, then clears the flag. calcCheckSum and calcCheckSumSub recompute a byte sum (each byte shifted left by its index modulo 4) and compare it with the header value.
  • Finding a sheet. Bdat::getFPSub(name) visits the registered archives in slot order and binary-searches each archive's sheets by strcmp on the sheet name, so sheets are sorted by name inside an archive. getFP(name) is that plus an (empty) error hook errFile. isExistFP(name) is getFPSub. getFP(fileIdx, idx) takes a sheet by position in an archive (asserts both indices).
  • Finding a column. A sheet has a hash table of its columns ("members"): Bdat::calcHash(name, size) hashes only the first 8 characters (hash = hash * 7 + char) modulo the table size, and Bdat::getMember(bdat, name) follows the collision chain comparing full names. getMember(bdat, index) returns the member by position (asserts member < getMemberSize). Column names longer than 8 characters therefore share a bucket when their first 8 characters agree.

Rows, ids and value kinds

  • Row ids are not row numbers. Bdat::getIdTop is the id of the first row, getIdCount the number of rows and getIdEnd is top + count - 1. calcId turns an id into a row index (id - top) and fails when the result is not below the count. Reading an id outside the sheet returns an empty value (a zeroed uVal) instead of failing; this is why the typed accessors of the game layer (below) all begin with a range check and return 0.
  • Three kinds of column (the kind byte of the variable description, getVar and friends): 1 scalar, 2 array (with an element count; getArray / getArrayVal, getArrayCount), 3 flag (a bit field inside another column, see below). getVar returns an empty value for any other kind.
  • Scalar types (getVarVal, getVarType): 1 u8, 2 u16, 3 u32, 4 s8, 5 s16, 6 32-bit (read as u32), 7 string (the cell holds an offset into the sheet, the result points at the text), 8 32-bit read raw. Callers reinterpret type 8 as a float (Bdat::Value::valueF32 in src/application/BF2/event/cam_util.cpp and dof_manager.cpp, and the f32 accessors of GfBdatBlade).
  • Flags (Bdat::getFlagVal(bdat, name, id, flagName)): the flag column names its parent column; the function checks that the parent matches, reads the parent as 8, 16 or 32 bits according to the parent's type, ANDs with the 32-bit mask stored in the flag's description and shifts right by the stored shift. Bdat::getShiftMask returns the shift and mask of a flag. Game code uses it e.g. in btl::BattleGlobal (Flag column of the map-revision table) and BattleCharacter (Flag column of the buff table, flags named SE and Buff).
  • Type-checked reads. The game calls Bdat::getValCheck(bdat, "Column", id, type) everywhere with the type code it expects, e.g. type 1 for a byte, 2 for a 16-bit value, 7 for a string. getValCheckSub (matched) tests that the column's declared type equals the requested type; convertType (matched) converts between the integer types and refuses (returns an empty value) when either side is a string or a float. The bodies of getValCheck, getArrayValCheck, getVal(name, id) and the message-table functions (getMSText, getMSLabel, getMSStyle, getMSTextIndirect) are not decompiled yet. Their prototypes are declared in include/bdat.h; what they do on a type mismatch beyond the matched helpers is therefore not shown in the code.

Loading files: fw::BdatManager

File: src/application/BF2/framework/bdat/bdat_manager.cpp (header include/fw/fw_bdat_manager.h). One of the framework managers (see Engine-Overview).

  • It owns 16 BdatInfo slots (matching the 16 slots of the library). A slot goes through four states: unused, loading, loaded but not registered, enabled (STATE_UNKNOWN_0..3, inferred roles). isEnableBdat(id) is true only in the last state; Framework::initializeManager waits for slots 0 and 1 to be enabled before the framework counts as initialised (src/application/BF2/framework/framework.cpp).
  • loadBdat(id, memHandle, path) builds the full path from a base path (setBasePath) and the name, normalises it and starts an asynchronous read (ml::FileResTh). If the slot already holds the same name and the same language folder, it returns at once; otherwise it unregisters the old file. reloadBdat(id) reloads a slot from its stored name. loadBdatFromMemory registers a block the caller already has.
  • Language. Impl::makePath prefixes the language folder (LangUtil::getLangFolder(), one folder name per selected language) to any file name that contains _ms.. So message tables (..._ms.bdat) are per language and everything else is shared. This is the only language handling in the manager. The event system loads its own text sheets as bdat/<lang folder>/<name>.bdat (src/application/BF2/event/mstxt_res.cpp), and the gimmick manager loads bdat/common_gmk.bdat with the sheet RSC_GmkSetList (src/application/BF2/gf/field/gf_field_gimmick.cpp).
  • Each frame BdatManager::update calls Impl::update, which gives every slot a chance to finish loading and register its sheets (updateLayer, not decompiled yet).
  • By name. BdatManager::getFp(name) looks the sheet up in a red-black map keyed by the CRC of the name (mm::mtl::hashStrCrc), filled by updateLayer. This is the lookup used by the game layer and by Bdat::getFP("EVT_...") call sites in the event code.

Naming every table: gf::GfDataBdat

File: src/application/BF2/gf/data/gf_data_bdat.cpp, ids in gf_bdat_id.h.

  • GfDataBdat is a singleton built by GfDataManager::initialize (see Save-Data). Its first act is to load lookat.bdat itself, wait for it (GfSleep(1) in a loop), and then call setupBdatInfo. The other files are loaded by code that this wiki has not located yet in the matched functions; the shape of loadBdat (it formats "%s.bdat") is known.
  • There are 413 table ids (GfDataBdat::BDAT_COUNT = 0x19d, the length of the game's name array _gfbdat::s_aszGfBdatName). setupBdatInfo fills an array of 413 sheet pointers by looking each name up through fw::BdatManager::getFp. GfDataBdat::getFP(id) returns that pointer, or null for an id out of range. The project's table-id constants are derived from the game's names by a written rule (BDAT_ + the table name split at case boundaries and upper-cased, see AGENTS.md); message tables get a _MS suffix when they would clash.
  • Naming by prefix (counted from gf_bdat_id.h): FLD 106, BTL 86, MNU 81, MENU 39, ITM 40, RSC 14, BLD 12, CHR 10, VO 7, SYS 6, EVT 5, FX 3, CAM 2, COL 1, CMM 1 (413 in total). Reading the table names, FLD is field data (maps, enemies, quests, gimmicks, time, weather), BTL battle, ITM items, BLD blade creation, CHR characters, EVT events, VO voice. MNU tables look like menu layout and behaviour data, while MENU tables are menu texts and categories, most of them ending in _MS. That reading is from names only; the contents are not listed here.
  • Hot reload. GfDataBdat listens to the game event system (IGfEvent::gevReloadBdat). When a reload is requested it calls bdatRefresh() on every registered accessor (gf_data_bdat.cpp, gevReloadBdat). The field manager, quest manager, game manager, auto-talk manager and others react to the same event (gf_field_manager.cpp, gf_quest_manager.cpp, gf_game_manager.cpp, gf_auto_talk.cpp). GfDataBdat::getReloadCount returns how many times the pointers were rebuilt. [INFERENCE] The mechanism lets data change without restarting the game, which looks like a development feature; the code that raises the event was not examined.

Typed accessors: the GfBdat* classes

GfDataBdat::initialize creates sixteen accessor singletons, in this order: GfBdatQuest, GfBdatBlade, GfBdatItem, GfBdatCond, GfBdatMaplist, GfBdatChrDr, GfBdatChrBl, GfBdatBgm, GfBdatChrEnArrange, GfBdatChrEnParam, GfBdatRsc, GfBdatBtl, GfBdatMsg, GfBdatLookAt, GfBdatShop, GfBdatUniqueMotion (files src/application/BF2/gf/data/gf_bdat_*.cpp). All implement IDataBdat::bdatRefresh.

The pattern, from gf_bdat_blade.cpp (see Blade-Creation):

  • Each class keeps a few CBdatWrapper * members (gf_bdat_wrapper.h), one per table; a wrapper is one pointer to the sheet and thin forwards to Bdat::getValCheck, getArrayValCheck, getFlagVal.
  • bdatRefresh() deletes the wrappers, builds them again from GfDataBdat::getFP(<id>), and then, for every column, calls an init<Column> helper that stores the result of Bdat::getMember(sheet, "<Column>") in a file-level array of member pointers. The column is therefore resolved by name once per refresh, not per read.
  • Each getter (getBLD_IdeaNeedPoint(id), ...) checks that the cached member is not null and that the id is inside [IdTop, IdTop + IdCount), and returns 0 otherwise (0.0 for float getters). Otherwise it calls wrapper.getValCheck(member, id, <type>) with the column's type and returns the field of the result.
  • Every table also has <Table>Start() and <Table>End() getters (first id and one past the last) and array columns get an (id, index) getter that dispatches through a table of the numbered getters.

GfBdatMsg (gf_bdat_msg.cpp) keeps pointers to the message sheets and returns text through Bdat::getMSText(sheet, id): names of enemies, NPCs, quests, map info, landmarks, collection items, crystals, precious items, field skills, drivers, blades... Which sheet a quest name comes from depends on the quest's category (GfBdatMsg::getFpQuest).

Battle, event and menu code often skip the accessor layer and read straight from the sheet: Bdat::getValCheck(gf::GfDataBdat::getFP(gf::BDAT_BTL_BULLET), "AddAtkNum", row, 1) (src/application/BF2/battle/arts_data.cpp) or, by name, Bdat::getValCheck(Bdat::getFP("EVT_dof"), "dist", id, 8) (src/application/BF2/event/dof_manager.cpp). Those call sites are the best source of column names and types.

Not decompiled yet

Bdat::getVal(bdat, name, id) (0x71006b4e78, 72 bytes), Bdat::getValCheck (three overloads, 0x71006b5d04, 0x71006b5fe8, 0x71006b60a4), Bdat::getArrayValCheck (three overloads, 0x71006b6148 to 0x71006b62c4), Bdat::getMSText, getMSTextIndirect, getMSStyle, getMSLabel (0x71006b6368 to 0x71006b6518), fw::BdatManager::Impl::updateLayer (0x7100153684, 668 bytes), Impl::find (0x7100153d00), Impl::unregistBdat(int) (0x7100152ecc) and the BdatManager constructor (0x71001540c0).

See also

Engine-Overview, Save-Data, Blade-Creation

Clone this wiki locally