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

refactor!: SessionBuilder makes systems + world immutable during session build + Add a rollback-safe world reset utility #489

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
114dce6
fix(stages): Move SystemStages has_started into resource to make startup
MaxCWhitehead Nov 12, 2024
2d27421
fix(stages): Single success system tracks completion in Resource for …
MaxCWhitehead Nov 12, 2024
956391e
fix(resources): Resources.contains now checks if cell has a value, not
MaxCWhitehead Nov 18, 2024
793db0f
feat(resources): Add `clear_owned_resources` to reset any non-shared …
MaxCWhitehead Nov 18, 2024
8de69f2
chore(clippy): Fix clippy warning in bones_matchmaker
MaxCWhitehead Nov 18, 2024
a8d1058
chore(deps): Move `tracing` to workspace dependency.
MaxCWhitehead Nov 18, 2024
4654b58
fix(sessions): Fixup plugins to use SessionBuilder.
MaxCWhitehead Nov 18, 2024
2098760
fix(sesssions): Store Sessions as shared resource, so not copied in
MaxCWhitehead Nov 18, 2024
bcd0bd5
feat(SystemStages): Use builder pattern for SystemStages to restrict
MaxCWhitehead Nov 19, 2024
4d37ad5
chore: Remove exisitng reset_internals impl, systems no longer mutable.
MaxCWhitehead Nov 19, 2024
3f2d8f9
feat: Convert to builder pattern for Session construction, and implement
MaxCWhitehead Nov 19, 2024
437ca6a
feat: handle_world_reset can be optionally provided SystemStages to
MaxCWhitehead Nov 22, 2024
69f9a1b
feat: ResetWorld may be provided with resources to override res state
MaxCWhitehead Nov 23, 2024
ad5ee59
fix: on session build, insert startup resources immediately.
MaxCWhitehead Dec 16, 2024
2e6576f
fix: RngGenerator now preserved using ResetWorld's reset_resources - fix
MaxCWhitehead Dec 25, 2024
089ac8a
fix: During reset, startup resources + preserved reset resources
MaxCWhitehead Dec 26, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ parking_lot = "0.12"
smallvec = "1.11"
ustr = "0.10"
iroh-net = "0.27"
tracing = "0.1"

[profile.release]
lto = true
11 changes: 6 additions & 5 deletions demos/asset_packs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ fn main() {

// Create a new session for the game menu. Each session is it's own bones world with it's own
// plugins, systems, and entities.
let menu_session = game.sessions.create("menu");
menu_session
game.sessions.create_with("menu", |builder| {
// Install the default bones_framework plugin for this session
.install_plugin(DefaultSessionPlugin)
// Add our menu system to the update stage
.add_system_to_stage(Update, menu_system);
builder
.install_plugin(DefaultSessionPlugin)
// Add our menu system to the update stage
.add_system_to_stage(Update, menu_system);
});

BonesBevyRenderer::new(game).app().run();
}
Expand Down
10 changes: 7 additions & 3 deletions demos/assets_minimal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ fn main() {

// Create a new session for the game menu. Each session is it's own bones world with it's own
// plugins, systems, and entities.
let menu_session = game.sessions.create("menu");
menu_session
// Install the default bones_framework plugin for this session
let mut menu_session_builder = SessionBuilder::new("menu");

// Install the default bones_framework plugin for this session
menu_session_builder
.install_plugin(DefaultSessionPlugin)
// Add our menu system to the update stage
.add_system_to_stage(Update, menu_system);

// Finalize session and register with game sessions.
menu_session_builder.finish_and_add(&mut game.sessions);

BonesBevyRenderer::new(game).app().run();
}

Expand Down
43 changes: 15 additions & 28 deletions demos/features/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub fn create_game() -> Game {
TilemapDemoMeta::register_schema();

// Create our menu session
game.sessions.create("menu").install_plugin(menu_plugin);
game.sessions.create_with("menu", menu_plugin);

game
}
Expand All @@ -139,12 +139,11 @@ struct MenuData {
}

/// Menu plugin
pub fn menu_plugin(session: &mut Session) {
pub fn menu_plugin(session: &mut SessionBuilder) {
// Register our menu system
session
// Install the bones_framework default plugins for this session
.install_plugin(DefaultSessionPlugin)
.world
// Initialize our menu data resource
.init_resource::<MenuData>();

Expand Down Expand Up @@ -198,9 +197,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("sprite_demo")
.install_plugin(sprite_demo_plugin);
sessions.create_with("sprite_demo", sprite_demo_plugin);
}

if BorderedButton::themed(&meta.button_style, localization.get("atlas-demo"))
Expand All @@ -211,9 +208,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("atlas_demo")
.install_plugin(atlas_demo_plugin);
sessions.create_with("atlas_demo", atlas_demo_plugin);
}

if BorderedButton::themed(&meta.button_style, localization.get("tilemap-demo"))
Expand All @@ -224,9 +219,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("tilemap_demo")
.install_plugin(tilemap_demo_plugin);
sessions.create_with("tilemap_demo", tilemap_demo_plugin);
}

if BorderedButton::themed(&meta.button_style, localization.get("audio-demo"))
Expand All @@ -237,9 +230,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("audio_demo")
.install_plugin(audio_demo_plugin);
sessions.create_with("audio_demo", audio_demo_plugin);
}

if BorderedButton::themed(&meta.button_style, localization.get("storage-demo"))
Expand All @@ -250,9 +241,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("storage_demo")
.install_plugin(storage_demo_plugin);
sessions.create_with("storage_demo", storage_demo_plugin);
}

