paavohtl Claude Opus 4.6 (1M context) commited on
Commit
07b71e5
Β·
1 Parent(s): e85f37c

refactor: use SoundId and Fixed types in sound functions

Browse files

Replace raw u32 sound IDs with SoundId enum and raw u32 volume/pitch
with Fixed type across queue_sound, play_sound_local, and hook
trampolines. Fixed is repr(transparent) over i32 so the hook ABIs
are unchanged.

All play_sound_local callers now use named SoundId variants
(ThrowRelease, RocketRelease, ShotgunFire, etc.) and Fixed::ONE
instead of 0x10000 magic numbers. Dynamic sound IDs from weapon
table data use SoundId::try_from() with graceful fallback.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-wormkit/src/replacements/sound.rs CHANGED
@@ -12,6 +12,7 @@ use std::sync::atomic::Ordering;
12
  use openwa_core::address::va;
13
  use openwa_core::audio::{play_sound, play_sound_pooled, SoundId};
14
  use openwa_core::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
 
15
  use openwa_core::task::{CGameTask, CTask};
16
 
17
  use crate::hook;
@@ -30,10 +31,10 @@ static SOUND_LOG_ENABLED: std::sync::atomic::AtomicBool = std::sync::atomic::Ato
30
  /// or sound is disabled.
