Skip to content

Commit

Permalink
Merge pull request #17 from dark0dave/feature/toJsonAndBackAgain
Browse files Browse the repository at this point in the history
feat(jsonAndBackAgain): Add feature to go from json to ie type
  • Loading branch information
dark0dave authored Mar 17, 2024
2 parents 10d503d + 4f49353 commit 6a52f69
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 10 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ repos:
rev: 3.0.0
hooks:
- id: git-dirty
- id: forbid-binary
exclude: >
(?x)^(
models/fixtures/.*?|docs/.*?
)$
- repo: https://github.com/commitizen-tools/commitizen
rev: v3.18.0
rev: v3.18.4
hooks:
- id: commitizen
stages: [commit-msg]
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use std::{
use args::Args;
use models::{
biff::Biff,
from_buffer,
from_buffer, from_json,
key::Key,
model::Model,
resources::types::{extension_to_resource_type, ResourceType},
save::Save,
spell::Spell,
tlk::Lookup,
};

Expand All @@ -31,18 +30,26 @@ fn write_file(path: &Path, extension: &str, buffer: &[u8]) {
}

fn json_back_to_ie_type(path: &Path) {
let extension = path
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.split(".")
.nth(1)
.unwrap_or_default()
.to_ascii_lowercase();

let resource_type = extension_to_resource_type(&extension);
let file_contents = read_file(path);
if let Ok(spell) = serde_json::from_slice::<Spell>(&file_contents) {
write_file(path, "spl", &spell.to_bytes())
} else {
panic!("Could not convert back to model")
}
let out = from_json(&file_contents, resource_type);
write_file(path, &extension, &out);
}

fn write_model(path: &Path, model: std::rc::Rc<dyn Model>, resource_type: ResourceType) {
let file_name = Path::new(path.file_stem().unwrap_or_default())
.with_extension(format!("{}.json", resource_type));
println!("{:?}", file_name);
println!("Saved as {:#?}", file_name);
if let Ok(file) = File::create(file_name) {
let mut json = serde_json::Serializer::new(file);
let mut format = <dyn Serializer>::erase(&mut json);
Expand Down
2 changes: 1 addition & 1 deletion models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ edition = "2021"
[dependencies]
erased-serde = "0.3"
serde = { version = "1.0.189", features = ["derive"] }
serde_json = "1.0.94"
flate2 = { version = "1.0.17" }

[dev-dependencies]
pretty_assertions = "1.3.0"
serde_json = "1.0.94"
tempfile = "3"
66 changes: 66 additions & 0 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,69 @@ pub fn from_buffer(buffer: &[u8], resource_type: ResourceType) -> Option<Rc<dyn
_ => None,
}
}

pub fn from_json(buffer: &[u8], resource_type: ResourceType) -> Vec<u8> {
let model: Rc<dyn Model> = match resource_type {
// I am skipping image files
ResourceType::FileTypeBmp => todo!(),
ResourceType::FileTypeMve => todo!(),
// I am skipping music files
ResourceType::FileTypeWav => todo!(),
// Skipping play back sounds
ResourceType::FileTypeWfx => todo!(),
// Skipping
ResourceType::FileTypePlt => todo!(),
// I am skipping image files
ResourceType::FileTypeBam => todo!(),
// I am skipping texture files
ResourceType::FileTypeWed => todo!(),
// I am skipping GUI defs
ResourceType::FileTypeChu => todo!(),
ResourceType::FileTypeTi => todo!(),
// I am skipping compress graphic files
ResourceType::FileTypeMos => todo!(),
ResourceType::FileTypeItm => Rc::new(serde_json::from_slice::<Item>(buffer).unwrap()),
ResourceType::FileTypeSpl => Rc::new(serde_json::from_slice::<Spell>(buffer).unwrap()),
// I am ignoring scripting files
ResourceType::FileTypeBcs => todo!(),
ResourceType::FileTypeIds => Rc::new(serde_json::from_slice::<Ids>(buffer).unwrap()),
ResourceType::FileTypeCre => Rc::new(serde_json::from_slice::<Creature>(buffer).unwrap()),
ResourceType::FileTypeAre => Rc::new(serde_json::from_slice::<Area>(buffer).unwrap()),
ResourceType::FileTypeDlg => Rc::new(serde_json::from_slice::<Dialogue>(buffer).unwrap()),
ResourceType::FileType2da => Rc::new(serde_json::from_slice::<TwoDA>(buffer).unwrap()),
// Game is a slow resource
ResourceType::FileTypeGam => Rc::new(serde_json::from_slice::<Game>(buffer).unwrap()),
ResourceType::FileTypeSto => Rc::new(serde_json::from_slice::<Store>(buffer).unwrap()),
ResourceType::FileTypeWmap => Rc::new(serde_json::from_slice::<WorldMap>(buffer).unwrap()),
ResourceType::FileTypeEff => Rc::new(serde_json::from_slice::<EffectV2>(buffer).unwrap()),
ResourceType::FileTypeBs => todo!(),
ResourceType::FileTypeChr => {
Rc::new(serde_json::from_slice::<ExpandedCharacter>(buffer).unwrap())
}
// I am skipping spell casting graphics
ResourceType::FileTypeVvc => todo!(),
// Skip visual effects
ResourceType::FileTypeVef => todo!(),
// I am skipping projectiles
ResourceType::FileTypePro => todo!(),
ResourceType::FileTypeBio => Rc::new(serde_json::from_slice::<Biography>(buffer).unwrap()),
ResourceType::FileTypeWbm => todo!(),
ResourceType::FileTypeFnt => todo!(),
ResourceType::FileTypeGui => todo!(),
ResourceType::FileTypeSql => todo!(),
// Skipping graphic data
ResourceType::FileTypePvrz => todo!(),
ResourceType::FileTypeGlsl => todo!(),
ResourceType::FileTypeTlk => Rc::new(serde_json::from_slice::<Lookup>(buffer).unwrap()),
ResourceType::FileTypeMenu => todo!(),
ResourceType::FileTypeTtf => todo!(),
ResourceType::FileTypePng => todo!(),
ResourceType::FileTypeBah => todo!(),
ResourceType::FileTypeIni => todo!(),
// Skipping sounds/ out of dialog text
ResourceType::FileTypeSrc => todo!(),
ResourceType::NotFound => todo!(),
_ => todo!(),
};
model.to_bytes()
}

0 comments on commit 6a52f69

Please sign in to comment.