Add export support

This commit is contained in:
2025-05-20 17:47:04 +08:00
commit e2e7399832
15 changed files with 1819 additions and 0 deletions

23
src/scripts/base.rs Normal file
View File

@@ -0,0 +1,23 @@
use crate::types::*;
use anyhow::Result;
pub trait ScriptBuilder {
fn default_encoding(&self) -> Encoding;
fn build_script(
&self,
filename: &str,
encoding: Encoding,
config: &ExtraConfig,
) -> Result<Box<dyn Script>>;
fn extensions(&self) -> &'static [&'static str];
fn script_type(&self) -> &'static ScriptType;
}
pub trait Script: std::fmt::Debug {
fn default_output_script_type(&self) -> OutputScriptType;
fn extract_messages(&self) -> Result<Vec<Message>>;
}

465
src/scripts/circus/info.rs Normal file
View File

@@ -0,0 +1,465 @@
pub struct Section {
beg: u8,
end: u8,
}
impl std::fmt::Debug for Section {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Section")
.field(&self.beg)
.field(&self.end)
.finish()
}
}
impl Section {
pub const fn new(beg: u8, end: u8) -> Self {
Section { beg, end }
}
pub fn its(&self, key: u8) -> bool {
return (!(self.beg == self.end && self.beg == 0xFF))
&& (key >= self.beg && key <= self.end);
}
}
#[derive(Debug)]
pub struct ScriptInfo {
pub name: &'static str,
pub version: u16,
/// \[op: byte\] \[arg1: uint8\] \[arg2: uint8\]
pub uint8x2: Section,
/// \[op: byte\] \[arg1: uint8\] \[arg2: string\]
pub uint8str: Section,
/// \[op: byte\] \[arg1: string\]
pub string: Section,
/// \[op: byte\] \[arg1: encstr\]
pub encstr: Section,
/// \[op: byte\] \[arg1: uint16\] \[arg2: uint16\] \[arg3: uint16\] \[arg4: uint16\]
pub uint16x4: Section,
/// the opcode for unencrypted strings in scene text
pub optunenc: u8,
pub deckey: u8,
pub nameopcode: u8,
}
const SCRIPT_INFO: [ScriptInfo; 31] = [
ScriptInfo::new(
"ffexa",
0x7B69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x43,
0x20,
0xFF,
),
ScriptInfo::new(
"ffexs",
0x7B6B,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x4B),
(0x4c, 0x4F),
(0x50, 0xFF),
0x43,
0x20,
0xFF,
),
ScriptInfo::new(
"ef",
0x466A,
(0x00, 0x28),
(0x2A, 0x2F),
(0x30, 0x4A),
(0x4B, 0x4E),
(0x4F, 0xFF),
0x46,
0x20,
0xFF,
),
ScriptInfo::new(
"dcos",
0x315D,
(0x00, 0x2B),
(0xFF, 0xFF),
(0x2C, 0x45),
(0x46, 0x49),
(0x4A, 0xFF),
0x42,
0x20,
0xFF,
),
ScriptInfo::new(
"ktlep",
0x6E69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dcws",
0x656C,
(0x00, 0x2B),
(0x2C, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0x48,
0x20,
0xFF,
),
ScriptInfo::new(
"dcsv",
0x636C,
(0x00, 0x2B),
(0x2C, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0x46,
0x20,
0xFF,
),
ScriptInfo::new(
"dcpc",
0x3D63,
(0x00, 0x2C),
(0xFF, 0xFF),
(0x2D, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x44,
0x20,
0xFF,
),
ScriptInfo::new(
"dcmems",
0x315D,
(0x00, 0x2B),
(0xFF, 0xFF),
(0x2C, 0x45),
(0x46, 0x49),
(0x4A, 0xFF),
0x42,
0x20,
0xFF,
),
ScriptInfo::new(
"dcdx",
0x7769,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dcas",
0x4E69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x43,
0x20,
0xFF,
),
ScriptInfo::new(
"dcbs",
0x3163,
(0x00, 0x2B),
(0xFF, 0xFF),
(0x2C, 0x48),
(0x49, 0x4C),
(0x4D, 0xFF),
0xFF,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2fl",
0x9C69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2bs",
0x316C,
(0x00, 0x2B),
(0x2C, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0xFF,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2dm",
0x9D72,
(0x00, 0x29),
(0x2A, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0x44,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2fy",
0x3866,
(0x00, 0x2E),
(0xFF, 0xFF),
(0x2F, 0x4B),
(0x4C, 0x4F),
(0x50, 0xFF),
0x48,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2cckko",
0x026C,
(0x00, 0x2B),
(0x2C, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0xFF,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2ccotm",
0x016C,
(0x00, 0x2B),
(0x2C, 0x31),
(0x32, 0x4C),
(0x4D, 0x50),
(0x51, 0xFF),
0xFF,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2sc",
0x3B69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2ty",
0x5F69,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0xFF,
0x20,
0xFF,
),
ScriptInfo::new(
"dc2pc",
0x5769,
(0x00, 0x28),
(0x29, 0x2E),
(0x2F, 0x49),
(0x4A, 0x4D),
(0x4E, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dc3rx",
0x9772,
(0x00, 0x2B),
(0x2C, 0x33),
(0x34, 0x4E),
(0x4F, 0x52),
(0x53, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dc3pp",
0x9872,
(0x00, 0x2A),
(0x2B, 0x32),
(0x33, 0x4E),
(0x4F, 0x51),
(0x52, 0xFF),
0x45,
0x20,
0xFF,
),
ScriptInfo::new(
"dc3wy",
0xA09F,
(0x00, 0x38),
(0x39, 0x41),
(0x42, 0x5F),
(0x60, 0x63),
(0x64, 0xFF),
0x55,
0x20,
0xFF,
),
ScriptInfo::new(
"dc3dd",
0xA5A8,
(0x00, 0x38),
(0x39, 0x43),
(0x44, 0x62),
(0x63, 0x67),
(0x68, 0xFF),
0x58,
0x20,
0xFF,
),
ScriptInfo::new(
"dc4",
0xAAB6,
(0x00, 0x3A),
(0x3B, 0x47),
(0x48, 0x68),
(0x69, 0x6D),
(0x6E, 0xFF),
0x5D,
0x20,
0xFF,
),
ScriptInfo::new(
"dc4ph",
0xABB6,
(0x00, 0x3A),
(0x3B, 0x47),
(0x48, 0x68),
(0x69, 0x6D),
(0x6E, 0xFF),
0x5D,
0x20,
0xFF,
),
ScriptInfo::new(
"ds",
0x9F9A,
(0x00, 0x38),
(0x39, 0x4A),
(0x41, 0x5E),
(0x5F, 0x62),
(0x63, 0xFF),
0x54,
0x20,
0xFF,
),
ScriptInfo::new(
"dsif",
0xA1A1,
(0x00, 0x39),
(0x3A, 0x42),
(0x43, 0x60),
(0x61, 0x64),
(0x65, 0xFF),
0x56,
0x20,
0x62,
),
ScriptInfo::new(
"tmpl",
0xA6B4,
(0x00, 0x3B),
(0x3A, 0x46),
(0x46, 0x67),
(0x68, 0x6E),
(0x6D, 0xFF),
0x5C,
0x20,
0x69,
),
ScriptInfo::new(
"nightshade",
0x0871,
(0x00, 0x2B),
(0x2C, 0x33),
(0x34, 0x4E),
(0x4F, 0x52),
(0x53, 0xFF),
0x43,
0x01,
0xFF,
),
];
impl ScriptInfo {
pub const fn new(
name: &'static str,
version: u16,
uint8x2: (u8, u8),
uint8str: (u8, u8),
string: (u8, u8),
encstr: (u8, u8),
uint16x4: (u8, u8),
optunenc: u8,
deckey: u8,
nameopcode: u8,
) -> Self {
ScriptInfo {
name,
version,
uint8x2: Section::new(uint8x2.0, uint8x2.1),
uint8str: Section::new(uint8str.0, uint8str.1),
string: Section::new(string.0, string.1),
encstr: Section::new(encstr.0, encstr.1),
uint16x4: Section::new(uint16x4.0, uint16x4.1),
optunenc,
deckey,
nameopcode,
}
}
pub fn query(name: &str) -> Option<&'static ScriptInfo> {
for info in SCRIPT_INFO.iter() {
if info.name == name {
return Some(info);
}
}
None
}
pub fn query_by_version(version: u16) -> Option<&'static ScriptInfo> {
for info in SCRIPT_INFO.iter() {
if info.version == version {
return Some(info);
}
}
None
}
}

View File

@@ -0,0 +1,2 @@
mod info;
pub mod script;

View File

@@ -0,0 +1,218 @@
use super::info::*;
use crate::scripts::base::*;
use crate::types::*;
use crate::utils::encoding::decode_to_string;
use anyhow::Result;
pub struct CircusMesScriptBuilder {}
impl CircusMesScriptBuilder {
pub const fn new() -> Self {
CircusMesScriptBuilder {}
}
}
impl ScriptBuilder for CircusMesScriptBuilder {
fn default_encoding(&self) -> Encoding {
Encoding::Cp932
}
fn build_script(
&self,
filename: &str,
encoding: Encoding,
config: &ExtraConfig,
) -> Result<Box<dyn Script>> {
Ok(Box::new(CircusMesScript::new(
filename.as_ref(),
encoding,
config,
)?))
}
fn extensions(&self) -> &'static [&'static str] {
&["mes"]
}
fn script_type(&self) -> &'static ScriptType {
&ScriptType::Circus
}
}
#[derive(Debug)]
struct Token {
offset: usize,
length: usize,
value: u8,
}
pub struct CircusMesScript {
data: Vec<u8>,
encoding: Encoding,
is_new_ver: bool,
version: u16,
info: &'static ScriptInfo,
asm_bin_offset: usize,
blocks_offset: usize,
tokens: Vec<Token>,
}
impl CircusMesScript {
pub fn new(filename: &str, encoding: Encoding, config: &ExtraConfig) -> Result<Self> {
let data = crate::utils::files::read_file(filename)?;
let head0 = i32::from_le_bytes(data[0..4].try_into()?);
let head1 = i32::from_le_bytes(data[4..8].try_into()?);
let mut is_new_ver = false;
let mut version = 0;
let mut info = config
.circus_mes_type
.as_ref()
.and_then(|name| ScriptInfo::query(name.as_ref()));
let mut asm_bin_offset = 0;
let mut blocks_offset = 0;
if head1 == 0x3 {
let offset = head0 * 0x6 + 0x4;
if data.len() > offset as usize {
if data.len() > offset as usize + 3 {
version =
u16::from_le_bytes(data[offset as usize..offset as usize + 2].try_into()?);
if info.is_none() {
info = ScriptInfo::query_by_version(version);
}
asm_bin_offset = offset as usize + 3;
}
blocks_offset = 8;
}
is_new_ver = true;
} else {
let offset = head0 * 0x4 + 0x4;
if data.len() > offset as usize {
if data.len() > offset as usize + 2 {
version =
u16::from_le_bytes(data[offset as usize..offset as usize + 2].try_into()?);
if info.is_none() {
info = ScriptInfo::query_by_version(version);
}
asm_bin_offset = offset as usize + 2;
}
blocks_offset = 4;
}
}
let info = info.ok_or(anyhow::anyhow!("Failed to detect version."))?;
let mut tokens = Vec::new();
let mut offset = 0;
let asm_bin_size = if asm_bin_offset == 0 {
0
} else {
data.len() - asm_bin_offset
};
while offset < asm_bin_size {
let value = data[asm_bin_offset + offset];
let length = if info.uint8x2.its(value) {
0x03
} else if info.uint8str.its(value) {
let mut len = 0x3;
let mut temp = data[asm_bin_offset + offset + len - 1];
while temp != 0x00 {
len += 0x1;
if asm_bin_offset + offset + len >= data.len() {
break;
}
temp = data[asm_bin_offset + offset + len - 1];
}
len
} else if info.string.its(value) || info.encstr.its(value) {
let mut len = 1;
let mut temp = data[asm_bin_offset + offset + len - 1];
while temp != 0x00 {
len += 0x1;
if asm_bin_offset + offset + len >= data.len() {
break;
}
temp = data[asm_bin_offset + offset + len - 1];
}
len
} else if info.uint16x4.its(value) {
0x09
} else {
return Err(anyhow::anyhow!(format!(
"Unknown token type: 0x{:02X} at offset {}",
value,
asm_bin_offset + offset
)));
};
let token = Token {
offset,
length,
value,
};
offset += length;
tokens.push(token);
}
Ok(CircusMesScript {
data,
encoding,
is_new_ver,
version,
info,
asm_bin_offset,
blocks_offset,
tokens,
})
}
}
impl std::fmt::Debug for CircusMesScript {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CircusMesScript")
.field("encoding", &self.encoding)
.field("is_new_ver", &self.is_new_ver)
.field("version", &self.version)
.field("info", &self.info)
.field("asm_bin_offset", &self.asm_bin_offset)
.field("blocks_offset", &self.blocks_offset)
.field("tokens", &self.tokens)
.finish_non_exhaustive()
}
}
impl Script for CircusMesScript {
fn default_output_script_type(&self) -> OutputScriptType {
OutputScriptType::Json
}
fn extract_messages(&self) -> Result<Vec<Message>> {
let mut mes = vec![];
let mut name = None;
for token in self.tokens.iter() {
let mut t = None;
if self.info.encstr.its(token.value) {
let mut text = self.data[self.asm_bin_offset + token.offset + 1
..self.asm_bin_offset + token.offset + token.length]
.to_vec();
for t in text.iter_mut() {
*t = (*t).overflowing_add(self.info.deckey).0;
}
t = Some(decode_to_string(self.encoding, &text)?);
// println!("Token(enc): {:?}, {}", token, t.as_ref().unwrap());
} else if token.value == self.info.optunenc {
let text = &self.data[self.asm_bin_offset + token.offset + 1
..self.asm_bin_offset + token.offset + token.length];
t = Some(decode_to_string(self.encoding, text)?);
// println!("Token: {:?}, {}", token, t.as_ref().unwrap());
}
match t {
Some(t) => {
if token.value == self.info.nameopcode {
name = Some(t);
} else {
let message = Message::new(t, name.take());
mes.push(message);
}
}
None => {}
}
}
Ok(mes)
}
}

12
src/scripts/mod.rs Normal file
View File

@@ -0,0 +1,12 @@
pub mod base;
pub mod circus;
pub use base::{Script, ScriptBuilder};
lazy_static::lazy_static! {
pub static ref BUILDER: Vec<Box<dyn ScriptBuilder + Sync + Send>> = vec![
Box::new(circus::script::CircusMesScriptBuilder::new()),
];
pub static ref ALL_EXTS: Vec<String> =
BUILDER.iter().flat_map(|b| b.extensions()).map(|s| s.to_string()).collect();
}