Skip to content

Commit

Permalink
fix: set_default_public_segment and set_public_segments are no longer…
Browse files Browse the repository at this point in the history
… panicking (#479)
  • Loading branch information
pajotg authored Jan 6, 2024
1 parent 3966607 commit f1b9c7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Unreleased
- Remove `Resolvable` trait, moving its functionality to `MaybeHasId`
- Remove `ObjectWithId` and `ObjectWithMaybeId` enums

### Bugfixes:

- Fix `raw_memory::set_public_segments` and `set_default_public_segment` argument conversion

0.19.0 (2023-12-20)
===================

Expand Down
18 changes: 14 additions & 4 deletions src/raw_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ extern "C" {
fn set_active_foreign_segment(username: &JsString, segment_id: Option<u8>);

#[wasm_bindgen(static_method_of = RawMemory, js_name = setDefaultPublicSegment)]
fn set_default_public_segment(segment_id: Option<u8>);
fn set_default_public_segment(segment_id: JsValue);

#[wasm_bindgen(static_method_of = RawMemory, js_name = setPublicSegments)]
fn set_public_segments(segment_ids: &[u8]);
fn set_public_segments(segment_ids: &Array);
}

/// Get a [`JsHashMap<u8, String>`] with all of the segments requested on
Expand Down Expand Up @@ -108,15 +108,25 @@ pub fn set_active_foreign_segment(username: &JsString, segment_id: Option<u8>) {
///
/// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.setDefaultPublicSegment)
pub fn set_default_public_segment(segment_id: Option<u8>) {
RawMemory::set_default_public_segment(segment_id)
RawMemory::set_default_public_segment(
segment_id
.map(|f| JsValue::from_f64(f as f64))
.unwrap_or(JsValue::NULL),
)
}

/// Sets which of your memory segments are readable to other players as
/// foreign segments, overriding previous settings.
///
/// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.setPublicSegments)
pub fn set_public_segments(segment_ids: &[u8]) {
RawMemory::set_public_segments(segment_ids)
let segment_ids: Array = segment_ids
.iter()
.map(|s| *s as f64)
.map(JsValue::from_f64)
.collect();

RawMemory::set_public_segments(&segment_ids)
}

#[wasm_bindgen]
Expand Down

0 comments on commit f1b9c7b

Please sign in to comment.