Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using runtime schemas as metadata assets. #265

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demos/scripting/assets/demo-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sprite: image.png
4 changes: 1 addition & 3 deletions demos/scripting/assets/game.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
plugins:
- plugin1.plugin.lua
version: 1
info: ./info.yaml
sprite: ./image.png
data: ./demo-data.yaml
1 change: 1 addition & 0 deletions demos/scripting/assets/pack.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
root: game.yaml
schemas:
- schemas/DemoSprite.yaml
- schemas/ScriptingDemoData.yaml
5 changes: 4 additions & 1 deletion demos/scripting/assets/plugin1.plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ local function startup()
local meta = assets.root
local entities = resources:get(Entities)

local data_handle = meta.data
local data = assets:get(data_handle)

local ent = entities:create()
components:insert(ent, Transform:create())
local sprite = Sprite:create()
sprite.image = meta.sprite
sprite.image = data.sprite
components:insert(ent, sprite)
components:insert(ent, DemoSprite:create())
end
Expand Down
9 changes: 9 additions & 0 deletions demos/scripting/assets/schemas/ScriptingDemoData.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# A marker type for our demo sprite.
name: ScriptingDemoData
full_name: demo_scripting::ScriptingDemoData
asset_extension: demo-data
kind: !Struct
fields:
- name: sprite
schema: UntypedHandle

49 changes: 4 additions & 45 deletions demos/scripting/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,16 @@ use bones_framework::prelude::*;
#[repr(C)]
struct GameMeta {
plugins: SVec<Handle<LuaPlugin>>,
version: u32,
sprite: Handle<Image>,
info: Handle<GameInfoMeta>,
}

#[derive(HasSchema, Default, Clone)]
#[repr(C)]
#[type_data(metadata_asset("info"))]
struct GameInfoMeta {
name: String,
gravity: f32,
}

#[derive(HasSchema, Default, Clone)]
#[repr(C)]
struct DemoData {
name: String,
age: f32,
favorite_things: SVec<String>,
attributes: SMap<String, f32>,
best_friend: Maybe<String>,
state: DemoState,
}

#[derive(HasSchema, Default, Clone)]
#[repr(C, u8)]
pub enum DemoState {
#[default]
Ready,
Thinking(f32),
Finished {
score: u32,
},
data: Handle<SchemaBox>,
}

fn main() {
let mut game = Game::new();
game.install_plugin(DefaultGamePlugin);
game.shared_resource_mut::<AssetServer>()
.unwrap()
.register_default_assets();
GameMeta::register_schema();
DemoData::register_schema();

game.sessions
.create("launch")
Expand Down Expand Up @@ -75,17 +45,6 @@ fn launch_game_session(
meta.plugins.iter().copied().collect(),
))
.add_startup_system(game_startup);

game_session.world.insert_resource(DemoData {
name: "default name".into(),
age: 10.0,
favorite_things: ["candy".into(), "rain".into()].into_iter().collect(),
attributes: [("coolness".into(), 50.0), ("friendliness".into(), 10.57)]
.into_iter()
.collect(),
best_friend: Some("Jane".into()).into(),
state: DemoState::Thinking(20.),
});
}

fn game_startup(
Expand Down
2 changes: 1 addition & 1 deletion framework_crates/bones_asset/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl SchemaMetaAssetLoader {
}
}

