From 07b5fbce1ad3198cb95b82a2be180e8ad4becf01 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 25 Aug 2024 20:45:29 -0700 Subject: [PATCH] Adding duplicate entity checks as well --- assets/scenes/00.scene | 1 + assets/scenes/00/a.entity | 1 - src/save.rs | 44 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/assets/scenes/00.scene b/assets/scenes/00.scene index 37e8e3d..95eb7ce 100644 --- a/assets/scenes/00.scene +++ b/assets/scenes/00.scene @@ -1 +1,2 @@ 00/a.entity +00/a.entity diff --git a/assets/scenes/00/a.entity b/assets/scenes/00/a.entity index 80a0fec..20cc19d 100644 --- a/assets/scenes/00/a.entity +++ b/assets/scenes/00/a.entity @@ -1,2 +1 @@ name "Hello world" -name "Hello bevy" diff --git a/src/save.rs b/src/save.rs index 2da2144..423e7c2 100644 --- a/src/save.rs +++ b/src/save.rs @@ -100,7 +100,6 @@ impl SaveEntity { ); } }); - // Check for duplicate component types and emit an error // TODO: It would be nice if this emitted a line refernece instead of a parsed struct! let l = entity.components.len(); @@ -111,12 +110,20 @@ impl SaveEntity { let c2 = &entity.components[j]; let t1 = c1.type_id; let t2 = c2.type_id; - if t1 == t2 { + if cfg!(debug_assertions) { + debug_assert!( + t1 != t2, + "Duplicate components in {:?}: \n\t{:?}\n\t{:?}", + load_context.asset_path(), + c1.data, + c2.data + ); + } else { error!( "Duplicate components in {:?}: \n\t{:?}\n\t{:?}", load_context.asset_path(), c1.data, - c2.data, + c2.data ); } } @@ -198,6 +205,37 @@ impl AssetLoader for SaveSceneLoader { .map(|path| load_context.load(path)) .collect(); + // Assert there are no duplicate entities in the file + { + let l = entities.len(); + (0..l).for_each(|i| { + (i..l).for_each(|j| { + if i != j { + let e1 = &entities[i]; + let e2 = &entities[j]; + if cfg!(debug_assertions) { + debug_assert!( + e1 != e2, + "Duplicate entities in scene {:?}:\n\t{:?}\n\t{:?}", + load_context.asset_path(), + e1, + e2 + ); + } else { + if e1 == e2 { + error!( + "Duplicate entities in scene {:?}:\n\t{:?}\n\t{:?}", + load_context.asset_path(), + e1, + e2 + ) + } + } + } + }); + }); + } + Ok(SaveScene { entities }) }