This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
2,349 additions
and
901 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"rust-analyzer.cargo.features": "all", | ||
"todo-tree.filtering.excludeGlobs": [ | ||
"Cargo.lock", | ||
"**/target" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name="bevy_stardust_extras" | ||
version="0.1.0" | ||
edition="2021" | ||
authors=["Veritius <[email protected]>"] | ||
license="MIT OR Apache-2.0" | ||
description="Miscellaneous utilities for bevy_stardust" | ||
repository="https://github.com/veritius/bevy_stardust/" | ||
keywords=["bevy", "gamedev", "networking"] | ||
|
||
[dependencies.bevy] | ||
version = "0.14" | ||
default-features = false | ||
|
||
[dependencies.bevy_stardust] | ||
version = "0.6" | ||
path = "../stardust" | ||
|
||
[dependencies.octs] | ||
version = "0.4.0" | ||
optional = true | ||
|
||
[features] | ||
octs = ["dep:octs"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-APACHE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# bevy_stardust_extras | ||
Miscellaneous functionality that doesn't belong in `bevy_stardust`, but aren't significant enough to have its own crate. Includes various tools for testing and writing examples, as well as some tricks for encoding. | ||
|
||
| Bevy version | Stardust version | Crate version | | ||
|--------------|------------------|---------------| | ||
| `0.14.0` | `0.6.0` | `0.1.0` | | ||
|
||
## Feature flags | ||
- `octs` - Adds implementations for traits from the `octs` crate. | ||
|
||
## License | ||
bevy_stardust_extras is free and open source software. It's licensed under: | ||
* MIT License ([LICENSE-MIT](LICENSE-MIT) or [http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT)) | ||
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)) | ||
|
||
at your option. | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![doc = include_str!("../README.md")] | ||
#![warn(missing_docs)] | ||
|
||
pub mod link; | ||
pub mod numbers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//! A simple transport layer using inter-thread communications, intended for use in tests and examples. | ||
//! | ||
//! Usage is simple, just add [`LinkTransportPlugin`] to all involved apps. | ||
//! Then, use [`pair`] to create two [`Link`] components that communicate with eachother. | ||
//! These 'links' don't do any kind of handshake. Once added to an entity, they communicate immediately. | ||
use std::sync::{mpsc::{channel, Receiver, Sender, TryRecvError}, Mutex}; | ||
use bevy::prelude::*; | ||
use bevy_stardust::prelude::*; | ||
|
||
/// Adds a simple transport plugin for apps part of the same process. | ||
/// See the [top level documentation](self) for more information. | ||
pub struct LinkTransportPlugin; | ||
|
||
impl Plugin for LinkTransportPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(PreUpdate, (recv_link_data, remove_disconnected) | ||
.chain().in_set(NetworkRecv::Receive)); | ||
|
||
app.add_systems(PostUpdate, (send_link_data, remove_disconnected) | ||
.chain().in_set(NetworkSend::Transmit)); | ||
} | ||
} | ||
|
||
/// A connection to another `Link`, made with [`pair`]. | ||
/// | ||
/// A `Link` will only communicate with its counterpart. | ||
#[derive(Component)] | ||
pub struct Link(SideInner); | ||
|
||
/// Creates two connected [`Link`] objects. | ||
pub fn pair() -> (Link, Link) { | ||
let (left_tx, left_rx) = channel(); | ||
let (right_tx, right_rx) = channel(); | ||
|
||
let left = Link(SideInner { | ||
sender: left_tx, | ||
receiver: Mutex::new(right_rx), | ||
disconnected: false, | ||
}); | ||
|
||
let right = Link(SideInner { | ||
sender: right_tx, | ||
receiver: Mutex::new(left_rx), | ||
disconnected: false, | ||
}); | ||
|
||
return (left, right); | ||
} | ||
|
||
struct SideInner { | ||
sender: Sender<ChannelMessage>, | ||
// Makes the struct Sync, so it can be in a Component. | ||
// Use Exclusive when it's stabilised. | ||
receiver: Mutex<Receiver<ChannelMessage>>, | ||
disconnected: bool, | ||
} | ||
|
||
fn recv_link_data( | ||
mut query: Query<(&mut Link, &mut PeerMessages<Incoming>), With<Peer>>, | ||
) { | ||
query.par_iter_mut().for_each(|(mut link, mut queue)| { | ||
let receiver = link.0.receiver.get_mut().unwrap(); | ||
loop { | ||
match receiver.try_recv() { | ||
Ok(message) => { | ||
queue.push_one(message); | ||
}, | ||
|
||
Err(TryRecvError::Empty) => { break }, | ||
|
||
Err(TryRecvError::Disconnected) => { | ||
link.0.disconnected = true; | ||
break; | ||
}, | ||
} | ||
} | ||
}); | ||
} | ||
|
||
fn send_link_data( | ||
mut query: Query<(&mut Link, &PeerMessages<Outgoing>), With<Peer>>, | ||
) { | ||
query.par_iter_mut().for_each(|(mut link, queue)| { | ||
let sender = &link.0.sender; | ||
'outer: for (channel, queue) in queue { | ||
for payload in queue { | ||
match sender.send(ChannelMessage { channel, message: payload }) { | ||
Ok(_) => {}, | ||
Err(_) => { | ||
link.0.disconnected = true; | ||
break 'outer; | ||
}, | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
fn remove_disconnected( | ||
mut commands: Commands, | ||
mut query: Query<(Entity, &Link, Option<&mut PeerLifestage>)>, | ||
mut events: EventWriter<PeerDisconnectedEvent>, | ||
) { | ||
for (entity, link, stage) in query.iter_mut() { | ||
if link.0.disconnected { | ||
debug!("Link on entity {entity:?} disconnected"); | ||
commands.entity(entity).remove::<Link>(); | ||
|
||
events.send(PeerDisconnectedEvent { | ||
peer: entity, | ||
reason: DisconnectReason::Unspecified, | ||
comment: None, | ||
}); | ||
|
||
if let Some(mut stage) = stage { | ||
*stage = PeerLifestage::Closed; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Types for working with numbers, such as efficient encoding and easier logic. | ||
mod sequence; | ||
mod varint; | ||
|
||
pub use sequence::Sequence; | ||
pub use varint::VarInt; |
Oops, something went wrong.