asr/game_engine/godot/core/templates/
hash_map.rs

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
//! <https://github.com/godotengine/godot/blob/07cf36d21c9056fb4055f020949fb90ebd795afb/core/templates/hash_map.h>

use core::{iter, marker::PhantomData, num::NonZeroU32};

use crate::{
    game_engine::godot::{Ptr, SizeInTargetProcess},
    Address64, Error, Process,
};

use super::{
    hashfuncs::{fastmod, HASH_TABLE_SIZE_PRIMES, HASH_TABLE_SIZE_PRIMES_INV},
    Hash,
};

#[allow(unused)]
mod offsets {
    pub const ELEMENTS: u32 = 0x8;
    pub const HASHES: u32 = 0x10;
    pub const HEAD_ELEMENT: u32 = 0x18;
    pub const TAIL_ELEMENT: u32 = 0x20;
    pub const CAPACITY_INDEX: u32 = 0x28;
    pub const NUM_ELEMENTS: u32 = 0x2C;

    pub mod element {
        pub const NEXT: u32 = 0x00;
        pub const PREV: u32 = 0x08;
        pub const KEY: u32 = 0x10;
    }
}

impl<K, V> SizeInTargetProcess for HashMap<K, V> {
    const SIZE: u64 = 0x30;
}

const EMPTY_HASH: u32 = 0;

/// A hash map that maps keys to values. This is not publicly exposed as such in
/// Godot, because it's a template class. The closest equivalent is the general
/// [`Dictionary`](https://docs.godotengine.org/en/4.2/classes/class_dictionary.html).
///
/// Check the [`Ptr`] documentation to see all the methods you can call on it.
#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct HashMap<K, V>(PhantomData<fn() -> (K, V)>);

#[derive(Debug, Copy, Clone)]
#[repr(transparent)]
struct HashMapElement<K, V>(PhantomData<fn() -> (K, V)>);

impl<K: 'static, V: 'static> Ptr<HashMapElement<K, V>> {
    fn next(self, process: &Process) -> Result<Self, Error> {
        self.read_at_byte_offset(offsets::element::NEXT, process)
    }

    fn prev(self, process: &Process) -> Result<Self, Error> {
        self.read_at_byte_offset(offsets::element::PREV, process)
    }

    fn key(self) -> Ptr<K> {
        Ptr::new(self.addr() + offsets::element::KEY)
    }

    fn value(self) -> Ptr<V>
    where
        K: SizeInTargetProcess,
    {
        Ptr::new(self.addr() + offsets::element::KEY + K::SIZE)
    }
}

impl<K: 'static, V: 'static> Ptr<HashMap<K, V>> {
    /// Returns an iterator over the key-value pairs in this hash map.
    pub fn iter<'a>(&'a self, process: &'a Process) -> impl Iterator<Item = (Ptr<K>, Ptr<V>)> + 'a
    where
        K: SizeInTargetProcess,
    {
        let mut current: Ptr<HashMapElement<K, V>> = Ptr::new(
            self.read_at_byte_offset(offsets::HEAD_ELEMENT, process)
                .unwrap_or_default(),
        );
        iter::from_fn(move || {
            if current.is_null() {
                return None;
            }
            let pair = (current.key(), current.value());
            current = current.next(process).ok()?;
            Some(pair)
        })
    }

    /// Returns a backwards iterator over the key-value pairs in this hash map.
    pub fn iter_back<'a>(
        &'a self,
        process: &'a Process,
    ) -> impl Iterator<Item = (Ptr<K>, Ptr<V>)> + 'a
    where
        K: SizeInTargetProcess,
    {
        let mut current: Ptr<HashMapElement<K, V>> = Ptr::new(
            self.read_at_byte_offset(offsets::TAIL_ELEMENT, process)
                .unwrap_or_default(),
        );
        iter::from_fn(move || {
            if current.is_null() {
                return None;
            }
            let pair = (current.key(), current.value());
            current = current.prev(process).ok()?;
            Some(pair)
        })
    }

    /// Returns the value associated with the given key, or [`None`] if the key
    /// is not in the hash map.
    pub fn get<Q>(self, key: &Q, process: &Process) -> Result<Option<Ptr<V>>, Error>
    where
        K: Hash<Q> + SizeInTargetProcess,
    {
        match self.lookup_pos(key, process)? {
            Some(element) => Ok(Some(element.value())),
            None => Ok(None),
        }
    }

    /// Returns the number of elements in this hash map.
    pub fn size(self, process: &Process) -> Result<u32, Error> {
        self.read_at_byte_offset(offsets::NUM_ELEMENTS, process)
    }

    fn get_capacity_index(self, process: &Process) -> Result<u32, Error> {
        self.read_at_byte_offset(offsets::CAPACITY_INDEX, process)
    }

    fn lookup_pos<Q>(
        self,
        key: &Q,
        process: &Process,
    ) -> Result<Option<Ptr<HashMapElement<K, V>>>, Error>
    where
        K: Hash<Q>,
    {
        let capacity_index = self.get_capacity_index(process)?;

        let capacity = *HASH_TABLE_SIZE_PRIMES
            .get(capacity_index as usize)
            .ok_or(Error {})?;

        let capacity_inv = *HASH_TABLE_SIZE_PRIMES_INV
            .get(capacity_index as usize)
            .ok_or(Error {})?;

        let hash = Self::hash(key);
        let mut pos = fastmod(hash, capacity_inv, capacity);
        let mut distance = 0;

        let [elements_ptr, hashes_ptr]: [Address64; 2] =
            self.read_at_byte_offset(offsets::ELEMENTS, process)?;

        for _ in 0..10000 {
            let current_hash: u32 =
                process.read(hashes_ptr + pos.checked_mul(4).ok_or(Error {})?)?;

            if current_hash == EMPTY_HASH {
                return Ok(None);
            }

            if distance > get_probe_length(pos, current_hash, capacity, capacity_inv) {
                return Ok(None);
            }

            if current_hash == hash {
                let element_ptr: Ptr<HashMapElement<K, V>> =
                    process.read(elements_ptr + pos.checked_mul(8).ok_or(Error {})?)?;
                let element_key = element_ptr.key().deref(process)?;
                if K::eq(&element_key, key, process) {
                    return Ok(Some(element_ptr));
                }
            }

            pos = fastmod(pos.wrapping_add(1), capacity_inv, capacity);
            distance += 1;
        }

        Err(Error {})
    }

    fn hash<Q>(key: &Q) -> u32
    where
        K: Hash<Q>,
    {
        let hash = K::hash_of_lookup_key(key);

        if hash == EMPTY_HASH {
            EMPTY_HASH + 1
        } else {
            hash
        }
    }
}

fn get_probe_length(p_pos: u32, p_hash: u32, p_capacity: NonZeroU32, p_capacity_inv: u64) -> u32 {
    let original_pos = fastmod(p_hash, p_capacity_inv, p_capacity);
    fastmod(
        p_pos
            .wrapping_sub(original_pos)
            .wrapping_add(p_capacity.get()),
        p_capacity_inv,
        p_capacity,
    )
}