bevy_modloader 0.1.2

A library allowing you to load and unload Bevy mods
Documentation
use bevy_app::App;
use bevy_config_system::ConfigKey;
use bevy_ecs::{system::Commands, world::World};
use bevy_utils::HashSet;

use crate::mod_object::{ModConfig, ModLoaderInterface, ModState};

#[derive(Default)]
pub struct RustModVTable {
    pub build: Option<Box<dyn FnOnce(&mut App) + Send + Sync>>,
    pub build_cleanup: Option<Box<dyn FnOnce(&mut App) + Send + Sync>>,

    pub load: Option<
        Box<dyn FnMut(ModState, &ModConfig, Commands, &World) -> Result<(), String> + Send + Sync>,
    >,
    pub config_keys: Option<HashSet<ConfigKey>>,
}

impl ModLoaderInterface for RustModVTable {
    fn build(&mut self, app: &mut App) {
        if let Some(build) = self.build.take() {
            build(app);
        }
    }

    fn get_config_keys(&self) -> HashSet<ConfigKey> {
        self.config_keys.clone().unwrap_or_default()
    }

    fn build_cleanup(&mut self, app: &mut App) {
        if let Some(build_cleanup) = self.build_cleanup.take() {
            build_cleanup(app);
        }
    }

    fn load(
        &mut self,
        state: ModState,
        config: &ModConfig,
        commands: Commands,
        world: &World,
    ) -> Result<(), String> {
        if let Some(load) = self.load.as_mut() {
            load(state, config, commands, world)
        } else {
            Ok(())
        }
    }

    fn exclusive(&self) -> bool {
        false
    }
}