mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-07 13:28:47 +08:00
Add support to create script file. Add bgi ._bsi file support
This commit is contained in:
@@ -79,6 +79,34 @@ pub trait ScriptBuilder: std::fmt::Debug {
|
||||
"This script type does not support creating an archive."
|
||||
))
|
||||
}
|
||||
|
||||
fn can_create_file(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn create_file<'a>(
|
||||
&'a self,
|
||||
_filename: &'a str,
|
||||
_writer: Box<dyn WriteSeek + 'a>,
|
||||
_encoding: Encoding,
|
||||
_file_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
Err(anyhow::anyhow!(
|
||||
"This script type does not support creating directly."
|
||||
))
|
||||
}
|
||||
|
||||
fn create_file_filename(
|
||||
&self,
|
||||
filename: &str,
|
||||
output_filename: &str,
|
||||
encoding: Encoding,
|
||||
file_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
let f = std::fs::File::create(output_filename)?;
|
||||
let f = std::io::BufWriter::new(f);
|
||||
self.create_file(filename, Box::new(f), encoding, file_encoding)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ArchiveContent {
|
||||
|
||||
156
src/scripts/bgi/bsi.rs
Normal file
156
src/scripts/bgi/bsi.rs
Normal file
@@ -0,0 +1,156 @@
|
||||
use crate::ext::io::*;
|
||||
use crate::scripts::base::*;
|
||||
use crate::types::*;
|
||||
use crate::utils::encoding::{decode_to_string, encode_string};
|
||||
use anyhow::Result;
|
||||
use std::collections::BTreeMap;
|
||||
use std::ffi::CString;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BGIBsiScriptBuilder {}
|
||||
|
||||
impl BGIBsiScriptBuilder {
|
||||
pub fn new() -> Self {
|
||||
BGIBsiScriptBuilder {}
|
||||
}
|
||||
}
|
||||
|
||||
impl ScriptBuilder for BGIBsiScriptBuilder {
|
||||
fn default_encoding(&self) -> Encoding {
|
||||
Encoding::Utf8
|
||||
}
|
||||
|
||||
fn build_script(
|
||||
&self,
|
||||
buf: Vec<u8>,
|
||||
_filename: &str,
|
||||
encoding: Encoding,
|
||||
_archive_encoding: Encoding,
|
||||
config: &ExtraConfig,
|
||||
) -> Result<Box<dyn Script>> {
|
||||
Ok(Box::new(BGIBsiScript::new(buf, encoding, config)?))
|
||||
}
|
||||
|
||||
fn extensions(&self) -> &'static [&'static str] {
|
||||
&["_bsi"]
|
||||
}
|
||||
|
||||
fn script_type(&self) -> &'static ScriptType {
|
||||
&ScriptType::BGIBsi
|
||||
}
|
||||
|
||||
fn can_create_file(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn create_file<'a>(
|
||||
&'a self,
|
||||
filename: &'a str,
|
||||
writer: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
file_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
create_file(filename, writer, encoding, file_encoding)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BGIBsiScript {
|
||||
pub data: BTreeMap<String, BTreeMap<String, String>>,
|
||||
}
|
||||
|
||||
impl BGIBsiScript {
|
||||
pub fn new(buf: Vec<u8>, encoding: Encoding, _config: &ExtraConfig) -> Result<Self> {
|
||||
let mut data = BTreeMap::new();
|
||||
let mut reader = MemReader::new(buf);
|
||||
let section_count = reader.read_u32()?;
|
||||
for _ in 0..section_count {
|
||||
let section_name = reader.read_cstring()?;
|
||||
let section_name = decode_to_string(encoding, section_name.as_bytes())?;
|
||||
let mut section_data = BTreeMap::new();
|
||||
let entry_count = reader.read_u32()?;
|
||||
for _ in 0..entry_count {
|
||||
let key = reader.read_cstring()?;
|
||||
let key = decode_to_string(encoding, key.as_bytes())?;
|
||||
let value = reader.read_cstring()?;
|
||||
let value = decode_to_string(encoding, value.as_bytes())?;
|
||||
section_data.insert(key, value);
|
||||
}
|
||||
data.insert(section_name, section_data);
|
||||
}
|
||||
if !reader.is_eof() {
|
||||
eprintln!(
|
||||
"Warning: BGIBsiScript data not fully read, remaining bytes: {}",
|
||||
reader.data.len() - reader.pos
|
||||
);
|
||||
crate::COUNTER.inc_warning();
|
||||
}
|
||||
Ok(BGIBsiScript { data })
|
||||
}
|
||||
}
|
||||
|
||||
impl Script for BGIBsiScript {
|
||||
fn default_output_script_type(&self) -> OutputScriptType {
|
||||
OutputScriptType::Custom
|
||||
}
|
||||
|
||||
fn is_output_supported(&self, output: OutputScriptType) -> bool {
|
||||
matches!(output, OutputScriptType::Custom)
|
||||
}
|
||||
|
||||
fn default_format_type(&self) -> FormatOptions {
|
||||
FormatOptions::None
|
||||
}
|
||||
|
||||
fn custom_output_extension(&self) -> &'static str {
|
||||
"json"
|
||||
}
|
||||
|
||||
fn custom_export(&self, filename: &std::path::Path, encoding: Encoding) -> Result<()> {
|
||||
let s = serde_json::to_string_pretty(&self.data)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to write BSI Map data to JSON: {}", e))?;
|
||||
let mut writer = crate::utils::files::write_file(filename)?;
|
||||
let s = encode_string(encoding, &s, false)?;
|
||||
writer.write_all(&s)?;
|
||||
writer.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn custom_import<'a>(
|
||||
&'a self,
|
||||
custom_filename: &'a str,
|
||||
file: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
output_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
create_file(custom_filename, file, encoding, output_encoding)
|
||||
}
|
||||
}
|
||||
|
||||
fn create_file<'a>(
|
||||
custom_filename: &'a str,
|
||||
mut writer: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
output_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
let input = crate::utils::files::read_file(custom_filename)?;
|
||||
let s = decode_to_string(output_encoding, &input)?;
|
||||
let data: BTreeMap<String, BTreeMap<String, String>> = serde_json::from_str(&s)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to read BSI Map data from JSON: {}", e))?;
|
||||
writer.write_u32(data.len() as u32)?;
|
||||
for (section_name, section_data) in data {
|
||||
let section_name_bytes = encode_string(encoding, §ion_name, false)?;
|
||||
let section_name = CString::new(section_name_bytes)?;
|
||||
writer.write_cstring(§ion_name)?;
|
||||
writer.write_u32(section_data.len() as u32)?;
|
||||
for (key, value) in section_data {
|
||||
let key_bytes = encode_string(encoding, &key, false)?;
|
||||
let key = CString::new(key_bytes)?;
|
||||
writer.write_cstring(&key)?;
|
||||
let value_bytes = encode_string(encoding, &value, false)?;
|
||||
let value = CString::new(value_bytes)?;
|
||||
writer.write_cstring(&value)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod bsi;
|
||||
mod parser;
|
||||
pub mod script;
|
||||
|
||||
@@ -49,6 +49,20 @@ impl ScriptBuilder for EscudeBinListBuilder {
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn can_create_file(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn create_file<'a>(
|
||||
&'a self,
|
||||
filename: &'a str,
|
||||
writer: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
file_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
create_file(filename, writer, encoding, file_encoding)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -257,6 +271,37 @@ impl EscudeBinList {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_file<'a>(
|
||||
custom_filename: &'a str,
|
||||
mut writer: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
output_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
let input = crate::utils::files::read_file(custom_filename)?;
|
||||
let s = decode_to_string(output_encoding, &input)?;
|
||||
let entries: Vec<ListEntry> = serde_json::from_str(&s)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to read Escude list from JSON: {}", e))?;
|
||||
writer.write_all(b"LIST")?;
|
||||
writer.write_u32(0)?; // Placeholder for size
|
||||
let mut total_size = 0;
|
||||
for entry in entries {
|
||||
let cur_pos = writer.stream_position()?;
|
||||
writer.write_u32(entry.id)?;
|
||||
writer.write_u32(0)?; // Placeholder for size
|
||||
entry.data.pack(&mut writer, false, encoding)?;
|
||||
let end_pos = writer.stream_position()?;
|
||||
let size = (end_pos - cur_pos - 8) as u32; // 8 bytes for id and size
|
||||
writer.seek(std::io::SeekFrom::Start(cur_pos + 4))?; // Seek to size position
|
||||
writer.write_u32(size)?;
|
||||
writer.seek(std::io::SeekFrom::Start(end_pos))?; // Seek to end
|
||||
total_size += size + 8;
|
||||
}
|
||||
writer.seek(std::io::SeekFrom::Start(4))?; // Seek back to size position
|
||||
writer.write_u32(total_size)?;
|
||||
writer.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Script for EscudeBinList {
|
||||
fn default_output_script_type(&self) -> OutputScriptType {
|
||||
OutputScriptType::Custom
|
||||
@@ -287,33 +332,11 @@ impl Script for EscudeBinList {
|
||||
fn custom_import<'a>(
|
||||
&'a self,
|
||||
custom_filename: &'a str,
|
||||
mut writer: Box<dyn WriteSeek + 'a>,
|
||||
writer: Box<dyn WriteSeek + 'a>,
|
||||
encoding: Encoding,
|
||||
output_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
let input = crate::utils::files::read_file(custom_filename)?;
|
||||
let s = decode_to_string(output_encoding, &input)?;
|
||||
let entries: Vec<ListEntry> = serde_json::from_str(&s)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to read Escude list from JSON: {}", e))?;
|
||||
writer.write_all(b"LIST")?;
|
||||
writer.write_u32(0)?; // Placeholder for size
|
||||
let mut total_size = 0;
|
||||
for entry in entries {
|
||||
let cur_pos = writer.stream_position()?;
|
||||
writer.write_u32(entry.id)?;
|
||||
writer.write_u32(0)?; // Placeholder for size
|
||||
entry.data.pack(&mut writer, false, encoding)?;
|
||||
let end_pos = writer.stream_position()?;
|
||||
let size = (end_pos - cur_pos - 8) as u32; // 8 bytes for id and size
|
||||
writer.seek(std::io::SeekFrom::Start(cur_pos + 4))?; // Seek to size position
|
||||
writer.write_u32(size)?;
|
||||
writer.seek(std::io::SeekFrom::Start(end_pos))?; // Seek to end
|
||||
total_size += size + 8;
|
||||
}
|
||||
writer.seek(std::io::SeekFrom::Start(4))?; // Seek back to size position
|
||||
writer.write_u32(total_size)?;
|
||||
writer.flush()?;
|
||||
Ok(())
|
||||
create_file(custom_filename, writer, encoding, output_encoding)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@ lazy_static::lazy_static! {
|
||||
Box::new(circus::script::CircusMesScriptBuilder::new()),
|
||||
#[cfg(feature = "bgi")]
|
||||
Box::new(bgi::script::BGIScriptBuilder::new()),
|
||||
#[cfg(feature = "bgi")]
|
||||
Box::new(bgi::bsi::BGIBsiScriptBuilder::new()),
|
||||
#[cfg(feature = "escude-arc")]
|
||||
Box::new(escude::archive::EscudeBinArchiveBuilder::new()),
|
||||
#[cfg(feature = "escude")]
|
||||
|
||||
Reference in New Issue
Block a user