Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/darksouls3/src/sprj/chr_ins.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ptr::NonNull;
use std::{borrow::Cow, ptr::NonNull};

use shared::{
FromStatic, InstanceError, InstanceResult, OwnedPtr, Subclass, Superclass, UnknownStruct,
Expand Down Expand Up @@ -258,11 +258,11 @@ impl PlayerIns {
pub unsafe fn local_player() -> InstanceResult<&'static mut Self> {
unsafe {
let Ok(world_chr_man) = WorldChrMan::instance() else {
return Err(InstanceError::NotFound);
return Err(InstanceError::NotFound(Cow::Borrowed("PlayerIns")));
};

let Some(mut player) = world_chr_man.main_player else {
return Err(InstanceError::NotFound);
return Err(InstanceError::NotFound(Cow::Borrowed("PlayerIns")));
};

Ok(player.as_mut())
Expand Down
8 changes: 2 additions & 6 deletions crates/darksouls3/src/sprj/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{borrow::Cow, fmt, ops, ptr, sync::LazyLock};

use pelite::pe64::Pe;

use shared::{FromStatic, InstanceError, InstanceResult, Program, util::IncompleteArrayField};
use shared::{FromStatic, InstanceResult, Program, util::IncompleteArrayField};

use super::ItemId;
use crate::rva;
Expand All @@ -20,11 +20,7 @@ impl FromStatic for MapItemMan {

/// Returns the singleton instance of `MapItemMan`.
unsafe fn instance() -> InstanceResult<&'static mut Self> {
let target = Program::current()
.rva_to_va(rva::get().map_item_man_ptr)
.map_err(|_| InstanceError::NotFound)? as *const *mut Self;

unsafe { (*target).as_mut() }.ok_or(InstanceError::Null)
unsafe { shared::load_static_indirect(rva::get().map_item_man_ptr) }
}
}

Expand Down
10 changes: 4 additions & 6 deletions crates/eldenring/src/cs/chr_ins.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use bitfield::bitfield;
use std::fmt::Display;
use std::mem::transmute;
use std::ptr::NonNull;
use std::{borrow::Cow, fmt::Display, mem::transmute, ptr::NonNull};

use bitfield::bitfield;
use pelite::pe64::Pe;
use vtable_rs::VPtr;

Expand Down Expand Up @@ -923,13 +921,13 @@ impl PlayerIns {
pub unsafe fn local_player() -> InstanceResult<&'static mut Self> {
unsafe {
let Ok(world_chr_man) = WorldChrMan::instance() else {
return Err(InstanceError::NotFound);
return Err(InstanceError::NotFound(Cow::Borrowed("PlayerIns")));
};

world_chr_man
.main_player
.as_deref_mut()
.ok_or(InstanceError::NotFound)
.ok_or(InstanceError::NotFound(Cow::Borrowed("PlayerIns")))
}
}
}
Expand Down
20 changes: 3 additions & 17 deletions crates/eldenring/src/dlcr.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{borrow::Cow, ptr::NonNull};

use shared::{FromStatic, OwnedPtr, Program};
use shared::{FromStatic, OwnedPtr};

use vtable_rs::VPtr;

use crate::{Vector, dlkr::DLAllocatorBase};
use crate::{Vector, dlkr::DLAllocatorBase, rva};

#[vtable_rs::vtable]
pub trait DLCipherKeyVmt {
Expand Down Expand Up @@ -346,20 +346,6 @@ impl FromStatic for CryptoSPIRegistry {
}

unsafe fn instance() -> fromsoftware_shared::InstanceResult<&'static mut Self> {
use crate::rva;
use pelite::pe64::Pe;

let target = Program::current()
.rva_to_va(rva::get().crypto_spi_registry)
.map_err(|_| fromsoftware_shared::InstanceError::NotFound)?
as *mut Option<NonNull<CryptoSPIRegistry>>;

unsafe {
target
.as_mut()
.and_then(|opt| opt.as_mut())
.map(|nn| nn.as_mut())
.ok_or(fromsoftware_shared::InstanceError::Null)
}
unsafe { shared::load_static_indirect(rva::get().crypto_spi_registry) }
}
}
19 changes: 10 additions & 9 deletions crates/shared/src/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ pub enum InstanceError {
/// The object's location wasn't found in the executable. This usually means
/// something is wrong with the logic of how the object is being loaded in
/// the first place.
#[error("Static object not found")]
NotFound,
#[error("Static object not found: {0}")]
NotFound(Cow<'static, str>),

/// The static object is defined, but it's currently set to null. For many
/// objects, this is a normal occurrence, and just means that the caller
/// should wait until it's defined to start using it.
#[error("Static object not initialized")]
Null,
#[error("Static object not initialized: {0}")]
Null(Cow<'static, str>),
}

/// A [Result] whose error type is [InstanceError].
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<T: FromSingleton> FromStatic for T {
unsafe fn instance() -> InstanceResult<&'static mut T> {
address_of::<T>()
.map(|mut ptr| unsafe { ptr.as_mut() })
.ok_or(InstanceError::NotFound)
.ok_or(InstanceError::NotFound(Self::name()))
}
}

Expand All @@ -91,9 +91,9 @@ impl<T: FromSingleton> FromStatic for T {
pub unsafe fn load_static_direct<T: FromStatic>(rva: Rva) -> InstanceResult<&'static mut T> {
let target = Program::current()
.rva_to_va(rva)
.map_err(|_| InstanceError::NotFound)? as *mut T;
.map_err(|_| InstanceError::NotFound(T::name()))? as *mut T;

unsafe { target.as_mut().ok_or(InstanceError::Null) }
unsafe { target.as_mut().ok_or(InstanceError::Null(T::name())) }
}

/// Loads a static reference to `T` from an [Rva] that points to a pointer to
Expand All @@ -107,13 +107,14 @@ pub unsafe fn load_static_direct<T: FromStatic>(rva: Rva) -> InstanceResult<&'st
pub unsafe fn load_static_indirect<T: FromStatic>(rva: Rva) -> InstanceResult<&'static mut T> {
let target = Program::current()
.rva_to_va(rva)
.map_err(|_| InstanceError::NotFound)? as *mut Option<NonNull<T>>;
.map_err(|_| InstanceError::NotFound(T::name()))?
as *mut Option<NonNull<T>>;

unsafe {
target
.as_mut()
.and_then(|opt| opt.as_mut())
.map(|nn| nn.as_mut())
.ok_or(InstanceError::Null)
.ok_or(InstanceError::Null(T::name()))
}
}