use crate::{dll::RLBotCoreInterface, interface::RLBotInterface, rlbot::RLBot};
use std::{error::Error, path::PathBuf, thread::sleep, time::Duration};
pub fn init() -> Result<RLBot, Box<dyn Error>> {
init_with_options(Default::default())
}
#[allow(clippy::needless_pass_by_value)]
pub fn init_with_options(options: InitOptions) -> Result<RLBot, Box<dyn Error>> {
let rlbot_dll_directory = options.rlbot_dll_directory.as_ref().map(PathBuf::as_path);
let dll = RLBotCoreInterface::load(rlbot_dll_directory)?;
wait_for_initialized(&dll)?;
Ok(RLBot::new(RLBotInterface::new(dll)))
}
fn wait_for_initialized(dll: &RLBotCoreInterface) -> Result<(), Box<dyn Error>> {
for _ in 0..100 {
if (dll.is_initialized)() {
return Ok(());
}
sleep(Duration::from_millis(10));
}
Err(From::from("RLBot did not become initialized"))
}
#[derive(Default)]
pub struct InitOptions {
rlbot_dll_directory: Option<PathBuf>,
}
impl InitOptions {
pub fn new() -> Self {
Self::default()
}
pub fn rlbot_dll_directory(mut self, rlbot_dll_directory: impl Into<PathBuf>) -> Self {
self.rlbot_dll_directory = Some(rlbot_dll_directory.into());
self
}
}