if BorderedButton::themed(&meta.button_style, localization.get("path2d-demo"))
Expand All @@ -263,9 +252,7 @@ fn menu_system(
session_options.delete = true;

// Create a session for the match
sessions
.create("path2d_demo")
.install_plugin(path2d_demo_plugin);
sessions.create_with("path2d_demo", path2d_demo_plugin);
}

if let Some(exit_bones) = &mut exit_bones {
Expand Down Expand Up @@ -293,7 +280,7 @@ fn menu_system(
}

/// Plugin for running the sprite demo.
fn sprite_demo_plugin(session: &mut Session) {
fn sprite_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_startup_system(sprite_demo_startup)
Expand Down Expand Up @@ -357,7 +344,7 @@ fn move_sprite(
}

/// Plugin for running the tilemap demo.
fn tilemap_demo_plugin(session: &mut Session) {
fn tilemap_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_startup_system(tilemap_startup_system)
Expand Down Expand Up @@ -403,7 +390,7 @@ fn tilemap_startup_system(
}

/// Plugin for running the atlas demo.
fn atlas_demo_plugin(session: &mut Session) {
fn atlas_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_startup_system(atlas_demo_startup)
Expand Down Expand Up @@ -451,7 +438,7 @@ fn atlas_demo_startup(
);
}

fn audio_demo_plugin(session: &mut Session) {
fn audio_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_system_to_stage(Update, back_to_menu_ui)
Expand All @@ -477,7 +464,7 @@ fn audio_demo_ui(
});
}

fn storage_demo_plugin(session: &mut Session) {
fn storage_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_system_to_stage(Update, storage_demo_ui)
Expand Down Expand Up @@ -507,7 +494,7 @@ fn storage_demo_ui(
});
}

fn path2d_demo_plugin(session: &mut Session) {
fn path2d_demo_plugin(session: &mut SessionBuilder) {
session
.install_plugin(DefaultSessionPlugin)
.add_startup_system(path2d_demo_startup)
Expand Down Expand Up @@ -558,7 +545,7 @@ fn back_to_menu_ui(
ui.add_space(20.0);
if ui.button(localization.get("back-to-menu")).clicked() {
session_options.delete = true;
sessions.create("menu").install_plugin(menu_plugin);
sessions.create_with("menu", menu_plugin);
}
});
});
Expand Down
13 changes: 7 additions & 6 deletions demos/hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ fn main() {

// Create a new session for the game menu. Each session is it's own bones world with it's own
// plugins, systems, and entities.
let menu_session = game.sessions.create("menu");
menu_session
// Install the default bones_framework plugin for this session
.install_plugin(DefaultSessionPlugin)
// Add our menu system to the update stage
.add_system_to_stage(Update, menu_system);
game.sessions.create_with("menu", |session| {
session
// Install the default bones_framework plugin for this session
.install_plugin(DefaultSessionPlugin)
// Add our menu system to the update stage
.add_system_to_stage(Update, menu_system);
});

BonesBevyRenderer::new(game).app().run();
}
Expand Down
26 changes: 14 additions & 12 deletions demos/scripting/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn main() {
.register_default_assets();
GameMeta::register_schema();

game.sessions
.create("launch")
.add_startup_system(launch_game_session);
game.sessions.create_with("launch", |builder| {
builder.add_startup_system(launch_game_session);
});

let mut renderer = BonesBevyRenderer::new(game);
renderer.app_namespace = (
Expand All @@ -41,15 +41,17 @@ fn launch_game_session(
mut session_ops: ResMut<SessionOptions>,
) {
session_ops.delete = true;
let game_session = sessions.create("game");
game_session
.install_plugin(DefaultSessionPlugin)
// Install the plugin that will load our lua plugins and run them in the game session
.install_plugin(LuaPluginLoaderSessionPlugin(
// Tell it to install the lua plugins specified in our game meta
Arc::new(meta.plugins.iter().copied().collect()),
))
.add_startup_system(game_startup);
// Build game session and add to `Sessions`
sessions.create_with("game", |builder| {
builder
.install_plugin(DefaultSessionPlugin)
// Install the plugin that will load our lua plugins and run them in the game session
.install_plugin(LuaPluginLoaderSessionPlugin(
// Tell it to install the lua plugins specified in our game meta
Arc::new(meta.plugins.iter().copied().collect()),
))
.add_startup_system(game_startup);
});
}

fn game_startup(
Expand Down
2 changes: 1 addition & 1 deletion framework_crates/bones_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.9"
sha2 = "0.10"
tracing = "0.1"
tracing = { workspace = true }
ulid = "1.0"
ustr = { workspace = true }

Expand Down
1 change: 1 addition & 0 deletions framework_crates/bones_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ atomicell = "0.2"
bitset-core = "0.1"
once_map = "0.4.12"
thiserror = "1.0"
tracing = { workspace = true }

[dev-dependencies]
glam = "0.24"
9 changes: 6 additions & 3 deletions framework_crates/bones_ecs/examples/pos_vel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ fn main() {
// Initialize an empty world
let mut world = World::new();

// Create a SystemStages to store the systems that we will run more than once.
let mut stages = SystemStages::with_core_stages();
// Build SystemStages to store the systems that we will run more than once.
let mut stages_builder = SystemStagesBuilder::with_core_stages();

// Add our systems to the system stages
stages
stages_builder
.add_startup_system(startup_system)
.add_system_to_stage(CoreStage::Update, pos_vel_system)
.add_system_to_stage(CoreStage::PostUpdate, print_system);

// Finish build to get `SystemStages`.
let mut stages = stages_builder.finish();

// Run our game loop for 10 frames
for _ in 0..10 {
stages.run(&mut world);
Expand Down
Loading