foch 0.0.2

EU4 mod analysis and merge toolkit
Documentation
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Cross-file global key namespace detection for the merge system.
//
// EU4 loads ALL files within a content family directory and builds a global
// namespace — two mods defining the same key in DIFFERENT files is still a
// conflict. This module detects such cross-file key conflicts.
#![allow(dead_code)]

use crate::game::eu4::Eu4;
use crate::game::eu4::content::MergeKeySource;
use crate::game::eu4::script::parser::{AstStatement, AstValue};
use crate::game::eu4::script::{ParsedScriptFile, is_decision_container_key};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};

use crate::input::ResolvedInputContributor;
use crate::model::GamePathBuf;

use super::normalize::normalize_defines_file;

/// A key contributor: which mod defined this key, in which file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KeyContributor {
	pub mod_id: String,
	pub file_path: GamePathBuf,
	pub precedence: usize,
	pub is_base_game: bool,
}

/// Result of scanning a family's global namespace.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FamilyKeyConflict {
	pub key: String,
	pub family_id: String,
	pub contributors: Vec<KeyContributor>,
}

/// The global key index for a content family.
#[derive(Clone, Debug, Default)]
pub struct FamilyKeyIndex {
	pub family_id: String,
	/// key_name → list of contributors (mod_id, file_path, precedence).
	pub entries: HashMap<String, Vec<KeyContributor>>,
}

/// Extract merge keys from a parsed script file according to the given
/// `MergeKeySource`. Returns a list of `(merge_key, statement_key)` pairs.
fn extract_keys(parsed: &ParsedScriptFile, merge_key_source: MergeKeySource) -> Vec<String> {
	match merge_key_source {
		MergeKeySource::AssignmentKey => extract_assignment_keys(parsed),
		MergeKeySource::FieldValue(field) => extract_field_value_keys(parsed, field),
		MergeKeySource::ContainerChildKey => extract_container_child_keys(parsed),
		MergeKeySource::ContainerChildFieldValue {
			containers,
			child_key_field,
			child_types,
		} => extract_container_child_field_value_keys(
			parsed,
			containers,
			child_key_field,
			child_types,
		),
		MergeKeySource::ChildFieldValue { .. } => extract_assignment_keys(parsed),
		MergeKeySource::LeafPath => extract_defines_keys(parsed),
	}
}

fn extract_assignment_keys(parsed: &ParsedScriptFile) -> Vec<String> {
	parsed
		.ast
		.statements
		.iter()
		.filter_map(|stmt| match stmt {
			AstStatement::Assignment {
				key,
				value: AstValue::Block { .. },
				..
			} => Some(key.clone()),
			_ => None,
		})
		.collect()
}

fn extract_field_value_keys(parsed: &ParsedScriptFile, field: &str) -> Vec<String> {
	parsed
		.ast
		.statements
		.iter()
		.filter_map(|stmt| {
			let AstStatement::Assignment {
				value: AstValue::Block { items, .. },
				..
			} = stmt
			else {
				return None;
			};
			scalar_assignment_value(items, field)
		})
		.collect()
}

fn extract_container_child_keys(parsed: &ParsedScriptFile) -> Vec<String> {
	let mut keys = Vec::new();
	for stmt in &parsed.ast.statements {
		let AstStatement::Assignment {
			key,
			value: AstValue::Block { items, .. },
			..
		} = stmt
		else {
			continue;
		};
		if !is_decision_container_key(key) {
			continue;
		}
		for item in items {
			if let AstStatement::Assignment {
				key: child_key,
				value: AstValue::Block { .. },
				..
			} = item
			{
				keys.push(child_key.clone());
			}
		}
	}
	keys
}

fn extract_container_child_field_value_keys(
	parsed: &ParsedScriptFile,
	containers: &[&str],
	child_key_field: &str,
	child_types: &[&str],
) -> Vec<String> {
	let mut keys = Vec::new();
	for stmt in &parsed.ast.statements {
		let AstStatement::Assignment { key, value, .. } = stmt else {
			continue;
		};
		if !containers.contains(&key.as_str()) {
			keys.push(key.clone());
			continue;
		}
		let AstValue::Block { items, .. } = value else {
			continue;
		};
		for child in items {
			if let Some(key) = container_child_field_value_key(child, child_key_field, child_types)
			{
				keys.push(key);
			}
		}
	}
	keys
}

fn container_child_field_value_key(
	stmt: &AstStatement,
	child_key_field: &str,
	child_types: &[&str],
) -> Option<String> {
	let AstStatement::Assignment { key, value, .. } = stmt else {
		return None;
	};
	if (child_types.is_empty() || child_types.contains(&key.as_str()))
		&& let AstValue::Block { items, .. } = value
		&& let Some(field_value) = scalar_assignment_value(items, child_key_field)
	{
		return Some(format!("{key}:{field_value}"));
	}
	Some(key.clone())
}

