commonlibsse_ng\re\g/
GameSettingCollection.rs

1//! # GameSettingCollection
2//!
3//! This module defines the `GameSettingCollection` struct, inheriting from `SettingCollectionMap<Setting>`.
4//! It represents the game's settings collection, including methods for interacting with settings and handling.
5
6use core::fmt;
7use std::collections::HashMap;
8
9use crate::re::Setting::{Setting, SettingValue};
10use crate::re::SettingCollectionMap::SettingCollectionMap;
11use crate::re::offsets_rtti::RTTI_GameSettingCollection;
12use crate::re::offsets_vtable::VTABLE_GameSettingCollection;
13use crate::rel::id::VariantID;
14
15/// Represents the game's settings collection.
16///
17/// Inherits from `SettingCollectionMap<Setting>`.
18///
19/// # Memory Layout:
20/// - `__base`: Base class `SettingCollectionMap<Setting>`
21/// - `handle`: Handle used for settings management
22#[repr(C)]
23#[derive(Clone, PartialEq)]
24pub struct GameSettingCollection {
25    pub __base: SettingCollectionMap<Setting>, // 0x000
26}
27const _: () = {
28    assert!(core::mem::offset_of!(GameSettingCollection, __base) == 0x0);
29    assert!(core::mem::size_of::<GameSettingCollection>() == 0x140);
30};
31
32impl fmt::Debug for GameSettingCollection {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        let mut map = f.debug_map();
35        for (key, value) in self.__base.settings.__base.__base.__base.iter() {
36            let key_str = unsafe {
37                if key.is_null() {
38                    "<null>"
39                } else {
40                    core::ffi::CStr::from_ptr(*key).to_str().unwrap_or("<invalid utf8>")
41                }
42            };
43            let value = unsafe { value.as_ref().map(|p| p.get_value()).unwrap_or_default() };
44            map.entry(&key_str, &value);
45        }
46        map.finish()
47    }
48}
49
50impl GameSettingCollection {
51    /// Address & Offset of the runtime type information (RTTI) identifier.
52    pub const RTTI: VariantID = RTTI_GameSettingCollection;
53
54    /// Address & Offset of the virtual function table.
55    pub const VTABLE: [VariantID; 1] = VTABLE_GameSettingCollection;
56
57    /// Gets the singleton instance of `GameSettingCollection`.
58    #[commonlibsse_ng_derive_internal::relocate(
59        cast_as = "*mut *mut GameSettingCollection",
60        default = "None",
61        deref_once,
62        id(se = 514622, ae = 400782)
63    )]
64    pub fn get_singleton() -> Option<&'static GameSettingCollection> {
65        |deref_type: DerefType| unsafe { deref_type.as_ref() }
66    }
67
68    pub fn to_hashmap(&self) -> HashMap<&str, SettingValue<'_>> {
69        let mut map = std::collections::HashMap::new();
70        for (key, value) in self.__base.settings.__base.__base.__base.iter() {
71            let key_str = unsafe {
72                if key.is_null() {
73                    "<null>"
74                } else {
75                    core::ffi::CStr::from_ptr(*key).to_str().unwrap_or("<invalid utf8>")
76                }
77            };
78            let value = unsafe { value.as_ref().map(|p| p.get_value()).unwrap_or_default() };
79            map.insert(key_str, value);
80        }
81        map
82    }
83}
84
85/// The virtual function table for `GameSettingCollection`.
86///
87/// This struct defines function pointers to simulate the C++ virtual functions.
88#[repr(C)]
89pub struct GameSettingCollectionVtbl {
90    /// Destructor function pointer.
91    pub CxxDrop: fn(this: &mut GameSettingCollection),
92
93    /// Function pointer for writing a setting.
94    pub WriteSetting: fn(this: &mut GameSettingCollection, setting: &Setting) -> bool,
95
96    /// Function pointer for reading a setting.
97    pub ReadSetting: fn(this: &mut GameSettingCollection, setting: &mut Setting) -> bool,
98
99    /// Function pointer for opening the handle.
100    pub OpenHandle: fn(this: &mut GameSettingCollection, create: bool) -> bool,
101
102    /// Function pointer for closing the handle.
103    pub CloseHandle: fn(this: &mut GameSettingCollection) -> bool,
104
105    /// Function pointer for the unknown function `0A`.
106    pub Unk_0A: fn(this: &mut GameSettingCollection),
107}
108
109impl Default for GameSettingCollectionVtbl {
110    #[inline]
111    fn default() -> Self {
112        Self::new()
113    }
114}
115
116impl GameSettingCollectionVtbl {
117    /// Creates a new default virtual table with stubbed functions.
118    #[inline]
119    pub const fn new() -> Self {
120        const fn CxxDrop(_this: &mut GameSettingCollection) {}
121
122        const fn WriteSetting(_this: &mut GameSettingCollection, _setting: &Setting) -> bool {
123            false
124        }
125
126        const fn ReadSetting(_this: &mut GameSettingCollection, _setting: &mut Setting) -> bool {
127            true
128        }
129
130        const fn OpenHandle(_this: &mut GameSettingCollection, _create: bool) -> bool {
131            true
132        }
133
134        const fn CloseHandle(_this: &mut GameSettingCollection) -> bool {
135            true
136        }
137
138        const fn Unk_0A(_this: &mut GameSettingCollection) {}
139
140        Self { CxxDrop, WriteSetting, ReadSetting, OpenHandle, CloseHandle, Unk_0A }
141    }
142}