Add support to decode/encode 0xff in JIS on all platform

This commit is contained in:
2025-08-12 22:54:20 +08:00
parent 9d184bd2d6
commit b9ee9d1e42
5 changed files with 161 additions and 79 deletions

View File

@@ -337,6 +337,7 @@ pub struct Ws2DisasmScript {
addresses: Vec<usize>,
/// Need encrypt when outputting
encrypted: bool,
encoding: Encoding,
}
impl Ws2DisasmScript {
@@ -352,13 +353,14 @@ impl Ws2DisasmScript {
config: &ExtraConfig,
decrypted: bool,
) -> Result<Self> {
match disassmble(&buf, encoding) {
match disassmble(&buf) {
Ok((addresses, texts)) => {
return Ok(Self {
data: MemReader::new(buf.to_vec()),
texts,
addresses,
encrypted: decrypted,
encoding,
});
}
Err(e) => {
@@ -389,15 +391,16 @@ impl Script for Ws2DisasmScript {
for text in &self.texts {
match text.typ {
StringType::Name => {
let text = text
.text
let text = decode_to_string(self.encoding, text.text.as_bytes(), false)?
.trim_start_matches("%LC")
.trim_start_matches("%LF")
.to_string();
name = Some(text);
}
StringType::Message => {
let message = text.text.trim_end_matches("%K%P").to_string();
let message = decode_to_string(self.encoding, text.text.as_bytes(), false)?
.trim_end_matches("%K%P")
.to_string();
messages.push(Message {
message,
name: name.take(),
@@ -431,11 +434,11 @@ impl Script for Ws2DisasmScript {
|s| Ok(s),
)?;
for s in &self.texts {
let text = match s.typ {
let mut encoded = match s.typ {
StringType::Name => {
let prefix = if s.text.starts_with("%LC") {
let prefix = if s.text.as_bytes().starts_with(b"%LC") {
"%LC"
} else if s.text.starts_with("%LF") {
} else if s.text.as_bytes().starts_with(b"%LF") {
"%LF"
} else {
""
@@ -456,10 +459,14 @@ impl Script for Ws2DisasmScript {
}
}
name = prefix.to_owned() + &name;
name
encode_string(encoding, &name, false)?
}
StringType::Message => {
let suffix = if s.text.ends_with("%K%P") { "%K%P" } else { "" };
let suffix = if s.text.as_bytes().ends_with(b"%K%P") {
"%K%P"
} else {
""
};
let m = match mess {
Some(m) => m,
None => {
@@ -473,11 +480,11 @@ impl Script for Ws2DisasmScript {
}
}
mess = mes.next();
message + suffix
message.push_str(suffix);
encode_string(encoding, &message, false)?
}
StringType::Internal => s.text.clone(),
StringType::Internal => s.text.as_bytes().to_vec(),
};
let mut encoded = encode_string(encoding, &text, false)?;
encoded.push(0); // Null terminator
patcher.copy_up_to(s.offset as u64)?;
patcher.replace_bytes(s.len as u64, &encoded)?;

View File

@@ -1,8 +1,7 @@
use crate::ext::io::*;
use crate::types::*;
use crate::utils::encoding::*;
use anyhow::Result;
use std::any::Any;
use std::ffi::CString;
pub trait Disasm: Sized {
fn disassmble(self) -> Result<(Vec<usize>, Vec<Ws2DString>)>;
@@ -36,7 +35,7 @@ pub enum StringType {
#[derive(Debug, Clone)]
pub struct Ws2DString {
pub text: String,
pub text: CString,
pub offset: usize,
pub len: usize,
pub typ: StringType,
@@ -47,17 +46,15 @@ struct DisasmBase<'a> {
opers: &'a [(u8, &'static [Oper])],
addresses: Vec<usize>,
texts: Vec<Ws2DString>,
encoding: Encoding,
}
impl<'a> DisasmBase<'a> {
pub fn new(data: &'a [u8], opers: &'a [(u8, &'static [Oper])], encoding: Encoding) -> Self {
pub fn new(data: &'a [u8], opers: &'a [(u8, &'static [Oper])]) -> Self {
DisasmBase {
reader: MemReaderRef::new(data),
opers,
addresses: Vec::new(),
texts: Vec::new(),
encoding,
}
}
@@ -120,10 +117,9 @@ impl<'a> DisasmBase<'a> {
S => {
let offset = self.reader.pos;
let s = self.reader.read_cstring()?;
let decoded = decode_to_string(self.encoding, s.as_bytes(), false)?;
let len = s.as_bytes_with_nul().len();
let str = Ws2DString {
text: decoded,
text: s,
offset,
len,
typ: StringType::Internal,
@@ -643,9 +639,9 @@ const V3_OPS: [(u8, &'static [Oper]); 165] = [
const OPS: [&[(u8, &'static [Oper])]; 3] = [&V1_OPS, &V2_OPS, &V3_OPS];
pub fn disassmble(data: &[u8], encoding: Encoding) -> Result<(Vec<usize>, Vec<Ws2DString>)> {
pub fn disassmble(data: &[u8]) -> Result<(Vec<usize>, Vec<Ws2DString>)> {
for op in &OPS {
let disasm = DisasmBase::new(data, op, encoding);
let disasm = DisasmBase::new(data, op);
match disasm.disassmble() {
Ok(result) => return Ok(result),
Err(_) => continue, // Try the next version if this one fails