pub struct DB { /* private fields */ }Expand description
In-memory representation of a decoded DB table.
Holds a complete DB table including header metadata and row data. The table data
is stored as a TableInMemory which provides access to rows and columns.
§Fields
mysterious_byte- Boolean flag of unknown purpose (observed as0or1)guid- Globally Unique Identifier for this table instancetable- The actual table data with definition and rows
§Getters
Fields have public getters via the getset crate:
mysterious_byte()- Get the mysterious byte valueguid()- Get the table’s GUIDtable()- Get reference to the table data
§The Mysterious Byte
The purpose of this byte is unknown, but it appears in all DB tables. Observed values
are 0 or 1 (interpreted as boolean). In Warhammer 2, a value of 0 can cause
crashes when tables are loaded by the game.
§GUID Handling
GUIDs are only present in some games (e.g., Warhammer series). Older games like Napoleon and Empire don’t use GUIDs, and adding them can crash those games. The encoding process respects the game’s GUID requirements.
§Example
use rpfm_lib::files::{Decodeable, db::DB, DecodeableExtraData, table::Table};
use rpfm_lib::schema::Schema;
use std::io::Cursor;
let mut extra = DecodeableExtraData::default();
extra.set_schema(Some(&schema));
extra.set_table_name(Some("units_tables"));
let mut reader = Cursor::new(table_data);
let db = DB::decode(&mut reader, &Some(extra)).unwrap();
// Access table data
let row_count = db.table().len();Implementations§
Source§impl DB
impl DB
Sourcepub fn mysterious_byte(&self) -> &bool
pub fn mysterious_byte(&self) -> &bool
Boolean flag of unknown purpose (always 0 or 1).
In Warhammer 2, a value of 0 can crash the game when loading tables.
Sourcepub fn guid(&self) -> &String
pub fn guid(&self) -> &String
Globally Unique Identifier for this table instance.
Only present in newer games. Empty string for games without GUID support.
Sourcepub fn table(&self) -> &TableInMemory
pub fn table(&self) -> &TableInMemory
The table data including definition and rows.
Source§impl DB
impl DB
Sourcepub fn new(
definition: &Definition,
definition_patch: Option<&DefinitionPatch>,
table_name: &str,
) -> Self
pub fn new( definition: &Definition, definition_patch: Option<&DefinitionPatch>, table_name: &str, ) -> Self
Creates a new empty DB table with the specified definition.
Initializes a DB table with no rows but with the structure defined by the provided
definition. The mysterious byte is set to true (safe default) and the GUID is empty.
§Arguments
definition- Schema definition specifying column types and structuredefinition_patch- Optional patches to modify the definitiontable_name- Name of the table (for internal tracking)
§Returns
A new empty DB table ready to have rows added.
§Example
use rpfm_lib::files::{db::DB, table::Table};
use rpfm_lib::schema::Definition;
let db = DB::new(&definition, None, "units_tables");
assert_eq!(db.table().len(), 0);Sourcepub fn read_header<R: ReadBytes>(
data: &mut R,
) -> Result<(i32, bool, String, u32)>
pub fn read_header<R: ReadBytes>( data: &mut R, ) -> Result<(i32, bool, String, u32)>
Decodes the header section of a DB table.
Reads the header bytes to extract metadata without decoding the full table data. This is useful for inspecting table properties before committing to a full decode.
§Header Format
The header contains optional and required fields:
- Optional GUID Marker (
0xFD 0xFE 0xFC 0xFF) + 2-byte sized UTF-16 string - Optional Version Marker (
0xFC 0xFD 0xFE 0xFF) + 4-byte signed integer - Mysterious Byte (1 byte boolean)
- Entry Count (4-byte unsigned integer)
§Arguments
data- Reader positioned at the start of the DB table
§Returns
A tuple containing:
version- Table version number (0 if no version marker present)mysterious_byte- Unknown boolean flagguid- Table GUID (empty string if no GUID marker present)entry_count- Number of rows in the table
§Errors
Returns RLibError::DecodingDBNotADBTable if:
- The data is less than 5 bytes (minimum valid header size)
- The data doesn’t conform to the DB header format
§Side Effects
After reading, the reader is positioned at the start of the table data section.
Sourcepub fn definition(&self) -> &Definition
pub fn definition(&self) -> &Definition
Returns the schema definition for this DB table.
Sourcepub fn patches(&self) -> &DefinitionPatch
pub fn patches(&self) -> &DefinitionPatch
Returns the definition patches applied to this DB table.
Sourcepub fn table_name(&self) -> &str
pub fn table_name(&self) -> &str
Returns the table name (e.g., "units_tables").
Sourcepub fn table_name_without_tables(&self) -> String
pub fn table_name_without_tables(&self) -> String
Returns the table name without the "_tables" suffix.
§Panics
Panics if the table name doesn’t end with "_tables".
Sourcepub fn data(&self) -> Cow<'_, [Vec<DecodedData>]>
pub fn data(&self) -> Cow<'_, [Vec<DecodedData>]>
Returns the table rows as a slice of decoded data.
Sourcepub fn data_mut(&mut self) -> &mut Vec<Vec<DecodedData>>
pub fn data_mut(&mut self) -> &mut Vec<Vec<DecodedData>>
Returns a mutable reference to the table rows.
Ensure modifications maintain valid structure matching the definition.
Sourcepub fn set_data(&mut self, data: &[Vec<DecodedData>]) -> Result<()>
pub fn set_data(&mut self, data: &[Vec<DecodedData>]) -> Result<()>
Replaces all table data with the provided rows.
§Errors
Returns an error if rows don’t match the table definition structure.
Sourcepub fn new_row(&self) -> Vec<DecodedData>
pub fn new_row(&self) -> Vec<DecodedData>
Creates a new row with default values from the table definition.
Sourcepub fn column_position_by_name(&self, column_name: &str) -> Option<usize>
pub fn column_position_by_name(&self, column_name: &str) -> Option<usize>
Returns the column index for a given column name, or None if not found.
Sourcepub fn set_definition(&mut self, new_definition: &Definition)
pub fn set_definition(&mut self, new_definition: &Definition)
Replaces the table definition and migrates existing data to match.
Use this to update tables to a newer schema version. Data is converted between compatible types where possible.
Sourcepub fn update(&mut self, new_definition: &Definition)
pub fn update(&mut self, new_definition: &Definition)
Alias for set_definition.
Sourcepub fn cascade_edition(
pack: &mut Pack,
schema: &Schema,
table_name: &str,
field: &Field,
definition: &Definition,
value_before: &str,
value_after: &str,
) -> Vec<ContainerPath>
pub fn cascade_edition( pack: &mut Pack, schema: &Schema, table_name: &str, field: &Field, definition: &Definition, value_before: &str, value_after: &str, ) -> Vec<ContainerPath>
Performs a cascade update of a value across all referencing tables in a Pack.
When a key field value is changed, this function finds all tables that reference that field and updates them accordingly. It also updates corresponding Loc entries if the edited field affects localisation keys.
§Arguments
pack- The Pack to search and update.schema- Schema containing table definitions and reference information.table_name- Name of the source table (e.g.,"units_tables").field- The field being edited.definition- Definition of the source table.value_before- Original value being replaced.value_after- New value to set.
§Returns
List of paths where references were found and updated.
Sourcepub fn merge(sources: &[&Self]) -> Result<Self>
pub fn merge(sources: &[&Self]) -> Result<Self>
Merges multiple DB tables into a single new table.
Combines all rows from the source tables. The first table’s definition and patches are used for the merged result. All source tables are converted to match this definition before merging.
§Errors
Returns an error if:
- Tables have different names (can’t merge
units_tableswithbuildings_tables) - Fewer than 2 tables are provided
Sourcepub fn tsv_import(
records: StringRecordsIter<'_, File>,
field_order: &HashMap<u32, String>,
schema: &Schema,
table_name: &str,
table_version: i32,
) -> Result<Self>
pub fn tsv_import( records: StringRecordsIter<'_, File>, field_order: &HashMap<u32, String>, schema: &Schema, table_name: &str, table_version: i32, ) -> Result<Self>
Imports a DB table from TSV (tab-separated values) format.
§Arguments
records- CSV reader iterator over TSV records.field_order- Mapping of column positions to field names.schema- Schema containing the table definition.table_name- Name of the table (e.g.,"units_tables").table_version- Version of the table definition to use.
§Errors
Returns an error if no matching definition is found in the schema.
Sourcepub fn tsv_export(
&self,
writer: &mut Writer<File>,
table_path: &str,
keys_first: bool,
) -> Result<()>
pub fn tsv_export( &self, writer: &mut Writer<File>, table_path: &str, keys_first: bool, ) -> Result<()>
Exports the DB table to TSV (tab-separated values) format.
§Arguments
writer- CSV writer for the output file.table_path- Path used in the TSV metadata header.keys_first- Iftrue, key columns are written before non-key columns.
Sourcepub fn altered(&self) -> bool
pub fn altered(&self) -> bool
Returns true if data was modified during decoding (e.g., invalid values corrected).
Sourcepub fn generate_twad_key_deletes_keys(&self, keys: &mut HashSet<String>)
pub fn generate_twad_key_deletes_keys(&self, keys: &mut HashSet<String>)
Generates combined primary keys to populate the twad_key_deletes table.
Different tables use different key concatenation rules. This function handles the table-specific key format for each known table type.
Trait Implementations§
Source§impl Decodeable for DB
impl Decodeable for DB
Source§impl<'de> Deserialize<'de> for DB
impl<'de> Deserialize<'de> for DB
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Encodeable for DB
impl Encodeable for DB
Source§fn encode<W: WriteBytes>(
&mut self,
buffer: &mut W,
extra_data: &Option<EncodeableExtraData<'_>>,
) -> Result<()>
fn encode<W: WriteBytes>( &mut self, buffer: &mut W, extra_data: &Option<EncodeableExtraData<'_>>, ) -> Result<()>
Source§impl From<TableInMemory> for DB
Implementation to create a DB from a Table.
impl From<TableInMemory> for DB
Implementation to create a DB from a Table.
Source§fn from(table: TableInMemory) -> Self
fn from(table: TableInMemory) -> Self
impl StructuralPartialEq for DB
Auto Trait Implementations§
impl Freeze for DB
impl RefUnwindSafe for DB
impl Send for DB
impl Sync for DB
impl Unpin for DB
impl UnsafeUnpin for DB
impl UnwindSafe for DB
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.