Skip to content

Commit 31760ea

Browse files
authored
feat: pre-register reflected components with the world at finalize (#314)
1 parent d0589ef commit 31760ea

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

.github/workflows/bevy_mod_scripting.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ jobs:
5858
steps:
5959
- name: Checkout
6060
uses: actions/checkout@v4
61+
- name: Rust Cache
62+
uses: Swatinem/rust-cache@v2.7.7
63+
with:
64+
# reasoning: we want to cache xtask, most of the jobs in the matrix will be sped up a good bit thanks to that
65+
save-if: ${{ github.ref == 'refs/heads/main' }}
66+
cache-all-crates: true
6167
- name: Generate matrix
6268
id: generate-matrix
6369
run: |
@@ -90,10 +96,13 @@ jobs:
9096
with:
9197
toolchain: stable
9298
override: true
93-
9499
- name: Rust Cache
95100
if: ${{ needs.check-needs-run.outputs.any-changes == 'true' }}
96-
uses: Swatinem/rust-cache@v2.7.3
101+
uses: Swatinem/rust-cache@v2.7.7
102+
with:
103+
# reasoning: we want to cache xtask, most of the jobs in the matrix will be sped up a good bit thanks to that
104+
save-if: ${{ github.ref == 'refs/heads/main' }}
105+
cache-all-crates: true
97106

98107
- name: Setup
99108
if: ${{ needs.check-needs-run.outputs.any-changes == 'true' }}
@@ -143,6 +152,13 @@ jobs:
143152
uses: actions/checkout@v4
144153
with:
145154
ref: ${{ github.head_ref || github.ref_name }}
155+
- name: Rust Cache
156+
if: ${{ needs.check-needs-run.outputs.any-changes == 'true' }}
157+
uses: Swatinem/rust-cache@v2.7.7
158+
with:
159+
# reasoning: we want to cache xtask, most of the jobs in the matrix will be sped up a good bit thanks to that
160+
save-if: ${{ github.ref == 'refs/heads/main' }}
161+
cache-all-crates: true
146162
- name: Setup Bot GitHub Credentials
147163
run: |
148164
git config user.name "github-actions[bot]"

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ fn once_per_app_finalize(app: &mut App) {
239239
extensions: asset_settings_extensions,
240240
preprocessor: None,
241241
});
242+
243+
// pre-register component id's
244+
pre_register_componnents(app);
245+
}
246+
247+
/// Ensures all types with `ReflectComponent` type data are pre-registered with component ID's
248+
fn pre_register_componnents(app: &mut App) {
249+
let type_registry = app
250+
.world_mut()
251+
.get_resource_or_init::<AppTypeRegistry>()
252+
.clone();
253+
let type_registry = type_registry.read();
254+
255+
let world = app.world_mut();
256+
for (_, data) in type_registry.iter_with_data::<ReflectComponent>() {
257+
data.register_component(world);
258+
}
242259
}
243260

244261
// One of registration of things that need to be done only once per app
@@ -398,4 +415,23 @@ mod test {
398415
.await
399416
.expect("Rhai loader not found");
400417
}
418+
419+
#[test]
420+
fn test_reflect_component_is_preregistered_in_app_finalize() {
421+
let mut app = App::new();
422+
423+
app.add_plugins(AssetPlugin::default());
424+
425+
#[derive(Component, Reflect)]
426+
#[reflect(Component)]
427+
struct Comp;
428+
429+
app.register_type::<Comp>();
430+
431+
assert!(app.world_mut().component_id::<Comp>().is_none());
432+
433+
once_per_app_finalize(&mut app);
434+
435+
assert!(app.world_mut().component_id::<Comp>().is_some());
436+
}
401437
}

crates/xtask/src/main.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ impl App {
284284
cmd.arg("--coverage");
285285
}
286286

287+
if let Some(jobs) = self.global_args.jobs {
288+
cmd.arg("--jobs").arg(jobs.to_string());
289+
}
290+
287291
match self.subcmd {
288292
Xtasks::Macros { macro_name } => {
289293
cmd.arg("macros").arg(macro_name.as_ref());
@@ -414,9 +418,24 @@ struct GlobalArgs {
414418
help = "The cargo profile to use for commands that support it"
415419
)]
416420
profile: Option<String>,
421+
422+
#[clap(
423+
long,
424+
global = true,
425+
value_name = "JOBS",
426+
help = "The number of parallel jobs to run at most"
427+
)]
428+
jobs: Option<usize>,
417429
}
418430

419431
impl GlobalArgs {
432+
pub fn with_max_jobs(self, jobs: usize) -> Self {
433+
Self {
434+
jobs: Some(jobs),
435+
..self
436+
}
437+
}
438+
420439
pub fn with_coverage(self) -> Self {
421440
Self {
422441
coverage: true,
@@ -809,6 +828,11 @@ impl Xtasks {
809828
args.push("--profile".to_owned());
810829
args.push(use_profile.to_owned());
811830
}
831+
832+
if let Some(jobs) = app_settings.jobs {
833+
args.push("--jobs".to_owned());
834+
args.push(jobs.to_string());
835+
}
812836
}
813837

814838
args.extend(app_settings.features.to_cargo_args());
@@ -1344,7 +1368,11 @@ impl Xtasks {
13441368

13451369
// and finally run tests with coverage
13461370
output.push(App {
1347-
global_args: default_args.clone().with_coverage(),
1371+
global_args: default_args
1372+
.clone()
1373+
.with_coverage()
1374+
// github actions has been throwing a lot of OOM SIGTERM's lately
1375+
.with_max_jobs(4),
13481376
subcmd: Xtasks::Test {
13491377
name: None,
13501378
package: None,

0 commit comments

Comments
 (0)