pub struct Schema { /* private fields */ }Expand description
Represents a complete schema file containing table definitions for a Total War game.
A Schema stores the structural definitions for all database tables in a Total War game.
Each table can have multiple Definition versions, allowing the schema to support
different versions of the same table across game patches.
§Structure
version: The structural version of the schema format itself (currently 5)definitions: A map of table names to their version historypatches: Runtime modifications to field properties
§Usage
use rpfm_lib::schema::Schema;
use std::path::Path;
let schema_path = Path::new("schemas/warhammer_3.ron");
let schema = Schema::load(schema_path, None)?;
// Get definitions for a specific table
if let Some(definitions) = schema.definitions_by_table_name("units_tables") {
println!("Found {} versions of units_tables", definitions.len());
}Implementations§
Source§impl Schema
impl Schema
Sourcepub fn version(&self) -> &u16
pub fn version(&self) -> &u16
The structural version of the schema format.
This is incremented when the schema format itself changes in backwards-incompatible ways.
Sourcepub fn definitions(&self) -> &HashMap<String, Vec<Definition>>
pub fn definitions(&self) -> &HashMap<String, Vec<Definition>>
Map of table names to their version definitions.
Each table can have multiple versions, stored as a Vec of Definition instances.
Serialization orders this map alphabetically for consistent output.
Sourcepub fn patches(&self) -> &HashMap<String, DefinitionPatch>
pub fn patches(&self) -> &HashMap<String, DefinitionPatch>
Map of table names to their patches.
Patches allow runtime modification of field properties without changing the base schema.
See DefinitionPatch for the patch structure.
Source§impl Schema
impl Schema
Sourcepub fn version_mut(&mut self) -> &mut u16
pub fn version_mut(&mut self) -> &mut u16
The structural version of the schema format.
This is incremented when the schema format itself changes in backwards-incompatible ways.
Sourcepub fn definitions_mut(&mut self) -> &mut HashMap<String, Vec<Definition>>
pub fn definitions_mut(&mut self) -> &mut HashMap<String, Vec<Definition>>
Map of table names to their version definitions.
Each table can have multiple versions, stored as a Vec of Definition instances.
Serialization orders this map alphabetically for consistent output.
Sourcepub fn patches_mut(&mut self) -> &mut HashMap<String, DefinitionPatch>
pub fn patches_mut(&mut self) -> &mut HashMap<String, DefinitionPatch>
Map of table names to their patches.
Patches allow runtime modification of field properties without changing the base schema.
See DefinitionPatch for the patch structure.
Source§impl Schema
impl Schema
Sourcepub fn set_version(&mut self, val: u16) -> &mut Self
pub fn set_version(&mut self, val: u16) -> &mut Self
The structural version of the schema format.
This is incremented when the schema format itself changes in backwards-incompatible ways.
Sourcepub fn set_definitions(
&mut self,
val: HashMap<String, Vec<Definition>>,
) -> &mut Self
pub fn set_definitions( &mut self, val: HashMap<String, Vec<Definition>>, ) -> &mut Self
Map of table names to their version definitions.
Each table can have multiple versions, stored as a Vec of Definition instances.
Serialization orders this map alphabetically for consistent output.
Sourcepub fn set_patches(
&mut self,
val: HashMap<String, DefinitionPatch>,
) -> &mut Self
pub fn set_patches( &mut self, val: HashMap<String, DefinitionPatch>, ) -> &mut Self
Map of table names to their patches.
Patches allow runtime modification of field properties without changing the base schema.
See DefinitionPatch for the patch structure.
Source§impl Schema
Implementation of Schema.
impl Schema
Implementation of Schema.
Sourcepub fn save_patches(
patches: &HashMap<String, DefinitionPatch>,
path: &Path,
) -> Result<()>
pub fn save_patches( patches: &HashMap<String, DefinitionPatch>, path: &Path, ) -> Result<()>
Saves patches to a local patches file, merging with existing patches.
This function loads existing patches from the file, merges the provided patches with them, and writes the combined patch set back to the file in RON format.
§Arguments
patches- The patches to add or updatepath- Path to the local patches file (must exist)
§Returns
Returns Ok if successful, or an error if:
- The file cannot be read or written
- The file contains invalid patch data
§Example
use std::collections::HashMap;
use std::path::Path;
use rpfm_lib::schema::{Schema, DefinitionPatch};
let mut patches: HashMap<String, DefinitionPatch> = HashMap::new();
// Add patches...
Schema::save_patches(&patches, Path::new("my_patches.ron"))?;Sourcepub fn remove_patches_for_table(table_name: &str, path: &Path) -> Result<()>
pub fn remove_patches_for_table(table_name: &str, path: &Path) -> Result<()>
Removes all local patches for a specific table.
This function loads the patches file, removes all patches for the specified table, and writes the updated patch set back to the file.
§Arguments
table_name- Name of the table to remove patches forpath- Path to the local patches file
§Returns
Returns Ok if successful, even if no there were no patches to remove, or an error
if file I/O fails.
Sourcepub fn remove_patches_for_table_and_field(
table_name: &str,
field_name: &str,
path: &Path,
) -> Result<()>
pub fn remove_patches_for_table_and_field( table_name: &str, field_name: &str, path: &Path, ) -> Result<()>
Removes all local patches for a specific field in a table.
This function loads the patches file, removes all patches for the specified table’s field, and writes the updated patch set back to the file. Other fields in the table are unaffected.
§Arguments
table_name- Name of the table containing the fieldfield_name- Name of the field to remove patches forpath- Path to the local patches file
§Returns
Returns Ok if successful, even if no there were no patches to remove, or an error
if file I/O fails.
Sourcepub fn patch_value(
&self,
table_name: &str,
column_name: &str,
key: &str,
) -> Option<&String>
pub fn patch_value( &self, table_name: &str, column_name: &str, key: &str, ) -> Option<&String>
Sourcepub fn patches_for_table(&self, table_name: &str) -> Option<&DefinitionPatch>
pub fn patches_for_table(&self, table_name: &str) -> Option<&DefinitionPatch>
Sourcepub fn add_patches_to_patch_set(
patch_set: &mut HashMap<String, DefinitionPatch>,
patches: &HashMap<String, DefinitionPatch>,
)
pub fn add_patches_to_patch_set( patch_set: &mut HashMap<String, DefinitionPatch>, patches: &HashMap<String, DefinitionPatch>, )
Merges patches into an existing patch set.
This function adds the provided patches to the patch set, merging them with any existing patches. If a patch already exists for a table/column/key combination, it will be extended with the new values.
§Arguments
patch_set- The patch set to merge into (modified in place)patches- The patches to add
§Note
After adding patches, you must re-retrieve any definitions you’ve already retrieved for the patches to take effect, as patches are applied when retrieving definitions.
Sourcepub fn add_definition(&mut self, table_name: &str, definition: &Definition)
pub fn add_definition(&mut self, table_name: &str, definition: &Definition)
Adds or updates a table definition in the schema.
If a definition with the same version already exists for this table, it will be replaced. Otherwise, the definition is added to the table’s version list.
§Arguments
table_name- Name of the tabledefinition- The definition to add or update
Sourcepub fn remove_definition(&mut self, table_name: &str, version: i32)
pub fn remove_definition(&mut self, table_name: &str, version: i32)
Removes a specific table definition version from the schema.
§Arguments
table_name- Name of the tableversion- Version number of the definition to remove
Sourcepub fn definitions_by_table_name_cloned(
&self,
table_name: &str,
) -> Option<Vec<Definition>>
pub fn definitions_by_table_name_cloned( &self, table_name: &str, ) -> Option<Vec<Definition>>
Sourcepub fn definitions_by_table_name(
&self,
table_name: &str,
) -> Option<&Vec<Definition>>
pub fn definitions_by_table_name( &self, table_name: &str, ) -> Option<&Vec<Definition>>
Sourcepub fn definitions_by_table_name_mut(
&mut self,
table_name: &str,
) -> Option<&mut Vec<Definition>>
pub fn definitions_by_table_name_mut( &mut self, table_name: &str, ) -> Option<&mut Vec<Definition>>
Sourcepub fn definition_newer(
&self,
table_name: &str,
candidates: &[Definition],
) -> Option<&Definition>
pub fn definition_newer( &self, table_name: &str, candidates: &[Definition], ) -> Option<&Definition>
Returns the newest compatible definition for a table based on candidate versions.
This function first tries to find a definition matching the highest version number from the candidates (typically from a dependency database). If that fails, it falls back to the first (newest) definition in the schema.
§Arguments
table_name- Name of the tablecandidates- List of candidate definitions (typically from dependencies)
§Returns
Returns the best matching definition, or None if the table is not found.
Sourcepub fn definition_by_name_and_version(
&self,
table_name: &str,
table_version: i32,
) -> Option<&Definition>
pub fn definition_by_name_and_version( &self, table_name: &str, table_version: i32, ) -> Option<&Definition>
Sourcepub fn definition_by_name_and_version_mut(
&mut self,
table_name: &str,
table_version: i32,
) -> Option<&mut Definition>
pub fn definition_by_name_and_version_mut( &mut self, table_name: &str, table_version: i32, ) -> Option<&mut Definition>
Sourcepub fn load(path: &Path, local_patches: Option<&Path>) -> Result<Self>
pub fn load(path: &Path, local_patches: Option<&Path>) -> Result<Self>
Loads a Schema from a RON file, optionally merging local patches.
This function loads a schema from a .ron file and applies any patches from both
the schema itself and an optional local patches file. Patches from the local file
are merged with schema patches and applied to all definitions.
§Arguments
path- Path to the schema.ronfilelocal_patches- Optional path to a local patches file
§Returns
Returns the loaded schema with all patches applied, or an error if loading fails.
Sourcepub fn save(&mut self, path: &Path) -> Result<()>
pub fn save(&mut self, path: &Path) -> Result<()>
Saves the schema to a RON file.
This function saves the schema to a .ron file, automatically:
- Creating parent directories if needed
- Sorting definitions by version (newest first)
- Cleaning up invalid references
- Moving certain patches from definitions to schema patches
§Arguments
path- Path where the schema file should be saved
§Returns
Returns Ok if saved successfully, or an error if file I/O fails.
Sourcepub fn save_json(&mut self, path: &Path) -> Result<()>
pub fn save_json(&mut self, path: &Path) -> Result<()>
Saves the schema to a JSON file.
This function saves the schema to a .json file at the specified path, automatically:
- Creating parent directories if needed
- Changing the extension to
.json - Sorting definitions by version (newest first)
- Pretty-printing the JSON output
§Arguments
path- Path where the schema file should be saved (extension will be changed to.json)
§Returns
Returns Ok if saved successfully, or an error if file I/O or serialization fails.
Sourcepub fn export_to_json(schema_folder_path: &Path) -> Result<()>
pub fn export_to_json(schema_folder_path: &Path) -> Result<()>
Exports all schema files in a folder to JSON format.
This function loads all schema files (.ron) for supported games from the specified folder
and saves them as .json files in the same location. This is primarily used for
compatibility with external tools that prefer JSON.
§Arguments
schema_folder_path- Path to the folder containing schema.ronfiles
§Returns
Returns Ok if all schemas are successfully exported, or an error if any operation fails.
§Note
This function processes schemas in parallel for better performance.
Sourcepub fn update(
schema_path: &Path,
schema_patches_path: &Path,
game_name: &str,
) -> Result<()>
pub fn update( schema_path: &Path, schema_patches_path: &Path, game_name: &str, ) -> Result<()>
Updates a schema from a legacy format to the current format.
This function handles migration of schema files from older structural versions (e.g., v4) to the current structural version (v5). It automatically detects the schema version and applies the necessary transformations.
§Arguments
schema_path- Path to the schema file to updateschema_patches_path- Path to the schema patches filegame_name- Name of the game this schema is for
§Returns
Returns Ok if the update succeeds, or an error if the update process fails.
Sourcepub fn referencing_columns_for_table(
&self,
table_name: &str,
definition: &Definition,
) -> HashMap<String, HashMap<String, Vec<String>>>
pub fn referencing_columns_for_table( &self, table_name: &str, definition: &Definition, ) -> HashMap<String, HashMap<String, Vec<String>>>
Returns all columns that reference fields in the specified table.
This function searches through all table definitions in the schema to find fields that have foreign key references pointing to the provided table’s fields.
§Arguments
table_name- Name of the table to find references todefinition- Definition of the table (used to get the field list)
§Returns
Returns a map where:
- Keys are local field names from the provided definition
- Values are maps of
table_name -> Vec<field_name>containing all referencing fields
§Example
For a factions_tables table, this might return:
{
"key": {
"units_tables": ["faction_key"],
"characters_tables": ["faction_key", "home_faction_key"]
}
}Sourcepub fn tables_and_columns_referencing_our_own(
&self,
table_name: &str,
column_name: &str,
fields: &[Field],
localised_fields: &[Field],
) -> (BTreeMap<String, Vec<String>>, bool)
pub fn tables_and_columns_referencing_our_own( &self, table_name: &str, column_name: &str, fields: &[Field], localised_fields: &[Field], ) -> (BTreeMap<String, Vec<String>>, bool)
Returns all tables and columns that reference the specified column, and whether LOC files may be affected.
This function performs a recursive search to find all fields that reference the specified column, including indirect references (fields that reference fields that reference the target column). It also checks if changing the column would affect localisation keys.
§Arguments
table_name- Name of the table containing the column (with or without_tablessuffix)column_name- Name of the column to find references tofields- The table’s field listlocalised_fields- The table’s localised field list
§Returns
Returns a tuple of:
- A map of
table_name -> Vec<field_name>containing all referencing fields (recursively) - A boolean indicating if LOC files may need updates (true if the column is a key field and the table has localised fields)
§Note
Recursion is supported for table references, but not for LOC field detection.
Sourcepub fn load_patches_from_str(
patch: &str,
) -> Result<HashMap<String, DefinitionPatch>>
pub fn load_patches_from_str( patch: &str, ) -> Result<HashMap<String, DefinitionPatch>>
Sourcepub fn load_definitions_from_str(
definition: &str,
) -> Result<HashMap<String, Definition>>
pub fn load_definitions_from_str( definition: &str, ) -> Result<HashMap<String, Definition>>
Sourcepub fn export_patches_to_str(
patches: &HashMap<String, DefinitionPatch>,
) -> Result<String>
pub fn export_patches_to_str( patches: &HashMap<String, DefinitionPatch>, ) -> Result<String>
Sourcepub fn export_definitions_to_str(
definitions: &HashMap<String, Definition>,
) -> Result<String>
pub fn export_definitions_to_str( definitions: &HashMap<String, Definition>, ) -> Result<String>
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Schema
impl<'de> Deserialize<'de> for Schema
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>,
impl Eq for Schema
impl StructuralPartialEq for Schema
Auto Trait Implementations§
impl Freeze for Schema
impl RefUnwindSafe for Schema
impl Send for Schema
impl Sync for Schema
impl Unpin for Schema
impl UnsafeUnpin for Schema
impl UnwindSafe for Schema
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
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().§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.