fn extract_defines_keys(parsed: &ParsedScriptFile) -> Vec<String> {
	normalize_defines_file(parsed)
		.map(|fragments| fragments.into_iter().map(|f| f.merge_key).collect())
		.unwrap_or_default()
}

fn scalar_assignment_value(items: &[AstStatement], expected_key: &str) -> Option<String> {
	for item in items {
		let AstStatement::Assignment { key, value, .. } = item else {
			continue;
		};
		if key != expected_key {
			continue;
		}
		if let AstValue::Scalar { value, .. } = value {
			return Some(value.as_text());
		}
	}
	None
}

/// Build a [`FamilyKeyIndex`] by scanning all files that belong to a content
/// family across every mod in the input.
///
/// `contributors_by_path` is the subset of the input file inventory that
/// belongs to this family (keyed by relative path).
pub(crate) fn build_family_key_index(
	family_id: &str,
	merge_key_source: MergeKeySource,
	contributors_by_path: &BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>,
	_profile: &Eu4,
) -> FamilyKeyIndex {
	let mut index = FamilyKeyIndex {
		family_id: family_id.to_string(),
		..Default::default()
	};

	for contributors in contributors_by_path.values() {
		for contributor in contributors {
			let parsed = crate::game::eu4::script::parse_script_file(
				&contributor.mod_id,
				&contributor.root_path,
				&contributor.relative_path,
			);

			let keys = extract_keys(&parsed, merge_key_source);
			for key in keys {
				index.entries.entry(key).or_default().push(KeyContributor {
					mod_id: contributor.mod_id.clone(),
					file_path: contributor.relative_path.clone(),
					precedence: contributor.precedence,
					is_base_game: contributor.is_base_game,
				});
			}
		}
	}

	index
}

/// Find all keys defined by multiple non-base-game mods (cross-file or
/// same-file). A key is considered conflicting when two or more *distinct*
/// non-base-game mods contribute it.
pub fn detect_key_conflicts(index: &FamilyKeyIndex) -> Vec<FamilyKeyConflict> {
	let mut conflicts = Vec::new();

	for (key, contributors) in &index.entries {
		let distinct_mod_count = contributors
			.iter()
			.filter(|c| !c.is_base_game)
			.map(|c| &c.mod_id)
			.collect::<std::collections::HashSet<_>>()
			.len();

		if distinct_mod_count >= 2 {
			conflicts.push(FamilyKeyConflict {
				key: key.clone(),
				family_id: index.family_id.clone(),
				contributors: contributors.clone(),
			});
		}
	}

	conflicts.sort_by(|a, b| a.key.cmp(&b.key));
	conflicts
}

