Add replacement support

This commit is contained in:
2025-05-22 10:26:19 +08:00
parent f64a702ef5
commit 3622cac8a8
6 changed files with 46 additions and 6 deletions

View File

@@ -80,6 +80,9 @@ pub struct ImportArgs {
#[arg(long)]
/// Name table file
pub name_csv: Option<String>,
#[arg(long)]
/// Replacement table file
pub replacement_json: Option<String>,
}
#[derive(Subcommand, Debug)]

View File

@@ -183,6 +183,7 @@ pub fn import_script(
imp_cfg: &args::ImportArgs,
is_dir: bool,
name_csv: Option<&std::collections::HashMap<String, String>>,
repl: Option<&types::ReplacementTable>,
) -> anyhow::Result<types::ScriptResult> {
eprintln!("Importing {}", filename);
let (script, builder) = parse_script(filename, arg, config)?;
@@ -253,7 +254,8 @@ pub fn import_script(
None => {}
}
format::fmt_message(&mut mes, fmt, *builder.script_type());
script.import_messages(mes, &patched_f, encoding)?;
script.import_messages(mes, &patched_f, encoding, repl)?;
Ok(types::ScriptResult::Ok)
}
@@ -312,6 +314,15 @@ fn main() {
}
None => None,
};
let repl = match &args.replacement_json {
Some(replacement_json) => {
let b = utils::files::read_file(replacement_json).unwrap();
let s = String::from_utf8(b).unwrap();
let table = serde_json::from_str::<types::ReplacementTable>(&s).unwrap();
Some(table)
}
None => None,
};
let (scripts, is_dir) =
utils::files::collect_files(&args.input, arg.recursive).unwrap();
if is_dir {
@@ -326,7 +337,15 @@ fn main() {
}
}
for script in scripts.iter() {
let re = import_script(&script, &arg, &cfg, args, is_dir, name_csv.as_ref());
let re = import_script(
&script,
&arg,
&cfg,
args,
is_dir,
name_csv.as_ref(),
repl.as_ref(),
);
match re {
Ok(s) => {
COUNTER.inc(s);

View File

@@ -32,5 +32,6 @@ pub trait Script: std::fmt::Debug {
messages: Vec<Message>,
filename: &str,
encoding: Encoding,
replacement: Option<&ReplacementTable>,
) -> Result<()>;
}

View File

@@ -141,6 +141,7 @@ impl Script for BGIScript {
_messages: Vec<Message>,
_filename: &str,
_encoding: Encoding,
_replacement: Option<&ReplacementTable>,
) -> Result<()> {
Ok(())
}

View File

@@ -228,17 +228,18 @@ impl Script for CircusMesScript {
messages: Vec<Message>,
filename: &str,
encoding: Encoding,
replacement: Option<&ReplacementTable>,
) -> Result<()> {
let mut repls = Vec::new();
if !encoding.is_jis() {
fn insert_repl(
repls: &mut Vec<(&'static str, String)>,
repls: &mut Vec<(String, String)>,
s: &'static str,
encoding: Encoding,
) -> Result<()> {
let jis = encode_string(Encoding::Cp932, s, true)?;
let out = decode_to_string(encoding, &jis)?;
repls.push((s, out));
repls.push((s.to_string(), out));
Ok(())
}
let _ = insert_repl(&mut repls, "", encoding);
@@ -251,6 +252,14 @@ impl Script for CircusMesScript {
crate::COUNTER.inc_warning();
}
}
match replacement {
Some(repl) => {
for (k, v) in repl.map.iter() {
repls.push((k.to_string(), v.to_string()));
}
}
None => {}
}
let mut buffer = Vec::with_capacity(self.data.len());
buffer.extend_from_slice(&self.data[..self.asm_bin_offset]);
let mut nmes = Vec::with_capacity(messages.len());
@@ -290,7 +299,7 @@ impl Script for CircusMesScript {
t
};
for i in repls.iter() {
s = s.replace(i.0, i.1.as_str());
s = s.replace(i.0.as_str(), i.1.as_str());
}
let mut text = encode_string(encoding, &s, false)?;
buffer.push(token.value);
@@ -323,7 +332,7 @@ impl Script for CircusMesScript {
t
};
for i in repls.iter() {
s = s.replace(i.0, i.1.as_str());
s = s.replace(i.0.as_str(), i.1.as_str());
}
buffer.push(token.value);
let text = encode_string(encoding, &s, false)?;

View File

@@ -1,5 +1,6 @@
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[serde(untagged, rename_all = "camelCase")]
@@ -235,3 +236,9 @@ pub struct NameTableCell {
#[serde(rename = "Count")]
pub count: usize,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ReplacementTable {
#[serde(flatten)]
pub map: HashMap<String, String>,
}