fmod/studio/event_description/general.rs
1// Copyright (c) 2024 Lily Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use std::{ffi::c_int, mem::MaybeUninit};
8
9use fmod_sys::*;
10use lanyard::Utf8CString;
11
12use crate::studio::EventDescription;
13use crate::Guid;
14
15impl EventDescription {
16 /// Retrieves the GUID.
17 pub fn get_id(&self) -> Result<Guid> {
18 let mut guid = MaybeUninit::zeroed();
19 unsafe {
20 FMOD_Studio_EventDescription_GetID(self.inner, guid.as_mut_ptr()).to_result()?;
21
22 let guid = guid.assume_init().into();
23
24 Ok(guid)
25 }
26 }
27
28 /// Retrieves the length of the timeline.
29 ///
30 /// A timeline's length is the largest of any logic markers, transition leadouts and the end of any trigger boxes on the timeline.
31 pub fn get_length(&self) -> Result<c_int> {
32 let mut length = 0;
33 unsafe {
34 FMOD_Studio_EventDescription_GetLength(self.inner, &mut length).to_result()?;
35 }
36 Ok(length)
37 }
38
39 /// Retrieves the path.
40 ///
41 /// The strings bank must be loaded prior to calling this function, otherwise [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] is returned.
42 // TODO: convert into possible macro for the sake of reusing code
43 pub fn get_path(&self) -> Result<Utf8CString> {
44 let mut string_len = 0;
45
46 // retrieve the length of the string.
47 // this includes the null terminator, so we don't need to account for that.
48 unsafe {
49 let error = FMOD_Studio_EventDescription_GetPath(
50 self.inner,
51 std::ptr::null_mut(),
52 0,
53 &mut string_len,
54 )
55 .to_error();
56
57 // we expect the error to be fmod_err_truncated.
58 // if it isn't, we return the error.
59 match error {
60 Some(error) if error != FMOD_RESULT::FMOD_ERR_TRUNCATED => return Err(error),
61 _ => {}
62 }
63 };
64
65 let mut path = vec![0u8; string_len as usize];
66 let mut expected_string_len = 0;
67
68 unsafe {
69 FMOD_Studio_EventDescription_GetPath(
70 self.inner,
71 // u8 and i8 have the same layout, so this is ok
72 path.as_mut_ptr().cast(),
73 string_len,
74 &mut expected_string_len,
75 )
76 .to_result()?;
77
78 debug_assert_eq!(string_len, expected_string_len);
79
80 // all public fmod apis return UTF-8 strings. this should be safe.
81 // if i turn out to be wrong, perhaps we should add extra error types?
82 let path = Utf8CString::from_utf8_with_nul_unchecked(path);
83
84 Ok(path)
85 }
86 }
87
88 /// Checks that the [`EventDescription`] reference is valid.
89 pub fn is_valid(&self) -> bool {
90 unsafe { FMOD_Studio_EventDescription_IsValid(self.inner).into() }
91 }
92}