Skip to content

Commit

Permalink
feat(deserailize): Deserialize
Browse files Browse the repository at this point in the history
Signed-off-by: dark0dave <dark0dave@mykolab.com>
  • Loading branch information
dark0dave committed Nov 13, 2023
1 parent 24d41b3 commit 83819f3
Show file tree
Hide file tree
Showing 35 changed files with 1,177 additions and 789 deletions.
54 changes: 8 additions & 46 deletions .github/workflows/safety.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,17 @@ concurrency:
cancel-in-progress: true
name: safety
jobs:
sanitizers:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Install nightly
uses: dtolnay/rust-toolchain@nightly
- run: |
# to get the symbolizer for debug symbol resolution
sudo apt install llvm
# to fix buggy leak analyzer:
# https://github.com/japaric/rust-san#unrealiable-leaksanitizer
# ensure there's a profile.dev section
if ! grep -qE '^[ \t]*[profile.dev]' Cargo.toml; then
echo >> Cargo.toml
echo '[profile.dev]' >> Cargo.toml
fi
# remove pre-existing opt-levels in profile.dev
sed -i '/^\s*\[profile.dev\]/,/^\s*\[/ {/^\s*opt-level/d}' Cargo.toml
# now set opt-level to 1
sed -i '/^\s*\[profile.dev\]/a opt-level = 1' Cargo.toml
cat Cargo.toml
name: Enable debug symbols
- name: cargo test -Zsanitizer=address
# only --lib --tests b/c of https://github.com/rust-lang/rust/issues/53945
run: cargo test --lib --tests --all-features --target x86_64-unknown-linux-gnu
env:
ASAN_OPTIONS: "detect_odr_violation=0:detect_leaks=0"
RUSTFLAGS: "-Z sanitizer=address"
- name: cargo test -Zsanitizer=leak
if: always()
run: cargo test --all-features --target x86_64-unknown-linux-gnu
env:
RUSTFLAGS: "-Z sanitizer=leak"
miri:
name: "Miri"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- run: |
echo "NIGHTLY=nightly-$(curl -s https://rust-lang.github.io/rustup-components-history/x86_64-unknown-linux-gnu/miri)" >> $GITHUB_ENV
- name: Install ${{ env.NIGHTLY }}
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.NIGHTLY }}
components: miri
- name: cargo miri test
- name: Install Miri
run: |
rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup
- name: Test with Miri
run: cargo miri test
env:
MIRIFLAGS: ""
MIRIFLAGS: '-Zmiri-backtrace=full -Zmiri-disable-isolation'
8 changes: 4 additions & 4 deletions Cargo.lock

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

41 changes: 32 additions & 9 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
pub mod args;
use std::{
fs::{self, File},
io::{BufReader, Read},
io::{BufReader, Read, Write},
path::Path,
process::exit,
str,
};

use args::Args;
Expand All @@ -13,12 +14,31 @@ use models::{
key::Key,
model::Model,
resources::types::{extention_to_resource_type, ResourceType},
spell::Spell,
tlk::Lookup,
};

use erased_serde::Serializer;

fn write_file(path: &Path, model: std::rc::Rc<dyn Model>) {
fn write_file(path: &Path, extension: &str, buffer: &[u8]) {
let file_name = Path::new(path.file_stem().unwrap_or_default()).with_extension(extension);
if let Ok(mut file) = File::create(file_name) {
if let Err(err) = file.write_all(buffer) {
println!("Error: {}", err);
}
}
}

fn json_back_to_ie_type(path: &Path) {
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")
}
}

fn write_model(path: &Path, model: std::rc::Rc<dyn Model>) {
let file_name = Path::new(path.file_stem().unwrap_or_default()).with_extension("json");
if let Ok(file) = File::create(file_name) {
let mut json = serde_json::Serializer::new(file);
Expand All @@ -29,7 +49,7 @@ fn write_file(path: &Path, model: std::rc::Rc<dyn Model>) {
}
}

fn from_file(path: &Path) -> Vec<u8> {
fn read_file(path: &Path) -> Vec<u8> {
let file = File::open(path).unwrap_or_else(|_| panic!("Could not open file: {:#?}", path));
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
Expand All @@ -40,7 +60,7 @@ fn from_file(path: &Path) -> Vec<u8> {
}

fn parse_tlk_file(path: &Path) -> Lookup {
let buffer = from_file(path);
let buffer = read_file(path);
Lookup::new(&buffer)
}

Expand All @@ -51,14 +71,14 @@ fn parse_key_file(path: &Path, buffer: &[u8]) -> Vec<Biff> {
key.bif_entries
.iter()
.map(|bif_ref| {
let buffer = from_file(&parent.join(bif_ref.name.to_string()).with_extension("bif"));
let buffer = read_file(&parent.join(bif_ref.name.to_string()).with_extension("bif"));
Biff::new(&buffer)
})
.collect()
}

fn get_model_from_file(path: &Path, json: bool) -> Vec<Biff> {
let buffer = from_file(path);
let buffer = read_file(path);
let extention = path
.extension()
.unwrap_or_default()
Expand All @@ -71,15 +91,18 @@ fn get_model_from_file(path: &Path, json: bool) -> Vec<Biff> {
if resource_type == ResourceType::NotFound {
return match extention.as_str() {
"key" => parse_key_file(path, &buffer),
"biff" => vec![Biff::new(&from_file(path))],
"json" => panic!(),
"biff" => vec![Biff::new(&read_file(path))],
"json" => {
json_back_to_ie_type(&path);
exit(0)
}
_ => panic!("Unprocessable file type: {:?}", path.as_os_str()),
};
}

let model = from_buffer(&buffer, resource_type).expect("Could not parse file");
if json {
write_file(path, model);
write_model(path, model);
} else {
println!("{:?}", model);
}
Expand Down
7 changes: 6 additions & 1 deletion models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ edition = "2021"

[dependencies]
erased-serde = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0.189", features = ["derive"] }

[dev-dependencies]
pretty_assertions = "1.3.0"
serde_json = "1.0.94"
tempfile = "3"
Binary file added models/fixtures/WORLDMAP.WMP
Binary file not shown.
Loading

0 comments on commit 83819f3

Please sign in to comment.