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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) 2024 Lily Lyons
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#[cfg(not(target_pointer_width = "64"))]
compile_error!("fmod-oxide's userdata system is currently only supported on 64-bit platforms.");

use std::{
    any::Any,
    sync::{Arc, RwLock},
};

use once_cell::sync::Lazy;
use slotmap::{HopSlotMap, Key, KeyData};

use crate::{
    studio::{Bank, CommandReplay, EventDescription, EventInstance, System as StudioSystem},
    ChannelControl, Dsp, DspConnection, Geometry, Reverb3D, Sound, SoundGroup, System,
};

#[derive(Default)]
struct UserdataStorage {
    // we use hopslotmap as we expect to iterate over all userdata often
    // denseslotmap might be better? it has vec iter times
    slotmap: HopSlotMap<UserdataKey, UserdataValue>,
}

slotmap::new_key_type! {
    pub struct UserdataKey;
}

struct UserdataValue {
    userdata: Userdata,
    owner: HasUserdata,
}

#[derive(PartialEq)]
pub(crate) enum HasUserdata {
    StudioSystem(StudioSystem),
    Bank(Bank),
    EventDescription(EventDescription),
    EventInstance(EventInstance),
    CommandReplay(CommandReplay),
    //
    Reverb3D(Reverb3D),
    SoundGroup(SoundGroup),
    ChannelControl(ChannelControl),
    Dsp(Dsp),
    Sound(Sound),
    Geometry(Geometry),
    DspConnection(DspConnection),
    System(System),
}

impl HasUserdata {
    fn get_raw_userdata(&self) -> fmod_sys::Result<*mut std::ffi::c_void> {
        match self {
            HasUserdata::StudioSystem(s) => s.get_raw_userdata(),
            HasUserdata::Bank(b) => b.get_raw_userdata(),
            HasUserdata::EventDescription(e) => e.get_raw_userdata(),
            HasUserdata::EventInstance(e) => e.get_raw_userdata(),
            HasUserdata::CommandReplay(c) => c.get_raw_userdata(),
            HasUserdata::Reverb3D(r) => r.get_raw_userdata(),
            HasUserdata::SoundGroup(s) => s.get_raw_userdata(),
            // may occasionally be called when the channel is lost, but this should return Err() in that case
            HasUserdata::ChannelControl(c) => c.get_raw_userdata(),
            HasUserdata::Dsp(d) => d.get_raw_userdata(),
            HasUserdata::Sound(s) => s.get_raw_userdata(),
            HasUserdata::Geometry(g) => g.get_raw_userdata(),
            HasUserdata::DspConnection(c) => c.get_raw_userdata(),
            HasUserdata::System(s) => s.get_raw_userdata(),
        }
    }

    fn is_valid(&self, key: UserdataKey) -> bool {
        match self {
            HasUserdata::StudioSystem(s) => s.is_valid(),
            HasUserdata::Bank(b) => b.is_valid(),
            HasUserdata::EventDescription(e) => e.is_valid(),
            HasUserdata::EventInstance(e) => e.is_valid(),
            HasUserdata::CommandReplay(c) => c.is_valid(),
            // System is ALWAYS valid
            HasUserdata::System(_) => true,
            // when released, get_raw_userdata will return null
            // this is a bit of a hack though (and not very safe)
            // this should almost never be called when released though, so it should be fine...?
            _ => {
                let userdata = self.get_raw_userdata();
                userdata.is_ok_and(|ptr| ptr == key.into())
            }
        }
    }
}

pub type Userdata = Arc<dyn Any + Send + Sync + 'static>;

static STORAGE: Lazy<RwLock<UserdataStorage>> = Lazy::new(Default::default);

pub(crate) fn insert_userdata(userdata: Userdata, owner: impl Into<HasUserdata>) -> UserdataKey {
    let mut storage = STORAGE.write().unwrap();
    storage.slotmap.insert(UserdataValue {
        userdata,
        owner: owner.into(),
    })
}

pub(crate) fn remove_userdata(key: UserdataKey) -> Option<Userdata> {
    let mut storage = STORAGE.write().unwrap();
    storage.slotmap.remove(key).map(|v| v.userdata)
}

pub(crate) fn get_userdata(key: UserdataKey) -> Option<Userdata> {
    let storage = STORAGE.read().unwrap();
    storage.slotmap.get(key).map(|v| v.userdata.clone())
}

pub(crate) fn set_userdata(key: UserdataKey, userdata: Userdata) {
    let mut storage = STORAGE.write().unwrap();
    match storage.slotmap.get_mut(key) {
        Some(v) => v.userdata = userdata,
        None => eprintln!("Warning: userdata key does not exist!"),
    }
}

pub(crate) fn cleanup_userdata() {
    let mut storage = STORAGE.write().unwrap();
    storage.slotmap.retain(|k, v| v.owner.is_valid(k));
}

pub(crate) fn clear_userdata() {
    let mut storage = STORAGE.write().unwrap();
    storage.slotmap.clear();
}

impl From<UserdataKey> for *mut std::ffi::c_void {
    fn from(key: UserdataKey) -> Self {
        key.data().as_ffi() as *mut std::ffi::c_void
    }
}

impl From<*mut std::ffi::c_void> for UserdataKey {
    fn from(ptr: *mut std::ffi::c_void) -> Self {
        UserdataKey::from(KeyData::from_ffi(ptr as u64))
    }
}

impl From<StudioSystem> for HasUserdata {
    fn from(s: StudioSystem) -> Self {
        HasUserdata::StudioSystem(s)
    }
}

impl From<Bank> for HasUserdata {
    fn from(b: Bank) -> Self {
        HasUserdata::Bank(b)
    }
}

impl From<EventDescription> for HasUserdata {
    fn from(e: EventDescription) -> Self {
        HasUserdata::EventDescription(e)
    }
}

impl From<EventInstance> for HasUserdata {
    fn from(e: EventInstance) -> Self {
        HasUserdata::EventInstance(e)
    }
}

impl From<CommandReplay> for HasUserdata {
    fn from(c: CommandReplay) -> Self {
        HasUserdata::CommandReplay(c)
    }
}

impl From<Reverb3D> for HasUserdata {
    fn from(r: Reverb3D) -> Self {
        HasUserdata::Reverb3D(r)
    }
}

impl From<SoundGroup> for HasUserdata {
    fn from(s: SoundGroup) -> Self {
        HasUserdata::SoundGroup(s)
    }
}

impl From<ChannelControl> for HasUserdata {
    fn from(c: ChannelControl) -> Self {
        HasUserdata::ChannelControl(c)
    }
}

impl From<Dsp> for HasUserdata {
    fn from(d: Dsp) -> Self {
        HasUserdata::Dsp(d)
    }
}

impl From<Sound> for HasUserdata {
    fn from(s: Sound) -> Self {
        HasUserdata::Sound(s)
    }
}

impl From<Geometry> for HasUserdata {
    fn from(g: Geometry) -> Self {
        HasUserdata::Geometry(g)
    }
}

impl From<DspConnection> for HasUserdata {
    fn from(c: DspConnection) -> Self {
        HasUserdata::DspConnection(c)
    }
}

impl From<System> for HasUserdata {
    fn from(s: System) -> Self {
        HasUserdata::System(s)
    }
}