Skip to main content

rpfm_lib/
lib.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Core library for reading and writing Total War game files.
12//!
13//! This crate provides comprehensive support for reading, writing, and manipulating file formats
14//! used by Creative Assembly in Total War games since Empire: Total War. It forms the foundation
15//! of the Rusted PackFile Manager (RPFM) project.
16//!
17//! # Supported Games
18//!
19//! - Total War: Shogun 2 Complete Edition
20//! - Total War: Pharaoh - Dynasties
21//! - Total War: Pharaoh
22//! - Total War: Warhammer 3
23//! - Total War Saga: Troy
24//! - Total War: Three Kingdoms
25//! - Total War: Warhammer 2
26//! - Total War: Warhammer
27//! - Total War Saga: Thrones of Britannia
28//! - Total War: Attila
29//! - Total War: Rome 2
30//! - Total War: Shogun 2
31//! - Total War: Napoleon
32//! - Total War: Empire
33//!
34//! # Supported File Formats
35//!
36//! The library supports 30+ file types including:
37//!
38//! - **Pack Files** (`.pack`): Container format for all game assets
39//! - **Database Tables** (`db/`): Game data with versioned schemas
40//! - **Localisation Files** (`.loc`): Translated text strings
41//! - **3D Models** (`.rigid_model_v2`): Unit and building meshes
42//! - **Animations**: AnimPack, AnimFragment, AnimsTable, MatchedCombat
43//! - **Audio**: Sound banks (`.bnk`), sound events, DAT containers
44//! - **Images**: DDS textures, atlases
45//! - **Maps**: BMD (battle map data), tile databases, vegetation
46//! - **Campaign**: ESF (save files), startpos
47//! - **UI**: UIC components, portrait settings, fonts
48//! - **Scripts**: Lua, XML, and other text formats
49//!
50//! See the [`files`] module for detailed information on each file type and its support level.
51//!
52//! # Architecture
53//!
54//! The library is organized into several key modules:
55//!
56//! - [`files`]: File format parsers and writers (30+ types)
57//! - [`schema`]: Database table schema definitions and versioning
58//! - [`binary`]: Low-level binary I/O utilities
59//! - [`games`]: Game-specific configuration and version detection
60//! - [`compression`]: LZ4, ZStd, and LZMA compression support
61//! - [`encryption`]: Pack file encryption/decryption
62//! - [`error`]: Error types and result handling
63//! - [`integrations`]: Optional Assembly Kit, Git and GitHub integration
64//!
65//! # Feature Flags
66//!
67//! - `integration_assembly_kit`: Enable Assembly Kit raw table parsing
68//! - `integration_git`: Enable Git repository operations
69//! - `all_formats` (default): Enable every `format_*` feature below
70//! - `format_<module>`: Enable decoding and encoding of the format in [`files`]`::<module>`. One per format:
71//!   `anim`, `animpack` (enables `anims_table`, `anim_fragment_battle` and `matched_combat`), `anims_table`,
72//!   `anim_fragment_battle`, `matched_combat`, `group_formations`, `sound_bank`, `sound_bank_database`,
73//!   `sound_events`, `audio`, `uic`, `portrait_settings`, `font`, `atlas`, `dat`, `trade_routes`,
74//!   `unit_variant`, `hlsl_compiled` and `image` (pulls in the `image` and `dds` crates).
75//!   With a format disabled its module doesn't exist, but its files are still detected by
76//!   [`FileType`](files::FileType), and decoded as [`Unknown`](files::unknown::Unknown) raw data, so they're saved intact.
77//!
78//! # Examples
79//!
80//! ## Reading a Pack File
81//!
82//! ```ignore
83//! use rpfm_lib::files::pack::Pack;
84//! use rpfm_lib::games::supported_games::{SupportedGames, KEY_WARHAMMER_3};
85//! use std::path::PathBuf;
86//!
87//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
88//! let path = PathBuf::from("path/to/pack.pack");
89//! let games = SupportedGames::default();
90//! let game_info = games.game(&KEY_WARHAMMER_3).unwrap();
91//! let pack = Pack::read_and_merge(&[path], game_info, true, false, false)?;
92//!
93//! for file in pack.files().values() {
94//!     println!("{}", file.path_in_container_raw());
95//! }
96//! # Ok(())
97//! # }
98//! ```
99//!
100//! ## Working with Database Tables
101//!
102//! ```ignore
103//! use rpfm_lib::files::{db::DB, Decodeable, DecodeableExtraData, RFile, RFileDecoded};
104//! use rpfm_lib::schema::Schema;
105//! use std::path::Path;
106//!
107//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
108//! let schema_path = Path::new("path/to/schema.ron");
109//! let schema = Schema::load(&schema_path, None)?;
110//! let mut extra_data = DecodeableExtraData::default();
111//! extra_data.set_schema(Some(&schema));
112//!
113//! // Decode a DB file (assuming pack is loaded)
114//! // let mut file = pack.files_by_path(&path, false).first().unwrap();
115//! // file.decode(&Some(extra_data), false, true)?;
116//! //
117//! // if let Ok(RFileDecoded::DB(db)) = file.decoded() {
118//! //     println!("Table {} has {} rows", db.table_name(), db.data().len());
119//! // }
120//! # Ok(())
121//! # }
122//! ```
123//!
124//! # Related Crates
125//!
126//! - `rpfm_extensions`: Higher-level features (dependencies, diagnostics, search, optimizer)
127//! - `rpfm_ui`: Qt-based desktop application
128
129use regex::Regex;
130
131use std::sync::LazyLock;
132
133pub mod binary;
134pub mod compression;
135pub mod encryption;
136pub mod error;
137pub mod files;
138pub mod games;
139pub mod integrations;
140pub mod notes;
141pub mod schema;
142pub mod utils;
143mod xml;
144
145#[cfg(test)] mod utils_test;
146
147/// Regular expression to identify database table file paths.
148///
149/// Matches paths in the format: `db/{table_name}_tables/{file}`
150///
151/// # Examples
152///
153/// - `db/units_tables/core_units.bin` → matches
154/// - `db/buildings_tables/data.bin` → matches
155/// - `data/units.txt` → does not match
156pub static REGEX_DB: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"db/[^/]+_tables/[^/]+$").unwrap());
157
158/// Regular expression to identify CEO database table file paths. These do not use the normal db prefix
159/// due to crashes when loading them in mods ingame.
160///
161/// Matches paths in the format: `ceo_db/{table_name}_tables/{file}`
162///
163/// # Examples
164///
165/// - `ceo_db/units_tables/core_units.bin` → matches
166/// - `ceo_db/buildings_tables/data.bin` → matches
167/// - `data/units.txt` → does not match
168pub static REGEX_CEO_DB: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"ceo_db/[^/]+_tables/[^/]+$").unwrap());
169
170/// Regular expression to identify portrait settings file paths.
171///
172/// Matches any path ending with `portrait_settings` followed by optional characters and `.bin`.
173///
174/// # Examples
175///
176/// - `portraits/portrait_settings.bin` → matches
177/// - `data/portrait_settings_v2.bin` → matches
178/// - `other/settings.bin` → does not match
179pub static REGEX_PORTRAIT_SETTINGS: LazyLock<Regex> = LazyLock::new(|| Regex::new(r".*portrait_settings\S*\.bin$").unwrap());