rpfm_cli 4.5.4

A CLI tool to automate all your Total War modding needs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//---------------------------------------------------------------------------//
// Copyright (c) 2017-2024 Ismael Gutiérrez González. All rights reserved.
//
// This file is part of the Rusted PackFile Manager (RPFM) project,
// which can be found here: https://github.com/Frodo45127/rpfm.
//
// This file is licensed under the MIT license, which can be found here:
// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
//---------------------------------------------------------------------------//

//! This module contains the `Pack` command functions.

use anyhow::{anyhow, Result};
use rayon::prelude::*;

use std::collections::BTreeMap;
use std::io::{BufReader, BufWriter};
use std::fs::File;
use std::path::{Path, PathBuf};

use rpfm_extensions::dependencies::Dependencies;
use rpfm_extensions::diagnostics::Diagnostics;

use rpfm_lib::binary::ReadBytes;
use rpfm_lib::files::{ContainerPath, Container, Decodeable, DecodeableExtraData, Encodeable, EncodeableExtraData, FileType, pack::Pack};
use rpfm_lib::games::{pfh_file_type::PFHFileType, supported_games::*};
use rpfm_lib::integrations::log::*;
use rpfm_lib::schema::Schema;
use rpfm_lib::utils::last_modified_time_from_file;

use crate::config::Config;

//---------------------------------------------------------------------------//
// 							Pack Command Variants
//---------------------------------------------------------------------------//

/// This function changes the Pack Type of the provided Pack.
pub fn set_pack_type(config: &Config, pack_path: &Path, pfh_file_type: PFHFileType) -> Result<()> {
    if config.verbose {
        info!("Changing PFH Type for Pack at {}.", pack_path.to_string_lossy());
    }

    let pack_path_str = pack_path.to_string_lossy().to_string();
    let mut reader = BufReader::new(File::open(pack_path)?);
    let mut extra_data = DecodeableExtraData::default();

    extra_data.set_disk_file_path(Some(&pack_path_str));
    extra_data.set_timestamp(last_modified_time_from_file(reader.get_ref())?);
    extra_data.set_data_size(reader.len()?);
    extra_data.set_lazy_load(true);
    extra_data.set_game_info(config.game.as_ref());

    let mut pack = Pack::decode(&mut reader, &Some(extra_data))?;
    pack.preload()?;
    pack.set_pfh_file_type(pfh_file_type);

    let games = SupportedGames::default();
    let encodeable_extra_data = EncodeableExtraData::new_from_game_info(games.game(config.game.clone().unwrap().key()).unwrap());

    let mut writer = BufWriter::new(File::create(pack_path)?);
    pack.encode(&mut writer, &Some(encodeable_extra_data))?;

    if config.verbose {
        info!("Changed PFH Type to {pfh_file_type}.");
    }

    Ok(())
}

/// This function list the contents of the provided Pack.
pub fn list(config: &Config, path: &Path) -> Result<()> {

    if config.verbose {
		info!("Listing Pack Contents.");
	}

    let mut reader = BufReader::new(File::open(path)?);
    let path_str = path.to_str().unwrap();

    let mut extra_data = DecodeableExtraData::default();
    extra_data.set_disk_file_path(Some(path_str));
    extra_data.set_timestamp(last_modified_time_from_file(reader.get_ref())?);
    extra_data.set_data_size(reader.len()?);
    extra_data.set_lazy_load(true);
    extra_data.set_game_info(config.game.as_ref());

    let pack = Pack::decode(&mut reader, &Some(extra_data))?;
    let files: BTreeMap<_, _> = pack.files().iter().collect();
    for (path, _) in files {
        println!("{path}");
    }

	Ok(())
}

