Large restructuring so lyra-scripting can create a Plugin
ci/woodpecker/push/debug Pipeline failed Details

This commit is contained in:
SeanOMik 2024-01-04 23:25:29 -05:00
parent 6caf235a6f
commit c673fd98ff
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
51 changed files with 831 additions and 551 deletions

1217
Cargo.lock generated Executable file → Normal file

File diff suppressed because it is too large Load Diff

View File

@ -9,34 +9,12 @@ members = [
"lyra-resource",
"lyra-ecs",
"lyra-reflect",
"lyra-scripting"
]
"lyra-scripting",
"lyra-game"]
[features]
scripting = ["dep:lyra-scripting"]
[dependencies]
lyra-resource = { path = "lyra-resource", version = "0.0.1" }
lyra-ecs = { path = "lyra-ecs" }
lyra-reflect = { path = "lyra-reflect" }
lyra-scripting = { path = "lyra-scripting", optional = true }
winit = "0.28.1"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = [ "tracing-log" ] }
tracing-log = "0.1.3"
tracing-appender = "0.2.2"
wgpu = "0.15.1"
async-std = { version = "1.12.0", features = [ "unstable", "attributes" ] }
cfg-if = "1"
bytemuck = { version = "1.12", features = [ "derive" ] }
image = { version = "0.24", default-features = false, features = ["png", "jpeg"] }
anyhow = "1.0"
instant = "0.1"
async-trait = "0.1.65"
glam = { version = "0.24.0", features = ["bytemuck", "debug-glam-assert"] }
gilrs-core = "0.5.6"
syn = "2.0.26"
quote = "1.0.29"
uuid = { version = "1.5.0", features = ["v4", "fast-rng"] }
itertools = "0.11.0"
lyra-game = { path = "lyra-game" }
lyra-scripting = { path = "lyra-scripting", optional = true }

View File

@ -231,6 +231,10 @@ impl World {
self.resources.insert(TypeId::of::<T>(), ResourceData::new(data));
}
pub fn add_resource_default<T: Default + 'static>(&mut self) {
self.resources.insert(TypeId::of::<T>(), ResourceData::new(T::default()));
}
/// Get a resource from the world, or insert it into the world with the provided
/// `fn` and return it.
pub fn get_resource_or_else<T: 'static, F>(&mut self, f: F) -> RefMut<T>

29
lyra-game/Cargo.toml Normal file
View File

@ -0,0 +1,29 @@
[package]
name = "lyra-game"
version = "0.0.1"
edition = "2021"
[dependencies]
lyra-resource = { path = "../lyra-resource" }
lyra-ecs = { path = "../lyra-ecs" }
lyra-reflect = { path = "../lyra-reflect" }
winit = "0.28.1"
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = [ "tracing-log" ] }
tracing-log = "0.1.3"
tracing-appender = "0.2.2"
wgpu = "0.15.1"
async-std = { version = "1.12.0", features = [ "unstable", "attributes" ] }
cfg-if = "1"
bytemuck = { version = "1.12", features = [ "derive" ] }
image = { version = "0.24", default-features = false, features = ["png", "jpeg"] }
anyhow = "1.0"
instant = "0.1"
async-trait = "0.1.65"
glam = { version = "0.24.0", features = ["bytemuck", "debug-glam-assert"] }
gilrs-core = "0.5.6"
syn = "2.0.26"
quote = "1.0.29"
uuid = { version = "1.5.0", features = ["v4", "fast-rng"] }
itertools = "0.11.0"

27
lyra-game/src/lib.rs Normal file
View File

@ -0,0 +1,27 @@
#![feature(hash_extract_if)]
#![feature(lint_reasons)]
#![feature(trait_alias)]
extern crate self as lyra_engine;
pub mod game;
pub mod render;
pub mod resources;
pub mod math;
pub mod input;
pub mod castable_any;
pub mod plugin;
pub mod change_tracker;
pub mod events;
pub use events::*;
pub mod delta_time;
pub use delta_time::*;
pub mod scene;
pub use lyra_resource as assets;
pub use lyra_ecs as ecs;
pub use plugin::DefaultPlugins;

