Skip to content

fix: prevent allocation and component ID ranges from overlapping #230

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

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
77 changes: 59 additions & 18 deletions crates/bevy_mod_scripting_core/src/bindings/access_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,35 @@ pub struct ReflectAccessId {

impl AccessMapKey for ReflectAccessId {
fn as_index(&self) -> u64 {
// project two linear non-negative ranges to a single linear non-negative range, offset by 1 to avoid 0
// y1 = 2x - 0 + 1
// y2 = 2x - 1 + 1
// project two linear non-negative ranges [0,inf] to a single linear non-negative range, offset by 1 to avoid 0
// y1 = 2x - 0 + 2 = 2x + 2
// y2 = 2x - 1 + 2 = 2x + 1
match self.kind {
ReflectAccessKind::ComponentOrResource => (self.id * 2) + 1,
ReflectAccessKind::ComponentOrResource => (self.id * 2) + 2,
ReflectAccessKind::Allocation => (self.id * 2) + 1,
ReflectAccessKind::Global => 0,
}
}

fn from_index(value: u64) -> Self {
// retrieve the kind of range based on if the value is odd or even
// y1 if even, y2 if odd
// to retrieve value of x:
// x1 = (y / 2) - 1
// x2 = ((y - 1) / 2) - 1

let (kind, id) = if value == 0 {
(ReflectAccessKind::Global, 0)
} else if value % 2 == 0 {
(ReflectAccessKind::ComponentOrResource, (value / 2) - 1)
} else {
(ReflectAccessKind::Allocation, ((value - 1) / 2) - 1)
};
Self { kind, id }
// reverse the projection
// x1 = (y1 - 2) / 2
// x2 = (y2 - 1) / 2

match value {
0 => ReflectAccessId {
kind: ReflectAccessKind::Global,
id: 0,
},
v if v % 2 == 0 => ReflectAccessId {
kind: ReflectAccessKind::ComponentOrResource,
id: (v - 2) / 2,
},
v => ReflectAccessId {
kind: ReflectAccessKind::Allocation,
id: (v - 1) / 2,
},
}
}
}

Expand Down Expand Up @@ -521,4 +525,41 @@ mod test {
.join()
.unwrap();
}

#[test]
fn test_as_and_from_index_for_access_id_non_overlapping() {
let global = ReflectAccessId::for_global();

let first_component = ReflectAccessId {
kind: ReflectAccessKind::ComponentOrResource,
id: 0,
};

let first_allocation = ReflectAccessId {
kind: ReflectAccessKind::Allocation,
id: 0,
};

let second_component = ReflectAccessId {
kind: ReflectAccessKind::ComponentOrResource,
id: 1,
};

let second_allocation = ReflectAccessId {
kind: ReflectAccessKind::Allocation,
id: 1,
};

assert_eq!(global.as_index(), 0);
assert_eq!(first_allocation.as_index(), 1);
assert_eq!(first_component.as_index(), 2);
assert_eq!(second_allocation.as_index(), 3);
assert_eq!(second_component.as_index(), 4);

assert_eq!(ReflectAccessId::from_index(0), global);
assert_eq!(ReflectAccessId::from_index(1), first_allocation);
assert_eq!(ReflectAccessId::from_index(2), first_component);
assert_eq!(ReflectAccessId::from_index(3), second_allocation);
assert_eq!(ReflectAccessId::from_index(4), second_component);
}
}
8 changes: 4 additions & 4 deletions release-plz.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ git_release_body = """
changelog_include = [
"bevy_mod_scripting_lua",
"bevy_mod_scripting_core",
# "bevy_mod_scripting_rhai",
"bevy_mod_scripting_rhai",
# "bevy_mod_scripting_rune",
"bevy_mod_scripting_functions",
]
Expand All @@ -53,9 +53,9 @@ version_group = "main"
name = "bevy_mod_scripting_core"
version_group = "main"

# [[package]]
# name = "bevy_mod_scripting_rhai"
# version_group = "main"
[[package]]
name = "bevy_mod_scripting_rhai"
version_group = "main"

# [[package]]
# name = "bevy_mod_scripting_rune"
Expand Down
Loading