1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use serde::{Deserialize, Serialize};
use crate::log_error;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct MumbleLink {
pub ui_tick: u32,
pub f_avatar_position: [f32; 3],
pub f_avatar_front: [f32; 3],
pub f_camera_position: [f32; 3],
pub f_camera_front: [f32; 3],
pub identity: CIdentity,
pub context: CMumbleContext,
}
#[derive(Deserialize, Clone, Debug, Default, Serialize)]
pub struct MumbleIdentity {
pub name: String,
pub profession: u32,
pub spec: u32,
pub race: u32,
pub map_id: u32,
pub world_id: u32,
pub team_color_id: u32,
pub commander: bool,
pub fov: f32,
pub uisz: u32,
}
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
pub struct MumbleContext {
pub server_address: u32,
pub map_id: u32,
pub map_type: u32,
pub shard_id: u32,
pub instance: u32,
pub build_id: u32,
pub ui_state: u32,
pub compass_width: u32,
pub compass_height: u32,
pub compass_rotation: f32,
pub player_x: f32,
pub player_y: f32,
pub map_center_x: f32,
pub map_center_y: f32,
pub map_scale: f32,
pub process_id: u32,
pub mount_index: u32,
}
#[derive(Copy, Clone, Deserialize, Default, Debug, Serialize)]
pub struct WindowDimensions {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
}
impl MumbleLink {
pub fn update(&mut self, link_ptr: *const CMumbleLink) -> anyhow::Result<()> {
let cmlink = match unsafe { link_ptr.as_ref() } {
Some(thing) => thing,
None => panic!("link_ptr.as_ref returned None when trying to update MumbleLink. ptr is null. something is very wrong"),
};
if self.ui_tick == cmlink.ui_tick {
return Ok(());
}
self.ui_tick = cmlink.ui_tick;
self.f_avatar_position = cmlink.f_avatar_position;
self.f_avatar_front = cmlink.f_avatar_front;
self.f_camera_position = cmlink.f_camera_position;
self.f_camera_front = cmlink.f_camera_front;
self.identity.update(link_ptr)?;
self.context.update(link_ptr);
Ok(())
}
pub fn update_from_slice(&mut self, buffer: &[u8]) -> anyhow::Result<()> {
assert!(buffer.len() >= 1093);
Ok(self.update(buffer.as_ptr() as *const CMumbleLink)?)
}
}
impl CIdentity {
pub fn update(&mut self, link_ptr: *const CMumbleLink) -> anyhow::Result<()> {
use widestring::U16CStr;
let id =
U16CStr::from_slice_with_nul(unsafe { &(*link_ptr).identity }).map_err(log_error)?;
let id = id.to_string().map_err(log_error)?;
*self = serde_json::from_str::<CIdentity>(&id).map_err(log_error)?;
Ok(())
}
}
impl MumbleContext {
pub fn update(&mut self, link_ptr: *const CMumbleLink) {
let mc = unsafe {
std::ptr::read_volatile(&(*link_ptr).context as *const u8 as *const CMumbleContext)
};
self.server_address = u32::from_ne_bytes([
mc.server_address[4],
mc.server_address[5],
mc.server_address[6],
mc.server_address[7],
]);
self.map_id = mc.map_id;
self.map_type = mc.map_type;
self.shard_id = mc.shard_id;
self.instance = mc.instance;
self.build_id = mc.build_id;
self.ui_state = mc.ui_state;
self.compass_width = mc.compass_width as u32;
self.compass_height = mc.compass_height as u32;
self.compass_rotation = mc.compass_rotation;
self.player_x = mc.player_x;
self.player_y = mc.player_y;
self.map_center_x = mc.map_center_x;
self.map_center_y = mc.map_center_y;
self.map_scale = mc.map_scale;
self.process_id = mc.process_id;
self.mount_index = mc.mount_index as u32;
}
}
impl CMumbleContext {
pub fn update(&mut self, link_ptr: *const CMumbleLink) {
let mc = unsafe {
std::ptr::read_volatile(&(*link_ptr).context as *const u8 as *const CMumbleContext)
};
*self = mc;
}
}
impl WindowDimensions {
#[cfg(target_os = "windows")]
pub fn update(&mut self, link_ptr: *const CMumbleLink) -> anyhow::Result<()> {
*self = crate::win::get_win_pos_dim(link_ptr)?;
Ok(())
}
pub fn copy_raw_bytes_into(&self, buffer: &mut [u8]) {
assert!(buffer.len() >= 16);
buffer.copy_from_slice(bytemuck::bytes_of(self));
}
}
unsafe impl bytemuck::Zeroable for WindowDimensions {
fn zeroed() -> Self {
unsafe { core::mem::zeroed() }
}
}
unsafe impl bytemuck::Pod for WindowDimensions {}
pub const C_MUMBLE_LINK_SIZE: usize = std::mem::size_of::<CMumbleLink>();
pub const USEFUL_C_MUMBLE_LINK_SIZE: usize = 1193;
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct CMumbleLink {
pub ui_version: u32,
pub ui_tick: u32,
pub f_avatar_position: [f32; 3],
pub f_avatar_front: [f32; 3],
pub f_avatar_top: [f32; 3],
pub name: [u8; 512],
pub f_camera_position: [f32; 3],
pub f_camera_front: [f32; 3],
pub f_camera_top: [f32; 3],
pub identity: [u16; 256],
pub context_len: u32,
pub context: [u8; 256],
pub description: [u8; 4096],
}
#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
#[repr(C)]
pub struct CMumbleContext {
pub server_address: [u8; 28],
pub map_id: u32,
pub map_type: u32,
pub shard_id: u32,
pub instance: u32,
pub build_id: u32,
pub ui_state: u32,
pub compass_width: u16,
pub compass_height: u16,
pub compass_rotation: f32,
pub player_x: f32,
pub player_y: f32,
pub map_center_x: f32,
pub map_center_y: f32,
pub map_scale: f32,
pub process_id: u32,
pub mount_index: u8,
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct CIdentity {
pub name: String,
pub profession: u32,
pub spec: u32,
pub race: u32,
pub map_id: u32,
pub world_id: u32,
pub team_color_id: u32,
pub commander: bool,
pub fov: f32,
pub uisz: u32,
}
impl CMumbleLink {
pub fn get_cmumble_link(link_ptr: *const CMumbleLink) -> CMumbleLink {
unsafe { std::ptr::read_volatile(link_ptr) }
}
pub fn is_valid(link_ptr: *const CMumbleLink) -> bool {
unsafe { (*link_ptr).ui_tick > 0 }
}
pub fn get_ui_tick(link_ptr: *const CMumbleLink) -> u32 {
unsafe { (*link_ptr).ui_tick }
}
#[cfg(target_os = "windows")]
pub fn new_ptr(key: &str) -> anyhow::Result<*const CMumbleLink> {
crate::win::create_link_shared_mem(key)
}
pub fn copy_raw_bytes_into(link_ptr: *const CMumbleLink, buffer: &mut [u8]) {
unsafe {
std::ptr::copy_nonoverlapping(link_ptr as *const u8, buffer.as_mut_ptr(), buffer.len());
}
}
}