Repository navigation
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.
-
The
Bdat::library: a small C-style reader of the file format. Matched, insrc/application/BF2/plugin/bdat.cpp(headerinclude/bdat.h). -
The game layer:
fw::BdatManager(loads files),gf::GfDataBdat(names every table with an id) and sixteengf::GfBdat*classes (typed accessors for one family of tables each).
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 slotidxof a global array of 16 slots (BDAT_FILE_MAX, the name comes from the assertion stringidx < BDAT_FILE_MAX) and immediately callsBdat::decodeScramble.unregistclears the slot. - The archive header holds a sheet count and a table of sheet offsets (
unknown001inbdat.cpp). The sheet header (unknown002, packed, its fields run up to offset0x24) has, with roles inferred from the code: a flags byte at0x04(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:
decodeScrambleSubruns 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.calcCheckSumandcalcCheckSumSubrecompute 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 bystrcmpon the sheet name, so sheets are sorted by name inside an archive.getFP(name)is that plus an (empty) error hookerrFile.isExistFP(name)isgetFPSub.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, andBdat::getMember(bdat, name)follows the collision chain comparing full names.getMember(bdat, index)returns the member by position (assertsmember < getMemberSize). Column names longer than 8 characters therefore share a bucket when their first 8 characters agree.
-
Row ids are not row numbers.
Bdat::getIdTopis the id of the first row,getIdCountthe number of rows andgetIdEndistop + count - 1.calcIdturns 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 zeroeduVal) 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
kindbyte of the variable description,getVarand friends):1scalar,2array (with an element count;getArray/getArrayVal,getArrayCount),3flag (a bit field inside another column, see below).getVarreturns an empty value for any other kind. -
Scalar types (
getVarVal,getVarType):1u8,2u16,3u32,4s8,5s16,632-bit (read as u32),7string (the cell holds an offset into the sheet, the result points at the text),832-bit read raw. Callers reinterpret type 8 as a float (Bdat::Value::valueF32insrc/application/BF2/event/cam_util.cppanddof_manager.cpp, and thef32accessors ofGfBdatBlade). -
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::getShiftMaskreturns the shift and mask of a flag. Game code uses it e.g. inbtl::BattleGlobal(Flagcolumn of the map-revision table) andBattleCharacter(Flagcolumn of the buff table, flags namedSEandBuff). -
Type-checked reads. The game calls
Bdat::getValCheck(bdat, "Column", id, type)everywhere with the type code it expects, e.g.type 1for a byte,2for a 16-bit value,7for 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 ofgetValCheck,getArrayValCheck,getVal(name, id)and the message-table functions (getMSText,getMSLabel,getMSStyle,getMSTextIndirect) are not decompiled yet. Their prototypes are declared ininclude/bdat.h; what they do on a type mismatch beyond the matched helpers is therefore not shown in the code.
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
BdatInfoslots (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::initializeManagerwaits 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.loadBdatFromMemoryregisters a block the caller already has. -
Language.
Impl::makePathprefixes 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 asbdat/<lang folder>/<name>.bdat(src/application/BF2/event/mstxt_res.cpp), and the gimmick manager loadsbdat/common_gmk.bdatwith the sheetRSC_GmkSetList(src/application/BF2/gf/field/gf_field_gimmick.cpp). - Each frame
BdatManager::updatecallsImpl::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 byupdateLayer. This is the lookup used by the game layer and byBdat::getFP("EVT_...")call sites in the event code.
File: src/application/BF2/gf/data/gf_data_bdat.cpp, ids in gf_bdat_id.h.
-
GfDataBdatis a singleton built byGfDataManager::initialize(see Save-Data). Its first act is to loadlookat.bdatitself, wait for it (GfSleep(1)in a loop), and then callsetupBdatInfo. The other files are loaded by code that this wiki has not located yet in the matched functions; the shape ofloadBdat(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).setupBdatInfofills an array of 413 sheet pointers by looking each name up throughfw::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, seeAGENTS.md); message tables get a_MSsuffix when they would clash. -
Naming by prefix (counted from
gf_bdat_id.h):FLD106,BTL86,MNU81,MENU39,ITM40,RSC14,BLD12,CHR10,VO7,SYS6,EVT5,FX3,CAM2,COL1,CMM1 (413 in total). Reading the table names,FLDis field data (maps, enemies, quests, gimmicks, time, weather),BTLbattle,ITMitems,BLDblade creation,CHRcharacters,EVTevents,VOvoice.MNUtables look like menu layout and behaviour data, whileMENUtables 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.
GfDataBdatlistens to the game event system (IGfEvent::gevReloadBdat). When a reload is requested it callsbdatRefresh()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::getReloadCountreturns 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.
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 toBdat::getValCheck,getArrayValCheck,getFlagVal. -
bdatRefresh()deletes the wrappers, builds them again fromGfDataBdat::getFP(<id>), and then, for every column, calls aninit<Column>helper that stores the result ofBdat::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 callswrapper.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.
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).