Skip to main content

TableInMemory

Struct TableInMemory 

Source
pub struct TableInMemory { /* private fields */ }
Expand description

In-memory representation of a decoded table with full data and schema.

This is the primary table implementation in RPFM, storing all table rows in memory along with their schema definition. Tables are typically accessed through the Table trait interface rather than directly.

§Fields

  • table_name: Identifies the table type (e.g., “units_tables”, “buildings_tables”)
  • definition: Complete schema definition including column types and constraints
  • definition_patch: Runtime modifications to the base schema for this specific table
  • table_data: All table rows as a Vec<Vec<DecodedData>> (outer vector is rows, inner is columns)
  • altered: Flag indicating if data was modified during decoding (e.g., invalid values corrected)

§Accessors

The struct uses the getset macro for automatic accessor generation:

  • table_name() / set_table_name(): Public getters/setters via getset
  • Schema and data: Accessed through Table trait methods for type safety

§Thread Safety

This struct implements Send + Sync (via the Table trait requirement), allowing safe concurrent read access and message passing between threads.

Implementations§

Source§

impl TableInMemory

Source

pub fn table_name(&self) -> &String

Table type identifier (e.g., “units_tables”).

Source

pub fn altered(&self) -> &bool

Flag indicating data was altered during decoding (e.g., invalid values corrected).

Source§

impl TableInMemory

Source

pub fn set_table_name(&mut self, val: String) -> &mut Self

Table type identifier (e.g., “units_tables”).

Source

pub fn set_altered(&mut self, val: bool) -> &mut Self

Flag indicating data was altered during decoding (e.g., invalid values corrected).

Source§

impl TableInMemory

Source

pub fn new( definition: &Definition, definition_patch: Option<&DefinitionPatch>, table_name: &str, ) -> Self

Creates a new empty table from a schema definition.

Initializes a table with no rows but with a complete schema definition. This is typically used when creating new tables from scratch or before importing data from external sources.

§Parameters
  • definition: Schema defining column structure, types, and constraints
  • definition_patch: Optional runtime modifications to the base schema
  • table_name: Table type identifier (e.g., “units_tables”)
§Examples
// Create empty table for manual data entry
let mut table = TableInMemory::new(definition, None, "units_tables");

// Add rows using the Table trait methods
let new_row = table.new_row();
table.data_mut().push(new_row);
Source

pub fn decode<R: ReadBytes>( data: &mut R, definition: &Definition, definition_patch: &DefinitionPatch, entry_count: Option<u32>, return_incomplete: bool, table_name: &str, ) -> Result<Self>

Decodes a table from binary data using the provided schema.

This is the primary method for loading tables from PackFiles. It reads the binary format used by Total War games and converts it into an in-memory representation.

§Parameters
  • data: Binary data reader positioned at the table start
  • definition: Schema definition for interpreting the binary data
  • definition_patch: Runtime schema modifications
  • entry_count: Optional row count (if None, reads from data stream)
  • return_incomplete: If true, returns partial data on decode errors instead of failing
  • table_name: Table type identifier
§Behavior
  • Reads entry count from stream if not provided
  • Decodes each row according to schema field definitions
  • Sets altered flag if invalid data is corrected during decoding
  • Can return incomplete tables for error recovery if requested
§Errors

Returns an error if:

  • Binary data is corrupted or truncated
  • Data types don’t match schema expectations
  • Field decoding fails (unless return_incomplete is true)
§Examples
// Decode table from binary PackFile data
let table = TableInMemory::decode(
    data,
    definition,
    &HashMap::new(),  // No patches
    Some(100),        // 100 entries
    false,            // Fail on errors
    "units_tables"
)?;
Source

pub fn encode<W: WriteBytes>(&self, data: &mut W) -> Result<()>

Encodes the table to binary format for writing to PackFiles.

Converts the in-memory table representation back to the binary format used by Total War games. This is the inverse of decode.

§Parameters
  • data: Binary writer to receive the encoded table
§Format

The binary output includes:

  • Entry count (u32)
  • Row data encoded according to field types
  • Applied schema patches are used during encoding
§Errors

Returns an error if:

  • Writing to the output stream fails
  • Data contains values that cannot be encoded in the target type
§Examples
// Encode table for saving to PackFile
table.encode(output)?;

Trait Implementations§

Source§

impl Clone for TableInMemory

Source§

fn clone(&self) -> TableInMemory

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TableInMemory

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for TableInMemory

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Atlas> for TableInMemory

Source§

fn from(value: Atlas) -> Self

Converts to this type from the input type.
Source§

impl From<TableInMemory> for Atlas

Source§

fn from(value: TableInMemory) -> Self

Converts to this type from the input type.
Source§

impl From<TableInMemory> for DB

Implementation to create a DB from a Table.

Source§

fn from(table: TableInMemory) -> Self

Converts to this type from the input type.
Source§

impl From<TableInMemory> for Loc

Implementation to create a Loc from a Table directly.

Source§

fn from(table: TableInMemory) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for TableInMemory

Source§

fn eq(&self, other: &TableInMemory) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TableInMemory

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Table for TableInMemory

Source§

fn name(&self) -> &str

Returns the table name (e.g., “units_tables”, “factions_tables”).
Source§

fn definition(&self) -> &Definition

Returns the table’s schema definition. Read more
Source§

fn patches(&self) -> &DefinitionPatch

Returns definition patches applied to this table. Read more
Source§

fn data(&self) -> Cow<'_, [Vec<DecodedData>]>

Returns the table’s row data. Read more
Source§

fn data_mut(&mut self) -> &mut Vec<Vec<DecodedData>>

Returns a mutable reference to the table’s row data. Read more
Source§

fn set_name(&mut self, val: String)

Sets the table name.
Source§

fn set_definition(&mut self, new_definition: &Definition)

Replaces the table’s definition and migrates data to match the new schema. Read more
Source§

fn set_data(&mut self, data: &[Vec<DecodedData>]) -> Result<()>

Replaces the table’s data with the provided rows. Read more
Source§

fn column_position_by_name(&self, column_name: &str) -> Option<usize>

Returns the column index for a given column name. Read more
Source§

fn is_empty(&self) -> bool

Returns true if the table contains no rows.
Source§

fn len(&self) -> usize

Returns the number of rows in the table.
Source§

fn rows_containing_data( &self, column_name: &str, data: &str, ) -> Option<(usize, Vec<usize>)>

This function tries to find all rows with the provided data, if they exists in this table.
Source§

fn new_row(&self) -> Vec<DecodedData>

Creates a new empty row with default values for all columns. Read more
Source§

impl StructuralPartialEq for TableInMemory

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,