mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-06 12:58:45 +08:00
Add name table support
This commit is contained in:
22
Cargo.lock
generated
22
Cargo.lock
generated
@@ -110,6 +110,27 @@ version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||
|
||||
[[package]]
|
||||
name = "csv"
|
||||
version = "1.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
|
||||
dependencies = [
|
||||
"csv-core",
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "csv-core"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.35"
|
||||
@@ -155,6 +176,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"csv",
|
||||
"encoding_rs",
|
||||
"lazy_static",
|
||||
"serde",
|
||||
|
||||
@@ -6,6 +6,7 @@ edition = "2024"
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
clap = { version = "4.5", features = ["derive"] }
|
||||
csv = "1.3"
|
||||
encoding_rs = "0.8"
|
||||
lazy_static = "1.5.0"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
||||
@@ -77,6 +77,9 @@ pub struct ImportArgs {
|
||||
#[arg(long, action = ArgAction::SetTrue)]
|
||||
/// Keep original line breaks in patched script (for fixed format)
|
||||
pub patched_keep_original: bool,
|
||||
#[arg(long)]
|
||||
/// Name table file
|
||||
pub name_csv: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
|
||||
16
src/main.rs
16
src/main.rs
@@ -182,6 +182,7 @@ pub fn import_script(
|
||||
config: &types::ExtraConfig,
|
||||
imp_cfg: &args::ImportArgs,
|
||||
is_dir: bool,
|
||||
name_csv: Option<&std::collections::HashMap<String, String>>,
|
||||
) -> anyhow::Result<types::ScriptResult> {
|
||||
eprintln!("Importing {}", filename);
|
||||
let (script, builder) = parse_script(filename, arg, config)?;
|
||||
@@ -245,6 +246,12 @@ pub fn import_script(
|
||||
},
|
||||
None => script.default_format_type(),
|
||||
};
|
||||
match name_csv {
|
||||
Some(name_table) => {
|
||||
utils::name_replacement::replace_message(&mut mes, name_table);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
format::fmt_message(&mut mes, fmt);
|
||||
script.import_messages(mes, &patched_f, encoding)?;
|
||||
Ok(types::ScriptResult::Ok)
|
||||
@@ -295,6 +302,13 @@ fn main() {
|
||||
}
|
||||
}
|
||||
args::Command::Import(args) => {
|
||||
let name_csv = match &args.name_csv {
|
||||
Some(name_csv) => {
|
||||
let name_table = utils::name_replacement::read_csv(name_csv).unwrap();
|
||||
Some(name_table)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let (scripts, is_dir) =
|
||||
utils::files::collect_files(&args.input, arg.recursive).unwrap();
|
||||
if is_dir {
|
||||
@@ -309,7 +323,7 @@ fn main() {
|
||||
}
|
||||
}
|
||||
for script in scripts.iter() {
|
||||
let re = import_script(&script, &arg, &cfg, args, is_dir);
|
||||
let re = import_script(&script, &arg, &cfg, args, is_dir, name_csv.as_ref());
|
||||
match re {
|
||||
Ok(s) => {
|
||||
counter.inc(s);
|
||||
|
||||
10
src/types.rs
10
src/types.rs
@@ -222,3 +222,13 @@ pub enum FormatOptions {
|
||||
/// Do not wrap line
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct NameTableCell {
|
||||
#[serde(rename = "JP_Name")]
|
||||
pub jp_name: String,
|
||||
#[serde(rename = "CN_Name")]
|
||||
pub cn_name: String,
|
||||
#[serde(rename = "Count")]
|
||||
pub count: usize,
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ pub mod encoding;
|
||||
#[cfg(windows)]
|
||||
mod encoding_win;
|
||||
pub mod files;
|
||||
pub mod name_replacement;
|
||||
|
||||
28
src/utils/name_replacement.rs
Normal file
28
src/utils/name_replacement.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use crate::types::*;
|
||||
use anyhow::Result;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn read_csv(path: &str) -> Result<HashMap<String, String>> {
|
||||
let mut reader = csv::ReaderBuilder::new()
|
||||
.has_headers(true)
|
||||
.from_path(path)?;
|
||||
let mut map = HashMap::new();
|
||||
for result in reader.deserialize() {
|
||||
let record: NameTableCell = result?;
|
||||
if record.jp_name.is_empty() || record.cn_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
map.insert(record.jp_name, record.cn_name);
|
||||
}
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
pub fn replace_message(mes: &mut Vec<Message>, name_table: &HashMap<String, String>) {
|
||||
for message in mes.iter_mut() {
|
||||
if let Some(name) = &message.name {
|
||||
if let Some(replace_name) = name_table.get(name) {
|
||||
message.name = Some(replace_name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user