Skip to content

Commit

Permalink
Make globals thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Jan 20, 2025
1 parent 6806b5d commit 2d93a91
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 103 deletions.
11 changes: 4 additions & 7 deletions arcdps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arcdps"
version = "0.15.1"
version = "0.16.0"
authors = ["Zerthox", "Greaka"]
edition = "2021"
description = "Rust bindings for ArcDPS plugins"
Expand All @@ -13,22 +13,19 @@ license = "MIT"
arcdps_codegen = { path = "../arcdps_codegen" }
arcdps-imgui = { version = "0.8.0", features = ["tables-api"] }
chrono = { version = "0.4.24", optional = true }
evtc = { path = "../evtc" }
evtc = { path = "../evtc", features = ["realtime"]}
log = { version = "0.4.17", features = ["std"], optional = true }
num_enum = "0.7.0"
serde = { version = "1.0.160", features = ["derive"], optional = true }
strum = { version = "0.26.1", features = ["derive"], optional = true }

[dependencies.windows]
version = "0.57.0"
features = [
windows = { version = "0.59.0", features = [
"System",
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
"Win32_System_LibraryLoader",
"Win32_Graphics_Dxgi",
"Win32_Graphics_Direct3D11",
]
] }

[features]
default = ["unwind"]
Expand Down
24 changes: 12 additions & 12 deletions arcdps/src/exports/has.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
use crate::globals::ARC_GLOBALS;
use crate::globals::ArcGlobals;

/// Checks whether export `e0` was found.
#[inline]
pub fn has_e0_config_path() -> bool {
unsafe { ARC_GLOBALS.e0 }.is_some()
ArcGlobals::get().e0.is_some()
}

/// Checks whether export `e3` was found.
#[inline]
pub fn has_e3_log_file() -> bool {
unsafe { ARC_GLOBALS.e3 }.is_some()
ArcGlobals::get().e3.is_some()
}

/// Checks whether export `e5` was found.
#[inline]
pub fn has_e5_colors() -> bool {
unsafe { ARC_GLOBALS.e5 }.is_some()
ArcGlobals::get().e5.is_some()
}

/// Checks whether export `e6` was found.
#[inline]
pub fn has_e6_ui_settings() -> bool {
unsafe { ARC_GLOBALS.e6 }.is_some()
ArcGlobals::get().e6.is_some()
}

/// Checks whether export `e7` was found.
#[inline]
pub fn has_e7_modifiers() -> bool {
unsafe { ARC_GLOBALS.e7 }.is_some()
ArcGlobals::get().e7.is_some()
}

/// Checks whether export `e8` was found.
#[inline]
pub fn has_e8_log_window() -> bool {
unsafe { ARC_GLOBALS.e8 }.is_some()
ArcGlobals::get().e8.is_some()
}

/// Checks whether export `e9` was found.
#[inline]
pub fn has_e9_add_event() -> bool {
unsafe { ARC_GLOBALS.e9 }.is_some()
ArcGlobals::get().e9.is_some()
}

/// Checks whether export `e10` was found.
#[inline]
pub fn has_e10_add_event_combat() -> bool {
unsafe { ARC_GLOBALS.e10 }.is_some()
ArcGlobals::get().e10.is_some()
}

/// Checks whether export `addextension2` was found.
#[inline]
pub fn has_add_extension() -> bool {
unsafe { ARC_GLOBALS.add_extension }.is_some()
ArcGlobals::get().add_extension.is_some()
}

/// Checks whether export `freeextension2` was found.
#[inline]
pub fn has_free_extension() -> bool {
unsafe { ARC_GLOBALS.free_extension }.is_some()
ArcGlobals::get().free_extension.is_some()
}

/// Checks whether export `listextension` was found.
#[inline]
pub fn has_list_extension() -> bool {
unsafe { ARC_GLOBALS.list_extension }.is_some()
ArcGlobals::get().list_extension.is_some()
}
10 changes: 4 additions & 6 deletions arcdps/src/exports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use self::has::*;

use crate::{
evtc::{Event, Profession},
globals::ARC_GLOBALS,
globals::ArcGlobals,
imgui::sys::ImVec4,
};
use num_enum::{IntoPrimitive, TryFromPrimitive};
Expand All @@ -32,7 +32,7 @@ use strum::{Display, EnumCount, EnumIter, IntoStaticStr, VariantNames};
/// Retrieves the ArcDPS version as string.
#[inline]
pub fn version() -> Option<&'static str> {
unsafe { ARC_GLOBALS.version }
ArcGlobals::get().version
}

