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
// 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/.

use fmod_sys::*;
use lanyard::Utf8CStr;
use std::{ffi::c_int, mem::MaybeUninit};

use crate::studio::{EventDescription, UserProperty};

impl EventDescription {
    /// Retrieves a user property by name.
    pub fn get_user_property(&self, name: &Utf8CStr) -> Result<UserProperty> {
        let mut property = MaybeUninit::uninit();
        unsafe {
            FMOD_Studio_EventDescription_GetUserProperty(
                self.inner,
                name.as_ptr(),
                property.as_mut_ptr(),
            )
            .to_result()?;

            let property = UserProperty::from_ffi(property.assume_init());
            Ok(property)
        }
    }

    /// Retrieves a user property by index.
    ///
    /// May be used in combination with [`EventDescription::user_property_count`] to enumerate event user properties.
    pub fn get_user_property_by_index(&self, index: c_int) -> Result<UserProperty> {
        let mut property = MaybeUninit::uninit();
        unsafe {
            FMOD_Studio_EventDescription_GetUserPropertyByIndex(
                self.inner,
                index,
                property.as_mut_ptr(),
            )
            .to_result()?;

            let property = UserProperty::from_ffi(property.assume_init());
            Ok(property)
        }
    }

    pub fn user_property_count(&self) -> Result<c_int> {
        let mut count = 0;
        unsafe {
            FMOD_Studio_EventDescription_GetUserPropertyCount(self.inner, &mut count)
                .to_result()?;
        }
        Ok(count)
    }
}