fmod/studio/command_replay/
query.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 fmod_sys::*;
8use lanyard::{Utf8CStr, Utf8CString};
9use std::{
10    ffi::{c_float, c_int},
11    mem::MaybeUninit,
12};
13
14use crate::{
15    get_string,
16    studio::{CommandInfo, CommandReplay, System},
17};
18
19impl CommandReplay {
20    /// Sets a path substition that will be used when loading banks with this replay.
21    ///
22    /// [`System::load_bank_file`] commands in the replay are redirected to load banks from the specified directory, instead of using the directory recorded in the captured commands.
23    pub fn set_bank_path(&self, path: &Utf8CStr) -> Result<()> {
24        unsafe { FMOD_Studio_CommandReplay_SetBankPath(self.inner, path.as_ptr()).to_result() }
25    }
26
27    /// Retrieves the command index corresponding to the given playback time.
28    ///
29    /// This function will return an index for the first command at or after `time`.
30    /// If `time` is greater than the total playback time then [`FMOD_RESULT::FMOD_ERR_EVENT_NOTFOUND`] is returned.
31    pub fn command_at_time(&self, time: c_float) -> Result<c_int> {
32        let mut index = 0;
33        unsafe {
34            FMOD_Studio_CommandReplay_GetCommandAtTime(self.inner, time, &mut index).to_result()?;
35        }
36        Ok(index)
37    }
38
39    /// Retrieves the number of commands in the replay.
40    pub fn get_command_count(&self) -> Result<c_int> {
41        let mut count = 0;
42        unsafe {
43            FMOD_Studio_CommandReplay_GetCommandCount(self.inner, &mut count).to_result()?;
44        }
45        Ok(count)
46    }
47
48    /// Retrieves command information.
49    pub fn get_command_info(&self, index: c_int) -> Result<CommandInfo> {
50        let mut info = MaybeUninit::zeroed();
51
52        unsafe {
53            FMOD_Studio_CommandReplay_GetCommandInfo(self.inner, index, info.as_mut_ptr())
54                .to_result()?;
55
56            let info = CommandInfo::from_ffi(info.assume_init());
57            Ok(info)
58        }
59    }
60
61    /// Retrieves the string representation of a command.
62    pub fn get_command_string(&self, index: c_int) -> Result<Utf8CString> {
63        let string = get_string(|buffer| unsafe {
64            FMOD_Studio_CommandReplay_GetCommandString(
65                self.inner,
66                index,
67                buffer.as_mut_ptr().cast::<i8>(),
68                buffer.len() as c_int,
69            )
70        })?;
71
72        Ok(string)
73    }
74
75    /// Retrieves the total playback time.
76    pub fn get_length(&self) -> Result<c_float> {
77        let mut length = 0.0;
78        unsafe {
79            FMOD_Studio_CommandReplay_GetLength(self.inner, &mut length).to_result()?;
80        }
81        Ok(length)
82    }
83
84    /// Retrieves the Studio System object associated with this replay object.
85    pub fn get_system(&self) -> Result<System> {
86        let mut system = std::ptr::null_mut();
87        unsafe {
88            FMOD_Studio_CommandReplay_GetSystem(self.inner, &mut system).to_result()?;
89            Ok(System::from(system))
90        }
91    }
92
93    /// Checks that the [`CommandReplay`] reference is valid.
94    pub fn is_valid(&self) -> bool {
95        unsafe { FMOD_Studio_CommandReplay_IsValid(self.inner).into() }
96    }
97}