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
use super::{
	BaseAliasUsage, BaseAnalysisSnapshot, BaseCsvRow, BaseJsonProperty, BaseKeyUsage,
	BaseLocalisationDefinition, BaseLocalisationDuplicate, BaseResourceReference,
	BaseScalarAssignment, BaseSymbolDefinition, BaseSymbolReference, BaseUiDefinition,
};
use crate::game::eu4::content::eu4_content_family_for_root_family;
use crate::game::eu4::script::classify_script_file;
use crate::model::{DocumentFamily, GamePath};
use relative_path::RelativePath;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CoverageClass {
	ExcludedNonGameplay,
	ParseOnly,
	SemanticComplete,
	GraphReady,
	MergeReady,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct RootCoverageEntry {
	pub root_family: String,
	pub coverage_class: CoverageClass,
	pub inventory_file_count: usize,
	pub document_count: usize,
	pub parse_failed_documents: usize,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub document_families: Vec<String>,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub script_file_kinds: Vec<String>,
	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
	pub semantic_counts: BTreeMap<String, usize>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct BaseCoverageReport {
	pub game: String,
	pub game_version: String,
	pub analysis_rules_version: String,
	pub generated_by_cli_version: String,
	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
	pub class_counts: BTreeMap<String, usize>,
	pub roots: Vec<RootCoverageEntry>,
}

#[derive(Default)]
struct CoverageAccumulator {
	inventory_file_count: usize,
	document_count: usize,
	parse_failed_documents: usize,
	document_families: BTreeMap<String, usize>,
	script_file_kinds: BTreeMap<String, usize>,
	semantic_counts: BTreeMap<String, usize>,
}

impl CoverageAccumulator {
	fn increment_document_family(&mut self, family: DocumentFamily) {
		let key = document_family_name(family).to_string();
		*self.document_families.entry(key).or_insert(0) += 1;
	}

	fn increment_script_file_kind(&mut self, kind: &str) {
		*self.script_file_kinds.entry(kind.to_string()).or_insert(0) += 1;
	}

	fn increment_semantic_count(&mut self, key: &str) {
		*self.semantic_counts.entry(key.to_string()).or_insert(0) += 1;
	}

	fn has_only_other_clausewitz(&self) -> bool {
		!self.script_file_kinds.is_empty()
			&& self
				.script_file_kinds
				.keys()
				.all(|item| item.as_str() == "other")
	}

	fn has_graph_semantics(&self) -> bool {
		self.semantic_counts.contains_key("symbol_definitions")
			|| self.semantic_counts.contains_key("symbol_references")
			|| self.semantic_counts.contains_key("alias_usages")
			|| self.semantic_counts.contains_key("key_usages")
			|| self.semantic_counts.contains_key("scalar_assignments")
	}

	fn has_semantic_surface(&self) -> bool {
		self.has_graph_semantics()
			|| self
				.semantic_counts
				.contains_key("localisation_definitions")
			|| self.semantic_counts.contains_key("localisation_duplicates")
			|| self.semantic_counts.contains_key("ui_definitions")
			|| self.semantic_counts.contains_key("resource_references")
			|| self.semantic_counts.contains_key("csv_rows")
			|| self.semantic_counts.contains_key("json_properties")
			|| self
				.script_file_kinds
				.keys()
				.any(|item| item.as_str() != "other")
	}
}

pub fn build_coverage_report(snapshot: &BaseAnalysisSnapshot) -> BaseCoverageReport {
	let mut roots: BTreeMap<String, CoverageAccumulator> = BTreeMap::new();
	for path in &snapshot.inventory_paths {
		let Some(root_family) = coverage_root_family(path) else {
			continue;
		};
		roots.entry(root_family).or_default().inventory_file_count += 1;
	}
	for document in &snapshot.documents {
		let Some(root_family) = coverage_root_family(&document.path) else {
			continue;
		};
		let entry = roots.entry(root_family).or_default();
		entry.document_count += 1;
		if !document.parse_ok {
			entry.parse_failed_documents += 1;
		}
		entry.increment_document_family(document.family);
		if document.family == DocumentFamily::Clausewitz {
			let kind = classify_script_file(&document.path);
			entry.increment_script_file_kind(kind.as_str());
		}
	}
	accumulate_semantic_counts(
		&mut roots,
		&snapshot.symbol_definitions,
		"symbol_definitions",
	);
	accumulate_semantic_counts(&mut roots, &snapshot.symbol_references, "symbol_references");
	accumulate_semantic_counts(&mut roots, &snapshot.alias_usages, "alias_usages");
	accumulate_semantic_counts(&mut roots, &snapshot.key_usages, "key_usages");
	accumulate_semantic_counts(
		&mut roots,
		&snapshot.scalar_assignments,
		"scalar_assignments",
	);
	accumulate_semantic_counts(
		&mut roots,
		&snapshot.localisation_definitions,
		"localisation_definitions",
	);
	accumulate_semantic_counts(
		&mut roots,
		&snapshot.localisation_duplicates,
		"localisation_duplicates",
	);
	accumulate_semantic_counts(&mut roots, &snapshot.ui_definitions, "ui_definitions");
	accumulate_semantic_counts(
		&mut roots,
		&snapshot.resource_references,
		"resource_references",
	);
	accumulate_semantic_counts(&mut roots, &snapshot.csv_rows, "csv_rows");
	accumulate_semantic_counts(&mut roots, &snapshot.json_properties, "json_properties");

	let mut class_counts: BTreeMap<String, usize> = BTreeMap::new();
	let entries: Vec<RootCoverageEntry> = roots
		.into_iter()
		.map(|(root_family, accumulator)| {
			let coverage_class = coverage_class_for_root(&root_family, &accumulator);
			*class_counts
				.entry(coverage_class_name(coverage_class).to_string())
				.or_insert(0) += 1;
			RootCoverageEntry {
				root_family,
				coverage_class,
				inventory_file_count: accumulator.inventory_file_count,
				document_count: accumulator.document_count,
				parse_failed_documents: accumulator.parse_failed_documents,
				document_families: accumulator.document_families.into_keys().collect(),
				script_file_kinds: accumulator.script_file_kinds.into_keys().collect(),
				semantic_counts: accumulator.semantic_counts,
			}
		})
		.collect();

	BaseCoverageReport {
		game: snapshot.game.clone(),
		game_version: snapshot.game_version.clone(),
		analysis_rules_version: snapshot.analysis_rules_version.clone(),
		generated_by_cli_version: snapshot.generated_by_cli_version.clone(),
		class_counts,
		roots: entries,
	}
}

fn coverage_class_for_root(root_family: &str, accumulator: &CoverageAccumulator) -> CoverageClass {
	if is_excluded_non_gameplay_root(root_family) {
		CoverageClass::ExcludedNonGameplay
	} else if let Some(classification) = content_family_coverage_class(root_family, accumulator) {
		classification
	} else if accumulator.has_only_other_clausewitz() {
		CoverageClass::ParseOnly
	} else if accumulator.has_graph_semantics() {
		CoverageClass::GraphReady
	} else if accumulator.has_semantic_surface() {
		CoverageClass::SemanticComplete
	} else {
		CoverageClass::ParseOnly
	}
}

fn content_family_coverage_class(
	root_family: &str,
	accumulator: &CoverageAccumulator,
) -> Option<CoverageClass> {
	let descriptor = eu4_content_family_for_root_family(root_family)?;
	if descriptor.capabilities.semantic_complete {
		return Some(CoverageClass::SemanticComplete);
	}
	if descriptor.capabilities.merge_ready {
		return Some(CoverageClass::MergeReady);
	}
	if descriptor.capabilities.graph_ready {
		return Some(if accumulator.has_graph_semantics() {
			CoverageClass::GraphReady
		} else {
			CoverageClass::ParseOnly
		});
	}
	Some(CoverageClass::ParseOnly)
}

pub fn coverage_class_name(classification: CoverageClass) -> &'static str {
	match classification {
		CoverageClass::ExcludedNonGameplay => "excluded_non_gameplay",
		CoverageClass::ParseOnly => "parse_only",
		CoverageClass::SemanticComplete => "semantic_complete",
		CoverageClass::GraphReady => "graph_ready",
		CoverageClass::MergeReady => "merge_ready",
	}
}

fn accumulate_semantic_counts<T>(
	roots: &mut BTreeMap<String, CoverageAccumulator>,
	items: &[T],
	metric_name: &str,
) where
	T: CoveragePath,
{
	for item in items {
		let Some(root_family) = coverage_root_family(item.coverage_path()) else {
			continue;
		};
		roots
			.entry(root_family)
			.or_default()
			.increment_semantic_count(metric_name);
	}
}

trait CoveragePath {
	fn coverage_path(&self) -> &GamePath;
}

impl CoveragePath for BaseSymbolDefinition {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseSymbolReference {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseAliasUsage {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseKeyUsage {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseScalarAssignment {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseLocalisationDefinition {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseLocalisationDuplicate {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseUiDefinition {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseResourceReference {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseCsvRow {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

impl CoveragePath for BaseJsonProperty {
	fn coverage_path(&self) -> &GamePath {
		&self.path
	}
}

/// The coverage root a game path counts toward: its content family's root
/// directory, spelled as the family id is.
fn coverage_root_family(path: &GamePath) -> Option<String> {
	if !is_tracked_non_binary_path(path) {
		return None;
	}
	let parts: Vec<&str> = path.iter().collect();
	match parts.as_slice() {
		["events", "common", group, ..] => {
			return Some(format!("events/common/{}", component_stem(group)));
		}
		["events", "decisions", ..] => return Some("events/decisions".to_string()),
		["map", "random", "tiles", _, ..] => return Some("map/random/tiles".to_string()),
		[
			"map",
			"random",
			"RandomLandNames.txt" | "RandomSeaNames.txt" | "RandomLakeNames.txt",
		] => {
			return Some("map/random_names".to_string());
		}
		["map", "random", "RNWScenarios.txt"] => return Some("map/random/scenarios".to_string()),
		_ => {}
	}
	match parts.as_slice() {
		[] | ["common" | "history" | "map"] => None,
		[top @ ("common" | "history" | "map"), file] => {
			Some(format!("{top}/{}", component_stem(file)))
		}
		[top @ ("common" | "history" | "map"), group, ..] => Some(format!("{top}/{group}")),
		[top, ..] => Some((*top).to_string()),
	}
}

/// A path component without its extension.
fn component_stem(name: &str) -> &str {
	RelativePath::new(name).file_stem().unwrap_or(name)
}

/// Text files the coverage report tracks, by extension in any case.
fn is_tracked_non_binary_path(path: &GamePath) -> bool {
	path.extension().is_some_and(|extension| {
		[
			"txt", "gui", "gfx", "asset", "mod", "lua", "yml", "yaml", "csv", "json",
		]
		.iter()
		.any(|tracked| extension.eq_ignore_ascii_case(tracked))
	})
}

fn is_excluded_non_gameplay_root(root_family: &str) -> bool {
	matches!(
		root_family,
		"licenses"
			| "patchnotes"
			| "ebook" | "legal_notes"
			| "builtin_dlc"
			| "dlc_metadata"
			| "hints" | "tools"
			| "tests" | "ThirdPartyLicenses.txt"
			| "map/random"
			| "checksum_manifest.txt"
			| "clausewitz_branch.txt"
			| "clausewitz_rev.txt"
			| "eu4_branch.txt"
			| "eu4_rev.txt"
			| "steam.txt"
			| "描述.txt"
			| "launcher-settings.json"
			| "settings-layout.json"
	)
}

pub fn document_family_name(family: DocumentFamily) -> &'static str {
	match family {
		DocumentFamily::Clausewitz => "clausewitz",
		DocumentFamily::Localisation => "localisation",
		DocumentFamily::Csv => "csv",
		DocumentFamily::Json => "json",
	}
}

pub fn write_coverage_report(path: &Path, snapshot: &BaseAnalysisSnapshot) -> Result<(), String> {
	let coverage = build_coverage_report(snapshot);
	let raw = serde_json::to_string_pretty(&coverage)
		.map_err(|err| format!("failed to serialize base data coverage: {err}"))?;
	fs::write(path, raw).map_err(|err| {
		format!(
			"failed to write base data coverage {}: {err}",
			path.display()
		)
	})
}

#[cfg(test)]
mod tests {
	use super::coverage_root_family;
	use crate::model::GamePath;

	#[test]
	fn coverage_roots_are_derived_from_game_path_components() {
		for (path, root) in [
			(
				"events/common/on_actions/x.txt",
				Some("events/common/on_actions"),
			),
			("events/common/foo.txt", Some("events/common/foo")),
			("events/decisions/x.txt", Some("events/decisions")),
			("events/x.txt", Some("events")),
			("map/random/tiles/tile0.txt", Some("map/random/tiles")),
			("map/random/RandomLakeNames.txt", Some("map/random_names")),
			("map/random/RNWScenarios.txt", Some("map/random/scenarios")),
			("map/random/other.txt", Some("map/random")),
			("map/area.txt", Some("map/area")),
			("common/ideas/00_x.txt", Some("common/ideas")),
			("common/achievements.txt", Some("common/achievements")),
			("common/defines.lua", Some("common/defines")),
			("common/Ideas/x.txt", Some("common/Ideas")),
			(
				"history/countries/SWE - Sweden.txt",
				Some("history/countries"),
			),
			("localisation/x_l_english.YML", Some("localisation")),
			("ThirdPartyLicenses.txt", Some("ThirdPartyLicenses.txt")),
			("gfx/x.dds", None),
			("common/no_extension", None),
		] {
			assert_eq!(
				coverage_root_family(GamePath::new(path).expect("valid game path")).as_deref(),
				root,
				"{path}"
			);
		}
	}
}