commonlibsse_ng\re\s/
SettingCollection.rs

1//! # SettingCollection
2//!
3//! This module defines the `SettingCollection<T>` trait and struct, representing a collection of settings.
4//! It supports insertion, removal, and file I/O operations.
5
6/// Represents a collection of settings.
7///
8/// This struct corresponds to the `SettingCollection<T>` C++ template class.
9///
10/// # Memory Layout:
11/// - `sub_key`: 260 bytes
12/// - `handle`: Pointer to the handle (0x110)
13#[repr(C)]
14#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
15pub struct SettingCollection<T> {
16    pub vtable: *const SettingCollectionVtbl<T>, // 0x00
17    pub subKey: [u8; 0x104],                     // 0x08
18    pub handle: *mut core::ffi::c_void,          // 0x110
19}
20
21const _: () = {
22    assert!(core::mem::offset_of!(SettingCollection::<()>, subKey) == 0x08);
23    assert!(core::mem::offset_of!(SettingCollection::<()>, handle) == 0x110);
24    assert!(core::mem::size_of::<SettingCollection::<()>>() == 0x118);
25};
26
27/// Trait representing the `SettingCollection` behavior.
28///
29/// This trait defines the collection operations for generic settings.
30pub trait SettingCollectionTrait<T> {
31    /// Inserts a setting into the collection.
32    ///
33    /// # Arguments
34    /// - `setting`: The setting to insert.
35    fn insert_setting(&mut self, setting: &mut T);
36
37    /// Removes a setting from the collection.
38    ///
39    /// # Arguments
40    /// - `setting`: The setting to remove.
41    fn remove_setting(&mut self, setting: &mut T);
42
43    /// Writes a setting to the collection.
44    ///
45    /// # Arguments
46    /// - `setting`: The setting to write.
47    ///
48    /// # Returns
49    /// - `true` if the write was successful.
50    /// - `false` otherwise.
51    fn write_setting(&mut self, setting: &mut T) -> bool;
52
53    /// Reads a setting from the collection.
54    ///
55    /// # Arguments
56    /// - `setting`: The setting to read.
57    ///
58    /// # Returns
59    /// - `true` if the read was successful.
60    /// - `false` otherwise.
61    fn read_setting(&mut self, setting: &mut T) -> bool;
62
63    /// Opens the handle for the collection.
64    ///
65    /// # Arguments
66    /// - `create`: Whether to create the handle.
67    ///
68    /// # Returns
69    /// - `true` if the handle was opened successfully.
70    /// - `false` otherwise.
71    fn open_handle(&mut self, create: bool) -> bool;
72
73    /// Closes the handle for the collection.
74    ///
75    /// # Returns
76    /// - `true` if the handle was closed successfully.
77    fn close_handle(&mut self) -> bool;
78
79    /// Unknown operation `0x07`.
80    fn unk_07(&mut self);
81
82    /// Writes all settings.
83    fn write_all_settings(&mut self);
84
85    /// Reads all settings.
86    fn read_all_settings(&mut self);
87}
88
89impl<T> SettingCollectionTrait<T> for SettingCollection<T> {
90    #[inline]
91    fn insert_setting(&mut self, _setting: &mut T) {
92        // Stub implementation, replace with actual behavior.
93    }
94
95    #[inline]
96    fn remove_setting(&mut self, _setting: &mut T) {
97        // Stub implementation, replace with actual behavior.
98    }
99
100    #[inline]
101    fn write_setting(&mut self, _setting: &mut T) -> bool {
102        // Stub: Always return `false` by default.
103        false
104    }
105
106    #[inline]
107    fn read_setting(&mut self, _setting: &mut T) -> bool {
108        // Stub: Always return `true` by default.
109        true
110    }
111
112    #[inline]
113    fn open_handle(&mut self, _create: bool) -> bool {
114        // Stub: Always return `false`.
115        false
116    }
117
118    #[inline]
119    fn close_handle(&mut self) -> bool {
120        // Stub: Always return `true`.
121        true
122    }
123
124    #[inline]
125    fn unk_07(&mut self) {
126        // Stub implementation.
127    }
128
129    #[inline]
130    fn write_all_settings(&mut self) {
131        // Stub: Add behavior if needed.
132    }
133
134    #[inline]
135    fn read_all_settings(&mut self) {
136        // Stub: Add behavior if needed.
137    }
138}
139
140/// The virtual function table for `SettingCollection<T>`.
141///
142/// This struct defines function pointers to simulate the C++ virtual functions.
143#[repr(C)]
144pub struct SettingCollectionVtbl<T> {
145    /// Destructor function pointer.
146    pub CxxDrop: fn(this: &mut SettingCollection<T>), // 0x00
147
148    pub InsertSetting: fn(this: &mut SettingCollection<T>, setting: &mut T), // 0x01
149    pub RemoveSetting: fn(this: &mut SettingCollection<T>, setting: &mut T), // 0x02
150    pub WriteSetting: fn(this: &mut SettingCollection<T>, setting: &mut T) -> bool, // 0x03
151    pub ReadSetting: fn(this: &mut SettingCollection<T>, setting: &mut T) -> bool, // 0x04
152    pub OpenHandle: fn(this: &mut SettingCollection<T>, create: bool) -> bool, // 0x05 - { return false; }
153    pub CloseHandle: fn(this: &mut SettingCollection<T>) -> bool, // 0x06 - { return true; }
154    pub Unk_07: fn(this: &mut SettingCollection<T>),              // 0x07 - { return 0; }
155    pub WriteAllSettings: fn(this: &mut SettingCollection<T>),    // 0x08 - { return handle != 0; }
156    pub ReadAllSettings: fn(this: &mut SettingCollection<T>),     // 0x09 - { return handle != 0; }
157}
158
159impl<T> Default for SettingCollectionVtbl<T> {
160    #[inline]
161    fn default() -> Self {
162        Self::new()
163    }
164}
165
166impl<T> SettingCollectionVtbl<T> {
167    /// Creates a new default virtual table with stubbed functions.
168    #[inline]
169    pub const fn new() -> Self {
170        const fn CxxDrop<T>(_this: &mut SettingCollection<T>) {}
171
172        const fn InsertSetting<T>(_this: &mut SettingCollection<T>, _setting: &mut T) {}
173
174        const fn RemoveSetting<T>(_this: &mut SettingCollection<T>, _setting: &mut T) {}
175
176        const fn WriteSetting<T>(_this: &mut SettingCollection<T>, _setting: &mut T) -> bool {
177            false
178        }
179
180        const fn ReadSetting<T>(_this: &mut SettingCollection<T>, _setting: &mut T) -> bool {
181            true
182        }
183
184        const fn OpenHandle<T>(_this: &mut SettingCollection<T>, _create: bool) -> bool {
185            false
186        }
187
188        const fn CloseHandle<T>(_this: &mut SettingCollection<T>) -> bool {
189            true
190        }
191
192        const fn Unk_07<T>(_this: &mut SettingCollection<T>) {}
193
194        const fn WriteAllSettings<T>(_this: &mut SettingCollection<T>) {}
195
196        const fn ReadAllSettings<T>(_this: &mut SettingCollection<T>) {}
197
198        Self {
199            CxxDrop,
200            InsertSetting,
201            RemoveSetting,
202            WriteSetting,
203            ReadSetting,
204            OpenHandle,
205            CloseHandle,
206            Unk_07,
207            WriteAllSettings,
208            ReadAllSettings,
209        }
210    }
211}