Skip to content

Commit d66848f

Browse files
committed
refactor: Give ownership of file content to FileEntry
Clean up Fileset construction and fix bug Don't bother normalising paths fix tests
1 parent e321685 commit d66848f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1167
-911
lines changed

ck3-tiger/benches/criterion.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
fs,
55
path::{Path, PathBuf},
66
};
7-
use tiger_lib::{Everything, ModFile};
7+
use tiger_lib::{Everything, Fileset};
88

99
static CONFIG_PATH: &str = "../benches/ck3.toml";
1010

@@ -26,16 +26,16 @@ fn workspace_path(s: &str) -> PathBuf {
2626
let p = PathBuf::from(s);
2727
if p.is_relative() {
2828
PathBuf::from("..").join(p)
29-
}
30-
else {
29+
} else {
3130
p
3231
}
3332
}
3433

3534
fn bench_multiple(c: &mut Criterion) {
3635
let content = fs::read_to_string(CONFIG_PATH).unwrap();
3736
let config: Config = toml::from_str(&content).unwrap();
38-
let mut modfile_paths = config.modfile_paths.iter().map(|p| workspace_path(p)).collect::<Vec<_>>();
37+
let mut modfile_paths =
38+
config.modfile_paths.iter().map(|p| workspace_path(p)).collect::<Vec<_>>();
3939

4040
if let Some(modfile_dir) = config.modfile_dir {
4141
let modfile_dir = workspace_path(&modfile_dir);
@@ -46,35 +46,26 @@ fn bench_multiple(c: &mut Criterion) {
4646
modfile_paths.extend(iter);
4747
}
4848

49+
let vanilla_dir = PathBuf::from(config.vanilla_dir);
50+
4951
let mut group = c.benchmark_group("benchmark");
5052
group.sample_size(config.sample_size.unwrap_or(10));
5153
for (index, modfile_path) in modfile_paths.iter().enumerate() {
52-
let modfile = ModFile::read(modfile_path).unwrap();
5354
group.bench_with_input(
54-
BenchmarkId::new(
55-
"mods",
56-
format!("{}. {}", index + 1, modfile.display_name().unwrap_or_default()),
57-
),
58-
&modfile,
59-
|b, modfile_ref| {
60-
b.iter(|| bench_mod(&config.vanilla_dir, modfile_ref));
55+
BenchmarkId::new("mods", format!("{}. {}", index + 1, modfile_path.display())),
56+
modfile_path,
57+
|b, modfile_path| {
58+
b.iter(|| bench_mod(&vanilla_dir, modfile_path.clone()));
6159
},
6260
);
6361
}
6462

6563
group.finish();
6664
}
6765

68-
fn bench_mod(vanilla_dir: &str, modfile: &ModFile) {
69-
let mut everything = Everything::new(
70-
None,
71-
Some(Path::new(vanilla_dir)),
72-
None,
73-
None,
74-
&modfile.modpath(),
75-
modfile.replace_paths(),
76-
)
77-
.unwrap();
66+
fn bench_mod(vanilla_dir: &Path, modfile_path: PathBuf) {
67+
let fileset = Fileset::builder(Some(vanilla_dir)).with_modfile(modfile_path).unwrap();
68+
let mut everything = Everything::new(fileset, None, None, None).unwrap();
7869
everything.load_all();
7970
everything.validate_all();
8071
everything.check_rivers();

ck3-tiger/src/bin/scan-mod-ck3-tiger.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::fs::write;
55
use std::mem::forget;
66
use std::path::PathBuf;
77

8-
use anyhow::{bail, Result};
8+
use anyhow::Result;
99
use clap::Parser;
1010
use serde_json::{json, to_string_pretty, Value};
1111
use strum::IntoEnumIterator;
1212

13-
use tiger_lib::{Everything, FileKind, Game, Item, ModFile};
13+
use tiger_lib::{Everything, FileKind, Fileset, Game, Item};
1414

1515
#[derive(Parser)]
1616
struct Cli {
@@ -34,16 +34,8 @@ fn main() -> Result<()> {
3434
if args.modpath.is_dir() {
3535
args.modpath.push("descriptor.mod");
3636
}
37-
let modfile = ModFile::read(&args.modpath)?;
38-
let modpath = modfile.modpath();
39-
if !modpath.exists() {
40-
eprintln!("Looking for mod in {}", modpath.display());
41-
bail!("Cannot find mod directory. Please make sure the .mod file is correct.");
42-
}
43-
eprintln!("Using mod directory: {}", modpath.display());
44-
45-
let mut everything =
46-
Everything::new(None, None, None, None, &modpath, modfile.replace_paths())?;
37+
let fileset = Fileset::builder(None).with_modfile(args.modpath)?;
38+
let mut everything = Everything::new(fileset, None, None, None)?;
4739

4840
everything.load_all();
4941

src/block/comparator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum Eq {
3232
Question,
3333
}
3434

35+
#[derive(Debug, Copy, Clone)]
3536
pub struct UnknownComparatorError;
3637

3738
impl FromStr for Comparator {

src/ck3/data/characters.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt::{Display, Formatter};
44
use std::path::PathBuf;
55
use std::str::FromStr;
66
use std::sync::atomic::Ordering;
7+
use std::sync::Arc;
78

89
use atomic_enum::atomic_enum;
910

@@ -14,7 +15,7 @@ use crate::context::ScopeContext;
1415
use crate::date::Date;
1516
use crate::effect::{validate_effect, validate_effect_field};
1617
use crate::everything::Everything;
17-
use crate::fileset::{FileEntry, FileHandler};
18+
use crate::files::{FileEntry, FileHandler};
1819
use crate::helpers::{TigerHashMap, TigerHashSet};
1920
use crate::item::Item;
2021
use crate::lowercase::Lowercase;
@@ -255,15 +256,15 @@ impl FileHandler<Block> for Characters {
255256
PathBuf::from("history/characters")
256257
}
257258

258-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
259+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
259260
if !entry.filename().to_string_lossy().ends_with(".txt") {
260261
return None;
261262
}
262263

263264
PdxFile::read(entry, parser)
264265
}
265266

266-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
267+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
267268
for (key, block) in block.drain_definitions_warn() {
268269
self.load_item(key, block);
269270
}

src/ck3/data/doctrines.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::Block;
45
use crate::ck3::modif::ModifKinds;
56
use crate::ck3::validate::validate_traits;
67
use crate::context::ScopeContext;
78
use crate::desc::validate_desc;
89
use crate::everything::Everything;
9-
use crate::fileset::{FileEntry, FileHandler};
10+
use crate::files::{FileEntry, FileHandler};
1011
use crate::helpers::{dup_error, TigerHashMap, TigerHashSet};
1112
use crate::item::Item;
1213
use crate::modif::validate_modifs;
@@ -105,15 +106,15 @@ impl FileHandler<Block> for Doctrines {
105106
PathBuf::from("common/religion/doctrines")
106107
}
107108

108-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
109+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
109110
if !entry.filename().to_string_lossy().ends_with(".txt") {
110111
return None;
111112
}
112113

113114
PdxFile::read(entry, parser)
114115
}
115116

116-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
117+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
117118
for (key, block) in block.drain_definitions_warn() {
118119
self.load_item(key, block);
119120
}

src/ck3/data/gameconcepts.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::Block;
45
use crate::everything::Everything;
5-
use crate::fileset::{FileEntry, FileHandler};
6+
use crate::files::{FileEntry, FileHandler};
67
use crate::helpers::{dup_error, TigerHashMap};
78
use crate::item::Item;
89
use crate::parse::ParserMemory;
@@ -47,15 +48,15 @@ impl FileHandler<Block> for GameConcepts {
4748
PathBuf::from("common/game_concepts")
4849
}
4950

50-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
51+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
5152
if !entry.filename().to_string_lossy().ends_with(".txt") {
5253
return None;
5354
}
5455

5556
PdxFile::read(entry, parser)
5657
}
5758

58-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
59+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
5960
for (key, block) in block.drain_definitions_warn() {
6061
self.load_item(key, block);
6162
}

src/ck3/data/interaction_cats.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::Block;
45
use crate::everything::Everything;
5-
use crate::fileset::{FileEntry, FileHandler};
6+
use crate::files::{FileEntry, FileHandler};
67
use crate::helpers::{dup_error, TigerHashMap};
78
use crate::item::Item;
89
use crate::parse::ParserMemory;
@@ -54,15 +55,15 @@ impl FileHandler<Block> for CharacterInteractionCategories {
5455
PathBuf::from("common/character_interaction_categories")
5556
}
5657

57-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
58+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
5859
if !entry.filename().to_string_lossy().ends_with(".txt") {
5960
return None;
6061
}
6162

6263
PdxFile::read(entry, parser)
6364
}
6465

65-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
66+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
6667
for (key, block) in block.drain_definitions_warn() {
6768
self.load_item(key, block);
6869
}

src/ck3/data/maa.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::Block;
45
use crate::ck3::validate::{validate_cost, validate_maa_stats};
56
use crate::context::ScopeContext;
67
use crate::everything::Everything;
7-
use crate::fileset::{FileEntry, FileHandler};
8+
use crate::files::{FileEntry, FileHandler};
89
use crate::helpers::{dup_error, TigerHashMap, TigerHashSet};
910
use crate::item::Item;
1011
use crate::lowercase::Lowercase;
@@ -73,15 +74,15 @@ impl FileHandler<Block> for MenAtArmsTypes {
7374
PathBuf::from("common/men_at_arms_types")
7475
}
7576

76-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
77+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
7778
if !entry.filename().to_string_lossy().ends_with(".txt") {
7879
return None;
7980
}
8081

8182
PdxFile::read(entry, parser)
8283
}
8384

84-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
85+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
8586
for (key, block) in block.drain_definitions_warn() {
8687
self.load_item(key, block);
8788
}

src/ck3/data/prov_history.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::{Block, BV};
45
use crate::ck3::data::provinces::ProvId;
56
use crate::ck3::data::titles::Titles;
67
use crate::date::Date;
78
use crate::everything::Everything;
8-
use crate::fileset::{FileEntry, FileHandler};
9+
use crate::files::{FileEntry, FileHandler};
910
use crate::helpers::{dup_error, TigerHashMap};
1011
use crate::item::Item;
1112
use crate::parse::ParserMemory;
@@ -83,15 +84,15 @@ impl FileHandler<Block> for ProvinceHistories {
8384
PathBuf::from("history/provinces")
8485
}
8586

86-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
87+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
8788
if !entry.filename().to_string_lossy().ends_with(".txt") {
8889
return None;
8990
}
9091

9192
PdxFile::read_detect_encoding(entry, parser)
9293
}
9394

94-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
95+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
9596
for (key, block) in block.drain_definitions_warn() {
9697
if let Ok(id) = key.as_str().parse() {
9798
self.load_item(id, key, block);

src/ck3/data/prov_terrain.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use crate::block::Block;
45
use crate::everything::Everything;
5-
use crate::fileset::{FileEntry, FileHandler};
6+
use crate::files::{FileEntry, FileHandler};
67
use crate::helpers::{dup_error, TigerHashMap};
78
use crate::item::Item;
89
use crate::parse::ParserMemory;
@@ -63,7 +64,7 @@ impl FileHandler<Block> for ProvinceTerrains {
6364
PathBuf::from("common/province_terrain")
6465
}
6566

66-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
67+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
6768
if !entry.filename().to_string_lossy().ends_with("province_terrain.txt") {
6869
// Omit _province_properties.txt
6970
return None;
@@ -72,7 +73,7 @@ impl FileHandler<Block> for ProvinceTerrains {
7273
PdxFile::read_detect_encoding(entry, parser)
7374
}
7475

75-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
76+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
7677
self.file_loc = Some(block.loc);
7778
for (key, value) in block.drain_assignments_warn() {
7879
if let Ok(id) = key.as_str().parse() {
@@ -140,15 +141,15 @@ impl FileHandler<Block> for ProvinceProperties {
140141
PathBuf::from("common/province_terrain")
141142
}
142143

143-
fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<Block> {
144+
fn load_file(&self, entry: &Arc<FileEntry>, parser: &ParserMemory) -> Option<Block> {
144145
if !entry.filename().to_string_lossy().ends_with("province_properties.txt") {
145146
// Omit _province_terrain.txt
146147
return None;
147148
}
148149
PdxFile::read_detect_encoding(entry, parser)
149150
}
150151

151-
fn handle_file(&mut self, _entry: &FileEntry, mut block: Block) {
152+
fn handle_file(&mut self, _entry: &Arc<FileEntry>, mut block: Block) {
152153
for (key, block) in block.drain_definitions_warn() {
153154
if let Ok(id) = key.as_str().parse() {
154155
self.load_item(id, key, block);

0 commit comments

Comments
 (0)