mirror of
https://github.com/lifegpc/msg-tool.git
synced 2026-06-08 05:48:46 +08:00
Use custom json serde method to import/export psb
This commit is contained in:
@@ -2,6 +2,7 @@ use crate::ext::io::*;
|
||||
use crate::ext::psb::*;
|
||||
use crate::scripts::base::*;
|
||||
use crate::types::*;
|
||||
use crate::utils::encoding::*;
|
||||
use anyhow::Result;
|
||||
use emote_psb::{PsbReader, PsbWriter};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
@@ -131,6 +132,10 @@ impl Script for ScnScript {
|
||||
FormatOptions::None
|
||||
}
|
||||
|
||||
fn is_output_supported(&self, _: OutputScriptType) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn extract_messages(&self) -> Result<Vec<Message>> {
|
||||
let mut messages = Vec::new();
|
||||
let root = self.psb.root();
|
||||
@@ -566,6 +571,34 @@ impl Script for ScnScript {
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn custom_export(&self, filename: &Path, encoding: Encoding) -> Result<()> {
|
||||
let s = json::stringify_pretty(self.psb.to_json(), 2);
|
||||
let mut f = crate::utils::files::write_file(filename)?;
|
||||
let b = encode_string(encoding, &s, false)?;
|
||||
f.write_all(&b)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn custom_import<'a>(
|
||||
&'a self,
|
||||
custom_filename: &'a str,
|
||||
file: Box<dyn WriteSeek + 'a>,
|
||||
_encoding: Encoding,
|
||||
output_encoding: Encoding,
|
||||
) -> Result<()> {
|
||||
let data = crate::utils::files::read_file(custom_filename)?;
|
||||
let s = decode_to_string(output_encoding, &data)?;
|
||||
let json = json::parse(&s)?;
|
||||
let mut psb = self.psb.clone();
|
||||
psb.from_json(&json)?;
|
||||
let psb = psb.to_psb();
|
||||
let writer = PsbWriter::new(psb, file);
|
||||
writer.finish().map_err(|e| {
|
||||
anyhow::anyhow!("Failed to write PSB to file {}: {:?}", self.filename, e)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
Reference in New Issue
Block a user