Skip to content

Commit 4c9dfbf

Browse files
authored
Change Title's inner value and message type to Text (#518)
* Change Title's inner value to Text * Change title and message serialization to serde_json
1 parent 076903b commit 4c9dfbf

File tree

10 files changed

+39
-16
lines changed

10 files changed

+39
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

feather/plugin-host/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tempfile = "3"
2525
vec-arena = "1"
2626
wasmer = { version = "2", default-features = false, features = [ "jit" ] }
2727
wasmer-wasi = { version = "2", default-features = false, features = [ "host-fs", "sys" ] }
28+
serde_json = "1"
2829

2930
[features]
3031
llvm = [ "wasmer/llvm" ]

feather/plugin-host/src/context.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,20 @@ impl PluginContext {
328328
}
329329
}
330330

331+
/// Accesses a `json`-encoded value in the plugin's memory space.
332+
pub fn read_json<T: DeserializeOwned>(
333+
&self,
334+
ptr: PluginPtr<u8>,
335+
len: u32,
336+
) -> anyhow::Result<T> {
337+
// SAFETY: we do not return a reference to these
338+
// bytes.
339+
unsafe {
340+
let bytes = self.deref_bytes(ptr.cast(), len)?;
341+
serde_json::from_slice(bytes).map_err(From::from)
342+
}
343+
}
344+
331345
/// Deserializes a component value in the plugin's memory space.
332346
pub fn read_component<T: Component>(&self, ptr: PluginPtr<u8>, len: u32) -> anyhow::Result<T> {
333347
// SAFETY: we do not return a reference to these

feather/plugin-host/src/host_calls/entity.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ pub fn entity_send_message(
1717
message_ptr: PluginPtr<u8>,
1818
message_len: u32,
1919
) -> anyhow::Result<()> {
20-
let message = cx.read_string(message_ptr, message_len)?;
20+
let message = cx.read_json(message_ptr, message_len)?;
2121
let entity = Entity::from_bits(entity);
22-
let _ = cx.game_mut().send_message(
23-
entity,
24-
ChatMessage::new(ChatKind::System, Text::from(message)),
25-
);
22+
let _ = cx
23+
.game_mut()
24+
.send_message(entity, ChatMessage::new(ChatKind::System, message));
2625
Ok(())
2726
}
2827

@@ -33,7 +32,7 @@ pub fn entity_send_title(
3332
title_ptr: PluginPtr<u8>,
3433
title_len: u32,
3534
) -> anyhow::Result<()> {
36-
let title = cx.read_bincode(title_ptr, title_len)?;
35+
let title = cx.read_json(title_ptr, title_len)?;
3736
let entity = Entity::from_bits(entity);
3837
cx.game_mut().send_title(entity, title);
3938
Ok(())

feather/server/src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,15 @@ impl Client {
485485
self.send_packet(Title::Reset);
486486
} else {
487487
if let Some(main_title) = title.title {
488-
self.send_packet(Title::SetTitle { text: main_title });
488+
self.send_packet(Title::SetTitle {
489+
text: main_title.to_string(),
490+
});
489491
}
490492

491493
if let Some(sub_title) = title.sub_title {
492-
self.send_packet(Title::SetSubtitle { text: sub_title })
494+
self.send_packet(Title::SetSubtitle {
495+
text: sub_title.to_string(),
496+
})
493497
}
494498

495499
self.send_packet(Title::SetTimesAndDisplay {

libcraft/text/src/title.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
use crate::Text;
12
use serde::{Deserialize, Serialize};
23

34
// Based on https://wiki.vg/index.php?title=Protocol&oldid=16459#Title
45
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
56
pub struct Title {
6-
pub title: Option<String>,
7-
pub sub_title: Option<String>,
7+
pub title: Option<Text>,
8+
pub sub_title: Option<Text>,
89
pub fade_in: u32,
910
pub stay: u32,
1011
pub fade_out: u32,

quill/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ quill-common = { path = "../common" }
1717
thiserror = "1"
1818
uuid = "0.8"
1919
itertools = "0.10.0"
20+
serde_json = "1"
2021

quill/api/src/entity.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use libcraft_text::Text;
12
use std::{marker::PhantomData, ptr};
23

34
use quill_common::{Component, Pointer, PointerMut};
@@ -92,16 +93,16 @@ impl Entity {
9293
///
9394
/// The message sends as a "system" message.
9495
/// See [the wiki](https://wiki.vg/Chat) for more details.
95-
pub fn send_message(&self, message: impl AsRef<str>) {
96-
let message = message.as_ref();
96+
pub fn send_message(&self, message: impl Into<Text>) {
97+
let message = message.into().to_string();
9798
unsafe {
9899
quill_sys::entity_send_message(self.id.0, message.as_ptr().into(), message.len() as u32)
99100
}
100101
}
101102

102103
/// Sends the given title to this entity.
103104
pub fn send_title(&self, title: &libcraft_text::Title) {
104-
let title = bincode::serialize(title).expect("failed to serialize Title");
105+
let title = serde_json::to_string(title).expect("failed to serialize Title");
105106
unsafe {
106107
quill_sys::entity_send_title(self.id.0, title.as_ptr().into(), title.len() as u32);
107108
}

quill/example-plugins/titles/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ fn title_system(plugin: &mut TitleExample, game: &mut Game) {
2727

2828
// Create a title to send to the player
2929
let title = Title {
30-
title: Some(Text::from(title_component).to_string()),
31-
sub_title: Some(Text::from(component).to_string()),
30+
title: Some(Text::from(title_component)),
31+
sub_title: Some(Text::from(component)),
3232
fade_in: 5,
3333
stay: 400,
3434
fade_out: 5,

quill/sys/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extern "C" {
9797
/// The given `Title` should contain at least a `title` or a `sub_title`
9898
///
9999
/// Does nothing if the entity does not exist or if it does not have the `Chat` component.
100-
pub fn entity_send_title(entity: EntityId, title_ptr: Pointer<u8>, title_len: u32);
100+
pub fn entity_send_title(entity: EntityId, title_json_ptr: Pointer<u8>, title_len: u32);
101101

102102
/// Creates an empty entity builder.
103103
///

0 commit comments

Comments
 (0)