2022-01-17 22:29:30 +00:00
|
|
|
use std::fmt::Write;
|
2021-10-15 14:36:05 +00:00
|
|
|
use std::io::Cursor;
|
|
|
|
|
|
|
|
use num_traits::FromPrimitive;
|
|
|
|
|
|
|
|
use crate::framework::error::GameError::InvalidValue;
|
|
|
|
use crate::framework::error::GameResult;
|
|
|
|
use crate::scripting::tsc::bytecode_utils::read_cur_varint;
|
2021-10-16 02:37:42 +00:00
|
|
|
use crate::scripting::tsc::opcodes::TSCOpCode;
|
2021-10-15 14:36:05 +00:00
|
|
|
use crate::scripting::tsc::text_script::TextScript;
|
|
|
|
|
|
|
|
impl TextScript {
|
|
|
|
pub fn decompile_event(&self, id: u16) -> GameResult<String> {
|
|
|
|
if let Some(bytecode) = self.event_map.get(&id) {
|
|
|
|
let mut result = String::new();
|
2022-01-17 22:29:30 +00:00
|
|
|
let mut cursor: Cursor<&[u8]> = Cursor::new(bytecode);
|
2021-10-15 14:36:05 +00:00
|
|
|
|
|
|
|
while let Ok(op_num) = read_cur_varint(&mut cursor) {
|
2021-10-16 02:37:42 +00:00
|
|
|
let op_maybe: Option<TSCOpCode> = FromPrimitive::from_i32(op_num);
|
2021-10-15 14:36:05 +00:00
|
|
|
|
|
|
|
if let Some(op) = op_maybe {
|
|
|
|
match op {
|
|
|
|
// Zero operand codes
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::AEp
|
|
|
|
| TSCOpCode::CAT
|
|
|
|
| TSCOpCode::CIL
|
|
|
|
| TSCOpCode::CLO
|
|
|
|
| TSCOpCode::CLR
|
|
|
|
| TSCOpCode::CPS
|
|
|
|
| TSCOpCode::CRE
|
|
|
|
| TSCOpCode::CSS
|
|
|
|
| TSCOpCode::END
|
|
|
|
| TSCOpCode::ESC
|
|
|
|
| TSCOpCode::FLA
|
|
|
|
| TSCOpCode::FMU
|
|
|
|
| TSCOpCode::FRE
|
|
|
|
| TSCOpCode::HMC
|
|
|
|
| TSCOpCode::INI
|
|
|
|
| TSCOpCode::KEY
|
|
|
|
| TSCOpCode::LDP
|
|
|
|
| TSCOpCode::MLP
|
|
|
|
| TSCOpCode::MM0
|
|
|
|
| TSCOpCode::MNA
|
|
|
|
| TSCOpCode::MS2
|
|
|
|
| TSCOpCode::MS3
|
|
|
|
| TSCOpCode::MSG
|
|
|
|
| TSCOpCode::NOD
|
|
|
|
| TSCOpCode::PRI
|
|
|
|
| TSCOpCode::RMU
|
|
|
|
| TSCOpCode::SAT
|
|
|
|
| TSCOpCode::SLP
|
|
|
|
| TSCOpCode::SMC
|
|
|
|
| TSCOpCode::SPS
|
|
|
|
| TSCOpCode::STC
|
|
|
|
| TSCOpCode::SVP
|
|
|
|
| TSCOpCode::TUR
|
|
|
|
| TSCOpCode::WAS
|
|
|
|
| TSCOpCode::ZAM
|
|
|
|
| TSCOpCode::HM2
|
|
|
|
| TSCOpCode::POP
|
|
|
|
| TSCOpCode::KE2
|
|
|
|
| TSCOpCode::FR2 => {
|
2022-01-17 22:29:30 +00:00
|
|
|
writeln!(&mut result, "{:?}()", op).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
// One operand codes
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::BOA
|
|
|
|
| TSCOpCode::BSL
|
|
|
|
| TSCOpCode::FOB
|
|
|
|
| TSCOpCode::FOM
|
|
|
|
| TSCOpCode::QUA
|
|
|
|
| TSCOpCode::UNI
|
|
|
|
| TSCOpCode::MYB
|
|
|
|
| TSCOpCode::MYD
|
|
|
|
| TSCOpCode::FAI
|
|
|
|
| TSCOpCode::FAO
|
|
|
|
| TSCOpCode::WAI
|
|
|
|
| TSCOpCode::FAC
|
|
|
|
| TSCOpCode::GIT
|
|
|
|
| TSCOpCode::NUM
|
|
|
|
| TSCOpCode::DNA
|
|
|
|
| TSCOpCode::DNP
|
|
|
|
| TSCOpCode::FLm
|
|
|
|
| TSCOpCode::FLp
|
|
|
|
| TSCOpCode::MPp
|
|
|
|
| TSCOpCode::SKm
|
|
|
|
| TSCOpCode::SKp
|
|
|
|
| TSCOpCode::EQp
|
|
|
|
| TSCOpCode::EQm
|
|
|
|
| TSCOpCode::MLp
|
|
|
|
| TSCOpCode::ITp
|
|
|
|
| TSCOpCode::ITm
|
|
|
|
| TSCOpCode::AMm
|
|
|
|
| TSCOpCode::UNJ
|
|
|
|
| TSCOpCode::MPJ
|
|
|
|
| TSCOpCode::YNJ
|
|
|
|
| TSCOpCode::EVE
|
|
|
|
| TSCOpCode::XX1
|
|
|
|
| TSCOpCode::SIL
|
|
|
|
| TSCOpCode::LIp
|
|
|
|
| TSCOpCode::SOU
|
|
|
|
| TSCOpCode::CMU
|
|
|
|
| TSCOpCode::SSS
|
|
|
|
| TSCOpCode::ACH
|
|
|
|
| TSCOpCode::S2MV
|
|
|
|
| TSCOpCode::S2PJ
|
|
|
|
| TSCOpCode::PSH => {
|
2021-10-15 14:36:05 +00:00
|
|
|
let par_a = read_cur_varint(&mut cursor)?;
|
|
|
|
|
2022-01-17 22:29:30 +00:00
|
|
|
writeln!(&mut result, "{:?}({})", op, par_a).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
// Two operand codes
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::FON
|
|
|
|
| TSCOpCode::MOV
|
|
|
|
| TSCOpCode::AMp
|
|
|
|
| TSCOpCode::NCJ
|
|
|
|
| TSCOpCode::ECJ
|
|
|
|
| TSCOpCode::FLJ
|
|
|
|
| TSCOpCode::ITJ
|
|
|
|
| TSCOpCode::SKJ
|
|
|
|
| TSCOpCode::AMJ
|
|
|
|
| TSCOpCode::SMP
|
|
|
|
| TSCOpCode::PSp
|
|
|
|
| TSCOpCode::IpN
|
|
|
|
| TSCOpCode::FFm => {
|
2021-10-15 14:36:05 +00:00
|
|
|
let par_a = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_b = read_cur_varint(&mut cursor)?;
|
|
|
|
|
2022-01-17 22:29:30 +00:00
|
|
|
writeln!(&mut result, "{:?}({}, {})", op, par_a, par_b).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
// Three operand codes
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::ANP | TSCOpCode::CNP | TSCOpCode::INP | TSCOpCode::TAM | TSCOpCode::CMP | TSCOpCode::INJ => {
|
2021-10-15 14:36:05 +00:00
|
|
|
let par_a = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_b = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_c = read_cur_varint(&mut cursor)?;
|
|
|
|
|
2022-01-17 22:29:30 +00:00
|
|
|
writeln!(&mut result, "{:?}({}, {}, {})", op, par_a, par_b, par_c).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
// Four operand codes
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::TRA | TSCOpCode::MNP | TSCOpCode::SNP => {
|
2021-10-15 14:36:05 +00:00
|
|
|
let par_a = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_b = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_c = read_cur_varint(&mut cursor)?;
|
|
|
|
let par_d = read_cur_varint(&mut cursor)?;
|
|
|
|
|
2022-01-17 22:29:30 +00:00
|
|
|
writeln!(&mut result, "{:?}({}, {}, {}, {})", op, par_a, par_b, par_c, par_d).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::_STR => {
|
2021-10-15 14:36:05 +00:00
|
|
|
let len = read_cur_varint(&mut cursor)?;
|
|
|
|
|
2022-01-17 22:29:30 +00:00
|
|
|
write!(&mut result, "%string(len = {}, value = \"", len).unwrap();
|
2021-10-15 14:36:05 +00:00
|
|
|
for _ in 0..len {
|
|
|
|
let chr = std::char::from_u32(read_cur_varint(&mut cursor)? as u32).unwrap_or('?');
|
|
|
|
match chr {
|
|
|
|
'\n' => {
|
|
|
|
result.push_str("\\n");
|
|
|
|
}
|
|
|
|
'\r' => {
|
|
|
|
result.push_str("\\r");
|
|
|
|
}
|
|
|
|
'\t' => {
|
|
|
|
result.push_str("\\t");
|
|
|
|
}
|
|
|
|
'\u{0000}'..='\u{001f}' | '\u{0080}'..='\u{ffff}' => {
|
2022-01-17 22:29:30 +00:00
|
|
|
result.push_str(&chr.escape_unicode().to_string());
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
result.push(chr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push_str("\")\n");
|
|
|
|
}
|
2021-10-16 02:37:42 +00:00
|
|
|
TSCOpCode::_NOP => result.push_str("%no_op()\n"),
|
|
|
|
TSCOpCode::_UNI => result.push_str("%unimplemented()\n"),
|
|
|
|
TSCOpCode::_END => result.push_str("%end_marker()\n"),
|
2021-10-15 14:36:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
} else {
|
|
|
|
Err(InvalidValue("Unknown script.".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|