Skip to content

Commit f402009

Browse files
TekhnaeRaavzicklag
authored andcommitted
feat: add shared_resource shortcuts
1 parent 665a026 commit f402009

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

framework_crates/bones_bevy_renderer/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct BonesGame(pub bones::Game);
6868
impl BonesGame {
6969
/// Shorthand for [`bones::AssetServer`] typed access to the shared resource
7070
pub fn asset_server(&self) -> Option<bones::Ref<'_, bones::AssetServer>> {
71-
self.0.shared_resource()
71+
self.0.get_shared_resource()
7272
}
7373
}
7474

@@ -166,7 +166,7 @@ impl BonesBevyRenderer {
166166
}
167167
app.init_resource::<BonesImageIds>();
168168

169-
if let Some(mut asset_server) = self.game.shared_resource_mut::<bones::AssetServer>() {
169+
if let Some(mut asset_server) = self.game.get_shared_resource_mut::<bones::AssetServer>() {
170170
asset_server.set_game_version(self.game_version);
171171
asset_server.set_io(asset_io(&self.asset_dir, &self.packs_dir));
172172

@@ -273,7 +273,7 @@ impl BonesBevyRenderer {
273273
}
274274

275275
fn egui_ctx_initialized(game: Res<BonesGame>) -> bool {
276-
game.shared_resource::<bones::EguiCtx>().is_some()
276+
game.get_shared_resource::<bones::EguiCtx>().is_some()
277277
}
278278

279279
fn assets_are_loaded(game: Res<BonesGame>) -> bool {
@@ -368,10 +368,9 @@ pub fn handle_asset_changes(
368368
mut bevy_egui_textures: ResMut<bevy_egui::EguiUserTextures>,
369369
mut bones_image_ids: ResMut<BonesImageIds>,
370370
) {
371-
if let Some(mut asset_server) = game.shared_resource_mut::<bones::AssetServer>() {
371+
if let Some(mut asset_server) = game.get_shared_resource_mut::<bones::AssetServer>() {
372372
asset_server.handle_asset_changes(|asset_server, handle| {
373-
let mut bones_egui_textures =
374-
game.shared_resource_mut::<bones::EguiTextures>().unwrap();
373+
let mut bones_egui_textures = game.shared_resource_mut::<bones::EguiTextures>();
375374
let Some(mut asset) = asset_server.get_asset_untyped_mut(handle) else {
376375
// There was an issue loading the asset. The error will have been logged.
377376
return;
@@ -394,7 +393,7 @@ pub fn handle_asset_changes(
394393

395394
#[cfg(not(target_arch = "wasm32"))]
396395
fn handle_exits(game: Res<BonesGame>, mut exits: EventWriter<bevy::app::AppExit>) {
397-
if **game.shared_resource::<bones::ExitBones>().unwrap() {
396+
if **game.shared_resource::<bones::ExitBones>() {
398397
exits.send(bevy::app::AppExit);
399398
}
400399
}

framework_crates/bones_bevy_renderer/src/rumble.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub fn handle_bones_rumble(
1212
game: ResMut<BonesGame>,
1313
mut rumble_requests: EventWriter<BevyGamepadRumbleRequest>,
1414
) {
15-
if let Some(mut bones_rumble_requests) = game.shared_resource_mut::<bones::GamepadsRumble>() {
15+
if let Some(mut bones_rumble_requests) = game.get_shared_resource_mut::<bones::GamepadsRumble>()
16+
{
1617
while let Some(request) = bones_rumble_requests.requests.pop_front() {
1718
match request {
1819
bones::GamepadRumbleRequest::AddRumble {

framework_crates/bones_framework/tests/reset.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn startup_system_reset() {
2424
game.step(Instant::now());
2525

2626
// Verify startup system ran and incremented only once
27-
assert_eq!(game.shared_resource::<Counter>().unwrap().0, 1);
27+
assert_eq!(game.shared_resource::<Counter>().0, 1);
2828

2929
// Add command that will trigger reset on next step
3030
{
@@ -43,7 +43,7 @@ pub fn startup_system_reset() {
4343
game.step(Instant::now());
4444

4545
// Shared resource is not included in reset, should be incremented 2nd time
46-
assert_eq!(game.shared_resource::<Counter>().unwrap().0, 2);
46+
assert_eq!(game.shared_resource::<Counter>().0, 2);
4747
}
4848

4949
/// Verify that single success systems run again (until success condition)

framework_crates/bones_lib/src/lib.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,9 @@ impl Game {
429429
self
430430
}
431431
#[track_caller]
432-
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
433-
pub fn shared_resource<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
432+
/// Get the shared resource of a given type out of this [`Game`]s shared
433+
/// resources if it exists.
434+
pub fn get_shared_resource<T: HasSchema>(&self) -> Option<Ref<'_, T>> {
434435
let res = self
435436
.shared_resources
436437
.iter()
@@ -448,8 +449,9 @@ impl Game {
448449
}
449450

450451
#[track_caller]
451-
/// Get the shared resource of a given type out of this [`Game`]s shared resources.
452-
pub fn shared_resource_mut<T: HasSchema>(&self) -> Option<RefMut<'_, T>> {
452+
/// Get the mutable shared resource of a given type out of this [`Game`]s
453+
/// shared resources if it exists.
454+
pub fn get_shared_resource_mut<T: HasSchema>(&self) -> Option<RefMut<'_, T>> {
453455
let res = self
454456
.shared_resources
455457
.iter()
@@ -466,6 +468,22 @@ impl Game {
466468
}
467469
}
468470

471+
#[track_caller]
472+
/// Get the shared resource of a given type out of this [`Game`]s shared
473+
/// resources. Panics if the resource doesn't exist.
474+
pub fn shared_resource<T: HasSchema>(&self) -> Ref<'_, T> {
475+
self.get_shared_resource()
476+
.expect("shared resource not found")
477+
}
478+
479+
#[track_caller]
480+
/// Get the mutable shared resource of a given type out of this [`Game`]s
481+
/// shared resources. Panics if it doesn't exist.
482+
pub fn shared_resource_mut<T: HasSchema>(&self) -> RefMut<'_, T> {
483+
self.get_shared_resource_mut()
484+
.expect("shared resource not found")
485+
}
486+
469487
/// Get the shared resource cell of a given type out of this [`Game`]s shared resources.
470488
pub fn shared_resource_cell<T: HasSchema>(&self) -> Option<AtomicResource<T>> {
471489
let res = self
@@ -485,7 +503,7 @@ impl Game {
485503
{
486504
self.insert_shared_resource(T::default());
487505
}
488-
self.shared_resource_mut::<T>().unwrap()
506+
self.shared_resource_mut::<T>()
489507
}
490508

491509
/// Insert a resource that will be shared across all game sessions.

0 commit comments

Comments
 (0)