paavohtl Claude Opus 4.6 commited on
Commit
24a8d66
·
1 Parent(s): 0437e81

feat: add Win32 registry helpers to openwa-lib

Browse files

New registry.rs module with read_profile_int, read_profile_string,
and delete_key_recursive. These replace MFC's CWinApp registry helpers
for direct HKCU access to WA's configuration keys.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

crates/openwa-lib/src/wa/mod.rs CHANGED
@@ -2,4 +2,5 @@
2
 
3
  pub mod frontend;
4
  pub mod mfc;
 
5
  pub mod resource;
 
2
 
3
  pub mod frontend;
4
  pub mod mfc;
5
+ pub mod registry;
6
  pub mod resource;
crates/openwa-lib/src/wa/registry.rs ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //! Win32 registry helpers for WA configuration.
2
+
3
+ use windows_sys::Win32::System::Registry::*;
4
+
5
+ const WA_REGISTRY_PATH: &str = "Software\\Team17SoftwareLTD\\WormsArmageddon\\";
6
+
7
+ /// Read an integer value from the WA registry.
8
+ ///
9
+ /// Opens `HKCU\Software\Team17SoftwareLTD\WormsArmageddon\{section}\{key}`
10
+ /// and reads it as a DWORD. Returns `default` on any failure.
11
+ ///
12
+ /// This replaces MFC's `CWinApp::GetProfileIntW` for the registry-mode path.
13
+ pub fn read_profile_int(section: &str, key: &str, default: u32) -> u32 {
14
+ let mut full_path = String::with_capacity(WA_REGISTRY_PATH.len() + section.len() + 1);
15
+ full_path.push_str(WA_REGISTRY_PATH);
16
+ full_path.push_str(section);
17
+ full_path.push('\0');
18
+
19
+ let mut key_buf = String::with_capacity(key.len() + 1);
20
+ key_buf.push_str(key);
21
+ key_buf.push('\0');
22
+
23
+ unsafe {
24
+ let mut hkey: HKEY = core::ptr::null_mut();
25
+ let status = RegOpenKeyExA(
26
+ HKEY_CURRENT_USER,
27
+ full_path.as_ptr(),
28
+ 0,
29
+ KEY_READ,
30
+ &mut hkey,
31
+ );
32
+ if status != 0 {
33
+ return default;
34
+ }
35
+
36
+ let mut value: u32 = 0;
37
+ let mut value_size: u32 = 4;
38
+ let mut value_type: u32 = 0;
39
+ let status = RegQueryValueExA(
40
+ hkey,
41
+ key_buf.as_ptr(),
42
+ core::ptr::null_mut(),
43
+ &mut value_type,
44
+ &mut value as *mut u32 as *mut u8,
45
+ &mut value_size,
46
+ );
47
+ RegCloseKey(hkey);
48
+
49
+ if status == 0 && value_type == REG_DWORD {
50
+ value
51
+ } else {
52
+ default
53
+ }
54
+ }
55
+ }
56
+
57
+ /// Read a string value from the WA registry.
58
+ ///
59
+ /// Opens `HKCU\Software\Team17SoftwareLTD\WormsArmageddon\{section}\{key}`
60
+ /// and reads it as a REG_SZ string into `buf`. Returns the number of bytes
61
+ /// written (excluding null terminator), or 0 on failure.
62
+ pub fn read_profile_string(section: &str, key: &str, buf: &mut [u8]) -> usize {
63
+ let mut full_path = String::with_capacity(WA_REGISTRY_PATH.len() + section.len() + 1);
64
+ full_path.push_str(WA_REGISTRY_PATH);
65
+ full_path.push_str(section);
66
+ full_path.push('\0');
67
+
68
+ let mut key_buf = String::with_capacity(key.len() + 1);
69
+ key_buf.push_str(key);
70
+ key_buf.push('\0');
71
+
72
+ unsafe {
73
+ let mut hkey: HKEY = core::ptr::null_mut();
74
+ let status = RegOpenKeyExA(
75
+ HKEY_CURRENT_USER,
76
+ full_path.as_ptr(),
77
+ 0,
78
+ KEY_READ,
79
+ &mut hkey,
80
+ );
81
+ if status != 0 {
82
+ return 0;
83
+ }
84
+
85
+ let mut value_size: u32 = buf.len() as u32;
86
+ let mut value_type: u32 = 0;
87
+ let status = RegQueryValueExA(
88
+ hkey,
89
+ key_buf.as_ptr(),
90
+ core::ptr::null_mut(),
91
+ &mut value_type,
92
+ buf.as_mut_ptr(),
93
+ &mut value_size,
94
+ );
95
+ RegCloseKey(hkey);
96
+
97
+ if status == 0 && value_type == REG_SZ && value_size > 0 {
98
+ // value_size includes null terminator
99
+ (value_size - 1) as usize
100
+ } else {
101
+ 0
102
+ }
103
+ }
104
+ }
105
+
106
+ /// Recursively delete a registry key and all its subkeys.
107
+ ///
108
+ /// Replacement for WA's Registry__DeleteKeyRecursive (0x4E4D10).
109
+ /// Returns 0 on success, Win32 error code on failure.
110
+ pub unsafe fn delete_key_recursive(parent: HKEY, subkey: &str) -> u32 {
111
+ if subkey.is_empty() {
112
+ return 0x3F2; // ERROR_INVALID_PARAMETER equivalent used by original
113
+ }
114
+
115
+ let mut subkey_buf = String::with_capacity(subkey.len() + 1);
116
+ subkey_buf.push_str(subkey);
117
+ subkey_buf.push('\0');
118
+
119
+ let mut hkey: HKEY = core::ptr::null_mut();
120
+ let status = RegOpenKeyExA(
121
+ parent,
122
+ subkey_buf.as_ptr(),
123
+ 0,
124
+ KEY_READ | KEY_ENUMERATE_SUB_KEYS,
125
+ &mut hkey,
126
+ );
127
+ if status != 0 {
128
+ return status;
129
+ }
130
+
131
+ // Enumerate and recursively delete all child keys
132
+ let mut name_buf = vec![0u8; 256];
133
+ loop {
134
+ let mut name_len = name_buf.len() as u32;
135
+ let status = RegEnumKeyExA(
136
+ hkey,
137
+ 0, // always index 0 since we delete as we go
138
+ name_buf.as_mut_ptr(),
139
+ &mut name_len,
140
+ core::ptr::null_mut(),
141
+ core::ptr::null_mut(),
142
+ core::ptr::null_mut(),
143
+ core::ptr::null_mut(),
144
+ );
145
+
146
+ if status == 0x103 {
147
+ // ERROR_NO_MORE_ITEMS — all children deleted
148
+ break;
149
+ }
150
+ if status != 0 {
151
+ RegCloseKey(hkey);
152
+ return status;
153
+ }
154
+
155
+ if name_len as usize == name_buf.len() {
156
+ // Name buffer too small, grow it
157
+ name_buf.resize(name_buf.len() * 2, 0);
158
+ continue;
159
+ }
160
+
161
+ // Null-terminate and recurse
162
+ name_buf[name_len as usize] = 0;
163
+ let child_name = core::str::from_utf8_unchecked(&name_buf[..name_len as usize]);
164
+ let result = delete_key_recursive(hkey, child_name);
165
+ if result != 0 {
166
+ RegCloseKey(hkey);
167
+ return result;
168
+ }
169
+ }
170
+
171
+ // All children deleted, now delete the key itself
172
+ let status = RegDeleteKeyA(parent, subkey_buf.as_ptr());
173
+ RegCloseKey(hkey);
174
+ status
175
+ }