31
  pub unsafe fn queue_sound(
32
  ddgame: *mut DDGame,
33
- sound_id: u32,
34
  flags: u32,
35
- volume: u32,
36
- pitch: u32,
37
  ) -> Option<*mut SoundQueueEntry> {
38
  let g = &mut *ddgame;
39
  if g.sound_queue_count >= 16 || g.sound.is_null() {
@@ -41,10 +42,10 @@ pub unsafe fn queue_sound(
41
  }
42
  let entry = &mut g.sound_queue[g.sound_queue_count as usize];
43
  *entry = SoundQueueEntry {
44
- sound_id,
45
  flags,
46
- volume,
47
- pitch,
48
  reserved: 0,
49
  is_local: 0,
50
  _pad: [0; 3],
@@ -65,8 +66,8 @@ unsafe extern "thiscall" fn hook_play_sound_global(
65
  this: *const CGameTask,
66
  sound_id: u32,
67
  flags: u32,
68
- volume: u32,
69
- pitch: u32,
70
  ) -> u32 {
71
  if SOUND_LOG_ENABLED.load(Ordering::Relaxed) {
72
  let sound_name = SoundId::try_from(sound_id)
@@ -78,7 +79,11 @@ unsafe extern "thiscall" fn hook_play_sound_global(
78
  ));
79
  }
80
 
81
- queue_sound((*this).base.ddgame, sound_id, flags, volume, pitch).is_some() as u32
 
 
 
 
82
  }
83
 
84
  // ============================================================
@@ -91,8 +96,8 @@ hook::usercall_trampoline!(fn trampoline_play_sound_local; impl_fn = play_sound_
91
  regs = [eax, ecx, edi]; stack_params = 2; ret_bytes = "0x8");
92
 
93
  unsafe extern "cdecl" fn play_sound_local_impl(
94
- pitch: u32,
95
- volume: u32,
96
  task: *mut CGameTask,
97
  sound_id: u32,
98
  flags: u32,
@@ -102,14 +107,18 @@ unsafe extern "cdecl" fn play_sound_local_impl(
102
  .map(|s| format!("{s:?}"))
103
  .unwrap_or_else(|v| format!("#{v}"));
104
  let _ = log_line(&format!(
105
- "[Sound] Local: eax={pitch} ecx=0x{volume:08X} task=0x{task:08X?} \
106
  id={sound_id}({sound_name}) flags={flags}"
107
  ));
108
  }
109
 
 
 
 
 
110
  let gt = &*task;
111
  let ddgame = gt.base.ddgame;
112
- let entry = match queue_sound(ddgame, sound_id, flags, volume, pitch) {
113
  Some(e) => e,
114
  None => return 0,
115
  };
@@ -136,10 +145,10 @@ unsafe extern "cdecl" fn play_sound_local_impl(
136
  /// increments the task's local sound count. Returns true on success.
137
  pub(crate) unsafe fn play_sound_local(
138
  task: *mut CGameTask,
139
- sound_id: u32,
140
  flags: u32,
141
- volume: u32,
142
- pitch: u32,
143
  ) -> bool {
144
  let gt = &*task;
145
  let ddgame = gt.base.ddgame;
 
12
  use openwa_core::address::va;
13
  use openwa_core::audio::{play_sound, play_sound_pooled, SoundId};
14
  use openwa_core::engine::{DDGame, DDGameWrapper, SoundQueueEntry};
15
+ use openwa_core::fixed::Fixed;
16
  use openwa_core::task::{CGameTask, CTask};
17
 
18
  use crate::hook;
 
31
  /// or sound is disabled.
32
  pub unsafe fn queue_sound(
33
  ddgame: *mut DDGame,
34
+ sound_id: SoundId,
35
  flags: u32,
36
+ volume: Fixed,
37
+ pitch: Fixed,
38
  ) -> Option<*mut SoundQueueEntry> {
39
  let g = &mut *ddgame;
40
  if g.sound_queue_count >= 16 || g.sound.is_null() {
 
42
  }
43
  let entry = &mut g.sound_queue[g.sound_queue_count as usize];
44
  *entry = SoundQueueEntry {
45
+ sound_id: sound_id as u32,
46
  flags,
47
+ volume: volume.0 as u32,
48
+ pitch: pitch.0 as u32,
49
  reserved: 0,
50
  is_local: 0,
51
  _pad: [0; 3],
 
66
  this: *const CGameTask,
67
  sound_id: u32,
68
  flags: u32,
69
+ volume: Fixed,
70
+ pitch: Fixed,
71
  ) -> u32 {
72
  if SOUND_LOG_ENABLED.load(Ordering::Relaxed) {
73
  let sound_name = SoundId::try_from(sound_id)
 
79
  ));
80
  }
81
 
82
+ let sid = match SoundId::try_from(sound_id) {
83
+ Ok(s) => s,
84
+ Err(_) => return 0,
85
+ };
86
+ queue_sound((*this).base.ddgame, sid, flags, volume, pitch).is_some() as u32
87
  }
88
 
89
  // ============================================================
 
96
  regs = [eax, ecx, edi]; stack_params = 2; ret_bytes = "0x8");
97
 
98
  unsafe extern "cdecl" fn play_sound_local_impl(
99
+ pitch: Fixed,
100
+ volume: Fixed,
101
  task: *mut CGameTask,
102
  sound_id: u32,
103
  flags: u32,
 
107
  .map(|s| format!("{s:?}"))
108
  .unwrap_or_else(|v| format!("#{v}"));
109
  let _ = log_line(&format!(
110
+ "[Sound] Local: pitch={pitch} volume={volume} task=0x{task:08X?} \
111
  id={sound_id}({sound_name}) flags={flags}"
112
  ));
113
  }
114
 
115
+ let sid = match SoundId::try_from(sound_id) {
116
+ Ok(s) => s,
117
+ Err(_) => return 0,
118
+ };
119
  let gt = &*task;
120
  let ddgame = gt.base.ddgame;
121
+ let entry = match queue_sound(ddgame, sid, flags, volume, pitch) {
122
  Some(e) => e,
123
  None => return 0,
124
  };
 
145
  /// increments the task's local sound count. Returns true on success.
146
  pub(crate) unsafe fn play_sound_local(
147
  task: *mut CGameTask,
148
+ sound_id: SoundId,
149
  flags: u32,
150
+ volume: Fixed,
151
+ pitch: Fixed,
152
  ) -> bool {
153
  let gt = &*task;
154
  let ddgame = gt.base.ddgame;
crates/openwa-wormkit/src/replacements/weapon.rs CHANGED
@@ -16,6 +16,7 @@
16
  use core::sync::atomic::{AtomicU32, Ordering};
17
 
18
  use openwa_core::address::va;
 
19
  use openwa_core::engine::ddgame::{self, TeamArenaRef};
20
  use openwa_core::fixed::Fixed;
21
  use openwa_core::game::weapon::{WeaponEntry, WeaponFireParams, WeaponSpawnData};
@@ -636,8 +637,8 @@ unsafe fn fire_indian_nuclear_test(worm: *mut CTaskWorm) {
636
  (*team).handle_message((*worm).as_task_ptr_mut(), 0x5A, 0x408, buf.as_ptr());
637
  }
638
 
639
- // PlaySoundGlobal(7, 8, 0x10000, 0x10000)
640
- sound::queue_sound(ddgame, 7, 8, 0x10000, 0x10000);
641
 
642
  // Message 0x51 (PoisonWorm): buf[0]=shot_count, buf[4]=2, buf[8]=team_index
643
  let team = lookup_team_task(worm);
@@ -649,8 +650,8 @@ unsafe fn fire_indian_nuclear_test(worm: *mut CTaskWorm) {
649
  (*team).handle_message((*worm).as_task_ptr_mut(), 0x51, 0x408, buf.as_ptr());
650
  }
651
 
652
- // PlaySoundGlobal(0x1E, 5, 0x10000, 0x10000)
653
- sound::queue_sound(ddgame, 0x1E, 5, 0x10000, 0x10000);
654
  }
655
 
656
  // ── Air Strike sub-function bridges ───────────────────────
 
16
  use core::sync::atomic::{AtomicU32, Ordering};
17
 
18
  use openwa_core::address::va;
19
+ use openwa_core::audio::SoundId;
20
  use openwa_core::engine::ddgame::{self, TeamArenaRef};
21
  use openwa_core::fixed::Fixed;
22
  use openwa_core::game::weapon::{WeaponEntry, WeaponFireParams, WeaponSpawnData};
 
637
  (*team).handle_message((*worm).as_task_ptr_mut(), 0x5A, 0x408, buf.as_ptr());
638
  }
639
 
640
+ // PlaySoundGlobal(IndianAnthem, 8, 0x10000, 0x10000)
641
+ sound::queue_sound(ddgame, SoundId::IndianAnthem, 8, Fixed::ONE, Fixed::ONE);
642
 
643
  // Message 0x51 (PoisonWorm): buf[0]=shot_count, buf[4]=2, buf[8]=team_index
644
  let team = lookup_team_task(worm);
 
650
  (*team).handle_message((*worm).as_task_ptr_mut(), 0x51, 0x408, buf.as_ptr());
651
  }
652
 
653
+ // PlaySoundGlobal(NukeFlash, 5, 0x10000, 0x10000)
654
+ sound::queue_sound(ddgame, SoundId::NukeFlash, 5, Fixed::ONE, Fixed::ONE);
655
  }
