
# Objective Resolves #3824. `unsafe` code should be the exception, not the norm in Rust. It's obviously needed for various use cases as it's interfacing with platforms and essentially running the borrow checker at runtime in the ECS, but the touted benefits of Bevy is that we are able to heavily leverage Rust's safety, and we should be holding ourselves accountable to that by minimizing our unsafe footprint. ## Solution Deny `unsafe_code` workspace wide. Add explicit exceptions for the following crates, and forbid it in almost all of the others. * bevy_ecs - Obvious given how much unsafe is needed to achieve performant results * bevy_ptr - Works with raw pointers, even more low level than bevy_ecs. * bevy_render - due to needing to integrate with wgpu * bevy_window - due to needing to integrate with raw_window_handle * bevy_utils - Several unsafe utilities used by bevy_ecs. Ideally moved into bevy_ecs instead of made publicly usable. * bevy_reflect - Required for the unsafe type casting it's doing. * bevy_transform - for the parallel transform propagation * bevy_gizmos - For the SystemParam impls it has. * bevy_assets - To support reflection. Might not be required, not 100% sure yet. * bevy_mikktspace - due to being a conversion from a C library. Pending safe rewrite. * bevy_dynamic_plugin - Inherently unsafe due to the dynamic loading nature. Several uses of unsafe were rewritten, as they did not need to be using them: * bevy_text - a case of `Option::unchecked` could be rewritten as a normal for loop and match instead of an iterator. * bevy_color - the Pod/Zeroable implementations were replaceable with bytemuck's derive macros.
58 lines
1.9 KiB
Rust
58 lines
1.9 KiB
Rust
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
#![forbid(unsafe_code)]
|
|
#![doc(
|
|
html_logo_url = "https://bevyengine.org/assets/icon.png",
|
|
html_favicon_url = "https://bevyengine.org/assets/icon.png"
|
|
)]
|
|
|
|
//! Systems and type definitions for gamepad handling in Bevy.
|
|
//!
|
|
//! This crate is built on top of [GilRs](gilrs), a library
|
|
//! that handles abstracting over platform-specific gamepad APIs.
|
|
|
|
mod converter;
|
|
mod gilrs_system;
|
|
mod rumble;
|
|
|
|
use bevy_app::{App, Plugin, PostUpdate, PreStartup, PreUpdate};
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_input::InputSystem;
|
|
use bevy_utils::{synccell::SyncCell, tracing::error};
|
|
use gilrs::GilrsBuilder;
|
|
use gilrs_system::{gilrs_event_startup_system, gilrs_event_system};
|
|
use rumble::{play_gilrs_rumble, RunningRumbleEffects};
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), derive(Resource))]
|
|
pub(crate) struct Gilrs(pub SyncCell<gilrs::Gilrs>);
|
|
|
|
/// Plugin that provides gamepad handling to an [`App`].
|
|
#[derive(Default)]
|
|
pub struct GilrsPlugin;
|
|
|
|
/// Updates the running gamepad rumble effects.
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
|
|
pub struct RumbleSystem;
|
|
|
|
impl Plugin for GilrsPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
match GilrsBuilder::new()
|
|
.with_default_filters(false)
|
|
.set_update_state(false)
|
|
.build()
|
|
{
|
|
Ok(gilrs) => {
|
|
#[cfg(target_arch = "wasm32")]
|
|
app.insert_non_send_resource(Gilrs(SyncCell::new(gilrs)));
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
app.insert_resource(Gilrs(SyncCell::new(gilrs)));
|
|
|
|
app.init_resource::<RunningRumbleEffects>()
|
|
.add_systems(PreStartup, gilrs_event_startup_system)
|
|
.add_systems(PreUpdate, gilrs_event_system.before(InputSystem))
|
|
.add_systems(PostUpdate, play_gilrs_rumble.in_set(RumbleSystem));
|
|
}
|
|
Err(err) => error!("Failed to start Gilrs. {}", err),
|
|
}
|
|
}
|
|
}
|