/// This function creates a new empty Pack with the provided path.
pub fn create(config: &Config, path: &Path) -> Result<()> {
    if config.verbose {
        info!("Creating new empty Mod Pack at {}.", path.to_string_lossy());
    }

    match &config.game {
        Some(game) => {
            let mut file = BufWriter::new(File::create(path)?);
            let mut pack = Pack::new_with_version(game.pfh_version_by_file_type(PFHFileType::Mod));

            let games = SupportedGames::default();
            let encodeable_extra_data = EncodeableExtraData::new_from_game_info(games.game(game.key()).unwrap());

            pack.encode(&mut file, &Some(encodeable_extra_data))?;
            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}

/// This function adds the provided files/folders to the provided Pack.
pub fn add(config: &Config, schema_path: &Option<PathBuf>, pack_path: &Path, file_path: &[(PathBuf, String)], folder_path: &[(PathBuf, String)]) -> Result<()> {
    if config.verbose {
        info!("Adding files/folders to a Pack at {}.", pack_path.to_string_lossy());
        info!("Tsv to Binary is: {}.", schema_path.is_some());
    }

    // Load the schema if we try to import tsv files.
    let schema = if let Some(schema_path) = schema_path {
        if schema_path.is_file() {
            Some(Schema::load(schema_path, None)?)
        } else {
            warn!("Schema path provided, but it doesn't point to a valid schema. Disabling `TSV to Binary`.");
            None
        }
    } else { None };

    let pack_path_str = pack_path.to_string_lossy().to_string();
    let mut reader = BufReader::new(File::open(pack_path)?);
    let mut extra_data = DecodeableExtraData::default();

    extra_data.set_disk_file_path(Some(&pack_path_str));
    extra_data.set_timestamp(last_modified_time_from_file(reader.get_ref())?);
    extra_data.set_data_size(reader.len()?);
    extra_data.set_lazy_load(true);
    extra_data.set_game_info(config.game.as_ref());

    let mut pack = Pack::decode(&mut reader, &Some(extra_data))?;

    for (folder_path, container_path) in folder_path {
        if config.verbose {
            info!("Adding folder: {container_path}");
        }

        pack.insert_folder(folder_path, container_path, &None, &schema, false)?;
    }

    for (file_path, container_path) in file_path {
        if config.verbose {
            info!("Adding file: {container_path}");
        }

        pack.insert_file(file_path, container_path, &schema)?;
    }

    pack.preload()?;

    let games = SupportedGames::default();
    let encodeable_extra_data = EncodeableExtraData::new_from_game_info(games.game(config.game.clone().unwrap().key()).unwrap());

    let mut writer = BufWriter::new(File::create(pack_path)?);
    pack.encode(&mut writer, &Some(encodeable_extra_data))?;

    if config.verbose {
        info!("Files/folders added.");
    }

    Ok(())
}

/// This function deletes the provided files/folders from the provided Pack.
pub fn delete(config: &Config, pack_path: &Path, file_path: &[String], folder_path: &[String]) -> Result<()> {
    if config.verbose {
        info!("Delete files/folders from a Pack at {}.", pack_path.to_string_lossy());
    }

    let pack_path_str = pack_path.to_string_lossy().to_string();
    let mut reader = BufReader::new(File::open(pack_path)?);
    let mut extra_data = DecodeableExtraData::default();

    extra_data.set_disk_file_path(Some(&pack_path_str));
    extra_data.set_timestamp(last_modified_time_from_file(reader.get_ref())?);
    extra_data.set_data_size(reader.len()?);
    extra_data.set_lazy_load(true);
    extra_data.set_game_info(config.game.as_ref());

    let mut pack = Pack::decode(&mut reader, &Some(extra_data))?;

    let mut container_paths = folder_path.iter().map(|x| ContainerPath::Folder(x.to_string())).collect::<Vec<_>>();
    container_paths.append(&mut file_path.iter().map(|x| ContainerPath::File(x.to_string())).collect::<Vec<_>>());
    let container_paths = ContainerPath::dedup(&container_paths);

    for container_path in container_paths {
        if config.verbose {
            info!("Deleting path: {}", container_path.path_raw());
        }

        pack.remove(&container_path);
    }

    pack.preload()?;

    let games = SupportedGames::default();
    let encodeable_extra_data = EncodeableExtraData::new_from_game_info(games.game(config.game.clone().unwrap().key()).unwrap());

    let mut writer = BufWriter::new(File::create(pack_path)?);
    pack.encode(&mut writer, &Some(encodeable_extra_data))?;

    if config.verbose {
        info!("Files/folders deleted.");
    }

    Ok(())
}

/// This function extracts the provided files/folders from the provided Pack, keeping their folder structure.
pub fn extract(config: &Config, schema_path: &Option<PathBuf>, pack_path: &Path, file_path: &[(String, PathBuf)], folder_path: &[(String, PathBuf)]) -> Result<()> {
    if config.verbose {
        info!("Extracting files/folders from a Pack at {}.", pack_path.to_string_lossy());
        info!("Tables as Tsv is: {}.", schema_path.is_some());
    }

    // Load the schema if we try to import tsv files.
    let schema = if let Some(schema_path) = schema_path {
        if schema_path.is_file() {
            Some(Schema::load(schema_path, None)?)
        } else {
            warn!("Schema path provided, but it doesn't point to a valid schema. Disabling `Table as TSV`.");
            None
        }
    } else { None };

    let pack_path_str = pack_path.to_string_lossy().to_string();
    let mut reader = BufReader::new(File::open(pack_path)?);
    let mut extra_data = DecodeableExtraData::default();

    extra_data.set_disk_file_path(Some(&pack_path_str));
    extra_data.set_timestamp(last_modified_time_from_file(reader.get_ref())?);
    extra_data.set_data_size(reader.len()?);
    extra_data.set_lazy_load(true);
    extra_data.set_game_info(config.game.as_ref());

    let mut pack = Pack::decode(&mut reader, &Some(extra_data))?;
    let mut extra_data = EncodeableExtraData::default();
    if let Some(game) = &config.game {
        extra_data = EncodeableExtraData::new_from_game_info(game);
    }

    let extra_data = Some(extra_data);

    for (container_path, folder_path) in folder_path {
        if config.verbose {
            info!("Extracting folder: {container_path}");
        }

        let container_path = ContainerPath::Folder(container_path.to_owned());
        pack.extract(container_path, folder_path, true, &schema, false, true, &extra_data, false)?;
    }

    for (container_path, file_path) in file_path {
        if config.verbose {
            info!("Extracting file: {container_path}");
        }

        let container_path = ContainerPath::File(container_path.to_owned());
        pack.extract(container_path, file_path, true, &schema, false, true, &extra_data, false)?;
    }

    if config.verbose {
        info!("Files/folders extracted.");
    }

    Ok(())
}


/// This function diagnose problems in the provided Packs.
pub fn diagnose(config: &Config, game_path: &Path, pak_path: &Path, schema_path: &Path, pack_paths: &[PathBuf]) -> Result<()> {
    if config.verbose {
        info!("Diagnosing problems in the following Packs:");
        for pack_path in pack_paths {
            info!(" - {}", pack_path.to_string_lossy());
        }
    }

    // Load both, the schema and the Packs to memory.
    let schema = Schema::load(schema_path, None)?;
    let mut pack = Pack::read_and_merge(pack_paths, config.game.as_ref().unwrap(), true, false, true)?;

    // Prepare the table's extra data,
    let mut extra_data = DecodeableExtraData::default();
    extra_data.set_schema(Some(&schema));
    let table_extra_data = Some(extra_data);

    // Decode the tables and locs from the packs, and return a list of the decoded tables for later.
    let tables = pack.files_by_type_mut(&[FileType::DB, FileType::Loc])
        .par_iter_mut()
        .filter_map(|file| {
            let _ = file.decode(&table_extra_data, true, false);
            if file.file_type() == FileType::DB {
                Some(file.path_in_container_split()[1].to_owned())
            } else {
                None
            }
        }).collect::<Vec<String>>();

    match &config.game {
        Some(game_info) => {

            // Build the dependencies cache for the game and generate the references for our specific Pack.
            let mut dependencies = Dependencies::default();
            dependencies.rebuild(&Some(schema.clone()), &pack.dependencies().iter().map(|x| x.1.clone()).collect::<Vec<_>>(), Some(pak_path), game_info, game_path, &PathBuf::new())?;
            dependencies.generate_local_db_references(&schema, &pack, &tables);

            // Trigger a diagnostics check.
            let mut diagnostics = Diagnostics::default();
            diagnostics.check(&mut pack, &mut dependencies, &schema, game_info, game_path, &[], false);

            if config.verbose {
                info!("Diagnosed problems in the following Packs:");
                for pack_path in pack_paths {
                    info!(" - {}", pack_path.to_string_lossy());
                }
                println!("Verbose mode detected. Marking beginning: ----------------------------");
            }

            println!("{}", diagnostics.json()?);

            if config.verbose {
                println!("----------------------------");
            }

            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}

/// This function merges the provided Packs into a new one, and saves it to the provided save path.
pub fn merge(config: &Config, save_pack_path: &Path, source_pack_paths: &[PathBuf]) -> Result<()> {
    if config.verbose {
        info!("Creating new Merged Mod Pack at {}.", save_pack_path.to_string_lossy());
        info!("Packs ready to be merged:");
        for source_pack_path in source_pack_paths {
            info!(" - {}", source_pack_path.to_string_lossy());
        }
    }

    match &config.game {
        Some(game) => {
            let mut pack = Pack::read_and_merge(source_pack_paths, config.game.as_ref().unwrap(), true, false, true)?;
            pack.save(Some(save_pack_path), game, &None)?;
            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}

/// This function adds a dependency to the provided Pack.
pub fn add_dependency(config: &Config, pack_path: &Path, dependency: &str) -> Result<()> {
    if config.verbose {
        info!("Adding a dependency ({}) to the following Pack: {}", dependency, pack_path.file_name().unwrap().to_string_lossy());
    }

    match &config.game {
        Some(game) => {
            let mut pack = Pack::read_and_merge(&[pack_path.to_path_buf()], config.game.as_ref().unwrap(), true, false, true)?;
            pack.dependencies_mut().push((true, dependency.to_owned()));
            pack.save(None, game, &None)?;
            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}

/// This function removes a dependency from the provided Pack.
pub fn remove_dependency(config: &Config, pack_path: &Path, dependency: &str) -> Result<()> {
    if config.verbose {
        info!("Removing a dependency ({}) from the following Pack: {}", dependency, pack_path.file_name().unwrap().to_string_lossy());
    }

    match &config.game {
        Some(game) => {
            let mut pack = Pack::read_and_merge(&[pack_path.to_path_buf()], config.game.as_ref().unwrap(), true, false, true)?;
            if let Some(pos) = pack.dependencies().iter().map(|(_, x)| x).position(|x| x == dependency) {
                pack.dependencies_mut().remove(pos);
            }
            pack.save(None, game, &None)?;
            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}

/// This function removes all dependencies from the provided Pack.
pub fn remove_all_dependencies(config: &Config, pack_path: &Path) -> Result<()> {
    if config.verbose {
        info!("Removing all dependencies from the following Pack: {}", pack_path.file_name().unwrap().to_string_lossy());
    }

    match &config.game {
        Some(game) => {
            let mut pack = Pack::read_and_merge(&[pack_path.to_path_buf()], config.game.as_ref().unwrap(), true, false, true)?;
            pack.dependencies_mut().clear();
            pack.save(None, game, &None)?;
            Ok(())
        }
        None => Err(anyhow!("No Game provided.")),
    }
}