656
 
657
  // ── Air Strike sub-function bridges ───────────────────────
crates/openwa-wormkit/src/replacements/weapon_release.rs CHANGED
@@ -6,6 +6,8 @@
6
  //! Convention: usercall(EAX=CTaskWorm*) + 4 stack params, RET 0x10.
7
 
8
  use openwa_core::address::va;
 
 
9
  use openwa_core::log::log_line;
10
  use openwa_core::rebase::rb;
11
  use openwa_core::task::worm::CTaskWorm;
@@ -278,11 +280,11 @@ unsafe extern "cdecl" fn weapon_release_impl(
278
  effect_state = 0x73;
279
  }
280
  2 => {
281
- sound::play_sound_local(task, 0x49, 3, 0x10000, 0x10000);
282
  call_stop_worm_sound(worm, stop_worm_sound_addr);
283
  }
284
  3 | 7 | 0xB | 0xC => {
285
- sound::play_sound_local(task, 0x4B, 3, 0x10000, 0x10000);
286
  call_stop_worm_sound(worm, stop_worm_sound_addr);
287
  }
288
  4 => {
@@ -293,17 +295,17 @@ unsafe extern "cdecl" fn weapon_release_impl(
293
  effect_state = 0x73;
294
  }
295
  5 => {
296
- sound::play_sound_local(task, 0x50, 3, 0x10000, 0x10000);
297
  do_effect = true;
298
  effect_state = 0x75;
299
  }
300
  6 => {
301
- sound::play_sound_local(task, 0x52, 3, 0x10000, 0x10000);
302
  do_effect = true;
303
  effect_state = 0x73;
304
  }
305
  10 => {
306
- sound::play_sound_local(task, 0x1D, 3, 0x10000, 0x10000);
307
  call_stop_worm_sound(worm, stop_worm_sound_addr);
308
  }
309
  _ => {}
@@ -311,28 +313,34 @@ unsafe extern "cdecl" fn weapon_release_impl(
311
  }
312
  2 => {
313
  if w._unknown_2cc == 0 || w._unknown_2c8 == 1 {
314
- let team_sound = (*w.ddgame()).team_sound_id(team_id);
315
- sound::play_sound_local(task, team_sound, 3, 0x10000, 0x10000);
 
 
316
  }
317
  }
318
  // Type 3: no sound
319
  4 => {
320
  match (*entry).fire_subtype_34 {
321
  2 => {
322
- sound::play_sound_local(task, 0x60, 3, 0x10000, 0x10000);
 
 
323
  }
324
  3 => {
325
- let sound_id = (*entry).fire_subtype_38 as u32;
326
- sound::play_sound_local(task, sound_id, 3, 0x10000, 0x10000);
 
327
  }
328
  4 => {
329
  // Sound ID at entry+0x40 = fire_params.spread (polymorphic use)
330
- let sound_id = (*entry).fire_params.spread as u32;
331
- sound::play_sound_local(task, sound_id, 3, 0x10000, 0x10000);
 
332
  }
333
  10 => {
334
  if w._unknown_208 == 0 {
335
- sound::play_sound_local(task, 0x43, 3, 0x10000, 0x10000);
336
  }
337
  }
338
  0xB => {
 
6
  //! Convention: usercall(EAX=CTaskWorm*) + 4 stack params, RET 0x10.
7
 
8
  use openwa_core::address::va;
9
+ use openwa_core::audio::SoundId;
10
+ use openwa_core::fixed::Fixed;
11
  use openwa_core::log::log_line;
12
  use openwa_core::rebase::rb;
13
  use openwa_core::task::worm::CTaskWorm;
 
280
  effect_state = 0x73;
281
  }
282
  2 => {
283
+ sound::play_sound_local(task, SoundId::ThrowRelease, 3, Fixed::ONE, Fixed::ONE);
284
  call_stop_worm_sound(worm, stop_worm_sound_addr);
285
  }
286
  3 | 7 | 0xB | 0xC => {
287
+ sound::play_sound_local(task, SoundId::RocketRelease, 3, Fixed::ONE, Fixed::ONE);
288
  call_stop_worm_sound(worm, stop_worm_sound_addr);
289
  }
290
  4 => {
 
295
  effect_state = 0x73;
296
  }
297
  5 => {
298
+ sound::play_sound_local(task, SoundId::ShotgunFire, 3, Fixed::ONE, Fixed::ONE);
299
  do_effect = true;
300
  effect_state = 0x75;
301
  }
302
  6 => {
303
+ sound::play_sound_local(task, SoundId::HandgunFire, 3, Fixed::ONE, Fixed::ONE);
304
  do_effect = true;
305
  effect_state = 0x73;
306
  }
307
  10 => {
308
+ sound::play_sound_local(task, SoundId::LongbowRelease, 3, Fixed::ONE, Fixed::ONE);
309
  call_stop_worm_sound(worm, stop_worm_sound_addr);
310
  }
311
  _ => {}
 
313
  }
314
  2 => {
315
  if w._unknown_2cc == 0 || w._unknown_2c8 == 1 {
316
+ let team_sound_raw = (*w.ddgame()).team_sound_id(team_id);
317
+ if let Ok(sid) = SoundId::try_from(team_sound_raw) {
318
+ sound::play_sound_local(task, sid, 3, Fixed::ONE, Fixed::ONE);
319
+ }
320
  }
321
  }
322
  // Type 3: no sound
323
  4 => {
324
  match (*entry).fire_subtype_34 {
325
  2 => {
326
+ sound::play_sound_local(
327
+ task, SoundId::BaseballBatRelease, 3, Fixed::ONE, Fixed::ONE,
328
+ );
329
  }
330
  3 => {
331
+ if let Ok(sid) = SoundId::try_from((*entry).fire_subtype_38 as u32) {
332
+ sound::play_sound_local(task, sid, 3, Fixed::ONE, Fixed::ONE);
333
+ }
334
  }
335
  4 => {
336
  // Sound ID at entry+0x40 = fire_params.spread (polymorphic use)
337
+ if let Ok(sid) = SoundId::try_from((*entry).fire_params.spread as u32) {
338
+ sound::play_sound_local(task, sid, 3, Fixed::ONE, Fixed::ONE);
339
+ }
340
  }
341
  10 => {
342
  if w._unknown_208 == 0 {
343
+ sound::play_sound_local(task, SoundId::Teleport, 3, Fixed::ONE, Fixed::ONE);
344
  }
345
  }
346
  0xB => {