View File

Before

Width:  |  Height:  |  Size: 545 B

After

Width:  |  Height:  |  Size: 545 B

View File

@ -13,6 +13,7 @@ lua = ["dep:mlua"]
lyra-ecs = { path = "../lyra-ecs" }
lyra-reflect = { path = "../lyra-reflect" }
lyra-resource = { path = "../lyra-resource" }
lyra-game = { path = "../lyra-game" }
thiserror = "1.0.50"
anyhow = "1.0.77"
tracing = "0.1.37"

View File

@ -1,4 +1,3 @@
#[cfg(feature = "lua")]
pub mod lua;
@ -14,6 +13,8 @@ pub use host::*;
pub mod script;
pub use script::*;
use lyra_game::game::Game;
#[allow(unused_imports)]
pub(crate) mod lyra_engine {
pub use lyra_ecs as ecs;
@ -57,4 +58,24 @@ impl Clone for ScriptBorrow {
data,
}
}
}
/// An extension trait that adds some helpful methods that makes it easier to do scripting things
pub trait GameScriptExt {
fn add_script_api_provider<T, P>(&mut self, provider: P)
where
T: ScriptHost,
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static;
}
impl GameScriptExt for Game {
fn add_script_api_provider<T, P>(&mut self, provider: P)
where
T: ScriptHost,
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static
{
let world = self.world();
let mut providers = world.get_resource_mut::<ScriptApiProviders<T>>();
providers.add_provider(provider);
}
}

View File

@ -2,6 +2,8 @@ pub mod dynamic_iter;
pub use dynamic_iter::*;
pub mod world;
use lyra_game::plugin::Plugin;
use lyra_resource::ResourceManager;
pub use world::*;
pub mod script;
@ -13,7 +15,7 @@ pub use loader::*;
#[cfg(test)]
mod test;
use std::ptr::NonNull;
use std::{ptr::NonNull, sync::Mutex};
use lyra_ecs::DynamicBundle;
use lyra_reflect::{Reflect, RegisteredType, FromType, AsRegisteredType};
@ -23,7 +25,7 @@ use mlua::{Lua, AnyUserDataExt};
pub const FN_NAME_INTERNAL_REFLECT_TYPE: &str = "__lyra_internal_reflect_type";
pub const FN_NAME_INTERNAL_REFLECT: &str = "__lyra_internal_reflect";
use crate::{ScriptBorrow, ScriptDynamicBundle};
use crate::{ScriptBorrow, ScriptDynamicBundle, ScriptApiProviders, ScriptContexts};
impl<'lua> mlua::FromLua<'lua> for ScriptBorrow {
fn from_lua(value: mlua::Value<'lua>, _lua: &'lua Lua) -> mlua::Result<Self> {
@ -110,4 +112,20 @@ impl mlua::UserData for ScriptDynamicBundle {
Ok(())
});
}
}
#[derive(Default)]
pub struct LuaScriptingPlugin;
impl Plugin for LuaScriptingPlugin {
fn setup(&self, game: &mut lyra_game::game::Game) {
let world = game.world();
world.add_resource_default::<LuaHost>();
world.add_resource_default::<ScriptApiProviders<LuaHost>>();
world.add_resource_default::<ScriptContexts<Mutex<mlua::Lua>>>();
let mut loader = world.get_resource_or_else(ResourceManager::default);
loader.register_loader::<LuaLoader>();
}
}

View File

@ -1,27 +1,4 @@
#![feature(hash_extract_if)]
#![feature(lint_reasons)]
#![feature(trait_alias)]
pub use lyra_game::*;
extern crate self as lyra_engine;
pub mod game;
pub mod render;
pub mod resources;
pub mod math;
pub mod input;
pub mod castable_any;
pub mod plugin;
pub mod change_tracker;
pub mod events;
pub use events::*;
pub mod delta_time;
pub use delta_time::*;
pub mod scene;
pub use lyra_resource as assets;
pub use lyra_ecs as ecs;
pub use plugin::DefaultPlugins;
#[cfg(feature = "scripting")]
pub use lyra_scripting::*;