Skip to content

Commit

Permalink
temp fix: remove null values
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Aug 27, 2023
1 parent c349ae6 commit 06e79e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate winreg;
use std::error::Error;
use winreg::RegValue;
use std::result::Result;
mod fps_settings;
mod game_selection;
Expand Down Expand Up @@ -27,7 +28,18 @@ fn run_program() -> Result<(), Box<dyn Error>> {
let game = game_selection::get_game_selection()?;
let (reg_key_path, value_name_contains) = registry_info::get_registry_info(&game)?;
let raw_value = registry_info::get_raw_value(&reg_key_path, &value_name_contains)?;
let mut json_value = raw_value::parse_raw_value(&raw_value)?;
let mut json_value = match raw_value::parse_raw_value(&raw_value) {
Ok(value) => value,
Err(_) => {
println!("Failed to parse value, attempting to clean value...");
match raw_value::parse_raw_value(&clean_raw_value(&raw_value)) {
Ok(value) => value,
Err(_) => {
return Err("Failed to parse raw value after cleaning".into());
}
}
}
};
fps_settings::print_current_values(&game, &json_value);
let new_json_value = fps_settings::get_new_fps_settings(&game, &mut json_value)?;
let new_raw_value = winreg::RegValue {
Expand All @@ -40,3 +52,18 @@ fn run_program() -> Result<(), Box<dyn Error>> {

Ok(())
}

// remove null bytes from raw value
fn clean_raw_value(raw_value: &RegValue) -> RegValue {
let mut bytes = raw_value.bytes.clone();
let mut new_bytes = Vec::new();
for byte in bytes.iter_mut() {
if *byte != 00 {
new_bytes.push(*byte);
}
}
RegValue {
bytes: new_bytes,
vtype: raw_value.vtype.clone(),
}
}
5 changes: 1 addition & 4 deletions src/registry_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ pub fn get_raw_value(
value_name_contains: &str,
) -> Result<winreg::RegValue, Box<dyn Error>> {
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
// println!("Opening {:?} \n", reg_key_path);
let reg_key = hkcu.open_subkey_with_flags(reg_key_path, KEY_ALL_ACCESS)?;
// println!("Opened {:?} \n", reg_key_path);
let values = reg_key
.enum_values()
.map(|x| x.unwrap().0)
.collect::<Vec<_>>();
// println!("Found values: {:?} \n", values);
let value_name = values
.iter()
.find(|&x| x.contains(value_name_contains))
.ok_or_else(|| format!("Value {} not found", value_name_contains))?;
// println!("Found {} at {:?} \n", value_name_contains, value_name);
println!("Found {} at {:?} \n", value_name_contains, value_name);
reg_key
.get_raw_value(value_name)
.map_err(|e| format!("Failed to get raw value: {}", e).into())
Expand Down

0 comments on commit 06e79e4

Please sign in to comment.