/// The kind of asset a type represents.
/// A [type data][bones_schema::alloc::TypeDatas] that indicates how to load a type as an asset.
#[derive(HasSchema)]
#[schema(opaque, no_default, no_clone)]
pub enum AssetKind {
Expand Down
4 changes: 2 additions & 2 deletions framework_crates/bones_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ unsafe impl HasSchema for UntypedHandle {
);
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
name: type_name::<Self>().into(),
full_name: format!("{}::{}", module_path!(), type_name::<Self>()).into(),
name: "UntypedHandle".into(),
full_name: format!("{}::{}", module_path!(), "UntypedHandle").into(),
type_id: Some(TypeId::of::<Self>()),
kind: SchemaKind::Struct(StructSchemaInfo {
fields: vec![StructFieldInfo {
Expand Down
14 changes: 12 additions & 2 deletions framework_crates/bones_asset/src/server/schema_loader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ffi::c_void;

use bones_utils::{default, ustr};
use bones_schema::alloc::TypeDatas;
use bones_utils::ustr;
use serde::Deserialize;

use crate::prelude::*;
Expand All @@ -14,6 +15,8 @@ struct SchemaMeta {
name: String,
full_name: String,
kind: SchemaKindMeta,
#[serde(default)]
asset_extension: Option<String>,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -171,11 +174,18 @@ impl<'de> Deserialize<'de> for PackSchema {
}
};

let type_data = TypeDatas::default();
if let Some(ext) = meta.asset_extension {
type_data
.insert(AssetKind::Metadata { extension: ext })
.unwrap();
}

let schema_data = SchemaData {
name,
full_name,
kind: schema_kind,
type_data: default(),
type_data,
type_id: None,
clone_fn: Some(unsafe { Unsafe::new(Box::leak(Box::new(clone_fn))) }),
drop_fn: Some(unsafe { Unsafe::new(Box::leak(Box::new(drop_fn))) }),
Expand Down
5 changes: 5 additions & 0 deletions framework_crates/bones_schema/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,11 @@ pub struct SchemaBox {
ptr: NonNull<c_void>,
schema: &'static Schema,
}
impl Default for SchemaBox {
fn default() -> Self {
SchemaBox::new(())
}
}
unsafe impl Sync for SchemaBox {}
unsafe impl Send for SchemaBox {}
impl std::fmt::Debug for SchemaBox {
Expand Down
35 changes: 21 additions & 14 deletions framework_crates/bones_schema/src/std_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use crate::{alloc::TypeDatas, prelude::*, raw_fns::*};
use std::{alloc::Layout, any::TypeId, hash::Hasher, sync::OnceLock, time::Duration};

macro_rules! impl_primitive {
($t:ty, $prim:ident) => {
($t:ty, $prim:expr ) => {
unsafe impl HasSchema for $t {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
name: stringify!($t).into(),
full_name: concat!("std::", stringify!($t)).into(),
kind: SchemaKind::Primitive(Primitive::$prim),
kind: SchemaKind::Primitive($prim),
type_id: Some(TypeId::of::<$t>()),
clone_fn: Some(<$t as RawClone>::raw_clone_cb()),
drop_fn: Some(<$t as RawDrop>::raw_drop_cb()),
Expand All @@ -34,18 +34,25 @@ macro_rules! impl_primitive {
};
}

impl_primitive!(String, String);
impl_primitive!(bool, Bool);
impl_primitive!(u8, U8);
impl_primitive!(u16, U16);
impl_primitive!(u32, U32);
impl_primitive!(u64, U64);
impl_primitive!(u128, U128);
impl_primitive!(i8, I8);
impl_primitive!(i16, I16);
impl_primitive!(i32, I32);
impl_primitive!(i64, I64);
impl_primitive!(i128, I128);
impl_primitive!(String, Primitive::String);
impl_primitive!(
(),
Primitive::Opaque {
size: std::mem::size_of::<()>(),
align: std::mem::align_of::<()>()
}
);
impl_primitive!(bool, Primitive::Bool);
impl_primitive!(u8, Primitive::U8);
impl_primitive!(u16, Primitive::U16);
impl_primitive!(u32, Primitive::U32);
impl_primitive!(u64, Primitive::U64);
impl_primitive!(u128, Primitive::U128);
impl_primitive!(i8, Primitive::I8);
impl_primitive!(i16, Primitive::I16);
impl_primitive!(i32, Primitive::I32);
impl_primitive!(i64, Primitive::I64);
impl_primitive!(i128, Primitive::I128);

macro_rules! schema_impl_float {
($t:ty, $prim:ident) => {
Expand Down
3 changes: 3 additions & 0 deletions framework_crates/bones_scripting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod lua;

use bones_asset::UntypedHandle;
use bones_lib::prelude::*;

/// The prelude.
Expand All @@ -22,6 +23,8 @@ impl Default for ScriptingGamePlugin {

impl GamePlugin for ScriptingGamePlugin {
fn install(self, game: &mut Game) {
UntypedHandle::register_schema();

if self.enable_lua {
game.install_plugin(lua::lua_game_plugin);
}
Expand Down
27 changes: 26 additions & 1 deletion framework_crates/bones_scripting/src/lua/bindings/ecsref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,32 @@ pub fn metatable(ctx: Context) -> Table {
let newvalue = newvalue.as_static_user_data::<EcsRef>()?;
let newvalue_b = newvalue.borrow();
let newvalue_ref = newvalue_b.schema_ref()?;
this_ref.write(newvalue_ref)?;

// If the current and new ref are asset handles
if this_ref
.schema()
.type_data
.get::<SchemaAssetHandle>()
.is_some()
&& newvalue_ref
.schema()
.type_data
.get::<SchemaAssetHandle>()
.is_some()
{
// SOUND: the `SchemaAssetHandle` type data asserts that these types
// are represented by `UntypedHandle`. Additionally, `SchemaAssetHandle`
// cannot be constructed outside the crate due to private fields, so it
// cannot be added to non-conforming types.
unsafe {
*this_ref.cast_mut_unchecked::<UntypedHandle>() =
*newvalue_ref.cast_unchecked::<UntypedHandle>()
}
} else {
// If we are not dealing with asset handles
// Attempt to write the new value
this_ref.write(newvalue_ref)?;
}
}
SchemaRefMutAccess::Primitive(p) => match (p, newvalue) {
(PrimitiveRefMut::Bool(b), Value::Boolean(newb)) => *b = newb,
Expand Down
Loading