mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-07 13:28:47 +08:00
Add support to Entis GLS engine XML Script
This commit is contained in:
@@ -47,22 +47,19 @@ impl ScriptBuilder for SrcXmlScriptBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Entis GLS engine XML Script
|
||||
pub struct SrcXmlScript {
|
||||
decoded: String,
|
||||
handle: Handle,
|
||||
lang: Option<String>,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for SrcXmlScript {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("SrcXmlScript")
|
||||
.field("handle", &self.handle)
|
||||
.field("lang", &self.lang)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl SrcXmlScript {
|
||||
/// Creates a new `SrcXmlScript` from the provided buffer and encoding.
|
||||
///
|
||||
/// * `buf` - The buffer containing the XML data.
|
||||
/// * `encoding` - The encoding of the XML data.
|
||||
/// * `config` - Additional configuration options.
|
||||
pub fn new(buf: Vec<u8>, encoding: Encoding, config: &ExtraConfig) -> Result<Self> {
|
||||
let decoded = decode_to_string(encoding, &buf, false)?;
|
||||
let dom = parse_document(RcDom::default(), Default::default())
|
||||
@@ -76,7 +73,6 @@ impl SrcXmlScript {
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
decoded,
|
||||
handle: dom.document,
|
||||
lang: config.entis_gls_srcxml_lang.clone(),
|
||||
})
|
||||
@@ -175,19 +171,127 @@ impl Script for SrcXmlScript {
|
||||
|
||||
fn import_messages<'a>(
|
||||
&'a self,
|
||||
_messages: Vec<Message>,
|
||||
messages: Vec<Message>,
|
||||
mut file: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
_replacement: Option<&'a ReplacementTable>,
|
||||
replacement: Option<&'a ReplacementTable>,
|
||||
) -> Result<()> {
|
||||
let dom = parse_document(RcDom::default(), Default::default())
|
||||
.from_utf8()
|
||||
.one(self.decoded.as_bytes());
|
||||
let root = dom.document;
|
||||
let root = self.handle.deep_clone(None)?;
|
||||
if !encoding.is_utf8() {
|
||||
let mut childrens = root.children.try_borrow_mut()?;
|
||||
if childrens.len() > 1 && childrens[0].is_processing_instruction("xml") {
|
||||
childrens.remove(0);
|
||||
let len = root.children.try_borrow()?.len();
|
||||
if len > 0 && root.children.try_borrow()?[0].is_processing_instruction("xml") {
|
||||
root.change_child(0, |data| {
|
||||
data.set_processing_instruction_content("version=\"1.0\"")
|
||||
})?;
|
||||
}
|
||||
}
|
||||
let mut lang = self.lang.clone();
|
||||
let mut mess = messages.iter();
|
||||
let mut mes = mess.next();
|
||||
for i in root.children.try_borrow()?.iter() {
|
||||
if i.is_element("xscript") {
|
||||
for code in i.children.try_borrow()?.iter() {
|
||||
if code.is_element("code") {
|
||||
for ins in code.children.try_borrow()?.iter() {
|
||||
if ins.is_element("msg") {
|
||||
let m = match mes {
|
||||
Some(m) => m,
|
||||
None => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Not enough messages provided"
|
||||
));
|
||||
}
|
||||
};
|
||||
let lan = match lang.as_ref() {
|
||||
Some(l) => l.as_str(),
|
||||
None => {
|
||||
for attr in ins.element_attr_keys()? {
|
||||
if attr.starts_with("name_")
|
||||
|| attr.starts_with("text_")
|
||||
{
|
||||
lang = Some(attr[5..].to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if lang.is_none() {
|
||||
lang = Some(String::new());
|
||||
}
|
||||
lang.as_ref().map(|s| s.as_str()).unwrap_or("")
|
||||
}
|
||||
};
|
||||
let name_ref = if lan.is_empty() {
|
||||
"name"
|
||||
} else {
|
||||
&format!("name_{}", lan)
|
||||
};
|
||||
let name = match &m.name {
|
||||
Some(name) => {
|
||||
let mut name = name.clone();
|
||||
if let Some(repl) = replacement {
|
||||
for (k, v) in &repl.map {
|
||||
name = name.replace(k, v);
|
||||
}
|
||||
}
|
||||
name
|
||||
}
|
||||
None => String::new(),
|
||||
};
|
||||
ins.set_attr_value(name_ref, &name)?;
|
||||
let message = m.message.clone();
|
||||
let text_ref = if lan.is_empty() {
|
||||
"text"
|
||||
} else {
|
||||
&format!("text_{}", lan)
|
||||
};
|
||||
ins.set_attr_value(text_ref, &message)?;
|
||||
mes = mess.next();
|
||||
} else if ins.is_element("select") {
|
||||
for menu in ins.children.try_borrow()?.iter() {
|
||||
if menu.is_element("menu") {
|
||||
let m = match mes {
|
||||
Some(m) => m,
|
||||
None => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Not enough messages provided"
|
||||
));
|
||||
}
|
||||
};
|
||||
let lan = match lang.as_ref() {
|
||||
Some(l) => l.as_str(),
|
||||
None => {
|
||||
for attr in ins.element_attr_keys()? {
|
||||
if attr.starts_with("name_")
|
||||
|| attr.starts_with("text_")
|
||||
{
|
||||
lang = Some(attr[5..].to_string());
|
||||
break;
|
||||
}
|
||||
}
|
||||
if lang.is_none() {
|
||||
lang = Some(String::new());
|
||||
}
|
||||
lang.as_ref().map(|s| s.as_str()).unwrap_or("")
|
||||
}
|
||||
};
|
||||
let text_ref = if lan.is_empty() {
|
||||
"text"
|
||||
} else {
|
||||
&format!("text_{}", lan)
|
||||
};
|
||||
let mut message = m.message.clone();
|
||||
if let Some(repl) = replacement {
|
||||
for (k, v) in &repl.map {
|
||||
message = message.replace(k, v);
|
||||
}
|
||||
}
|
||||
menu.set_attr_value(text_ref, &message)?;
|
||||
mes = mess.next();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let doc: SerializableHandle = root.clone().into();
|
||||
|
||||
Reference in New Issue
Block a user