Merge current master into portable characters while retaining 0.7.0

This commit is contained in:
Дмитрий committed 2026-10-19 13:47:05 +05:00
commit f40c0e1324
114 files changed
+6694 -600

No files matched your search

+193 -15
View File
@@ -130,7 +130,49 @@ fn conf_name(root: &Path, e: &Entry) -> String {
}
}
fn installed_module_commits(root: &Path) -> BTreeMap<String, String> {
let Ok(directory) = crate::registry::metadata_dir_for(root) else {
return Default::default();
};
let Ok((_, meta)) = crate::registry::MetaDir::open(&directory) else {
return Default::default();
};
let Some(version) = meta.core.version else {
return Default::default();
};
let candidates = [
directory
.join("manifests")
.join(format!("update-{version}.json")),
directory.join("manifests/base.json"),
];
for path in candidates {
let Ok(manifest) = crate::fsx::read_json::<serde_json::Value>(&path) else {
continue;
};
if manifest.get("version").and_then(|value| value.as_str()) != Some(&version) {
continue;
}
if let Some(commits) = manifest
.get("compatibility")
.and_then(|value| value.get("moduleCommits"))
.and_then(|value| value.as_object())
{
return commits
.iter()
.filter_map(|(name, commit)| {
let commit = commit.as_str()?;
(commit.len() == 40 && commit.bytes().all(|byte| byte.is_ascii_hexdigit()))
.then(|| (name.clone(), commit.chars().take(8).collect()))
})
.collect();
}
}
Default::default()
}
fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
let installed_commits = installed_module_commits(root);
let wildcard = crate::realms::state(root)
.map(|s| s.active == crate::realms::Mode::Wildcard)
.unwrap_or(true);
@@ -172,6 +214,18 @@ fn list_in(cat: &[Entry], root: &Path) -> Vec<ModuleView> {
} else {
None
};
let key = match e.id.as_str() {
"companions" => "bots",
"playerbots" => "squid",
"content-scaling" => "scaling",
"custom-races" => "races",
id => id,
};
let version = if installed {
version.or_else(|| installed_commits.get(key).cloned())
} else {
None
};
ModuleView {
version,
compatibility: if blocked {
@@ -277,6 +331,17 @@ pub fn set_enabled(root: &Path, meta: &Path, id: &str, on: bool) -> Result<()> {
}
}
}
if !on {
let _ = crate::custom_races::sync_bot_races_config(root, false);
} else {
let allow =
crate::custom_races::is_bot_custom_races_allowed(root).unwrap_or_else(|| {
read(&dir(root).join("coa_custom_races.conf"))
.and_then(|c| c.get("CoACustomRaces.BotsEnable").map(truthy))
.unwrap_or(false)
});
let _ = crate::custom_races::sync_bot_races_config(root, allow);
}
}
set_enabled_in(&catalog(), root, meta, id, on)
}
@@ -383,27 +448,40 @@ fn settings_in(cat: &[Entry], root: &Path, id: &str) -> Result<Vec<Setting>> {
}
}
}
let bot_custom_races_allowed = if id == "custom-races" {
crate::custom_races::is_bot_custom_races_allowed(root)
} else {
None
};
Ok(effective
.entries()
.map(|(k, v)| Setting {
key: k.to_string(),
value: fields
.map(|(k, v)| {
let mut val = fields
.get(k)
.and_then(|field| field.default_if_missing.as_ref())
.filter(|_| active.get(k).is_none())
.and_then(crate::squid::scalar)
.unwrap_or_else(|| v.to_string()),
default: fields
.get(k)
.and_then(|field| crate::squid::scalar(&field.default))
.or_else(|| dist.as_ref().and_then(|d| d.get(k)).map(str::to_string)),
field: fields.get(k).cloned(),
doc: dist
.as_ref()
.map(|d| d.doc_for(k))
.filter(|d| !d.is_empty())
.unwrap_or_else(|| active.doc_for(k))
.join(" "),
.unwrap_or_else(|| v.to_string());
if k == "CoACustomRaces.BotsEnable" {
if let Some(allowed) = bot_custom_races_allowed {
val = if allowed { "1".into() } else { "0".into() };
}
}
Setting {
key: k.to_string(),
value: val,
default: fields
.get(k)
.and_then(|field| crate::squid::scalar(&field.default))
.or_else(|| dist.as_ref().and_then(|d| d.get(k)).map(str::to_string)),
field: fields.get(k).cloned(),
doc: dist
.as_ref()
.map(|d| d.doc_for(k))
.filter(|d| !d.is_empty())
.unwrap_or_else(|| active.doc_for(k))
.join(" "),
}
})
.collect())
}
@@ -508,6 +586,12 @@ fn save_settings_in(
changed.push((key.clone(), value.to_string()));
}
}
if id == "custom-races" {
if let Some(bots_enable) = changes.get("CoACustomRaces.BotsEnable") {
let allow = truthy(bots_enable);
crate::custom_races::sync_bot_races_config(root, allow)?;
}
}
if changed.is_empty() {
return Ok(Vec::new());
}
@@ -567,6 +651,30 @@ mod tests {
(root, meta)
}
#[test]
fn module_revisions_come_from_the_installed_release_only() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("server");
fs::create_dir_all(&root).unwrap();
let mut meta =
crate::registry::InstallMeta::new(crate::registry::InstallKind::Imported, &root);
meta.core.version = Some("2.0.0".into());
let directory = crate::registry::MetaDir::create(&root, &meta).unwrap();
let value = serde_json::json!({"version": "2.0.0", "compatibility": {"moduleCommits": {"races": "a".repeat(40), "bots": "b".repeat(40)}}});
fsx::atomic_write_json(&directory.root.join("manifests/update-2.0.0.json"), &value)
.unwrap();
assert_eq!(
installed_module_commits(&root)
.get("races")
.map(String::as_str),
Some("aaaaaaaa")
);
let stale = serde_json::json!({"version": "1.0.0", "compatibility": {"moduleCommits": {"races": "c".repeat(40)}}});
fsx::atomic_write_json(&directory.root.join("manifests/update-2.0.0.json"), &stale)
.unwrap();
assert!(installed_module_commits(&root).is_empty());
}
#[test]
fn squid_missing_keys_use_code_defaults_and_saves_enforce_json_ranges() {
let d = tempfile::tempdir().unwrap();
@@ -1148,4 +1256,74 @@ mod tests {
.enabled
);
}
#[test]
fn custom_races_bot_settings_synchronize_playerbots_conf() {
let d = tempfile::tempdir().unwrap();
let (root, meta) = server(d.path());
let cat = catalog();
fs::write(
dir(&root).join("coa_custom_races.conf.dist"),
"CoACustomRaces.Enable = 1\nCoACustomRaces.BotsEnable = 0\n",
)
.unwrap();
fs::write(
dir(&root).join("coa_custom_races.conf"),
"CoACustomRaces.Enable = 1\nCoACustomRaces.BotsEnable = 0\n",
)
.unwrap();
fs::write(
dir(&root).join("playerbots.conf"),
"AiPlayerbot.Enabled = 1\n",
)
.unwrap();
// 1. Initially CoACustomRaces.BotsEnable is 0
let s = settings_in(&cat, &root, "custom-races").unwrap();
assert_eq!(
s.iter()
.find(|s| s.key == "CoACustomRaces.BotsEnable")
.unwrap()
.value,
"0"
);
// 2. Enable bots for custom races: should write experimental exclusions to playerbots.conf
let change_on = BTreeMap::from([("CoACustomRaces.BotsEnable".into(), "1".into())]);
assert_eq!(
save_settings_in(&cat, &root, &meta, "custom-races", &change_on).unwrap(),
["CoACustomRaces.BotsEnable"]
);
let pb = fs::read_to_string(dir(&root).join("playerbots.conf")).unwrap();
assert!(pb.contains("AiPlayerbot.ExcludedBotRaces"));
assert!(pb.contains(crate::custom_races::EXCLUDED_BOT_RACES_EXPERIMENTAL));
// Settings view now sees "1"
let s = settings_in(&cat, &root, "custom-races").unwrap();
assert_eq!(
s.iter()
.find(|s| s.key == "CoACustomRaces.BotsEnable")
.unwrap()
.value,
"1"
);
// 3. Disable bots for custom races: should write recommended exclusions to playerbots.conf
let change_off = BTreeMap::from([("CoACustomRaces.BotsEnable".into(), "0".into())]);
assert_eq!(
save_settings_in(&cat, &root, &meta, "custom-races", &change_off).unwrap(),
["CoACustomRaces.BotsEnable"]
);
let pb = fs::read_to_string(dir(&root).join("playerbots.conf")).unwrap();
assert!(pb.contains(crate::custom_races::EXCLUDED_BOT_RACES_RECOMMENDED));
// Settings view now sees "0"
let s = settings_in(&cat, &root, "custom-races").unwrap();
assert_eq!(
s.iter()
.find(|s| s.key == "CoACustomRaces.BotsEnable")
.unwrap()
.value,
"0"
);
}
}