-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.rs
738 lines (664 loc) · 28 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Provides functionality that allows an Amethyst game to communicate with an editor.
//!
//! [`SyncEditorSystem`] is the root system that will send your game's state data to an editor.
//! In order to visualize your game's state in an editor, you'll also need to create a
//! [`ReadComponentSystem`] or [`ReadResourceSystem`] for each component and resource that you want
//! to visualize. It is possible to automatically create these systems by creating a
//! [`SyncEditorBundle`] and registering each component and resource on it instead.
//!
//! # Example
//!
//! ```
//! extern crate amethyst;
//! extern crate amethyst_editor_sync;
//! #[macro_use]
//! extern crate serde;
//!
//! use amethyst::core::Transform;
//! use amethyst::ecs::*;
//! use amethyst::prelude::*;
//! use amethyst_editor_sync::*;
//!
//! # fn main() -> Result<(), amethyst::Error> {
//! // Specify every component that you want to view in the editor.
//! let components = type_set![MyComponent];
//!
//! // Do the same for your resources.
//! let resources = type_set![MyResource];
//!
//! // Read-only resources (i.e. that don't implement `DeserializeOwned`) can still
//! // be displayed in the editor, but they can't be edited.
//! let read_only_resources = type_set![ReadOnlyResource];
//!
//! // Create a SyncEditorBundle which will create all necessary systems to send the components
//! // to the editor.
//! let editor_sync_bundle = SyncEditorBundle::new()
//! // Register the default types from the engine.
//! .sync_default_types()
//! // Register the components and resources specified above.
//! .sync_components(&components)
//! .sync_resources(&resources)
//! .read_resources(&read_only_resources);
//!
//! let game_data = GameDataBuilder::default()
//! .with_bundle(editor_sync_bundle)?;
//! # Ok(())
//! # }
//!
//! // Make sure you enable serialization for your custom components and resources!
//! #[derive(Serialize, Deserialize)]
//! struct MyComponent {
//! foo: usize,
//! bar: String,
//! }
//!
//! impl Component for MyComponent {
//! type Storage = DenseVecStorage<Self>;
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct MyResource {
//! baz: usize,
//! }
//!
//! #[derive(Serialize)]
//! struct ReadOnlyResource {
//! important_entity: SerializableEntity,
//! }
//! ```
//!
//! # Usage
//! First, specify the components and resources that you want to see in the editor using the
//! [`type_set!`] macro.
//! Then create a [`SyncEditorBundle`] object and register the specified components and resources
//! with `sync_components` and `sync_resources` respectively. Some of the engine-specific types can
//! be registered automatically using the `sync_default_types` method. It is also possible to
//! specify the types individually using `sync_component` and `sync_resource`, which allows changing
//! the name of the type when viewed in the editor.
//!
//! Finally, add the [`SyncEditorBundle`] that you created to the game data.
extern crate amethyst;
extern crate crossbeam_channel;
#[macro_use]
extern crate log;
#[macro_use]
extern crate log_once;
#[macro_use]
extern crate serde;
extern crate serde_json;
use ::serializable_entity::DeserializableEntity;
use std::cmp::min;
use std::fmt::Write;
use std::collections::HashMap;
use std::io;
use std::str;
use std::time::*;
use amethyst::core::bundle::{Result as BundleResult, SystemBundle};
use amethyst::ecs::*;
use amethyst::shred::Resource;
use crossbeam_channel::{Receiver, Sender};
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::net::UdpSocket;
pub use editor_log::EditorLogger;
pub use ::serializable_entity::SerializableEntity;
pub use type_set::{ReadComponentSet, ReadResourceSet, TypeSet, WriteComponentSet, WriteResourceSet};
#[macro_use]
mod type_set;
mod editor_log;
mod serializable_entity;
mod systems;
const MAX_PACKET_SIZE: usize = 32 * 1024;
type SenderMap<T> = HashMap<&'static str, Sender<T>>;
type ComponentDeserializerMap = SenderMap<IncomingComponent>;
type ResourceDeserializerMap = SenderMap<serde_json::Value>;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Message<T> {
#[serde(rename = "type")]
ty: &'static str,
data: T,
}
#[derive(Debug, Clone, Default, Serialize)]
struct SerializedComponent<'a, T: 'a> {
name: &'static str,
data: HashMap<u32, &'a T>,
}
#[derive(Debug, Clone, Serialize)]
struct SerializedResource<'a, T: 'a> {
name: &'static str,
data: &'a T,
}
enum SerializedData {
Resource(String),
Component(String),
Message(String),
}
/// Messages sent from the editor to the game.
#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type")]
pub enum IncomingMessage {
ComponentUpdate {
id: String,
entity: DeserializableEntity,
data: serde_json::Value,
},
ResourceUpdate {
id: String,
data: serde_json::Value,
},
}
#[derive(Debug, Clone)]
pub struct IncomingComponent {
entity: Entity,
data: serde_json::Value,
}
/// A connection to an editor which allows sending messages via a [`SyncEditorSystem`].
///
/// Anything that needs to be able to send messages to the editor needs such a connection.
#[derive(Clone)]
pub struct EditorConnection {
sender: Sender<SerializedData>,
}
impl EditorConnection {
/// Construct a connection to the editor via sending messages to the [`SyncEditorSystem`].
fn new(sender: Sender<SerializedData>) -> Self {
Self { sender }
}
/// Send serialized data to the editor.
fn send_data(&self, data: SerializedData) {
self.sender.send(data);
}
/// Send an arbitrary message to the editor.
///
/// Note that the message types supported by the editor may differ between implementations.
pub fn send_message<T: Serialize>(&self, message_type: &'static str, data: T) {
let serialize_data = Message {
ty: message_type,
data,
};
if let Ok(serialized) = serde_json::to_string(&serialize_data) {
self.send_data(SerializedData::Message(serialized));
} else {
error!("Failed to serialize message");
}
}
}
/// Bundles all necessary systems for serializing all registered components and resources and
/// sending them to the editor.
pub struct SyncEditorBundle<T, U, V, W> where
T: ReadComponentSet,
U: ReadComponentSet + WriteComponentSet,
V: ReadResourceSet,
W: ReadResourceSet + WriteResourceSet,
{
send_interval: Duration,
read_components: TypeSet<T>,
write_components: TypeSet<U>,
read_resources: TypeSet<V>,
write_resources: TypeSet<W>,
sender: Sender<SerializedData>,
receiver: Receiver<SerializedData>,
}
impl SyncEditorBundle<(), (), (), ()> {
/// Construct an empty bundle.
pub fn new() -> Self {
let (sender, receiver) = crossbeam_channel::unbounded();
SyncEditorBundle {
send_interval: Duration::from_millis(200),
read_components: TypeSet::new(),
write_components: TypeSet::new(),
read_resources: TypeSet::new(),
write_resources: TypeSet::new(),
sender,
receiver,
}
}
}
impl Default for SyncEditorBundle<(), (), (), ()> {
fn default() -> Self { Self::new() }
}
impl<T, U, V, W> SyncEditorBundle<T, U, V, W> where
T: ReadComponentSet,
U: ReadComponentSet + WriteComponentSet,
V: ReadResourceSet,
W: ReadResourceSet + WriteResourceSet,
{
/// Synchronize amethyst types.
///
/// Currently only a small set is supported. This will be expanded in the future.
pub fn sync_default_types(
self
) -> SyncEditorBundle<
impl ReadComponentSet,
impl ReadComponentSet + WriteComponentSet,
impl ReadResourceSet,
impl ReadResourceSet + WriteResourceSet,
> {
use amethyst::renderer::{AmbientColor, Camera, Light};
use amethyst::core::{GlobalTransform, Transform};
let read_components = type_set![];
let write_components = type_set![Light, Camera, Transform, GlobalTransform];
let read_resources = type_set![];
let write_resources = type_set![AmbientColor];
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components.with_set(&read_components),
write_components: self.write_components.with_set(&write_components),
read_resources: self.read_resources.with_set(&read_resources),
write_resources: self.write_resources.with_set(&write_resources),
sender: self.sender,
receiver: self.receiver,
}
}
/// Register a component for synchronizing with the editor. This will result in a
/// [`ReadComponentSystem`] being added.
pub fn sync_component<C>(self, name: &'static str) -> SyncEditorBundle<T, impl ReadComponentSet + WriteComponentSet, V, W>
where
C: Component + Serialize + DeserializeOwned + Send + Sync,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components.with::<C>(name),
read_resources: self.read_resources,
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
/// Register a set of components for synchronizing with the editor. This will result
/// in a [`ReadComponentSystem`] being added for each component type in the set.
pub fn sync_components<C>(self, set: &TypeSet<C>) -> SyncEditorBundle<T, impl ReadComponentSet + WriteComponentSet, V, W>
where
C: ReadComponentSet + WriteComponentSet,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components.with_set(set),
read_resources: self.read_resources,
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
pub fn read_component<C>(self, name: &'static str) -> SyncEditorBundle<impl ReadComponentSet, U, V, W>
where
C: Component + Serialize + Send + Sync,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components.with::<C>(name),
write_components: self.write_components,
read_resources: self.read_resources,
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
pub fn read_components<C>(self, set: &TypeSet<C>) -> SyncEditorBundle<impl ReadComponentSet, U, V, W>
where
C: ReadComponentSet + WriteComponentSet
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components.with_set(set),
write_components: self.write_components,
read_resources: self.read_resources,
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
/// Registers a resource type to be synchronized with the editor.
///
/// At runtime, the state data for `R` will be sent to the editor for viewing and debugging.
/// The editor will also be able to send back changes to the resource's data, which will
/// automatically be applied to the local world state.
///
/// It is safe to register a resource type for the editor even if it's not also going to be
/// registered in the world. A warning will be emitted at runtime notifing that the resource
/// won't appear in the editor, however it will not otherwise be treated as an error.
pub fn sync_resource<R>(self, name: &'static str) -> SyncEditorBundle<T, U, V, impl ReadResourceSet + WriteResourceSet>
where
R: Resource + Serialize + DeserializeOwned,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components,
read_resources: self.read_resources,
write_resources: self.write_resources.with::<R>(name),
sender: self.sender,
receiver: self.receiver,
}
}
/// Registers a set of resource types to be synchronized with the editor.
///
/// At runtime, the state data for the resources in `R` will be sent to the editor for
/// viewing and debugging. The editor will also be able to send back changes to the
/// resource's data, which will automatically be applied to the local world state.
pub fn sync_resources<R>(self, set: &TypeSet<R>) -> SyncEditorBundle<T, U, V, impl ReadResourceSet + WriteResourceSet>
where
R: ReadResourceSet + WriteResourceSet,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components,
read_resources: self.read_resources,
write_resources: self.write_resources.with_set(set),
sender: self.sender,
receiver: self.receiver,
}
}
/// Registers a resource to be sent to the editor as read-only data.
///
/// At runtime, the state data for `R` will be sent to the editor for viewing, however
/// the editor will not be able to send back changes. If you would like to be able to
/// edit `R` in the editor, you can [implement `DeserializeOwned`] for it and then use
/// [`sync_resource`] to register it as read-write data.
///
/// [implement `DeserializeOwned`]: https://serde.rs/derive.html
/// [`sync_resource`]: #method.sync_resource
pub fn read_resource<R>(self, name: &'static str) -> SyncEditorBundle<T, U, impl ReadResourceSet, W>
where
R: Resource + Serialize,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components,
read_resources: self.read_resources.with::<R>(name),
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
/// Registers a set of resources to be sent to the editor as read-only data.
///
/// At runtime, the state data for the resources in `R` will be sent to the editor
/// for viewing, however the editor will not be able to send back changes. If you
/// would like to be able to edit any of the resources in `R` in the editor, you
/// can [implement `DeserializeOwned`] for them and then use [`sync_resources`] to
/// register them as read-write data.
///
/// [implement `DeserializeOwned`]: https://serde.rs/derive.html
/// [`sync_resources`]: #method.sync_resources
pub fn read_resources<R>(self, set: &TypeSet<R>) -> SyncEditorBundle<T, U, impl ReadResourceSet, W>
where
R: ReadResourceSet,
{
SyncEditorBundle {
send_interval: self.send_interval,
read_components: self.read_components,
write_components: self.write_components,
read_resources: self.read_resources.with_set(set),
write_resources: self.write_resources,
sender: self.sender,
receiver: self.receiver,
}
}
/// Sets the interval at which the current game state will be sent to the editor.
///
/// In order to reduce the amount of work the editor has to do to keep track of the latest
/// game state, the rate at which the game state is sent can be reduced. This defaults to
/// sending updated data every 200 ms. Setting this to 0 will ensure that data is sent every
/// frame.
///
/// Note that log output is sent every frame regardless of this interval, the interval only
/// controls how often the game's state is sent.
pub fn send_interval(mut self, send_interval: Duration) -> SyncEditorBundle<T, U, V, W> {
self.send_interval = send_interval;
self
}
/// Retrieve a connection to send messages to the editor via the [`SyncEditorSystem`].
pub fn get_connection(&self) -> EditorConnection {
EditorConnection::new(self.sender.clone())
}
}
impl<'a, 'b, T, U, V, W> SystemBundle<'a, 'b> for SyncEditorBundle<T, U, V, W> where
T: ReadComponentSet,
U: ReadComponentSet + WriteComponentSet,
V: ReadResourceSet,
W: ReadResourceSet + WriteResourceSet,
{
fn build(self, dispatcher: &mut DispatcherBuilder<'a, 'b>) -> BundleResult<()> {
let mut sync_system = SyncEditorSystem::from_channel(
self.sender,
self.receiver,
self.send_interval,
);
let connection = sync_system.get_connection();
// All systems must have finished editing data before syncing can start.
dispatcher.add_barrier();
self.read_components.create_component_read_systems(dispatcher, &connection);
self.write_components.create_component_read_systems(dispatcher, &connection);
self.read_resources.create_resource_read_systems(dispatcher, &connection);
self.write_resources.create_resource_read_systems(dispatcher, &connection);
// All systems must have finished serializing before it can be send to the editor.
dispatcher.add_barrier();
self.write_components.create_component_write_systems(
dispatcher,
&mut sync_system.component_deserializer_map,
);
self.write_resources.create_resource_write_systems(
dispatcher,
&mut sync_system.deserializer_map,
);
dispatcher.add(sync_system, "sync_editor_system", &[]);
Ok(())
}
}
/// A system that is in charge of coordinating a number of serialization systems and sending
/// their results to the editor.
pub struct SyncEditorSystem {
receiver: Receiver<SerializedData>,
sender: Sender<SerializedData>,
socket: UdpSocket,
// Map containing channels used to send incoming serialized component/resource data from the
// editor. Incoming data is sent to specialized systems that deserialize the data and update
// the corresponding local data.
deserializer_map: ResourceDeserializerMap,
component_deserializer_map: ComponentDeserializerMap,
send_interval: Duration,
next_send: Instant,
scratch_string: String,
incoming_buffer: Vec<u8>,
}
impl SyncEditorSystem {
pub fn new(send_interval: Duration) -> Self {
let (sender, receiver) = crossbeam_channel::unbounded();
Self::from_channel(sender, receiver, send_interval)
}
fn from_channel(sender: Sender<SerializedData>, receiver: Receiver<SerializedData>, send_interval: Duration) -> Self {
// Create the socket used for communicating with the editor.
//
// NOTE: We set the socket to nonblocking so that we don't block if there are no incoming
// messages to read. We `expect` on the call to `set_nonblocking` because the game will
// hang if the socket is still set to block when the game runs.
let socket = UdpSocket::bind("0.0.0.0:0").expect("Failed to bind socket");
socket.set_nonblocking(true).expect("Failed to make editor socket nonblocking");
let scratch_string = String::with_capacity(MAX_PACKET_SIZE);
Self {
receiver,
sender,
socket,
deserializer_map: HashMap::new(),
component_deserializer_map: ComponentDeserializerMap::new(),
send_interval,
next_send: Instant::now() + send_interval,
scratch_string,
incoming_buffer: Vec::with_capacity(1024),
}
}
/// Retrieve a connection to the editor via this system.
pub fn get_connection(&self) -> EditorConnection {
EditorConnection::new(self.sender.clone())
}
}
impl<'a> System<'a> for SyncEditorSystem {
type SystemData = Entities<'a>;
fn run(&mut self, entities: Self::SystemData) {
// Determine if we should send full state data this frame.
let now = Instant::now();
let send_this_frame = now >= self.next_send;
// Calculate when we should next send full state data.
//
// NOTE: We do `next_send += send_interval` instead of `next_send = now + send_interval`
// to ensure that state updates happen at a consistent cadence even if there are slight
// timing variations in when individual frames are sent.
//
// NOTE: We repeatedly add `send_interval` to `next_send` to ensure that the next send
// time is after `now`. This is to avoid running into a death spiral if a frame spike
// causes frame time to be so long that the next send time would still be in the past.
while self.next_send < now {
self.next_send += self.send_interval;
}
let mut components = Vec::new();
let mut resources = Vec::new();
let mut messages = Vec::new();
while let Some(serialized) = self.receiver.try_recv() {
match serialized {
SerializedData::Component(c) => components.push(c),
SerializedData::Resource(r) => resources.push(r),
SerializedData::Message(m) => messages.push(m),
}
}
let mut entity_data = Vec::<SerializableEntity>::new();
for (entity,) in (&*entities,).join() {
entity_data.push(entity.into());
}
let entity_string = serde_json::to_string(&entity_data)
.expect("Failed to serialize entities");
// Create the message and serialize it to JSON. If we don't need to send the full state
// data this frame, we discard entities, components, and resources, and only send the
// messages (e.g. log output) from the current frame.
if send_this_frame {
write!(
self.scratch_string,
r#"{{
"type": "message",
"data": {{
"entities": {},
"components": [{}],
"resources": [{}],
"messages": [{}]
}}
}}"#,
entity_string,
// Insert a comma between components so that it's valid JSON.
components.join(","),
resources.join(","),
messages.join(","),
);
} else {
write!(
self.scratch_string,
r#"{{
"type": "message",
"data": {{
"messages": [{}]
}}
}}"#,
// Insert a comma between components so that it's valid JSON.
messages.join(","),
);
}
// NOTE: We need to append a page feed character after each message since that's
// what node-ipc expects to delimit messages.
self.scratch_string.push_str("\u{C}");
// Send the message, breaking it up into multiple packets if the message is too large.
let editor_address = ([127, 0, 0, 1], 8000).into();
let mut bytes_sent = 0;
while bytes_sent < self.scratch_string.len() {
let bytes_to_send = min(self.scratch_string.len() - bytes_sent, MAX_PACKET_SIZE);
let end_offset = bytes_sent + bytes_to_send;
// Send the JSON message.
let bytes = self.scratch_string[bytes_sent..end_offset].as_bytes();
self.socket.send_to(bytes, editor_address).expect("Failed to send message");
bytes_sent += bytes_to_send;
}
self.scratch_string.clear();
// Read any incoming messages from the editor process.
let mut buf = [0; 1024];
loop {
// TODO: Verify that the incoming address matches the editor process address.
let (bytes_read, addr) = match self.socket.recv_from(&mut buf[..]) {
Ok(res) => res,
Err(error) => {
match error.kind() {
// If the read would block, it means that there was no incoming data and we
// should break from the loop.
io::ErrorKind::WouldBlock => break,
// This is an "error" that happens on Windows if no editor is running to
// receive the state update we just sent. The OS gives a "connection was
// forcibly closed" error when no socket receives the message, but we
// don't care if that happens (in fact, we use UDP specifically so that
// we can broadcast messages without worrying about establishing a
// connection).
io::ErrorKind::ConnectionReset => continue,
// All other error kinds should be indicative of a genuine error. For our
// purposes we still want to ignore them, but we'll at least log a warning
// in case it helps debug an issue.
_ => {
warn!("Error reading incoming: {:?}", error);
continue;
}
}
}
};
if addr != editor_address {
trace!("Packet received from unknown address {:?}", addr);
continue;
}
debug!("Packet: {:?}", &buf[..bytes_read]);
// Add the bytes from the incoming packet to the buffer.
self.incoming_buffer.extend_from_slice(&buf[..bytes_read]);
}
// Check the incoming buffer to see if any completed messages have been received.
while let Some(index) = self.incoming_buffer.iter().position(|&byte| byte == 0xC) {
// HACK: Manually introduce a scope here so that the compiler can tell when we're done
// using borrowing the message bytes from `self.incoming_buffer`. This can be removed
// once NLL is stable.
{
let message_bytes = &self.incoming_buffer[..index];
let result = str::from_utf8(message_bytes)
.ok()
.and_then(|message| serde_json::from_str(message).ok());
debug!("Message str: {:?}", result);
if let Some(message) = result {
debug!("Message: {:#?}", message);
match message {
IncomingMessage::ComponentUpdate { id, entity: entity_data, data } => {
let entity = entities.entity(entity_data.id);
// Skip the update if the entity is no longer valid.
if entity.gen().id() != entity_data.generation {
debug!("Entity {:?} had invalid generation {} (expected {})", entity_data, entity_data.generation, entity.gen().id());
continue;
}
if let Some(sender) = self.component_deserializer_map.get(&*id) {
sender.send(IncomingComponent {
entity,
data,
});
} else {
debug!("No deserializer found for component {:?}", id);
}
},
IncomingMessage::ResourceUpdate { id, data } => {
// TODO: Should we do something if there was no deserialer system for the
// specified ID?
if let Some(sender) = self.deserializer_map.get(&*id) {
// TODO: Should we do something to prevent this from blocking?
sender.send(data);
}
}
}
}
}
// Remove the message bytes from the beginning of the incoming buffer.
self.incoming_buffer.drain(..=index);
}
}
}