From e794d558a8d6b1ced67b579ae07291801f1d5754 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 20 Sep 2025 14:24:47 +0800 Subject: [PATCH] Add new options --- src/args.rs | 4 ++++ src/main.rs | 2 ++ src/scripts/softpal/scr/mod.rs | 28 +++++++++++++++++++++++++--- src/types.rs | 3 +++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/args.rs b/src/args.rs index 35d4ace..797f3f2 100644 --- a/src/args.rs +++ b/src/args.rs @@ -476,6 +476,10 @@ pub struct Arg { #[arg(long, global = true)] /// Disable multiple messages section support. pub no_multi_message: bool, + #[cfg(feature = "softpal")] + #[arg(long, global = true, visible_alias = "softpal-idx")] + /// Whether to add message index to Softpal src script when exporting. + pub softpal_add_message_index: bool, #[command(subcommand)] /// Command pub command: Command, diff --git a/src/main.rs b/src/main.rs index 5f38fc8..4051dd7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2734,6 +2734,8 @@ fn main() { psb_process_tlg: !arg.psb_no_process_tlg, #[cfg(feature = "softpal-img")] pgd_fake_compress: !arg.pgd_compress, + #[cfg(feature = "softpal-img")] + softpal_add_message_index: arg.softpal_add_message_index, }); match &arg.command { args::Command::Export { input, output } => { diff --git a/src/scripts/softpal/scr/mod.rs b/src/scripts/softpal/scr/mod.rs index b1e8e6d..6847d7b 100644 --- a/src/scripts/softpal/scr/mod.rs +++ b/src/scripts/softpal/scr/mod.rs @@ -64,6 +64,7 @@ pub struct SoftpalScript { texts: MemReader, encoding: Encoding, label_offsets: Vec, + add_message_index: bool, } impl SoftpalScript { @@ -72,7 +73,7 @@ impl SoftpalScript { buf: Vec, filename: &str, encoding: Encoding, - _config: &ExtraConfig, + config: &ExtraConfig, archive: Option<&Box>, ) -> Result { let texts = Self::load_texts_data(Self::load_file(filename, archive, "TEXT.DAT")?)?; @@ -85,6 +86,7 @@ impl SoftpalScript { encoding, texts, label_offsets, + add_message_index: config.softpal_add_message_index, }) } @@ -157,11 +159,21 @@ impl Script for SoftpalScript { fn extract_messages(&self) -> Result> { let mut messages = Vec::new(); let mut name = None; + let max_len = self.texts.data.len() as u32; for str in &self.strs { let addr = self.data.cpeek_u32_at(str.offset as u64)?; + if addr - 4 > max_len { + continue; + } + let idx = self.texts.cpeek_u32_at(addr as u64)?; let text = self.texts.cpeek_cstring_at(addr as u64 + 4)?; let text = decode_to_string(self.encoding, text.as_bytes(), false)?.replace("
", "\n"); + let text = if self.add_message_index { + format!("[{}]{}", idx, text) + } else { + text + }; match str.typ { StringType::Name => { if text.is_empty() { @@ -186,11 +198,21 @@ impl Script for SoftpalScript { let mut label = None; let mut name = None; let mut result = HashMap::new(); + let max_len = self.texts.data.len() as u32; for str in &self.strs { let addr = self.data.cpeek_u32_at(str.offset as u64)?; + if addr - 4 > max_len { + continue; + } + let idx = self.texts.cpeek_u32_at(addr as u64)?; let text = self.texts.cpeek_cstring_at(addr as u64 + 4)?; - let text = + let ptext = decode_to_string(self.encoding, text.as_bytes(), false)?.replace("
", "\n"); + let text = if self.add_message_index { + format!("[{}]{}", idx, ptext) + } else { + ptext.clone() + }; match str.typ { StringType::Name => { if text.is_empty() { @@ -213,7 +235,7 @@ impl Script for SoftpalScript { result.insert(key, messages); messages = Vec::new(); } - label = Some(text); + label = Some(ptext); } } } diff --git a/src/types.rs b/src/types.rs index fd4d596..63043a8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -454,6 +454,9 @@ pub struct ExtraConfig { /// Whether to use fake compression for Softpal Pgd images. Enabled by default. /// WARN: Compress may cause image broken. pub pgd_fake_compress: bool, + #[cfg(feature = "softpal")] + /// Whether to add message index to Softpal src script when exporting. + pub softpal_add_message_index: bool, } #[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]