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
//! Defer culture transformations whose references travel through script parameters.
//! Parameters are text substitution, so rewriting a call argument as a culture
//! could also change its non-culture uses. Track the closure without editing it.

use std::collections::{BTreeMap, BTreeSet};

use super::{reference_kinds, script_context};
use crate::game::eu4::script::parser::{AstStatement, AstValue};
use crate::game::schema::query::CompiledAliasCategory;
use crate::input::ResolvedInput;
use crate::model::{GamePath, GamePathBuf};

#[derive(Default)]
pub(super) struct ParameterReview {
	pub paths: BTreeSet<GamePathBuf>,
	pub findings: Vec<String>,
}

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum CallableKind {
	Effect,
	Trigger,
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct Callable {
	kind: CallableKind,
	name: String,
}

type ParameterPaths = BTreeMap<(Callable, String), BTreeSet<GamePathBuf>>;

struct Call {
	owner: Option<Callable>,
	callee: Callable,
	bindings: Vec<(String, String)>,
	path: GamePathBuf,
	location: String,
}

pub(super) fn review_parameter_flows(
	input: &ResolvedInput,
	mappings: &BTreeMap<String, String>,
) -> Result<ParameterReview, String> {
	if mappings.is_empty() {
		return Ok(ParameterReview::default());
	}
	let mut documents = Vec::new();
	let mut names = BTreeSet::new();
	let mut definitions = BTreeSet::new();
	let mut sensitive = ParameterPaths::new();
	let mut hazardous = BTreeMap::<Callable, BTreeSet<GamePathBuf>>::new();
	let mut review = ParameterReview::default();
	for (path, contributors) in &input.file_inventory {
		if !is_callable_document(path) {
			continue;
		}
		for contributor in contributors.iter().filter(|item| !item.is_synthetic_base) {
			let parsed = input.script_cache.load(contributor)?;
			for statement in &parsed.ast.statements {
				if let AstStatement::Assignment {
					key,
					value: AstValue::Block { items, .. },
					..
				} = statement
				{
					names.insert(key.clone());
					let owner = Callable {
						kind: callable_kind(path).expect("callable document"),
						name: key.clone(),
					};
					definitions.insert(owner.clone());
					collect_parameters(
						items,
						path,
						&mut vec![key.clone()],
						&owner,
						mappings,
						&mut sensitive,
						&mut hazardous,
					);
				}
			}
			documents.push((contributor.mod_id.clone(), parsed));
		}
	}
	if sensitive.is_empty() && hazardous.is_empty() {
		return Ok(review);
	}
	// The semantic indexes select callers; inspect the frozen ASTs so accepted
	// source repairs and forwarded parameter values are included in the review.
	let mut caller_paths = BTreeSet::new();
	for snapshot in input.mod_snapshots.iter().flatten() {
		caller_paths.extend(
			snapshot
				.semantic_index
				.references
				.iter()
				.filter(|reference| names.contains(&reference.name))
				.map(|reference| reference.path.clone()),
		);
		caller_paths.extend(
			snapshot
				.semantic_index
				.scalar_assignments
				.iter()
				.filter(|assignment| names.contains(&assignment.key))
				.map(|assignment| assignment.path.clone()),
		);
	}
	if let Some(base) = &input.installed_base_snapshot {
		caller_paths.extend(
			base.snapshot
				.symbol_references
				.iter()
				.filter(|reference| names.contains(&reference.name))
				.map(|reference| reference.path.clone()),
		);
		caller_paths.extend(
			base.snapshot
				.scalar_assignments
				.iter()
				.filter(|assignment| names.contains(&assignment.key))
				.map(|assignment| assignment.path.clone()),
		);
	}
	for path in caller_paths
		.into_iter()
		.filter(|path| !is_callable_document(path))
	{
		if let Some(contributors) = input.file_inventory.get(&path) {
			for contributor in contributors.iter().filter(|item| !item.is_synthetic_base) {
				documents.push((
					contributor.mod_id.clone(),
					input.script_cache.load(contributor)?,
				));
			}
		}
	}
	let mut calls = Vec::new();
	for (mod_id, parsed) in &documents {
		let path = &parsed.ast.path;
		for statement in &parsed.ast.statements {
			match statement {
				AstStatement::Assignment {
					key,
					value: AstValue::Block { items, .. },
					..
				} if is_callable_document(path) => {
					let owner = Callable {
						kind: callable_kind(path).expect("callable document"),
						name: key.clone(),
					};
					collect_calls(
						items,
						path,
						mod_id,
						Some(&owner),
						&mut vec![key.clone()],
						&definitions,
						&mut calls,
					);
				}
				_ => collect_calls(
					std::slice::from_ref(statement),
					path,
					mod_id,
					None,
					&mut Vec::new(),
					&definitions,
					&mut calls,
				),
			}
		}
	}
	// A parameter passed to another script inherits that script's culture use.
	// Union paths until stable, including cycles and multiple definitions.
	loop {
		let mut changed = false;
		for call in &calls {
			let Some(owner) = &call.owner else { continue };
			for (parameter, value) in &call.bindings {
				let Some(mut paths) = sensitive
					.get(&(call.callee.clone(), parameter.clone()))
					.cloned()
				else {
					continue;
				};
				paths.insert(call.path.clone());
				for forwarded in parameter_names(value) {
					let target = sensitive
						.entry((owner.clone(), forwarded.to_owned()))
						.or_default();
					let count = target.len();
					target.extend(paths.iter().cloned());
					changed |= count != target.len();
				}
			}
		}
		if !changed {
			break;
		}
	}
	for (name, paths) in &hazardous {
		review.paths.extend(paths.iter().cloned());
		review.findings.push(format!("script `{}` has a culture parameter default referring to a changed culture; review parameter substitution before accepting the transformation", name.name));
	}
	for call in &calls {
		for (parameter, value) in &call.bindings {
			if !refers_to_changed(value, mappings) {
				continue;
			}
			let Some(paths) = sensitive.get(&(call.callee.clone(), parameter.clone())) else {
				continue;
			};
			let mut paths = paths.clone();
			paths.insert(call.path.clone());
			review.paths.extend(paths.iter().cloned());
			review.findings.push(format!("{}: parameter `{parameter} = {value}` reaches a culture field through `{}`; review parameter substitution before accepting the transformation", call.location, call.callee.name));
			if let Some(owner) = &call.owner {
				hazardous.entry(owner.clone()).or_default().extend(paths);
			}
		}
	}
	// Callers of a withheld script must join the same review group, even when
	// they invoke a default or a script that supplies the changed ID itself.
	loop {
		let mut changed = false;
		for call in &calls {
			let Some(mut paths) = hazardous.get(&call.callee).cloned() else {
				continue;
			};
			paths.insert(call.path.clone());
			review.paths.extend(paths.iter().cloned());
			if let Some(owner) = &call.owner {
				let target = hazardous.entry(owner.clone()).or_default();
				let count = target.len();
				target.extend(paths);
				changed |= count != target.len();
			}
		}
		if !changed {
			break;
		}
	}
	Ok(review)
}

fn is_callable_document(path: &GamePath) -> bool {
	callable_kind(path).is_some()
}

fn callable_kind(path: &GamePath) -> Option<CallableKind> {
	// The game loads only `.txt` scripts here; a mod's `x.txt.bak` is not one.
	if path
		.extension()
		.is_none_or(|extension| !extension.eq_ignore_ascii_case("txt"))
	{
		None
	} else if path.is_inside(&["common", "scripted_effects"], str::eq) {
		Some(CallableKind::Effect)
	} else if path.is_inside(&["common", "scripted_triggers"], str::eq) {
		Some(CallableKind::Trigger)
	} else {
		None
	}
}

fn call_kinds(path: &GamePath, parents: &[String]) -> Vec<CallableKind> {
	let schema = super::super::cwt::rule_engine();
	let context = script_context(schema, path, parents);
	let mut kinds = Vec::new();
	for (kind, key, category) in [
		(
			CallableKind::Effect,
			"change_culture",
			CompiledAliasCategory::Effect,
		),
		(
			CallableKind::Trigger,
			"culture",
			CompiledAliasCategory::Trigger,
		),
	] {
		if context.is_some_and(|context| {
			schema.bind_field_matches(context, key).iter().any(|field| {
				field
					.alias()
					.is_some_and(|alias| alias.category == category)
			})
		}) {
			kinds.push(kind);
		}
	}
	// Unknown CWT contexts cannot establish a namespace. Audit both rather
	// than silently omit a possibly affected reference.
	if kinds.is_empty() {
		kinds.extend([CallableKind::Effect, CallableKind::Trigger]);
	}
	kinds
}

fn collect_parameters(
	statements: &[AstStatement],
	path: &GamePath,
	parents: &mut Vec<String>,
	owner: &Callable,
	mappings: &BTreeMap<String, String>,
	sensitive: &mut ParameterPaths,
	hazardous: &mut BTreeMap<Callable, BTreeSet<GamePathBuf>>,
) {
	let schema = super::super::cwt::rule_engine();
	let context = script_context(schema, path, parents);
	for statement in statements {
		let AstStatement::Assignment { key, value, .. } = statement else {
			continue;
		};
		let kinds = reference_kinds(schema, context, key);
		let mut uses = Vec::new();
		if kinds.culture_key {
			uses.push(key.clone());
		}
		match value {
			AstValue::Scalar { value, .. }
				if kinds.culture_value || (!kinds.group_value && key.contains("culture")) =>
			{
				uses.push(value.as_text())
			}
			AstValue::Block { items, .. } => {
				parents.push(key.clone());
				collect_parameters(items, path, parents, owner, mappings, sensitive, hazardous);
				parents.pop();
			}
			_ => {}
		}
		for value in uses {
			for name in parameter_names(&value) {
				sensitive
					.entry((owner.clone(), name.to_owned()))
					.or_default()
					.insert(path.to_owned());
				if refers_to_changed(&value, mappings) {
					hazardous
						.entry(owner.clone())
						.or_default()
						.insert(path.to_owned());
				}
			}
		}
	}
}

fn collect_calls(
	statements: &[AstStatement],
	path: &GamePath,
	mod_id: &str,
	owner: Option<&Callable>,
	parents: &mut Vec<String>,
	names: &BTreeSet<Callable>,
	calls: &mut Vec<Call>,
) {
	let kinds = call_kinds(path, parents);
	for statement in statements {
		let AstStatement::Assignment {
			key,
			value,
			key_span,
			..
		} = statement
		else {
			continue;
		};
		for &kind in &kinds {
			let callee = Callable {
				kind,
				name: key.clone(),
			};
			if !names.contains(&callee) {
				continue;
			}
			let bindings = match value {
				AstValue::Block { items, .. } => items
					.iter()
					.filter_map(|item| match item {
						AstStatement::Assignment {
							key,
							value: AstValue::Scalar { value, .. },
							..
						} => Some((key.clone(), value.as_text())),
						_ => None,
					})
					.collect(),
				_ => Vec::new(),
			};
			calls.push(Call {
				owner: owner.cloned(),
				callee,
				bindings,
				path: path.to_owned(),
				location: format!(
					"{mod_id}:{path}:{}:{}",
					key_span.start.line, key_span.start.column
				),
			});
		}
		if let AstValue::Block { items, .. } = value {
			parents.push(key.clone());
			collect_calls(items, path, mod_id, owner, parents, names, calls);
			parents.pop();
		}
	}
}

fn parameter_tokens(value: &str) -> impl Iterator<Item = &str> {
	value
		.split('$')
		.enumerate()
		.filter_map(|(index, part)| (index % 2 == 1).then_some(part))
}

fn parameter_names(value: &str) -> impl Iterator<Item = &str> {
	parameter_tokens(value)
		.map(|part| part.split('|').next().unwrap_or_default())
		.filter(|name| {
			!name.is_empty()
				&& name
					.bytes()
					.all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
		})
}

fn refers_to_changed(value: &str, mappings: &BTreeMap<String, String>) -> bool {
	mappings.contains_key(value)
		|| parameter_tokens(value).any(|part| {
			part.split_once('|')
				.is_some_and(|(_, default)| mappings.contains_key(default))
		})
}

#[cfg(test)]
mod tests {
	use super::{CallableKind, callable_kind};
	use crate::model::GamePath;

	#[test]
	fn only_txt_files_define_callables() {
		let path = |text| GamePath::new(text).expect("valid game path");
		assert!(matches!(
			callable_kind(path("common/scripted_effects/00_effects.txt")),
			Some(CallableKind::Effect)
		));
		assert!(callable_kind(path("common/scripted_effects/00_effects.txt.bak")).is_none());
		assert!(callable_kind(path("common/scripted_triggers/readme.md")).is_none());
	}
}