mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-19 11:25:56 +00:00
Keep SQUID settings usable with unknown metadata and preserve translations
This commit is contained in:
1 parent
f8301d7394
commit
654b7cddd3
6 files changed
+60
-16
No files matched your search
@@ -17,9 +17,12 @@ pub fn release(root: &Path) -> Option<Release> {
|
||||
if bytes.len() > 1024 * 1024 { continue; }
|
||||
let Ok(json) = serde_json::from_slice::<serde_json::Value>(&bytes) else { continue; };
|
||||
let safe = |key: &str| json.get(key).and_then(|value| value.as_str())
|
||||
.filter(|value| value.len() <= 80 && value.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))).map(str::to_owned);
|
||||
.filter(|value| !value.is_empty() && value.len() <= 80 && value.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))).map(str::to_owned);
|
||||
let tag = safe("tag").or_else(|| safe("version"));
|
||||
let commit = safe("commit").filter(|value| value.len() >= 7 && value.len() <= 40 && value.chars().all(|c| c.is_ascii_hexdigit()));
|
||||
let revision = if name == "CoA-Bots/release.json" {
|
||||
json.get("botsModuleRevision").and_then(|value| value.as_str()).and_then(|value| value.split_whitespace().next()).map(str::to_owned).or_else(|| safe("commit"))
|
||||
} else if name.ends_with(".settings.json") { None } else { safe("commit") };
|
||||
let commit = revision.filter(|value| value.len() >= 7 && value.len() <= 40 && value.chars().all(|c| c.is_ascii_hexdigit()));
|
||||
if tag.is_some() || commit.is_some() { return Some(Release { tag, commit }); }
|
||||
}
|
||||
None
|
||||
@@ -46,7 +49,7 @@ pub struct Field {
|
||||
#[derive(Deserialize)]
|
||||
struct Settings {
|
||||
format: u32,
|
||||
settings: Vec<Field>,
|
||||
settings: Vec<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
groups: std::collections::BTreeMap<String, String>,
|
||||
}
|
||||
@@ -54,26 +57,36 @@ struct Settings {
|
||||
pub fn fields(root: &Path) -> crate::Result<std::collections::BTreeMap<String, Field>> {
|
||||
let path = root.join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
if !path.is_file() { return Ok(Default::default()); }
|
||||
let bytes = std::fs::read(path)?;
|
||||
let invalid = || crate::Error::Invalid("The SquidBots settings JSON is invalid or uses an unsupported format.".into());
|
||||
if bytes.len() > 1024 * 1024 { return Err(invalid()); }
|
||||
let settings: Settings = serde_json::from_slice(&bytes).map_err(|_| invalid())?;
|
||||
if settings.format != 1 { return Err(invalid()); }
|
||||
let Ok(bytes) = std::fs::read(path) else { return Ok(Default::default()); };
|
||||
if bytes.len() > 1024 * 1024 { return Ok(Default::default()); }
|
||||
let Ok(settings) = serde_json::from_slice::<Settings>(&bytes) else { return Ok(Default::default()); };
|
||||
if settings.format != 1 { return Ok(Default::default()); }
|
||||
let mut fields = std::collections::BTreeMap::new();
|
||||
for mut field in settings.settings {
|
||||
for value in settings.settings {
|
||||
let Ok(mut field) = serde_json::from_value::<Field>(value) else { continue; };
|
||||
field.group_title = settings.groups.get(&field.group).cloned();
|
||||
if !matches!(field.kind.as_str(), "bool" | "int" | "float" | "string")
|
||||
|| field.key.len() > 200 || field.key.is_empty()
|
||||
|| !field.key.chars().all(|c| c.is_ascii_alphanumeric() || "._".contains(c))
|
||||
|| field.min.zip(field.max).is_some_and(|(min,max)| min > max)
|
||||
|| scalar(&field.default).is_none()
|
||||
|| field.default_if_missing.as_ref().is_some_and(|value| scalar(value).is_none())
|
||||
|| fields.contains_key(&field.key) { return Err(invalid()); }
|
||||
|| !default_type(&field.kind, &field.default)
|
||||
|| field.default_if_missing.as_ref().is_some_and(|value| !default_type(&field.kind, value))
|
||||
|| fields.contains_key(&field.key) { continue; }
|
||||
fields.insert(field.key.clone(), field);
|
||||
}
|
||||
Ok(fields)
|
||||
}
|
||||
|
||||
fn default_type(kind: &str, value: &serde_json::Value) -> bool {
|
||||
match kind {
|
||||
"bool" => value.is_boolean(),
|
||||
"int" => value.is_i64() || value.is_u64(),
|
||||
"float" => value.is_number(),
|
||||
"string" => value.is_string(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scalar(value: &serde_json::Value) -> Option<String> {
|
||||
match value {
|
||||
serde_json::Value::Bool(value) => Some(if *value { "1" } else { "0" }.into()),
|
||||
@@ -109,6 +122,29 @@ mod tests {
|
||||
assert!(validate(field, "5000").is_ok());
|
||||
for value in ["5001", "-1", "0.5", "NaN"] { assert!(validate(field, value).is_err()); }
|
||||
}
|
||||
#[test]
|
||||
fn unsupported_metadata_falls_back_and_bad_entries_do_not_hide_valid_fields() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let path = root.path().join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
for data in [r#"{"format":99,"settings":[]}"#, "invalid json", r#"{"format":1,"settings":{}}"#] {
|
||||
crate::fsx::atomic_write(&path, data.as_bytes()).unwrap();
|
||||
assert!(fields(root.path()).unwrap().is_empty());
|
||||
}
|
||||
crate::fsx::atomic_write(&path, br#"{"format":1,"settings":[{"key":"Broken","type":"enum","title":"Broken","description":"Unknown type","default":1},{"key":"Malformed"},{"key":"WrongDefault","type":"int","title":"Wrong","description":"Bad default","default":"oops"},{"key":"Good","type":"bool","title":"Good","description":"Good field","default":true}]}"#).unwrap();
|
||||
let parsed = fields(root.path()).unwrap();
|
||||
assert_eq!(parsed.len(), 1);
|
||||
assert!(parsed.contains_key("Good"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn imported_revision_uses_its_first_word_and_empty_tags_are_hidden() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
crate::fsx::atomic_write(&root.path().join("CoA-Bots/release.json"), br#"{"tag":"","botsModuleRevision":"48c4786a 2026-10-01 (coa branch)","commit":"abcdef01"}"#).unwrap();
|
||||
assert_eq!(release(root.path()).unwrap(), Release { tag: None, commit: Some("48c4786a".into()) });
|
||||
crate::fsx::atomic_write(&root.path().join("Core/configs/modules/playerbots.conf.settings.json"), br#"{"tag":"v1.8.1","commit":"11111111"}"#).unwrap();
|
||||
assert_eq!(release(root.path()).unwrap(), Release { tag: Some("v1.8.1".into()), commit: None });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diagnostics_only_expose_release_identifiers() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user