nexus\api/
v2.rs

1//! Addon API version 2.
2
3use super::{
4    data_link::{RawDataGetResource, RawDataShareResource},
5    event::{RawEventRaise, RawEventRaiseNotification, RawEventSubscribe},
6    gui::{ImguiFree, ImguiMalloc, RawGuiAddRender, RawGuiRemRender},
7    hook::{RawHookCreate, RawHookDisable, RawHookEnable, RawHookRemove},
8    keybind::{
9        RawKeybindDeregister, RawKeybindRegisterWithStringOld, RawKeybindRegisterWithStructOld,
10    },
11    localization::{RawLocalizationTranslate, RawLocalizationTranslateTo},
12    log::RawLog,
13    paths::{RawGetAddonDir, RawGetCommonDir, RawGetGameDir},
14    quick_access::{
15        RawQuickAccessAddContextMenu, RawQuickAccessAddShortcut, RawQuickAccessGeneric,
16    },
17    texture::{
18        RawTextureGet, RawTextureGetOrCreateFromFile, RawTextureGetOrCreateFromMemory,
19        RawTextureGetOrCreateFromResource, RawTextureGetOrCreateFromUrl, RawTextureLoadFromFile,
20        RawTextureLoadFromMemory, RawTextureLoadFromResource, RawTextureLoadFromUrl,
21    },
22    wnd_proc::{RawWndProcAddRem, RawWndProcSendToGame},
23};
24use windows::Win32::Graphics::{Direct3D11::ID3D11Device, Dxgi::IDXGISwapChain};
25
26/// Nexus addon API (version 2).
27#[derive(Debug, Clone)]
28#[repr(C)]
29pub struct AddonApi {
30    /// DirectX swap chain.
31    pub swap_chain: IDXGISwapChain,
32
33    /// ImGui context.
34    pub imgui_context: *mut imgui::sys::ImGuiContext,
35
36    /// ImGui malloc function.
37    pub imgui_malloc: Option<ImguiMalloc>,
38
39    /// ImGui free function.
40    pub imgui_free: Option<ImguiFree>,
41
42    /// Registers a new render callback.
43    pub register_render: RawGuiAddRender,
44
45    /// Removes a registered render callback.
46    pub deregister_render: RawGuiRemRender,
47
48    /// Returns the path to the game directory.
49    ///
50    /// For example `"C:\Program Files\Guild Wars 2\"`.
51    pub get_game_dir: RawGetGameDir,
52
53    /// Returns a path to `"\addons\{name}"`.
54    ///
55    /// Passing `null` or `""` returns `"\addons"` without trailing slash.
56    pub get_addon_dir: RawGetAddonDir,
57
58    /// Returns the path to the common addon folder.
59    ///
60    /// Alias for `get_addon_dir("common")`.
61    pub get_common_dir: RawGetCommonDir,
62
63    /// MinHook create.
64    pub hook_create: RawHookCreate,
65
66    /// MinHook remove.
67    pub hook_remove: RawHookRemove,
68
69    /// MinHook enable.
70    pub hook_enable: RawHookEnable,
71
72    /// MinHook disable.
73    pub hook_disable: RawHookDisable,
74
75    /// Logs a message to the log window and log file.
76    ///
77    /// Supports custom coloring for addon window messages, for example `<c=#FF0000>this text is red</c>`.
78    pub log: RawLog,
79
80    /// Raises an event to all subscribing addons.
81    pub event_raise: RawEventRaise,
82
83    /// Raises an event without a payload.
84    ///
85    /// Alias `event_raise("EV_FOO", null)`.
86    pub event_raise_notification: RawEventRaiseNotification,
87
88    /// Registers a new event callback.
89    pub event_subscribe: RawEventSubscribe,
90
91    /// Removes a registered event callback.
92    pub event_unsubscribe: RawEventSubscribe,
93
94    /// Registers a new [WNDPROC](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-wndproc) callback.
95    pub register_wnd_proc: RawWndProcAddRem,
96
97    /// Removes a registered [WNDPROC](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-wndproc) callback.
98    pub deregister_wnd_proc: RawWndProcAddRem,
99
100    /// Sends a [WNDPROC](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-wndproc) directly to the game, bypassing other hooks.
101    pub send_wnd_proc_to_game_only: RawWndProcSendToGame,
102
103    /// Registers a new keybind handler for a given named keybind.
104    ///
105    /// Keybind is a string like `"ALT+SHIFT+T`.
106    pub keybind_register_with_string: RawKeybindRegisterWithStringOld,
107
108    /// Registers a new keybind handler for a given named keybind.
109    ///
110    /// Keybind is a [`Keybind`](crate::keybind::Keybind) struct.
111    pub keybind_register_with_struct: RawKeybindRegisterWithStructOld,
112
113    /// Removes a registered keybind.
114    pub keybind_deregister: RawKeybindDeregister,
115
116    /// Returns a pointer to the requested resource of `null` if it does not exist.
117    pub get_resource: RawDataGetResource,
118
119    /// Allocates a shared resource of the given size and returns a pointer to it for writing.
120    pub share_resource: RawDataShareResource,
121
122    /// Returns a pointer to the [`Texture`](crate::texture::Texture) or `null` if it does not exist.
123    pub get_texture: RawTextureGet,
124
125    /// Returns a pointer to the [`Texture`](crate::texture::Texture) or creates it from the file if it does not exist.
126    pub get_texture_or_create_from_file: RawTextureGetOrCreateFromFile,
127
128    /// Returns a pointer to the [`Texture`](crate::texture::Texture) or creates it from the resource if it does not exist.
129    pub get_texture_or_create_from_resource: RawTextureGetOrCreateFromResource,
130
131    /// Returns a pointer to the [`Texture`](crate::texture::Texture) or creates it from the URL if it does not exist.
132    pub get_texture_or_create_from_url: RawTextureGetOrCreateFromUrl,
133
134    /// Returns a pointer to the [`Texture`](crate::texture::Texture) or creates it from the memory if it does not exist.
135    pub get_texture_or_create_from_memory: RawTextureGetOrCreateFromMemory,
136
137    /// Creates a texture from the file and passes it to the callback when finished.
138    pub load_texture_from_file: RawTextureLoadFromFile,
139
140    /// Creates a texture from the resource and passes it to the callback when finished.
141    pub load_texture_from_resource: RawTextureLoadFromResource,
142
143    /// Creates a texture from the URL and passes it to the callback when finished.
144    pub load_texture_from_url: RawTextureLoadFromUrl,
145
146    /// Creates a texture from the memory and passes it to the callback when finished.
147    pub load_texture_from_memory: RawTextureLoadFromMemory,
148
149    /// Adds a new shortcut icon to the quick access with the given texture identifiers.
150    /// When clicked the given keybind identifier will be called.
151    pub add_shortcut: RawQuickAccessAddShortcut,
152
153    /// Removes a shortcut with the given identifier from quick access.
154    pub remove_shortcut: RawQuickAccessGeneric,
155
156    /// Sends a notification icon to the given shortcut.
157    pub notify_shortcut: RawQuickAccessGeneric,
158
159    /// Adds a new ImGui callback fired when right-clicking the Nexus icon.
160    pub add_simple_shortcut: RawQuickAccessAddContextMenu,
161
162    /// Removes a registered simple shortcut callback.
163    pub remove_simple_shortcut: RawQuickAccessGeneric,
164
165    /// Translates the identifier into the current active language.
166    /// Returns the same identifier if unavailable.
167    pub translate: RawLocalizationTranslate,
168
169    /// Translates the identifier into the given language.
170    /// Returns the same identifier if unavailable.
171    pub translate_to: RawLocalizationTranslateTo,
172}
173
174unsafe impl Sync for AddonApi {}
175
176unsafe impl Send for AddonApi {}
177
178impl AddonApi {
179    /// Nexus Addon API version.
180    pub const VERSION: i32 = 2;
181
182    /// Retrieves the DirectX 11 device associated with the swap chain.
183    #[inline]
184    pub fn get_d3d11_device(&self) -> Option<ID3D11Device> {
185        unsafe { self.swap_chain.GetDevice() }.ok()
186    }
187}