Merge branch 'isse/plugin-spots' into 'master'

Register plugins before worldgen and support spots in plugins

See merge request veloren/veloren!4341
This commit is contained in:
Isse 2024-03-14 22:40:55 +00:00
commit 88a585c13f
9 changed files with 58 additions and 24 deletions

View File

@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- In commands that reference assets you can now use `#name` and press tab to cycle through assets with that name.
- Allow moving and resizing the chat with left and right mouse button respectively
- Missing plugins are requested from the server and cached locally
- Support for adding spots in plugins
### Changed

View File

@ -623,6 +623,8 @@ impl Client {
add_local_systems(dispatch_builder);
add_foreign_systems(dispatch_builder);
},
#[cfg(feature = "plugins")]
common_state::plugin::PluginMgr::from_asset_or_default(),
);
let mut missing_plugins: Vec<PluginHash> = Vec::new();
let mut local_plugins: Vec<PathBuf> = Vec::new();

View File

@ -178,6 +178,16 @@ pub struct PluginMgr {
}
impl PluginMgr {
pub fn from_asset_or_default() -> Self {
match Self::from_assets() {
Ok(plugin_mgr) => plugin_mgr,
Err(e) => {
tracing::error!(?e, "Failed to read plugins from assets");
PluginMgr::default()
},
}
}
pub fn from_assets() -> Result<Self, PluginError> {
let mut assets_path = (*ASSETS_PATH).clone();
assets_path.push("plugins");

View File

@ -156,6 +156,7 @@ impl State {
map_size_lg: MapSizeLg,
default_chunk: Arc<TerrainChunk>,
add_systems: impl Fn(&mut DispatcherBuilder),
#[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
) -> Self {
Self::new(
GameMode::Client,
@ -163,6 +164,8 @@ impl State {
map_size_lg,
default_chunk,
add_systems,
#[cfg(feature = "plugins")]
plugin_mgr,
)
}
@ -172,6 +175,7 @@ impl State {
map_size_lg: MapSizeLg,
default_chunk: Arc<TerrainChunk>,
add_systems: impl Fn(&mut DispatcherBuilder),
#[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
) -> Self {
Self::new(
GameMode::Server,
@ -179,6 +183,8 @@ impl State {
map_size_lg,
default_chunk,
add_systems,
#[cfg(feature = "plugins")]
plugin_mgr,
)
}
@ -188,6 +194,7 @@ impl State {
map_size_lg: MapSizeLg,
default_chunk: Arc<TerrainChunk>,
add_systems: impl Fn(&mut DispatcherBuilder),
#[cfg(feature = "plugins")] plugin_mgr: PluginMgr,
) -> Self {
prof_span!(guard, "create dispatcher");
let mut dispatch_builder =
@ -201,7 +208,14 @@ impl State {
drop(guard);
Self {
ecs: Self::setup_ecs_world(game_mode, Arc::clone(&pools), map_size_lg, default_chunk),
ecs: Self::setup_ecs_world(
game_mode,
Arc::clone(&pools),
map_size_lg,
default_chunk,
#[cfg(feature = "plugins")]
plugin_mgr,
),
thread_pool: pools,
dispatcher,
}
@ -215,6 +229,7 @@ impl State {
thread_pool: Arc<ThreadPool>,
map_size_lg: MapSizeLg,
default_chunk: Arc<TerrainChunk>,
#[cfg(feature = "plugins")] mut plugin_mgr: PluginMgr,
) -> specs::World {
prof_span!("State::setup_ecs_world");
let mut ecs = specs::World::new();
@ -340,28 +355,21 @@ impl State {
// Load plugins from asset directory
#[cfg(feature = "plugins")]
ecs.insert(match PluginMgr::from_assets() {
Ok(mut plugin_mgr) => {
let ecs_world = EcsWorld {
entities: &ecs.entities(),
health: ecs.read_component().into(),
uid: ecs.read_component().into(),
id_maps: &ecs.read_resource::<IdMaps>().into(),
player: ecs.read_component().into(),
};
if let Err(e) = plugin_mgr.load_event(&ecs_world, game_mode) {
tracing::debug!(?e, "Failed to run plugin init");
tracing::info!("Plugins disabled, enable debug logging for more information.");
PluginMgr::default()
} else {
plugin_mgr
}
},
Err(e) => {
tracing::debug!(?e, "Failed to read plugins from assets");
ecs.insert({
let ecs_world = EcsWorld {
entities: &ecs.entities(),
health: ecs.read_component().into(),
uid: ecs.read_component().into(),
id_maps: &ecs.read_resource::<IdMaps>().into(),
player: ecs.read_component().into(),
};
if let Err(e) = plugin_mgr.load_event(&ecs_world, game_mode) {
tracing::debug!(?e, "Failed to run plugin init");
tracing::info!("Plugins disabled, enable debug logging for more information.");
PluginMgr::default()
},
} else {
plugin_mgr
}
});
ecs

View File

@ -37,6 +37,7 @@ mod tests {
|dispatch_builder| {
dispatch::<character_behavior::Sys>(dispatch_builder, &[]);
},
common_state::plugin::PluginMgr::default(),
);
let msm = MaterialStatManifest::load().cloned();
state.ecs_mut().insert(msm);

View File

@ -43,6 +43,7 @@ pub fn setup(add_systems: impl Fn(&mut specs::DispatcherBuilder)) -> State {
DEFAULT_WORLD_CHUNKS_LG,
Arc::new(TerrainChunk::water(0)),
add_systems,
common_state::plugin::PluginMgr::default(),
);
state.ecs_mut().insert(MaterialStatManifest::with_empty());
state.ecs_mut().insert(AbilityMap::load().cloned());

View File

@ -280,6 +280,10 @@ impl Server {
let pools = State::pools(GameMode::Server);
// Load plugins before generating the world.
#[cfg(feature = "plugins")]
let plugin_mgr = PluginMgr::from_asset_or_default();
#[cfg(feature = "worldgen")]
let (world, index) = World::generate(
settings.world_seed,
@ -337,6 +341,8 @@ impl Server {
weather::add_server_systems(dispatcher_builder);
}
},
#[cfg(feature = "plugins")]
plugin_mgr,
);
register_event_busses(state.ecs_mut());
state.ecs_mut().insert(battlemode_buffer);

View File

@ -27,7 +27,7 @@ singleplayer = ["server"]
simd = ["vek/platform_intrinsics"]
tracy = ["common-frontend/tracy", "client/tracy"]
tracy-memory = ["tracy"] # enables heap profiling with tracy
plugins = ["client/plugins", "common-assets/plugins"]
plugins = ["client/plugins", "common-assets/plugins", "server/plugins"]
egui-ui = ["voxygen-egui", "egui", "egui_wgpu_backend", "egui_winit_platform"]
shaderc-from-source = ["shaderc/build-from-source"]
discord = ["discord-sdk"]

View File

@ -4,7 +4,7 @@ use crate::{
Canvas,
};
use common::{
assets::{Asset, AssetExt, AssetHandle, RonLoader},
assets::{Asset, AssetCombined, AssetHandle, Concatenate, RonLoader},
generation::EntityInfo,
terrain::{BiomeKind, Structure, TerrainChunkSize},
vol::RectVolSize,
@ -686,9 +686,14 @@ impl Asset for RonSpots {
const EXTENSION: &'static str = "ron";
}
impl Concatenate for RonSpots {
fn concatenate(self, b: Self) -> Self { Self(self.0.concatenate(b.0)) }
}
lazy_static! {
static ref RON_PROPERTIES: RonSpots = {
let spots: AssetHandle<RonSpots> = AssetExt::load_expect("world.manifests.spots");
let spots: AssetHandle<RonSpots> =
RonSpots::load_expect_combined_static("world.manifests.spots");
RonSpots(spots.read().0.to_vec())
};
}