From 57fa1aa6f6a1dcae7fdf3ed79aa4c15f5f3fea4e Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 31 Jan 2026 20:57:37 +0800 Subject: [PATCH] Fix name may occur in text block in old version ast file --- src/scripts/artemis/ast/mod.rs | 42 ++++++++++++++++++++++++++++++-- src/scripts/artemis/ast/types.rs | 5 ++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/scripts/artemis/ast/mod.rs b/src/scripts/artemis/ast/mod.rs index 5d69c92..a0326c8 100644 --- a/src/scripts/artemis/ast/mod.rs +++ b/src/scripts/artemis/ast/mod.rs @@ -173,10 +173,21 @@ impl Script for AstScript { } else { None } + } else if te["name"].is_array() && &te["name"][0] == "name" { + // Some scripts put the name block inside the text block + if let Some(n) = te["name"]["name"].as_string() { + Some(n) + } else { + None + } } else { None }; for item in te.members() { + // Some scripts contain non-array items, skip them (such as name block) + if !item.is_array() { + continue; + } let message = text::TextGenerator::new().generate(item)?; messages.push(Message { name: nam.clone(), @@ -403,6 +414,7 @@ impl Script for AstScript { } } }; + let mut name_find = false; if root["text"][NumKey(text_index)]["name"].is_array() { let name = match mes { Some(m) => m.name.clone(), @@ -419,7 +431,9 @@ impl Script for AstScript { } let nlan = self.lang.as_ref().map(|s| s.as_str()).unwrap_or("name"); root["text"][NumKey(text_index)]["name"][nlan].set_string(name); + name_find = true; } + let mut arr = Value::new_array(); let origin_count = { let text = &root["text"][NumKey(text_index)]; let mut tex = &text[lan]; @@ -431,9 +445,33 @@ impl Script for AstScript { } } } - tex.len() + let arr_len = tex.arr_len(); + if arr_len < tex.len() { + for item in tex.members() { + if !item.is_array() { + // Copy non-array items (such as name block) back to the array + arr.push_member(item.clone()); + } + } + } + arr_len }; - let mut arr = Value::new_array(); + if !name_find && arr["name"].is_array() && &arr["name"][0] == "name" { + let name = match mes { + Some(m) => m.name.clone(), + None => return Err(anyhow::anyhow!("Message name is missing.")), + }; + let mut name = match name { + Some(n) => n, + None => return Err(anyhow::anyhow!("Message name is missing.")), + }; + if let Some(repl) = replacement { + for (k, v) in &repl.map { + name = name.replace(k, v); + } + } + arr["name"]["name"].set_string(name); + } for _ in 0..origin_count { let m = match mes { Some(m) => m, diff --git a/src/scripts/artemis/ast/types.rs b/src/scripts/artemis/ast/types.rs index 60827c4..6faa79d 100644 --- a/src/scripts/artemis/ast/types.rs +++ b/src/scripts/artemis/ast/types.rs @@ -206,6 +206,11 @@ impl Value { } } + /// Returns the number of array members in the array. + pub fn arr_len(&self) -> usize { + self.members().filter(|s| s.is_array()).count() + } + /// Inserts a member at the specified index in the array. /// /// If the index is out of bounds, it appends the value to the end of the array.