arcdps/
lib.rs

1//! Bindings for [ArcDPS](https://www.deltaconnected.com/arcdps/) plugins.
2//!
3//! # Usage
4//! Plugins export information for ArcDPS via the [`export!`] macro.
5//! To see which fields are supported by it, have a look at [`SupportedFields`].
6//!
7//! ```no_run
8//! # mod test {
9//! use std::error::Error;
10//! use arcdps::{Agent, Event, StateChange};
11//!
12//! arcdps::export! {
13//!     name: "Example Plugin",
14//!     sig: 0x12345678, // change this to a random number
15//!     init,
16//!     combat: custom_combat_name,
17//! }
18//!
19//! fn init() -> Result<(), Option<String>> {
20//!     // may return an error to indicate load failure
21//!     Ok(())
22//! }
23//!
24//! fn custom_combat_name(
25//!     event: Option<&Event>,
26//!     src: Option<&Agent>,
27//!     dst: Option<&Agent>,
28//!     skill_name: Option<&str>,
29//!     id: u64,
30//!     revision: u64,
31//! ) {
32//!     if let Some(event) = event {
33//!         if let StateChange::EnterCombat = event.get_statechange() {
34//!             // source agent has entered combat
35//!         }
36//!     }
37//! }
38//! # }
39//! ```
40//!
41//! # Unofficial Extras
42//! [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases) support is hidden behind the `extras` feature flag.
43//!
44//! ```no_run
45//! # mod test {
46//! use arcdps::extras::{UserInfoIter, UserRole};
47//!
48//! arcdps::export! {
49//!     name: "Example Plugin",
50//!     sig: 123,
51//!     extras_squad_update,
52//! }
53//!
54//! fn extras_squad_update(users: UserInfoIter) {
55//!     for user in users {
56//!         if let UserRole::SquadLeader | UserRole::Lieutenant = user.role {
57//!             // user can place markers
58//!         }
59//!     }
60//! }
61//! # }
62//! ```
63//!
64//! # Initializing manually
65//!
66//! When not using the [`export!`] macro, Arc, ImGui, and DirectX information has to be initialized manually.
67//! Accessing Arc information/exports or ImGui without initializing them will **panic**.
68//!
69//! ```ignore
70//! use arcdps::{init_arc, init_dxgi, init_imgui};
71//!
72//! unsafe {
73//!     init_arc(arc_handle, arc_version);
74//!     init_imgui(imgui_ctx, malloc, free);
75//!     init_dxgi(id3d, d3d_version);
76//! }
77//! ```
78
79#![allow(clippy::missing_safety_doc)]
80
81pub mod callbacks;
82pub mod evtc;
83pub mod exports;
84
85#[cfg(feature = "extras")]
86pub mod extras;
87
88#[cfg(feature = "log")]
89pub mod log;
90
91mod globals;
92mod util;
93
94#[cfg(feature = "panic")]
95mod panic;
96
97#[cfg(feature = "codegen")]
98pub use arcdps_codegen::export;
99
100pub use crate::globals::{
101    arc::init_arc,
102    dxgi::{d3d11_device, d3d_version, dxgi_swap_chain, init_dxgi},
103    imgui::{imgui_context, init_imgui, with_ui},
104};
105pub use crate::util::strip_account_prefix;
106pub use arcdps_imgui as imgui;
107pub use evtc::{
108    Activation, Affinity, Agent, AgentOwned, Attribute, BuffCategory, BuffCycle, BuffRemove,
109    CustomSkill, Event, Language, Profession, Specialization, StateChange, Strike,
110};
111
112use callbacks::*;
113
114#[cfg(feature = "extras")]
115use extras::callbacks::*;
116
117/// Reference on what fields are currently supported by the [`export!`] macro.
118///
119/// This struct is not used anywhere.
120pub struct SupportedFields {
121    /// Name of the plugin.
122    pub name: &'static str,
123
124    /// Unique signature of the plugin.
125    ///
126    /// Pick a random number that is not used by other modules.
127    pub sig: u32,
128
129    /// Callback for plugin load.
130    ///
131    /// May return an error with an optional error message to signal load failure.
132    pub init: Option<InitFunc>,
133
134    /// Callback for plugin unload.
135    pub release: Option<ReleaseFunc>,
136
137    /// Callback for plugin unload.
138    // TODO: higher level abstraction?
139    pub update_url: Option<UpdateUrlFunc>,
140
141    /// Raw WndProc callback.
142    pub raw_wnd_nofilter: Option<RawWndProcCallback>,
143
144    /// Raw ImGui callback.
145    pub raw_imgui: Option<RawImguiCallback>,
146
147    /// Raw options callback.
148    pub raw_options_end: Option<RawOptionsCallback>,
149
150    /// Raw combat callback.
151    pub raw_combat: Option<RawCombatCallback>,
152
153    /// Raw filtered WndProc callback.
154    pub raw_wnd_filter: Option<RawWndProcCallback>,
155
156    /// Raw options windows callback.
157    pub raw_options_windows: Option<RawOptionsWindowsCallback>,
158
159    /// Raw local combat callback.
160    pub raw_combat_local: Option<RawCombatCallback>,
161
162    /// Callback for key presses.
163    ///
164    /// Returning `true` will allow ArcDPS and GW2 to receive the key press.
165    /// First parameter indicates the [virtual key code](https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes).
166    /// Second parameter is `true` if the key was pressed and `false` when released.
167    /// Third parameter is `true` if the key was down before this event occurred, for example by holding it down.
168    pub wnd_nofilter: Option<WndProcCallback>,
169
170    /// Callback for area combat events.
171    ///
172    /// May be called asynchronously, use `id` to keep track of order.
173    /// First event id will be `2`.
174    ///
175    /// At least one participant will be a party/squad member or minion of, or a buff applied by squad in the case of buff remove.
176    /// Not all statechanges are present in the realtime API, see [`StateChange`] for details.
177    ///
178    /// No `event` and `src.elite == 0` indicates a tracking change.
179    /// Player was added when `src.prof != 0`, otherwise removed.
180    /// When added `dst.name` contains the account name,
181    /// `dst.id` the instance id,
182    /// `dst.prof` the [`Profession`],
183    /// `dst.elite` the elite [`Specialization`],
184    /// `dst.is_self` whether the added player is self (local player),
185    /// `src.team` the team and `dst.team` the subgroup.
186    ///
187    /// No `event` and `src.elite != 0` indicates a target change.
188    /// `src.id` will contain the new target.
189    ///
190    /// *Note that Arc's realtime combat API comes with an intentional delay and filtering.*
191    pub combat: Option<CombatCallback>,
192
193    /// Callback for standalone UI creation.
194    ///
195    /// Provides an [`imgui::Ui`] for drawing.
196    /// The second parameter is `true` whenever the player is **not** in character select, loading screens or forced cameras.
197    pub imgui: Option<ImguiCallback>,
198
199    /// Callback for plugin settings UI creation.
200    ///
201    /// Provides an [`imgui::Ui`] for drawing.
202    pub options_end: Option<OptionsCallback>,
203
204    /// Callback for local combat events.
205    ///
206    /// Same as [`combat`](Self::combat) but for events from chat log.
207    pub combat_local: Option<CombatCallback>,
208
209    /// Callback for filtered key presses.
210    ///
211    /// Same as [`wnd_nofilter`](Self::wnd_nofilter) but filtered to only notify when modifiers are pressed.
212    pub wnd_filter: Option<WndProcCallback>,
213
214    /// Callback for options windows.
215    ///
216    /// Called for each window checkbox in ArcDPS settings.
217    /// Last call will always be with [`None`].
218    /// Does not draw the checkbox if returning `true`.
219    pub options_windows: Option<OptionsWindowsCallback>,
220
221    /// Raw extras init callback.
222    ///
223    /// *Requires the `"extras"` feature.*
224    #[cfg(feature = "extras")]
225    pub raw_extras_init: Option<RawExtrasSubscriberInit>,
226
227    /// Raw extras squad update callback.
228    ///
229    /// *Requires the `"extras"` feature.*
230    #[cfg(feature = "extras")]
231    pub raw_extras_squad_update: Option<RawExtrasSquadUpdateCallback>,
232
233    /// Raw extras language changed callback.
234    ///
235    /// *Requires the `"extras"` feature.*
236    #[cfg(feature = "extras")]
237    pub raw_extras_language_changed: Option<RawExtrasLanguageChangedCallback>,
238
239    /// Raw extras keybind changed callback.
240    ///
241    /// *Requires the `"extras"` feature.*
242    #[cfg(feature = "extras")]
243    pub raw_extras_keybind_changed: Option<RawExtrasKeybindChangedCallback>,
244
245    /// Raw extras chat message callback.
246    ///
247    /// *Requires the `"extras"` feature.*
248    #[cfg(feature = "extras")]
249    pub raw_extras_chat_message: Option<RawExtrasChatMessageCallback>,
250
251    /// Initialization callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
252    ///
253    /// Can be called before or after ArcDPS [`init`](Self::init).
254    /// Receives information about the Unofficial Extras addon and the current player account name as parameters.
255    ///
256    /// *Requires the `"extras"` feature.*
257    #[cfg(feature = "extras")]
258    pub extras_init: Option<ExtrasInitFunc>,
259
260    /// Squad update callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
261    ///
262    /// Called whenever anything in the squad changes.
263    /// Only the users that changed are sent.
264    /// If a user was removed from the squad, their `role` will be set to [`UserRole::None`](crate::extras::UserRole::None).
265    ///
266    /// *Requires the `"extras"` feature.*
267    #[cfg(feature = "extras")]
268    pub extras_squad_update: Option<ExtrasSquadUpdateCallback>,
269
270    /// Language changed callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
271    ///
272    /// Called whenever the language is changed, either by changing it in the UI or by pressing the translation key (Right Ctrl by default).
273    ///
274    /// Will be called directly after initialization, with the current language, to get the startup language.
275    ///
276    /// *Requires the `"extras"` feature.*
277    #[cfg(feature = "extras")]
278    pub extras_language_changed: Option<ExtrasLanguageChangedCallback>,
279
280    /// Keybind changed callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
281    ///
282    /// Called whenever a keybind is changed, either by changing it in the ingame UI or with the presets feature of Unofficial Extras.
283    /// It is called for every keybind separately.
284    ///
285    /// After initialization this is called for every current keybind that exists.
286    /// If you want to get a single keybind, at any time you want, call the exported function.
287    ///
288    /// *Requires the `"extras"` feature.*
289    #[cfg(feature = "extras")]
290    pub extras_keybind_changed: Option<ExtrasKeybindChangedCallback>,
291
292    /// Squad chat message callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
293    ///
294    /// Called whenever a chat message is sent in your party/squad.
295    ///
296    /// *Requires the `"extras"` feature.*
297    #[cfg(feature = "extras")]
298    pub extras_squad_chat_message: Option<ExtrasSquadChatMessageCallback>,
299
300    /// Chat message callback for [Unofficial Extras](https://github.com/Krappa322/arcdps_unofficial_extras_releases).
301    ///
302    /// Called on different chat messages.
303    ///
304    /// *Requires the `"extras"` feature.*
305    #[cfg(feature = "extras")]
306    pub extras_chat_message: Option<ExtrasChatMessageCallback>,
307}
308
309/// Exports for usage in macros.
310#[doc(hidden)]
311pub mod __macro {
312    pub use crate::{
313        globals::imgui::{with_ui, FreeFn, MallocFn},
314        util::{str_from_cstr, str_to_wide, strip_account_prefix},
315    };
316    pub use std::ffi::{c_char, c_void};
317    pub use windows::Win32::{
318        Foundation::{HMODULE, HWND, LPARAM, WPARAM},
319        UI::WindowsAndMessaging::{WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP},
320    };
321
322    use crate::{
323        exports::has_e3_log_file,
324        globals::{dxgi::init_dxgi, imgui::init_imgui},
325        imgui, init_arc,
326    };
327
328    #[cfg(feature = "log")]
329    use crate::{exports::has_e8_log_window, log::ArcDpsLogger};
330
331    /// Internally used function to initialize with information received from Arc.
332    #[inline]
333    #[allow(clippy::too_many_arguments)]
334    pub unsafe fn init(
335        arc_version: *const c_char,
336        arc_handle: HMODULE,
337        imgui_ctx: *mut imgui::sys::ImGuiContext,
338        malloc: Option<MallocFn>,
339        free: Option<FreeFn>,
340        id3d: *mut c_void,
341        d3d_version: u32,
342        name: &'static str,
343    ) {
344        // arc exports have to be retrieved before panic hook & logging
345        init_arc(arc_handle, arc_version);
346
347        // only set panic hook if log file export was found
348        if has_e3_log_file() {
349            #[cfg(feature = "panic")]
350            crate::panic::init_panic_hook(name);
351
352            // only set logger if log file & window exports were found
353            #[cfg(feature = "log")]
354            if has_e8_log_window() {
355                let result = log::set_boxed_logger(Box::new(ArcDpsLogger::new(name)));
356                if result.is_ok() {
357                    log::set_max_level(log::LevelFilter::Trace);
358                }
359            }
360        }
361
362        // initialize imgui & dxgi
363        init_imgui(imgui_ctx, malloc, free);
364        init_dxgi(id3d, d3d_version);
365    }
366}