添加 BGI 脚本导入重复字符串的支持;新增 WriteAt 特性以支持在特定偏移写入数据

This commit is contained in:
2025-06-07 18:18:56 +08:00
parent 5533e901f7
commit f1e35c904c
6 changed files with 277 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ use crate::scripts::base::*;
use crate::types::*;
use crate::utils::encoding::{decode_to_string, encode_string};
use anyhow::Result;
use std::collections::BTreeMap;
#[derive(Debug)]
pub struct BGIScriptBuilder {}
@@ -52,6 +53,7 @@ pub struct BGIScript {
strings: Vec<BGIString>,
is_v1: bool,
offset: usize,
import_duplicate: bool,
}
impl std::fmt::Debug for BGIScript {
@@ -63,7 +65,7 @@ impl std::fmt::Debug for BGIScript {
}
impl BGIScript {
pub fn new(data: Vec<u8>, encoding: Encoding, _config: &ExtraConfig) -> Result<Self> {
pub fn new(data: Vec<u8>, encoding: Encoding, config: &ExtraConfig) -> Result<Self> {
let data = MemReader::new(data);
if data.data.starts_with(b"BurikoCompiledScriptVer1.00\0") {
let mut parser = V1Parser::new(data.to_ref(), encoding)?;
@@ -76,6 +78,7 @@ impl BGIScript {
strings,
is_v1: true,
offset,
import_duplicate: config.bgi_import_duplicate,
})
} else {
let mut parser = V0Parser::new(data.to_ref());
@@ -87,6 +90,7 @@ impl BGIScript {
strings,
is_v1: false,
offset: 0,
import_duplicate: config.bgi_import_duplicate,
})
}
}
@@ -138,11 +142,181 @@ impl Script for BGIScript {
fn import_messages<'a>(
&'a self,
_messages: Vec<Message>,
_filename: Box<dyn WriteSeek + 'a>,
_encoding: Encoding,
_replacement: Option<&'a ReplacementTable>,
messages: Vec<Message>,
mut file: Box<dyn WriteSeek + 'a>,
encoding: Encoding,
replacement: Option<&'a ReplacementTable>,
) -> Result<()> {
if !self.import_duplicate {
let mut str_map = BTreeMap::new();
let mut mes = messages.iter();
let mut cur_mes = mes.next();
for curs in self.strings.iter() {
if !curs.is_internal() {
if cur_mes.is_none() {
cur_mes = mes.next();
}
}
if str_map.contains_key(&curs.address) {
continue;
}
let nmes = match curs.typ {
BGIStringType::Internal => self.read_string(curs.address)?,
BGIStringType::Name => match &cur_mes {
Some(m) => {
if let Some(name) = &m.name {
let mut name = name.clone();
if let Some(replacement) = replacement {
for (key, value) in replacement.map.iter() {
name = name.replace(key, value);
}
}
name
} else {
return Err(anyhow::anyhow!("Name is missing for message."));
}
}
None => return Err(anyhow::anyhow!("No enough messages.")),
},
BGIStringType::Message => {
let mes = match &cur_mes {
Some(m) => {
let mut message = m.message.clone();
if let Some(replacement) = replacement {
for (key, value) in replacement.map.iter() {
message = message.replace(key, value);
}
}
message
}
None => return Err(anyhow::anyhow!("No enough messages.")),
};
cur_mes.take();
mes
}
};
str_map.insert(curs.address, nmes);
}
if cur_mes.is_some() || mes.next().is_some() {
return Err(anyhow::anyhow!("Some messages were not processed."));
}
let mut old_offset = 0;
let mut new_offset = 0;
let mut new_address_map = BTreeMap::new();
for (address, nmes) in str_map {
let bgi_str_old_offset = address + self.offset;
if old_offset < bgi_str_old_offset {
file.write_all(&self.data.data[old_offset..bgi_str_old_offset])?;
new_offset += bgi_str_old_offset - old_offset;
old_offset = bgi_str_old_offset;
}
let old_str_len = self
.data
.cpeek_cstring_at(bgi_str_old_offset)?
.as_bytes_with_nul()
.len();
let nmes = encode_string(encoding, &nmes, false)?;
file.write_all(&nmes)?;
file.write_u8(0)?; // null terminator
let new_address = new_offset - self.offset;
new_address_map.insert(address, new_address);
old_offset += old_str_len;
new_offset += nmes.len() + 1; // +1 for null terminator
}
if old_offset < self.data.data.len() {
file.write_all(&self.data.data[old_offset..])?;
}
for bgis in self.strings.iter() {
let new_address = new_address_map.get(&bgis.address).ok_or(anyhow::anyhow!(
"Address {} not found in new address map.",
bgis.address
))?;
file.write_u32_at(bgis.offset, *new_address as u32)?;
}
return Ok(());
}
let mut mes = messages.iter();
let mut cur_mes = None;
let mut strs = self.strings.iter();
let mut nstrs = Vec::new();
let mut cur_str = strs.next();
let mut old_offset = 0;
let mut new_offset = 0;
while let Some(curs) = cur_str {
if !curs.is_internal() {
if cur_mes.is_none() {
cur_mes = mes.next();
}
}
let bgi_str_old_offset = curs.address + self.offset;
if old_offset < bgi_str_old_offset {
file.write_all(&self.data.data[old_offset..bgi_str_old_offset])?;
new_offset += bgi_str_old_offset - old_offset;
old_offset = bgi_str_old_offset;
}
let old_str_len = self
.data
.cpeek_cstring_at(curs.address + self.offset)?
.as_bytes_with_nul()
.len();
let nmes = match curs.typ {
BGIStringType::Internal => self.read_string(curs.address)?,
BGIStringType::Name => match &cur_mes {
Some(m) => {
if let Some(name) = &m.name {
let mut name = name.clone();
if let Some(replacement) = replacement {
for (key, value) in replacement.map.iter() {
name = name.replace(key, value);
}
}
name
} else {
return Err(anyhow::anyhow!("Name is missing for message."));
}
}
None => return Err(anyhow::anyhow!("No enough messages.")),
},
BGIStringType::Message => {
let mes = match &cur_mes {
Some(m) => {
let mut message = m.message.clone();
if let Some(replacement) = replacement {
for (key, value) in replacement.map.iter() {
message = message.replace(key, value);
}
}
message
}
None => return Err(anyhow::anyhow!("No enough messages.")),
};
cur_mes.take();
mes
}
};
let nmes = encode_string(encoding, &nmes, false)?;
file.write_all(&nmes)?;
file.write_u8(0)?;
let new_str_len = nmes.len() + 1; // +1 for null terminator
let new_address = new_offset - self.offset;
nstrs.push(BGIString {
offset: curs.offset,
address: new_address,
typ: curs.typ.clone(),
});
old_offset += old_str_len;
new_offset += new_str_len;
cur_str = strs.next();
}
if cur_mes.is_some() || mes.next().is_some() {
return Err(anyhow::anyhow!("Some messages were not processed."));
}
for str in nstrs {
file.write_u32_at(str.offset, str.address as u32)?;
}
if old_offset < self.data.data.len() {
file.write_all(&self.data.data[old_offset..])?;
}
Ok(())
}
}