/// Group file-inventory entries by content family for namespace analysis.
///
/// Returns `family_id → { relative_path → contributors }`.
pub(crate) fn group_by_family(
	file_inventory: &BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>,
	profile: &Eu4,
) -> HashMap<String, BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>> {
	let mut grouped: HashMap<String, BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>>> =
		HashMap::new();

	for (rel_path, contributors) in file_inventory {
		if let Some(descriptor) = profile.classify_content_family(rel_path) {
			grouped
				.entry(descriptor.id.as_str().to_string())
				.or_default()
				.insert(rel_path.clone(), contributors.clone());
		}
	}

	grouped
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::path::PathBuf;

	fn make_contributor(mod_id: &str, precedence: usize, is_base_game: bool) -> KeyContributor {
		KeyContributor {
			mod_id: mod_id.to_string(),
			file_path: GamePathBuf::parse(&format!("common/test/{mod_id}.txt"))
				.expect("valid game path"),
			precedence,
			is_base_game,
		}
	}

	fn make_index(family_id: &str, entries: Vec<(&str, Vec<KeyContributor>)>) -> FamilyKeyIndex {
		FamilyKeyIndex {
			family_id: family_id.to_string(),
			entries: entries
				.into_iter()
				.map(|(k, v)| (k.to_string(), v))
				.collect(),
		}
	}

	#[test]
	fn single_mod_no_conflicts() {
		let index = make_index(
			"scripted_triggers",
			vec![
				("trigger_a", vec![make_contributor("mod_a", 1, false)]),
				("trigger_b", vec![make_contributor("mod_a", 1, false)]),
			],
		);

		let conflicts = detect_key_conflicts(&index);
		assert!(
			conflicts.is_empty(),
			"single mod should produce no conflicts"
		);
	}

	#[test]
	fn two_mods_same_key_same_file_detected() {
		let index = make_index(
			"scripted_triggers",
			vec![(
				"my_trigger",
				vec![
					KeyContributor {
						mod_id: "mod_a".to_string(),
						file_path: GamePathBuf::parse("common/scripted_triggers/shared.txt")
							.expect("valid game path"),
						precedence: 1,
						is_base_game: false,
					},
					KeyContributor {
						mod_id: "mod_b".to_string(),
						file_path: GamePathBuf::parse("common/scripted_triggers/shared.txt")
							.expect("valid game path"),
						precedence: 2,
						is_base_game: false,
					},
				],
			)],
		);

		let conflicts = detect_key_conflicts(&index);
		assert_eq!(conflicts.len(), 1);
		assert_eq!(conflicts[0].key, "my_trigger");
		assert_eq!(conflicts[0].contributors.len(), 2);
	}

	#[test]
	fn two_mods_same_key_different_files_detected() {
		let index = make_index(
			"scripted_triggers",
			vec![(
				"my_trigger",
				vec![
					KeyContributor {
						mod_id: "mod_a".to_string(),
						file_path: GamePathBuf::parse("common/scripted_triggers/a.txt")
							.expect("valid game path"),
						precedence: 1,
						is_base_game: false,
					},
					KeyContributor {
						mod_id: "mod_b".to_string(),
						file_path: GamePathBuf::parse("common/scripted_triggers/b.txt")
							.expect("valid game path"),
						precedence: 2,
						is_base_game: false,
					},
				],
			)],
		);

		let conflicts = detect_key_conflicts(&index);
		assert_eq!(conflicts.len(), 1);
		assert_eq!(conflicts[0].key, "my_trigger");
		// Verify contributors come from different files
		let paths: Vec<&str> = conflicts[0]
			.contributors
			.iter()
			.map(|c| c.file_path.as_str())
			.collect();
		assert!(paths.contains(&"common/scripted_triggers/a.txt"));
		assert!(paths.contains(&"common/scripted_triggers/b.txt"));
	}

	#[test]
	fn base_game_plus_one_mod_not_a_conflict() {
		let index = make_index(
			"scripted_triggers",
			vec![(
				"vanilla_trigger",
				vec![
					make_contributor("base_game", 0, true),
					make_contributor("mod_a", 1, false),
				],
			)],
		);

		let conflicts = detect_key_conflicts(&index);
		assert!(
			conflicts.is_empty(),
			"base game + one mod should not be a conflict (mod overrides base)"
		);
	}

	#[test]
	fn base_game_plus_two_mods_is_conflict() {
		let index = make_index(
			"diplomatic_actions",
			vec![(
				"declarewar",
				vec![
					make_contributor("base_game", 0, true),
					make_contributor("imperial_circles", 1, false),
					make_contributor("europa_expanded", 2, false),
				],
			)],
		);

		let conflicts = detect_key_conflicts(&index);
		assert_eq!(conflicts.len(), 1);
		assert_eq!(conflicts[0].key, "declarewar");
		// All three contributors are present (base + two mods)
		assert_eq!(conflicts[0].contributors.len(), 3);
	}

	#[test]
	fn group_by_family_groups_correctly() {
		use crate::game::eu4::content::eu4;

		let profile = eu4();
		let mut inventory: BTreeMap<GamePathBuf, Vec<ResolvedInputContributor>> = BTreeMap::new();

		let game_path = |text: &str| GamePathBuf::parse(text).expect("valid game path");
		let trigger_path = game_path("common/scripted_triggers/my_mod.txt");
		let effect_path = game_path("common/scripted_effects/my_mod.txt");
		let unclassified_path = game_path("some/random/file.txt");

		let contributor = |relative_path: &GamePathBuf| ResolvedInputContributor {
			mod_id: "test_mod".to_string(),
			root_path: PathBuf::from("/mods/test"),
			relative_path: relative_path.clone(),
			precedence: 1,
			is_base_game: false,
			is_synthetic_base: false,
			parse_ok_hint: None,
			mod_hash: Some("hash-test_mod".to_string()),
		};

		for path in [&trigger_path, &effect_path, &unclassified_path] {
			inventory.insert(path.clone(), vec![contributor(path)]);
		}

		let grouped = group_by_family(&inventory, profile);

		// Unclassified paths should not appear in any family
		for paths in grouped.values() {
			assert!(
				!paths.contains_key(&unclassified_path),
				"unclassified path should not appear in any family"
			);
		}

		// scripted_triggers and scripted_effects should each have one entry
		let trigger_family = grouped
			.values()
			.find(|paths| paths.contains_key(&trigger_path));
		assert!(
			trigger_family.is_some(),
			"scripted_triggers path should be grouped"
		);

		let effect_family = grouped
			.values()
			.find(|paths| paths.contains_key(&effect_path));
		assert!(
			effect_family.is_some(),
			"scripted_effects path should be grouped"
		);
	}

	#[test]
	fn conflicts_are_sorted_by_key() {
		let index = make_index(
			"test",
			vec![
				(
					"zebra",
					vec![
						make_contributor("mod_a", 1, false),
						make_contributor("mod_b", 2, false),
					],
				),
				(
					"alpha",
					vec![
						make_contributor("mod_a", 1, false),
						make_contributor("mod_b", 2, false),
					],
				),
			],
		);

		let conflicts = detect_key_conflicts(&index);
		assert_eq!(conflicts.len(), 2);
		assert_eq!(conflicts[0].key, "alpha");
		assert_eq!(conflicts[1].key, "zebra");
	}
}