Skip to content

Commit

Permalink
wrap one more func
Browse files Browse the repository at this point in the history
  • Loading branch information
chayleaf committed Jul 5, 2023
1 parent bd29f3a commit a6cdee3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ To use:
2. Move `libkonigsberg.so`/`konigsberg.dll` to where
`libsteam_api.so`/`steam_api.dll` used to be

Warning: mold linker can't build this at the moment of writing this
3 changes: 3 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ unsafe fn sym<T>(name: &str) -> Result<libloading::Symbol<T>, libloading::Error>
}

import!(fn SteamInternal_FindOrCreateUserInterface(hSteamUser: HSteamUser, pszVersion: *const c_char) -> *mut c_void);
import!(fn SteamAPI_SteamApps_v008() -> *mut ISteamApps);
// future proof?
import!(fn SteamAPI_SteamApps_v009() -> *mut ISteamApps);

#[cfg(feature = "rebuild-reexports")]
include!(concat!(env!("OUT_DIR"), "/reexports.rs"));
Expand Down
37 changes: 25 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
ffi::{c_char, c_void, CStr},
sync::Mutex,
};
use steamworks_sys::{AppId_t, HSteamUser};
use steamworks_sys::{AppId_t, HSteamUser, ISteamApps};

mod ffi;

Expand All @@ -27,13 +27,15 @@ struct Offsets {
// offset_b_get_dlc_data_by_index: Option<usize>,
}

unsafe fn offsets(ver: *const c_char) -> Option<Offsets> {
let ver = CStr::from_ptr(ver).to_str().ok()?;
if !ver.starts_with("STEAMAPPS_INTERFACE_VERSION")
|| ver.ends_with("001")
|| ver.ends_with("002")
{
return None;
unsafe fn offsets(ver: Option<*const c_char>) -> Option<Offsets> {
if let Some(ver) = ver {
let ver = CStr::from_ptr(ver).to_str().ok()?;
if !ver.starts_with("STEAMAPPS_INTERFACE_VERSION")
|| ver.ends_with("001")
|| ver.ends_with("002")
{
return None;
}
}
Some(Offsets {
offset_b_is_dlc_installed: 7,
Expand Down Expand Up @@ -61,13 +63,13 @@ unsafe extern "C" fn b_is_dlc_installed(_app_id: AppId_t) -> bool {
true
}

unsafe fn patch_ptr(ver: *const c_char, ret: *mut c_void) -> *mut c_void {
unsafe fn patch_ptr(ver: Option<*const c_char>, ret: *mut c_void) -> *mut c_void {
static PATCH_DONE: OnceCell<Mutex<HashSet<usize>>> = OnceCell::new();
if !ret.is_null() {
if let Some(ofs) = offsets(ver) {
let done = PATCH_DONE.get_or_init(Default::default);
let patch_done = PATCH_DONE.get_or_init(Default::default);
let vtable = *(ret as *mut *mut usize);
let mut lock = done.lock().unwrap();
let mut lock = patch_done.lock().unwrap();
if lock.contains(&(vtable as usize)) {
return ret;
}
Expand Down Expand Up @@ -97,5 +99,16 @@ pub unsafe extern "C" fn SteamInternal_FindOrCreateUserInterface(
user: HSteamUser,
ver: *const c_char,
) -> *mut c_void {
patch_ptr(ver, ffi::SteamInternal_FindOrCreateUserInterface(user, ver))
patch_ptr(Some(ver), ffi::SteamInternal_FindOrCreateUserInterface(user, ver))
}

#[allow(non_snake_case, clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn SteamAPI_SteamApps_v008() -> *mut ISteamApps {
patch_ptr(None, ffi::SteamAPI_SteamApps_v008() as *mut c_void) as *mut ISteamApps
}
#[allow(non_snake_case, clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn SteamAPI_SteamApps_v009() -> *mut ISteamApps {
patch_ptr(None, ffi::SteamAPI_SteamApps_v009() as *mut c_void) as *mut ISteamApps
}
1 change: 0 additions & 1 deletion src/reexports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ reexport!(fn SteamAPI_ISteamUserStats_GetGlobalStatHistoryInt64(self_ : * mut IS
reexport!(fn SteamAPI_ISteamUserStats_GetGlobalStatHistoryDouble(self_ : * mut ISteamUserStats, pchStatName : * const :: std :: os :: raw :: c_char, pData : * mut f64, cubData : uint32) -> int32);
reexport!(fn SteamAPI_ISteamUserStats_GetAchievementProgressLimitsInt32(self_ : * mut ISteamUserStats, pchName : * const :: std :: os :: raw :: c_char, pnMinProgress : * mut int32, pnMaxProgress : * mut int32) -> bool);
reexport!(fn SteamAPI_ISteamUserStats_GetAchievementProgressLimitsFloat(self_ : * mut ISteamUserStats, pchName : * const :: std :: os :: raw :: c_char, pfMinProgress : * mut f32, pfMaxProgress : * mut f32) -> bool);
reexport!(fn SteamAPI_SteamApps_v008() -> * mut ISteamApps);
reexport!(fn SteamAPI_ISteamApps_BIsSubscribed(self_ : * mut ISteamApps) -> bool);
reexport!(fn SteamAPI_ISteamApps_BIsLowViolence(self_ : * mut ISteamApps) -> bool);
reexport!(fn SteamAPI_ISteamApps_BIsCybercafe(self_ : * mut ISteamApps) -> bool);
Expand Down

0 comments on commit a6cdee3

Please sign in to comment.