/// Retrieves the config path from ArcDPS.
Expand Down Expand Up @@ -385,8 +385,6 @@ pub enum AddExtensionResult {
/// This uses version 2 (`freeextension2`) of the extension API.
#[inline]
pub fn free_extension(sig: u32) -> Option<HMODULE> {
match unsafe { raw::free_extension(sig) } {
HMODULE(0) => None,
handle => Some(handle),
}
let handle = unsafe { raw::free_extension(sig) };
(!handle.is_invalid()).then_some(handle)
}
28 changes: 15 additions & 13 deletions arcdps/src/exports/raw.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Raw ArcDPS exports.
use crate::{evtc::Event, globals::ARC_GLOBALS, imgui::sys::ImVec4};
use crate::{evtc::Event, globals::ArcGlobals, imgui::sys::ImVec4};
use std::{ffi::c_void, os::raw::c_char};
use windows::Win32::Foundation::HMODULE;

/// Returns the handle to the ArcDPS dll.
pub unsafe fn handle() -> HMODULE {
ARC_GLOBALS.handle
ArcGlobals::get().handle
}

/// Signature of the `e0` export. See [`e0_config_path`] for details.
Expand All @@ -15,7 +15,7 @@ pub type Export0 = unsafe extern "C" fn() -> *const u16;
/// Retrieves path to ArcDPS ini config file as wide char string.
#[inline]
pub unsafe fn e0_config_path() -> *const u16 {
ARC_GLOBALS.e0.expect("failed to find arc export e0")()
ArcGlobals::get().e0.expect("failed to find arc export e0")()
}

/// Signature of the `e3` export. See [`e3_log_file`] for details.
Expand All @@ -24,7 +24,7 @@ pub type Export3 = unsafe extern "C" fn(string: *const c_char);
/// Logs a string to `arcdps.log` file.
#[inline]
pub unsafe fn e3_log_file(string: *const c_char) {
ARC_GLOBALS.e3.expect("failed to find arc export e3")(string)
ArcGlobals::get().e3.expect("failed to find arc export e3")(string)
}

/// Signature of the `e5` export. See [`e5_colors`] for details.
Expand All @@ -33,7 +33,7 @@ pub type Export5 = unsafe extern "C" fn(out: *mut [*mut ImVec4; 5]);
/// Writes color array pointers to buffer.
#[inline]
pub unsafe fn e5_colors(buffer: *mut [*mut ImVec4; 5]) {
ARC_GLOBALS.e5.expect("failed to find arc export e5")(buffer)
ArcGlobals::get().e5.expect("failed to find arc export e5")(buffer)
}

/// Signature of the `e6` export. See [`e6_ui_settings`] for details.
Expand All @@ -42,7 +42,7 @@ pub type Export6 = unsafe extern "C" fn() -> u64;
/// Retrieves bit mask of current ArcDPS UI settings.
#[inline]
pub unsafe fn e6_ui_settings() -> u64 {
ARC_GLOBALS.e6.expect("failed to find arc export e6")()
ArcGlobals::get().e6.expect("failed to find arc export e6")()
}

/// Signature of the `e7` export. See [`e7_modifiers`] for details.
Expand All @@ -51,7 +51,7 @@ pub type Export7 = unsafe extern "C" fn() -> u64;
/// Retrieves modifier keys as virtual key codes.
#[inline]
pub unsafe fn e7_modifiers() -> u64 {
ARC_GLOBALS.e7.expect("failed to find arc export e7")()
ArcGlobals::get().e7.expect("failed to find arc export e7")()
}

/// Signature of the `e8` export. See [`e8_log_window`] for details.
Expand All @@ -62,7 +62,7 @@ pub type Export8 = unsafe extern "C" fn(string: *const c_char);
/// Colors are HTML-like: `<c=#aaaaaa>colored text</c>`.
#[inline]
pub unsafe fn e8_log_window(string: *const c_char) {
ARC_GLOBALS.e8.expect("failed to find arc export e8")(string)
ArcGlobals::get().e8.expect("failed to find arc export e8")(string)
}

/// Signature of the `e9` export. See [`e9_add_event`] for details.
Expand All @@ -74,7 +74,7 @@ pub type Export9 = unsafe extern "C" fn(event: *const Event, sig: u32);
/// Event will end up processed like ArcDPS events and logged to EVTC.
#[inline]
pub unsafe fn e9_add_event(event: *const Event, sig: u32) {
ARC_GLOBALS.e9.expect("failed to find arc export e9")(event, sig)
ArcGlobals::get().e9.expect("failed to find arc export e9")(event, sig)
}

/// Signature of the `e10` export. See [`e10_add_event_combat`] for details.
Expand All @@ -88,7 +88,9 @@ pub type Export10 = unsafe extern "C" fn(event: *const Event, sig: u32);
/// Contrary to [`e9_add_event`], the `skill_id` is treated as skill id and will be added to the EVTC skill table.
#[inline]
pub unsafe fn e10_add_event_combat(event: *const Event, sig: u32) {
ARC_GLOBALS.e10.expect("failed to find arc export e10")(event, sig)
ArcGlobals::get()
.e10
.expect("failed to find arc export e10")(event, sig)
}

/// Signature of the `addextension2` export. See [`add_extension`] for details.
Expand All @@ -102,7 +104,7 @@ pub type ExportAddExtension = unsafe extern "C" fn(handle: HMODULE) -> u32;
/// This uses version 2 (`addextension2`) of the extension API.
#[inline]
pub unsafe fn add_extension(handle: HMODULE) -> u32 {
ARC_GLOBALS
ArcGlobals::get()
.add_extension
.expect("failed to find arc export addextension2")(handle)
}
Expand All @@ -120,7 +122,7 @@ pub type ExportFreeExtension = unsafe extern "C" fn(sig: u32) -> HMODULE;
/// This uses version 2 (`freeextension2`) of the extension API.
#[inline]
pub unsafe fn free_extension(sig: u32) -> HMODULE {
ARC_GLOBALS
ArcGlobals::get()
.free_extension
.expect("failed to find arc export freeextension2")(sig)
}
Expand All @@ -136,7 +138,7 @@ pub type ExportListExtension = unsafe extern "C" fn(callback_fn: *const c_void);
pub unsafe fn list_extension(callback_fn: *const c_void) {
// TODO: is this sync?
// TODO: bindings should check for uninitialized
ARC_GLOBALS
ArcGlobals::get()
.list_extension
.expect("failed to find arc export listextension")(callback_fn)
}
Loading

0 comments on commit 2d93a91

Please sign in to comment.