Skip to content

Commit

Permalink
Replace load_header_only with a ParseOptions struct
Browse files Browse the repository at this point in the history
I keep forgetting whether that parameter is load_header_only or load_whole_plugin, so use a struct to be more explicit about that.
  • Loading branch information
Ortham committed Jun 26, 2024
1 parent 1238712 commit b1910f5
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 143 deletions.
10 changes: 5 additions & 5 deletions benches/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use criterion::Criterion;
use esplugin::{GameId, Plugin};
use esplugin::{GameId, ParseOptions, Plugin};

// HearthFires.esm is a 3.8 MB file, so it's got plenty of content without being
// large enough to slow down benchmarking much.
Expand All @@ -15,22 +15,22 @@ fn criterion_benchmark(c: &mut Criterion) {
let mut plugin = Plugin::new(GAME_ID, Path::new(PLUGIN_TO_PARSE));

b.iter(|| {
assert!(plugin.parse_file(true).is_ok());
assert!(plugin.parse_file(ParseOptions::header_only()).is_ok());
});
});

c.bench_function("Plugin.parse_file() full", |b| {
let mut plugin = Plugin::new(GAME_ID, Path::new(PLUGIN_TO_PARSE));

b.iter(|| {
assert!(plugin.parse_file(false).is_ok());
assert!(plugin.parse_file(ParseOptions::whole_plugin()).is_ok());
});
});

c.bench_function("Plugin.overlaps_with()", |b| {
let mut plugin = Plugin::new(GAME_ID, Path::new(PLUGIN_TO_PARSE));

assert!(plugin.parse_file(false).is_ok());
assert!(plugin.parse_file(ParseOptions::whole_plugin()).is_ok());

b.iter(|| {
assert!(plugin.overlaps_with(&plugin).unwrap());
Expand All @@ -40,7 +40,7 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("Plugin.count_override_records()", |b| {
let mut plugin = Plugin::new(GAME_ID, Path::new(PLUGIN_TO_PARSE));

assert!(plugin.parse_file(false).is_ok());
assert!(plugin.parse_file(ParseOptions::whole_plugin()).is_ok());

b.iter(|| {
assert_eq!(plugin.count_override_records().unwrap(), 1272);
Expand Down
19 changes: 16 additions & 3 deletions ffi/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{f32, panic, ptr};

use libc::{c_char, c_float, size_t};

use esplugin::{GameId, Plugin, PluginMetadata};
use esplugin::{GameId, ParseOptions, Plugin, PluginMetadata};

use crate::constants::*;
use crate::error::{error, handle_error};
Expand Down Expand Up @@ -102,7 +102,14 @@ pub unsafe extern "C" fn esp_plugin_parse(plugin_ptr: *mut Plugin, load_header_o
error(ESP_ERROR_NULL_POINTER, "Null pointer passed")
} else {
let plugin = &mut *plugin_ptr;
match plugin.parse_file(load_header_only) {

let options = if load_header_only {
ParseOptions::header_only()
} else {
ParseOptions::whole_plugin()
};

match plugin.parse_file(options) {
Ok(_) => ESP_OK,
Err(e) => handle_error(e),
}
Expand Down Expand Up @@ -310,7 +317,13 @@ pub unsafe extern "C" fn esp_plugin_is_valid(
Err(x) => return x,
};

*is_valid = Plugin::is_valid(mapped_game_id, rust_path, load_header_only);
let options = if load_header_only {
ParseOptions::header_only()
} else {
ParseOptions::whole_plugin()
};

*is_valid = Plugin::is_valid(mapped_game_id, rust_path, options);

ESP_OK
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::convert::{TryFrom, TryInto};

pub use crate::error::{Error, MoreDataNeeded, ParsingErrorKind};
pub use crate::game_id::GameId;
pub use crate::plugin::{plugins_metadata, Plugin, PluginMetadata};
pub use crate::plugin::{plugins_metadata, ParseOptions, Plugin, PluginMetadata};

mod error;
mod form_id;
Expand Down
Loading

0 comments on commit b1910f5

Please sign in to comment.