#[cfg(feature = "studio-bridge")]
pub mod headless;
pub mod launcher;
pub mod runtime;
#[cfg(feature = "native")]
pub use launcher::{launch, launch_with};
use anyhow::{anyhow, Result};
use arora_behavior_tree::{
arora_generated::behavior_tree::status::Status, run_behavior_tree, schema_groot,
tree_node::TreeNode, BehaviorTree, ModuleFunction,
};
use arora_engine::engine::{EngineBuilder, PinnedEngine};
#[cfg(feature = "native")]
use arora_engine::executor::{native::NativeExecutor, wasm::WebAssemblyExecutor};
use std::collections::HashMap;
use std::rc::Rc;
use uuid::Uuid;
pub struct Arora {
engine: PinnedEngine,
function_index: Rc<HashMap<Uuid, ModuleFunction>>,
}
impl Arora {
pub async fn start() -> Result<Self> {
let engine = build_engine()?;
Ok(Self {
engine,
function_index: Rc::new(HashMap::new()),
})
}
pub fn run_groot_xml(&mut self, xml: &str) -> Result<Status> {
let groot = schema_groot::BehaviorTree::try_from_groot_xml(xml)
.map_err(|e| anyhow!("failed to parse Groot XML: {e:?}"))?;
let mut variables = HashMap::new();
let tree_node: TreeNode = groot
.root
.try_into_tree_node(self.function_index.as_ref(), &mut variables)
.map_err(|e| anyhow!("failed to build behavior tree from Groot: {e:?}"))?;
let behavior: BehaviorTree = tree_node
.try_into()
.map_err(|e| anyhow!("failed to instantiate behavior tree: {e:?}"))?;
run_behavior_tree(
&behavior,
self.function_index.clone(),
&mut self.engine,
false,
)
.map_err(|e| anyhow!("behavior tree run failed: {e:?}"))
}
pub fn run_forever(&mut self) -> Result<()> {
loop {
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
}
#[cfg(feature = "native")]
fn build_engine() -> Result<PinnedEngine> {
Ok(EngineBuilder::new()
.add_executor(
WebAssemblyExecutor::new()
.map_err(|e| anyhow!("failed to create wasm executor: {e}"))?,
)
.add_executor(NativeExecutor::new())
.build())
}
#[cfg(not(feature = "native"))]
fn build_engine() -> Result<PinnedEngine> {
use arora_engine::executor::browser::BrowserExecutor;
Ok(EngineBuilder::new()
.add_executor(BrowserExecutor::new())
.build())
}