Skip to content

Commit 433f916

Browse files
Improve VX support (#68)
* Upgrade deps * Abstract some extract fns into common fn * Upgrade deps
1 parent 2cdfd0e commit 433f916

File tree

10 files changed

+67
-121
lines changed

10 files changed

+67
-121
lines changed

Cargo.lock

Lines changed: 33 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpgm-common-types/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT OR Apache-2.0"
66

77
[dependencies]
8-
flate2 = { version = "1.0.33" }
8+
flate2 = { version = "1.0.34" }
99
ruby-marshal = { git = "https://github.com/nathaniel-daniel/ruby-marshal-rs" }
1010
ruby-marshal-derive = { git = "https://github.com/nathaniel-daniel/ruby-marshal-rs" }
11-
serde = { version = "1.0.209", features = [ "derive" ] }
11+
serde = { version = "1.0.214", features = [ "derive" ] }

rpgm-common-types/src/script.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use flate2::bufread::ZlibDecoder;
22
use flate2::bufread::ZlibEncoder;
3+
use flate2::Compression;
34
use ruby_marshal::ArrayValue;
45
use ruby_marshal::FromValue;
56
use ruby_marshal::FromValueContext;
@@ -12,6 +13,8 @@ use ruby_marshal::ValueArena;
1213
use ruby_marshal::ValueHandle;
1314
use std::io::Read;
1415

16+
const ARRAY_LEN: usize = 3;
17+
1518
/// A list of compressed scripts
1619
#[derive(Debug)]
1720
pub struct CompressedScriptList {
@@ -66,7 +69,7 @@ impl std::fmt::Display for ScriptFromValueError {
6669
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6770
match self {
6871
Self::InvalidArrayLen { len } => {
69-
write!(f, "invalid script array len of {len}, expected 3")
72+
write!(f, "invalid script array len of {len}, expected {ARRAY_LEN}")
7073
}
7174
Self::InvalidName { .. } => {
7275
write!(f, "the script name is invalid")
@@ -113,7 +116,7 @@ impl<'a> FromValue<'a> for CompressedScript {
113116
let script = script.value();
114117

115118
let array_len = script.len();
116-
if array_len != 3 {
119+
if array_len != ARRAY_LEN {
117120
return Err(ScriptFromValueError::InvalidArrayLen { len: array_len }.into());
118121
}
119122

@@ -184,7 +187,8 @@ impl IntoValue for Script {
184187
let id = self.id.into_value(arena)?;
185188
let name = arena.create_string(self.name.into()).into_raw();
186189

187-
let mut encoder = ZlibEncoder::new(self.data.as_bytes(), Default::default());
190+
let compression = Compression::default();
191+
let mut encoder = ZlibEncoder::new(self.data.as_bytes(), compression);
188192
let mut data = Vec::new();
189193
encoder
190194
.read_to_end(&mut data)

rpgm-tool/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
license = "MIT OR Apache-2.0"
66

77
[dependencies]
8-
anyhow = "1.0.86"
8+
anyhow = "1.0.91"
99
argh = "0.1.12"
1010
camino = "1.1.9"
1111
memchr = "2.7.4"
@@ -14,7 +14,7 @@ rgssad = { git = "https://github.com/nathaniel-daniel/rgssad-rs", version = "0.0
1414
rpgmxp-types = { version = "0.0.0", path = "../rpgmxp-types" }
1515
rpgmvx-types = { version = "0.0.0", path = "../rpgmvx-types" }
1616
ruby-marshal = { git = "https://github.com/nathaniel-daniel/ruby-marshal-rs", version = "0.0.0" }
17-
serde = "1.0.209"
18-
serde_json = "1.0.127"
17+
serde = "1.0.214"
18+
serde_json = "1.0.132"
1919
walkdir = "2.5.0"
2020
rpgm-common-types = { version = "0.0.0", path = "../rpgm-common-types" }

rpgm-tool/src/commands/compile_assets/file_sink.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl FileSink {
8888
// Create a windows-style path.
8989
let path = path_components.join("\\");
9090

91-
writer.write_entry(&path, size, reader)?;
91+
writer.write_file(&path, size, reader)?;
9292
}
9393
}
9494

rpgm-tool/src/commands/extract_assets.rs

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ fn extract_xp(
258258
extract_map_infos(entry, output_path)?;
259259
}
260260
["Data", "System.rxdata"] if !options.skip_extract_system => {
261-
extract_xp_system(entry, output_path)?;
261+
extract_ruby_data::<rpgmxp_types::System>(entry, output_path)?;
262262
}
263263
["Data", file]
264264
if !options.skip_extract_maps && crate::util::is_map_file_name(file, "rxdata") =>
265265
{
266-
extract_xp_map(entry, output_path)?;
266+
extract_ruby_data::<rpgmxp_types::Map>(entry, output_path)?;
267267
}
268268
_ => {
269269
let temp_path = nd_util::with_push_extension(&output_path, "temp");
@@ -294,12 +294,12 @@ fn extract_vx(
294294
extract_map_infos(entry, output_path)?;
295295
}
296296
["Data", "System.rvdata"] if !options.skip_extract_system => {
297-
extract_vx_system(entry, output_path)?;
297+
extract_ruby_data::<rpgmvx_types::System>(entry, output_path)?;
298298
}
299299
["Data", file]
300300
if !options.skip_extract_maps && crate::util::is_map_file_name(file, "rvdata") =>
301301
{
302-
extract_vx_map(entry, output_path)?;
302+
extract_ruby_data::<rpgmvx_types::Map>(entry, output_path)?;
303303
}
304304
_ => {
305305
let temp_path = nd_util::with_push_extension(&output_path, "temp");
@@ -462,92 +462,25 @@ where
462462
Ok(())
463463
}
464464

465-
fn extract_xp_system<P>(file: impl std::io::Read, path: P) -> anyhow::Result<()>
465+
fn extract_ruby_data<T>(file: impl std::io::Read, path: impl AsRef<Path>) -> anyhow::Result<()>
466466
where
467-
P: AsRef<Path>,
468-
{
469-
let path = path.as_ref();
470-
let path = path.with_extension("json");
471-
472-
let arena = ruby_marshal::load(file)?;
473-
let ctx = FromValueContext::new(&arena);
474-
let system: rpgmxp_types::System = ctx.from_value(arena.root())?;
475-
476-
let temp_path = nd_util::with_push_extension(&path, "temp");
477-
let mut file = File::create_new(&temp_path)?;
478-
serde_json::to_writer_pretty(&mut file, &system)?;
479-
file.flush()?;
480-
file.sync_all()?;
481-
std::fs::rename(temp_path, path)?;
482-
483-
Ok(())
484-
}
485-
486-
fn extract_vx_system<P>(file: impl std::io::Read, path: P) -> anyhow::Result<()>
487-
where
488-
P: AsRef<Path>,
467+
T: serde::Serialize + for<'a> ruby_marshal::FromValue<'a>,
489468
{
490469
let path = path.as_ref();
491470
let path = path.with_extension("json");
492471

493472
let arena = ruby_marshal::load(file)?;
494473
let ctx = FromValueContext::new(&arena);
495-
let system: rpgmvx_types::System = ctx.from_value(arena.root())?;
474+
let data: T = ctx.from_value(arena.root())?;
496475

476+
// TODO: Lock?
477+
// TODO: Drop delete guard for file?
497478
let temp_path = nd_util::with_push_extension(&path, "temp");
498479
let mut file = File::create_new(&temp_path)?;
499-
serde_json::to_writer_pretty(&mut file, &system)?;
480+
serde_json::to_writer_pretty(&mut file, &data)?;
500481
file.flush()?;
501482
file.sync_all()?;
502483
std::fs::rename(temp_path, path)?;
503484

504485
Ok(())
505486
}
506-
507-
fn extract_xp_map<P>(file: impl std::io::Read, path: P) -> anyhow::Result<()>
508-
where
509-
P: AsRef<Path>,
510-
{
511-
use rpgmxp_types::Map;
512-
513-
let path = path.as_ref();
514-
let path = path.with_extension("json");
515-
516-
let arena = ruby_marshal::load(file)?;
517-
let ctx = FromValueContext::new(&arena);
518-
let map: Map = ctx.from_value(arena.root())?;
519-
let map = serde_json::to_string_pretty(&map)?;
520-
521-
// TODO: Lock?
522-
// TODO: Drop delete guard for file?
523-
let temp_path = nd_util::with_push_extension(&path, "temp");
524-
std::fs::write(&temp_path, map)?;
525-
526-
std::fs::rename(temp_path, path)?;
527-
528-
Ok(())
529-
}
530-
531-
fn extract_vx_map<P>(file: impl std::io::Read, path: P) -> anyhow::Result<()>
532-
where
533-
P: AsRef<Path>,
534-
{
535-
use rpgmvx_types::Map;
536-
537-
let path = path.as_ref();
538-
let path = path.with_extension("json");
539-
540-
let arena = ruby_marshal::load(file)?;
541-
let ctx = FromValueContext::new(&arena);
542-
let map: Map = ctx.from_value(arena.root())?;
543-
let map = serde_json::to_string_pretty(&map)?;
544-
545-
// TODO: Lock?
546-
// TODO: Drop delete guard for file?
547-
let temp_path = nd_util::with_push_extension(&path, "temp");
548-
std::fs::write(&temp_path, map)?;
549-
550-
std::fs::rename(temp_path, path)?;
551-
552-
Ok(())
553-
}

0 commit comments

Comments
 (0)