mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-11 15:38:48 +08:00
Add export support
This commit is contained in:
69
src/args.rs
Normal file
69
src/args.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::types::*;
|
||||
use clap::{ArgAction, ArgGroup, Parser, Subcommand};
|
||||
|
||||
/// Tools for export and import scripts
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(group = ArgGroup::new("encodingg").multiple(false), group = ArgGroup::new("output_encodingg").multiple(false))]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct Arg {
|
||||
#[arg(short = 't', long, value_enum, global = true)]
|
||||
/// Script type
|
||||
pub script_type: Option<ScriptType>,
|
||||
#[arg(short = 'T', long, value_enum, global = true)]
|
||||
/// Output script type
|
||||
pub output_type: Option<OutputScriptType>,
|
||||
#[arg(short = 'e', long, value_enum, global = true, group = "encodingg")]
|
||||
/// Script encoding
|
||||
pub encoding: Option<TextEncoding>,
|
||||
#[cfg(windows)]
|
||||
#[arg(short = 'c', long, value_enum, global = true, group = "encodingg")]
|
||||
/// Script code page
|
||||
pub code_page: Option<u32>,
|
||||
#[arg(
|
||||
short = 'E',
|
||||
long,
|
||||
value_enum,
|
||||
global = true,
|
||||
group = "output_encodingg"
|
||||
)]
|
||||
/// Output text encoding
|
||||
pub output_encoding: Option<TextEncoding>,
|
||||
#[cfg(windows)]
|
||||
#[arg(
|
||||
short = 'C',
|
||||
long,
|
||||
value_enum,
|
||||
global = true,
|
||||
group = "output_encodingg"
|
||||
)]
|
||||
/// Output code page
|
||||
pub output_code_page: Option<u32>,
|
||||
#[arg(long, value_enum, global = true)]
|
||||
/// Circus Game
|
||||
pub circus_mes_type: Option<CircusMesType>,
|
||||
#[arg(short, long, action = ArgAction::SetTrue, global = true)]
|
||||
/// Search for script files in the directory recursively
|
||||
pub recursive: bool,
|
||||
#[arg(global = true, action = ArgAction::SetTrue, short, long)]
|
||||
/// Print backtrace on error
|
||||
pub backtrace: bool,
|
||||
#[command(subcommand)]
|
||||
/// Command
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug)]
|
||||
/// Commands
|
||||
pub enum Command {
|
||||
/// Extract from script
|
||||
Export {
|
||||
/// Input script file or directory
|
||||
input: String,
|
||||
/// Output file or directory
|
||||
output: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn parse_args() -> Arg {
|
||||
Arg::parse()
|
||||
}
|
||||
200
src/main.rs
Normal file
200
src/main.rs
Normal file
@@ -0,0 +1,200 @@
|
||||
pub mod args;
|
||||
pub mod scripts;
|
||||
pub mod types;
|
||||
pub mod utils;
|
||||
|
||||
fn get_encoding(arg: &args::Arg, builder: &Box<dyn scripts::ScriptBuilder + Send + Sync>) -> types::Encoding {
|
||||
match &arg.encoding {
|
||||
Some(enc) => {
|
||||
return match enc {
|
||||
&types::TextEncoding::Default => {
|
||||
builder.default_encoding()
|
||||
}
|
||||
&types::TextEncoding::Auto => {
|
||||
types::Encoding::Auto
|
||||
}
|
||||
&types::TextEncoding::Cp932 => {
|
||||
types::Encoding::Cp932
|
||||
}
|
||||
&types::TextEncoding::Utf8 => {
|
||||
types::Encoding::Utf8
|
||||
}
|
||||
&types::TextEncoding::Gb2312 => {
|
||||
types::Encoding::Gb2312
|
||||
}
|
||||
};
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
match &arg.code_page {
|
||||
Some(code_page) => {
|
||||
return types::Encoding::CodePage(*code_page);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
builder.default_encoding()
|
||||
}
|
||||
|
||||
fn get_output_encoding(arg: &args::Arg) -> types::Encoding {
|
||||
match &arg.output_encoding {
|
||||
Some(enc) => {
|
||||
return match enc {
|
||||
&types::TextEncoding::Default => {
|
||||
types::Encoding::Utf8
|
||||
}
|
||||
&types::TextEncoding::Auto => {
|
||||
types::Encoding::Utf8
|
||||
}
|
||||
&types::TextEncoding::Cp932 => {
|
||||
types::Encoding::Cp932
|
||||
}
|
||||
&types::TextEncoding::Utf8 => {
|
||||
types::Encoding::Utf8
|
||||
}
|
||||
&types::TextEncoding::Gb2312 => {
|
||||
types::Encoding::Gb2312
|
||||
}
|
||||
};
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
match &arg.code_page {
|
||||
Some(code_page) => {
|
||||
return types::Encoding::CodePage(*code_page);
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
types::Encoding::Utf8
|
||||
}
|
||||
|
||||
pub fn parse_script(filename: &str, arg: &args::Arg, config: &types::ExtraConfig) -> anyhow::Result<Box<dyn scripts::Script>> {
|
||||
match &arg.script_type {
|
||||
Some(typ) => {
|
||||
for builder in scripts::BUILDER.iter() {
|
||||
if typ == builder.script_type() {
|
||||
let encoding = get_encoding(arg, builder);
|
||||
return Ok(builder.build_script(filename, encoding, config)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
for builder in scripts::BUILDER.iter() {
|
||||
let exts = builder.extensions();
|
||||
for ext in exts {
|
||||
if filename.to_lowercase().ends_with(ext) {
|
||||
let encoding = get_encoding(arg, builder);
|
||||
return Ok(builder.build_script(filename, encoding, config)?);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(anyhow::anyhow!("Unsupported script type"))
|
||||
}
|
||||
|
||||
pub fn export_script(
|
||||
filename: &str,
|
||||
arg: &args::Arg,
|
||||
config: &types::ExtraConfig,
|
||||
output: &Option<String>,
|
||||
is_dir: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
eprintln!("Exporting {}", filename);
|
||||
let script = parse_script(filename, arg, config)?;
|
||||
// println!("{:?}", script);
|
||||
let mes = script.extract_messages()?;
|
||||
// for m in mes.iter() {
|
||||
// println!("{:?}", m);
|
||||
// }
|
||||
if mes.is_empty() {
|
||||
eprintln!("No messages found");
|
||||
return Ok(());
|
||||
}
|
||||
let of = match &arg.output_type {
|
||||
Some(t) => t.clone(),
|
||||
None => script.default_output_script_type(),
|
||||
};
|
||||
let f = if filename == "-" {
|
||||
String::from("-")
|
||||
} else {
|
||||
match output.as_ref() {
|
||||
Some(output) => {
|
||||
if is_dir {
|
||||
let f = std::path::PathBuf::from(filename);
|
||||
let mut pb = std::path::PathBuf::from(output);
|
||||
if let Some(fname) = f.file_name() {
|
||||
pb.push(fname);
|
||||
}
|
||||
pb.set_extension(of.as_ref());
|
||||
pb.to_string_lossy().into_owned()
|
||||
} else {
|
||||
output.clone()
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let mut pb = std::path::PathBuf::from(filename);
|
||||
pb.set_extension(of.as_ref());
|
||||
pb.to_string_lossy().into_owned()
|
||||
}
|
||||
}
|
||||
};
|
||||
match of {
|
||||
types::OutputScriptType::Json => {
|
||||
let enc = get_output_encoding(arg);
|
||||
let s = serde_json::to_string_pretty(&mes)?;
|
||||
let b = utils::encoding::encode_string(enc, &s)?;
|
||||
let mut f = utils::files::write_file(&f)?;
|
||||
f.write_all(&b)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let arg = args::parse_args();
|
||||
if arg.backtrace {
|
||||
unsafe { std::env::set_var("RUST_LIB_BACKTRACE", "1") };
|
||||
}
|
||||
let cfg = types::ExtraConfig {
|
||||
circus_mes_type: arg.circus_mes_type.clone(),
|
||||
};
|
||||
match &arg.command {
|
||||
args::Command::Export { input, output } => {
|
||||
let (scripts, is_dir) = utils::files::collect_files(input, arg.recursive).unwrap();
|
||||
if is_dir {
|
||||
match &output {
|
||||
Some(output) => {
|
||||
let op = std::path::Path::new(output);
|
||||
if op.exists() {
|
||||
if !op.is_dir() {
|
||||
eprintln!("Output path is not a directory");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
std::fs::create_dir_all(op).unwrap();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
eprintln!("Output path is not specified");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
for script in scripts.iter() {
|
||||
let re = export_script(&script, &arg, &cfg, output, is_dir);
|
||||
match re {
|
||||
Ok(_) => {
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error exporting {}: {}", script, e);
|
||||
if arg.backtrace {
|
||||
eprintln!("Backtrace: {:?}", e.backtrace());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/scripts/base.rs
Normal file
23
src/scripts/base.rs
Normal 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
465
src/scripts/circus/info.rs
Normal 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
|
||||
}
|
||||
}
|
||||
2
src/scripts/circus/mod.rs
Normal file
2
src/scripts/circus/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod info;
|
||||
pub mod script;
|
||||
218
src/scripts/circus/script.rs
Normal file
218
src/scripts/circus/script.rs
Normal 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
12
src/scripts/mod.rs
Normal 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();
|
||||
}
|
||||
187
src/types.rs
Normal file
187
src/types.rs
Normal file
@@ -0,0 +1,187 @@
|
||||
use clap::ValueEnum;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
|
||||
#[serde(untagged, rename_all = "camelCase")]
|
||||
/// Text Encoding
|
||||
pub enum Encoding {
|
||||
/// Automatically detect encoding
|
||||
Auto,
|
||||
/// UTF-8 encoding
|
||||
Utf8,
|
||||
/// Shift-JIS encoding
|
||||
Cp932,
|
||||
/// GB2312 encoding
|
||||
Gb2312,
|
||||
/// Code page encoding (Windows only)
|
||||
#[cfg(windows)]
|
||||
CodePage(u32),
|
||||
}
|
||||
|
||||
impl Default for Encoding {
|
||||
fn default() -> Self {
|
||||
Encoding::Utf8
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]
|
||||
/// Text Encoding
|
||||
pub enum TextEncoding {
|
||||
/// Use script's default encoding
|
||||
Default,
|
||||
/// Automatically detect encoding
|
||||
Auto,
|
||||
/// UTF-8 encoding
|
||||
Utf8,
|
||||
/// Shift-JIS encoding
|
||||
Cp932,
|
||||
/// GB2312 encoding
|
||||
Gb2312,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]
|
||||
/// Script type
|
||||
pub enum OutputScriptType {
|
||||
/// Text script
|
||||
Txt,
|
||||
/// JSON which can be used for GalTransl
|
||||
Json,
|
||||
}
|
||||
|
||||
impl AsRef<str> for OutputScriptType {
|
||||
fn as_ref(&self) -> &str {
|
||||
match self {
|
||||
OutputScriptType::Txt => "txt",
|
||||
OutputScriptType::Json => "json",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub enum CircusMesType {
|
||||
/// fortissimo//Akkord:Bsusvier
|
||||
Ffexa,
|
||||
/// fortissimo EXS//Akkord:nächsten Phase
|
||||
Ffexs,
|
||||
/// Eternal Fantasy
|
||||
Ef,
|
||||
/// D.C.〜ダ・カーポ〜 温泉編
|
||||
Dcos,
|
||||
/// ことり Love Ex P
|
||||
Ktlep,
|
||||
/// D.C.WhiteSeason
|
||||
Dcws,
|
||||
/// D.C. Summer Vacation
|
||||
Dcsv,
|
||||
/// D.C.P.C.(Vista)
|
||||
Dcpc,
|
||||
/// D.C.〜ダ・カーポ〜 MEMORIES DISC
|
||||
Dcmems,
|
||||
/// D.C. Dream X’mas
|
||||
Dcdx,
|
||||
/// D.C.A.S. 〜ダ・カーポ〜アフターシーズンズ
|
||||
Dcas,
|
||||
/// D.C.II 春風のアルティメットバトル!
|
||||
Dcbs,
|
||||
/// D.C.II Fall in Love
|
||||
Dc2fl,
|
||||
/// D.C.II 春風のアルティメットバトル!
|
||||
Dc2bs,
|
||||
/// D.C.II Dearest Marriage
|
||||
Dc2dm,
|
||||
/// D.C.II 〜featuring Yun2〜
|
||||
Dc2fy,
|
||||
/// D.C.II C.C. 月島小恋のらぶらぶバスルーム
|
||||
Dc2cckko,
|
||||
/// D.C.II C.C. 音姫先生のどきどき特別授業
|
||||
Dc2ccotm,
|
||||
/// D.C.II Spring Celebration
|
||||
Dc2sc,
|
||||
/// D.C.II To You
|
||||
Dc2ty,
|
||||
/// D.C.II P.C.
|
||||
Dc2pc,
|
||||
/// D.C.III RX-rated
|
||||
Dc3rx,
|
||||
/// D.C.III P.P.~ダ・カーポIII プラチナパートナー~
|
||||
Dc3pp,
|
||||
/// D.C.III WithYou
|
||||
Dc3wy,
|
||||
/// D.C.III DreamDays
|
||||
Dc3dd,
|
||||
/// D.C.4 ~ダ・カーポ4~
|
||||
Dc4,
|
||||
/// D.C.4 Plus Harmony 〜ダ・カーポ4〜 プラスハーモニー
|
||||
Dc4ph,
|
||||
/// D.S. -Dal Segno-
|
||||
Ds,
|
||||
/// D.S.i.F. -Dal Segno- in Future
|
||||
Dsif,
|
||||
/// てんぷれ!
|
||||
Tmpl,
|
||||
/// 百花百狼/Hyakka Hyakurou
|
||||
Nightshade,
|
||||
}
|
||||
|
||||
impl AsRef<str> for CircusMesType {
|
||||
fn as_ref(&self) -> &str {
|
||||
match self {
|
||||
CircusMesType::Ffexa => "ffexa",
|
||||
CircusMesType::Ffexs => "ffexs",
|
||||
CircusMesType::Ef => "ef",
|
||||
CircusMesType::Dcos => "dcos",
|
||||
CircusMesType::Ktlep => "ktlep",
|
||||
CircusMesType::Dcws => "dcws",
|
||||
CircusMesType::Dcsv => "dcsv",
|
||||
CircusMesType::Dcpc => "dcpc",
|
||||
CircusMesType::Dcmems => "dcmems",
|
||||
CircusMesType::Dcdx => "dcdx",
|
||||
CircusMesType::Dcas => "dcas",
|
||||
CircusMesType::Dcbs => "dcbs",
|
||||
CircusMesType::Dc2fl => "dc2fl",
|
||||
CircusMesType::Dc2bs => "dc2bs",
|
||||
CircusMesType::Dc2dm => "dc2dm",
|
||||
CircusMesType::Dc2fy => "dc2fy",
|
||||
CircusMesType::Dc2cckko => "dc2cckko",
|
||||
CircusMesType::Dc2ccotm => "dc2ccotm",
|
||||
CircusMesType::Dc2sc => "dc2sc",
|
||||
CircusMesType::Dc2ty => "dc2ty",
|
||||
CircusMesType::Dc2pc => "dc2pc",
|
||||
CircusMesType::Dc3rx => "dc3rx",
|
||||
CircusMesType::Dc3pp => "dc3pp",
|
||||
CircusMesType::Dc3wy => "dc3wy",
|
||||
CircusMesType::Dc3dd => "dc3dd",
|
||||
CircusMesType::Dc4 => "dc4",
|
||||
CircusMesType::Dc4ph => "dc4ph",
|
||||
CircusMesType::Ds => "ds",
|
||||
CircusMesType::Dsif => "dsif",
|
||||
CircusMesType::Tmpl => "tmpl",
|
||||
CircusMesType::Nightshade => "nightshade",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ExtraConfig {
|
||||
pub circus_mes_type: Option<CircusMesType>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, ValueEnum, PartialEq, Eq, PartialOrd, Ord)]
|
||||
/// Script type
|
||||
pub enum ScriptType {
|
||||
/// Circus MES script
|
||||
Circus,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Message {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub name: Option<String>,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new(message: String, name: Option<String>) -> Self {
|
||||
Message { message, name }
|
||||
}
|
||||
}
|
||||
|
||||
106
src/utils/encoding.rs
Normal file
106
src/utils/encoding.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
use crate::types::*;
|
||||
|
||||
pub fn decode_to_string(encoding: Encoding, data: &[u8]) -> Result<String, anyhow::Error> {
|
||||
match encoding {
|
||||
Encoding::Auto => decode_to_string(Encoding::Utf8, data)
|
||||
.or_else(|_| decode_to_string(Encoding::Cp932, data))
|
||||
.or_else(|_| decode_to_string(Encoding::Gb2312, data)),
|
||||
Encoding::Utf8 => Ok(String::from_utf8(data.to_vec())?),
|
||||
Encoding::Cp932 => {
|
||||
let result = encoding_rs::SHIFT_JIS.decode(data);
|
||||
if result.2 {
|
||||
Err(anyhow::anyhow!("Failed to decode Shift-JIS"))
|
||||
} else {
|
||||
Ok(result.0.to_string())
|
||||
}
|
||||
}
|
||||
Encoding::Gb2312 => {
|
||||
let result = encoding_rs::GBK.decode(data);
|
||||
if result.2 {
|
||||
Err(anyhow::anyhow!("Failed to decode GB2312"))
|
||||
} else {
|
||||
Ok(result.0.to_string())
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
Encoding::CodePage(code_page) => {
|
||||
Ok(super::encoding_win::decode_to_string(code_page, data)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode_string(encoding: Encoding, data: &str) -> Result<Vec<u8>, anyhow::Error> {
|
||||
match encoding {
|
||||
Encoding::Auto => Ok(data.as_bytes().to_vec()),
|
||||
Encoding::Utf8 => Ok(data.as_bytes().to_vec()),
|
||||
Encoding::Cp932 => {
|
||||
let result = encoding_rs::SHIFT_JIS.encode(data);
|
||||
if result.2 {
|
||||
Err(anyhow::anyhow!("Failed to encode Shift-JIS"))
|
||||
} else {
|
||||
Ok(result.0.to_vec())
|
||||
}
|
||||
}
|
||||
Encoding::Gb2312 => {
|
||||
let result = encoding_rs::GBK.encode(data);
|
||||
if result.2 {
|
||||
Err(anyhow::anyhow!("Failed to encode GB2312"))
|
||||
} else {
|
||||
Ok(result.0.to_vec())
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
Encoding::CodePage(code_page) => {
|
||||
Ok(super::encoding_win::encode_string(code_page, data)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_to_string() {
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
Encoding::Utf8,
|
||||
&[228, 184, 173, 230, 150, 135, 230, 181, 139, 232, 175, 149]
|
||||
)
|
||||
.unwrap(),
|
||||
"中文测试".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
Encoding::Cp932,
|
||||
&[
|
||||
130, 171, 130, 225, 130, 215, 130, 194, 130, 187, 130, 211, 130, 198
|
||||
]
|
||||
)
|
||||
.unwrap(),
|
||||
"きゃべつそふと".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(Encoding::Gb2312, &[214, 208, 206, 196]).unwrap(),
|
||||
"中文".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
Encoding::Auto,
|
||||
&[228, 184, 173, 230, 150, 135, 230, 181, 139, 232, 175, 149]
|
||||
)
|
||||
.unwrap(),
|
||||
"中文测试".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
Encoding::Auto,
|
||||
&[
|
||||
130, 171, 130, 225, 130, 215, 130, 194, 130, 187, 130, 211, 130, 198
|
||||
]
|
||||
)
|
||||
.unwrap(),
|
||||
"きゃべつそふと".to_string()
|
||||
);
|
||||
#[cfg(windows)]
|
||||
assert_eq!(
|
||||
decode_to_string(Encoding::CodePage(936), &[214, 208, 206, 196]).unwrap(),
|
||||
"中文".to_string()
|
||||
);
|
||||
}
|
||||
121
src/utils/encoding_win.rs
Normal file
121
src/utils/encoding_win.rs
Normal file
@@ -0,0 +1,121 @@
|
||||
use windows_sys::Win32::Foundation::GetLastError;
|
||||
use windows_sys::Win32::Globalization::{MB_ERR_INVALID_CHARS, MultiByteToWideChar, WideCharToMultiByte};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct WinError {
|
||||
pub code: u32,
|
||||
}
|
||||
|
||||
impl WinError {
|
||||
pub fn new(code: u32) -> Self {
|
||||
WinError { code }
|
||||
}
|
||||
|
||||
pub fn from_last_error() -> Self {
|
||||
let code = unsafe { GetLastError() };
|
||||
WinError::new(code)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for WinError {}
|
||||
|
||||
impl std::fmt::Display for WinError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Windows error code: {}", self.code)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode_to_string(cp: u32, data: &[u8]) -> Result<String, WinError> {
|
||||
let needed_len = unsafe {
|
||||
MultiByteToWideChar(
|
||||
cp,
|
||||
MB_ERR_INVALID_CHARS,
|
||||
data.as_ptr() as _,
|
||||
data.len() as i32,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
if needed_len == 0 {
|
||||
return Err(WinError::from_last_error());
|
||||
}
|
||||
let mut wc = Vec::with_capacity(needed_len as usize);
|
||||
wc.resize(needed_len as usize, 0);
|
||||
let result = unsafe {
|
||||
MultiByteToWideChar(
|
||||
cp,
|
||||
MB_ERR_INVALID_CHARS,
|
||||
data.as_ptr() as _,
|
||||
data.len() as i32,
|
||||
wc.as_mut_ptr(),
|
||||
needed_len,
|
||||
)
|
||||
};
|
||||
if result == 0 {
|
||||
return Err(WinError::from_last_error());
|
||||
}
|
||||
Ok(String::from_utf16_lossy(&wc))
|
||||
}
|
||||
|
||||
pub fn encode_string(cp: u32, data: &str) -> Result<Vec<u8>, WinError> {
|
||||
let wstr = data.encode_utf16().collect::<Vec<u16>>();
|
||||
let needed_len = unsafe {
|
||||
WideCharToMultiByte(
|
||||
cp,
|
||||
0,
|
||||
wstr.as_ptr(),
|
||||
wstr.len() as i32,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if needed_len == 0 {
|
||||
return Err(WinError::from_last_error());
|
||||
}
|
||||
let mut mb = Vec::with_capacity(needed_len as usize);
|
||||
mb.resize(needed_len as usize, 0);
|
||||
let result = unsafe {
|
||||
WideCharToMultiByte(
|
||||
cp,
|
||||
0,
|
||||
wstr.as_ptr(),
|
||||
wstr.len() as i32,
|
||||
mb.as_mut_ptr(),
|
||||
needed_len,
|
||||
std::ptr::null_mut(),
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if result == 0 {
|
||||
return Err(WinError::from_last_error());
|
||||
}
|
||||
Ok(mb)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_to_string() {
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
65001,
|
||||
&[228, 184, 173, 230, 150, 135, 230, 181, 139, 232, 175, 149]
|
||||
)
|
||||
.unwrap(),
|
||||
"中文测试".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(
|
||||
932,
|
||||
&[
|
||||
130, 171, 130, 225, 130, 215, 130, 194, 130, 187, 130, 211, 130, 198
|
||||
]
|
||||
)
|
||||
.unwrap(),
|
||||
"きゃべつそふと".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
decode_to_string(936, &[214, 208, 206, 196]).unwrap(),
|
||||
"中文".to_string()
|
||||
);
|
||||
}
|
||||
67
src/utils/files.rs
Normal file
67
src/utils/files.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
use crate::scripts::ALL_EXTS;
|
||||
|
||||
pub fn find_files(path: &String, recursive: bool) -> io::Result<Vec<String>> {
|
||||
let mut result = Vec::new();
|
||||
let dir_path = Path::new(&path);
|
||||
|
||||
if dir_path.is_dir() {
|
||||
for entry in fs::read_dir(dir_path)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_file()
|
||||
&& path.extension().map_or(true, |ext| {
|
||||
ALL_EXTS.contains(&ext.to_string_lossy().to_lowercase())
|
||||
})
|
||||
{
|
||||
if let Some(path_str) = path.to_str() {
|
||||
result.push(path_str.to_string());
|
||||
}
|
||||
} else if recursive && path.is_dir() {
|
||||
if let Some(path_str) = path.to_str() {
|
||||
let mut sub_files = find_files(&path_str.to_string(), recursive)?;
|
||||
result.append(&mut sub_files);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn collect_files(path: &String, recursive: bool) -> io::Result<(Vec<String>, bool)> {
|
||||
let pa = Path::new(path);
|
||||
if pa.is_dir() {
|
||||
return Ok((find_files(path, recursive)?, true));
|
||||
}
|
||||
if pa.is_file() {
|
||||
return Ok((vec![path.clone()], false));
|
||||
}
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!("Path {} is neither a file nor a directory", pa.display()),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn read_file<F: AsRef<Path> + ?Sized>(f: &F) -> io::Result<Vec<u8>> {
|
||||
let mut content = Vec::new();
|
||||
if f.as_ref() == Path::new("-") {
|
||||
io::stdin().read_to_end(&mut content)?;
|
||||
} else {
|
||||
content = fs::read(f)?;
|
||||
}
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
pub fn write_file<F: AsRef<Path> + ?Sized>(f: &F) -> io::Result<Box<dyn Write>> {
|
||||
Ok(if f.as_ref() == Path::new("-") {
|
||||
Box::new(io::stdout())
|
||||
} else {
|
||||
Box::new(fs::File::create(f)?)
|
||||
})
|
||||
}
|
||||
4
src/utils/mod.rs
Normal file
4
src/utils/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod encoding;
|
||||
#[cfg(windows)]
|
||||
mod encoding_win;
|
||||
pub mod files;
|
||||
Reference in New Issue
Block a user