Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CryZe committed Jan 28, 2024
1 parent 1517f5e commit f256b2e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ time = { version = "0.3.5", default-features = false }

[target.'cfg(target_os = "wasi")'.dependencies]
libm = { version = "0.2.7", optional = true }
wasi = { version = "0.11.0", default-features = false }
wasi = { version = "0.11.0+wasi-snapshot-preview1", default-features = false }

[features]
alloc = []
Expand Down
11 changes: 10 additions & 1 deletion src/file_format/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{

use bytemuck::{Pod, Zeroable};

use crate::{string::ArrayCString, Address, Endian, Error, FromEndian, Process};
use crate::{string::ArrayCString, Address, Endian, Error, FromEndian, PointerSize, Process};

// Based on:
// https://refspecs.linuxfoundation.org/elf/elf.pdf
Expand Down Expand Up @@ -99,6 +99,15 @@ impl Bitness {
pub fn is_64(self) -> bool {
self == Self::BITNESS_64
}

/// Returns the pointer size for the given bitness.
pub const fn pointer_size(self) -> Option<PointerSize> {
Some(match self {
Self::BITNESS_64 => PointerSize::Bit64,
Self::BITNESS_32 => PointerSize::Bit32,
_ => return None,
})
}
}

/// Segment type identifier for the ELF program header
Expand Down
12 changes: 11 additions & 1 deletion src/file_format/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{fmt, mem};

use bytemuck::{Pod, Zeroable};

use crate::{string::ArrayCString, Address, Error, FromEndian, Process};
use crate::{string::ArrayCString, Address, Error, FromEndian, PointerSize, Process};

// Reference:
// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
Expand Down Expand Up @@ -236,6 +236,16 @@ impl MachineType {
pub const THUMB: Self = Self(0x1c2);
/// MIPS little-endian WCE v2
pub const WCEMIPSV2: Self = Self(0x169);

/// Returns the pointer size for the given machine type. Only the most
/// common machine types are supported.
pub const fn pointer_size(self) -> Option<PointerSize> {
Some(match self {
Self::AMD64 | Self::ARM64 | Self::IA64 => PointerSize::Bit64,
Self::I386 | Self::ARM => PointerSize::Bit32,
_ => return None,
})
}
}

/// Reads the size of the image of a module (`exe` or `dll`) from the given
Expand Down
12 changes: 8 additions & 4 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,19 @@ pub struct UntilProcessCloses<'a, F> {
future: F,
}

impl<F: Future<Output = ()>> Future for UntilProcessCloses<'_, F> {
type Output = ();
impl<T, F: Future<Output = T>> Future for UntilProcessCloses<'_, F> {
type Output = Option<T>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if !self.process.is_open() {
return Poll::Ready(());
return Poll::Ready(None);
}
// SAFETY: We are simply projecting the Pin.
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().future).poll(cx) }
unsafe {
Pin::new_unchecked(&mut self.get_unchecked_mut().future)
.poll(cx)
.map(Some)
}
}
}

Expand Down
15 changes: 4 additions & 11 deletions src/game_engine/unity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,9 @@ mod scene;
pub use self::scene::*;

fn value_from_string(value: &str) -> Option<u32> {
let mut temp_val = None;

if value.starts_with("0x") && value.len() > 2 {
if let Some(hex_val) = value.get(2..value.len()) {
if let Ok(val) = u32::from_str_radix(hex_val, 16) {
temp_val = Some(val)
}
}
} else if let Ok(val) = value.parse::<u32>() {
temp_val = Some(val)
if let Some(rem) = value.strip_prefix("0x") {
u32::from_str_radix(rem, 16).ok()
} else {
value.parse().ok()
}
temp_val
}
8 changes: 4 additions & 4 deletions src/runtime/settings/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ pub fn add_title(key: &str, description: &str, heading_level: u32) {
/// of settings. The description is what's shown to the user. The key of the
/// default option to show needs to be specified.
#[inline]
pub fn add_choice(key: &str, description: &str, default_item_key: &str) {
pub fn add_choice(key: &str, description: &str, default_option_key: &str) {
// SAFETY: We provide valid pointers and lengths to key, description and
// default_item_key. They are also guaranteed to be valid UTF-8 strings.
// default_option_key. They are also guaranteed to be valid UTF-8 strings.
unsafe {
sys::user_settings_add_choice(
key.as_ptr(),
key.len(),
description.as_ptr(),
description.len(),
default_item_key.as_ptr(),
default_item_key.len(),
default_option_key.as_ptr(),
default_option_key.len(),
)
}
}
Expand Down

0 comments on commit f256b2e

Please sign in to comment.