mirror of
https://github.com/IntQuant/noita_entangled_worlds.git
synced 2025-10-19 07:03:16 +00:00
Move omni peer stuff into it's own module.
This commit is contained in:
parent
60d9494e72
commit
86f1ed7e24
6 changed files with 156 additions and 146 deletions
2
noita-proxy/Cargo.lock
generated
2
noita-proxy/Cargo.lock
generated
|
@ -1693,7 +1693,7 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
|
|||
|
||||
[[package]]
|
||||
name = "noita-proxy"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
dependencies = [
|
||||
"bitcode",
|
||||
"clipboard",
|
||||
|
|
|
@ -3,7 +3,7 @@ members = ["tangled"]
|
|||
|
||||
[package]
|
||||
name = "noita-proxy"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
@ -6,6 +6,7 @@ use bitcode::{Decode, Encode};
|
|||
use clipboard::{ClipboardContext, ClipboardProvider};
|
||||
use eframe::egui::{self, Align2, Color32};
|
||||
use mod_manager::{Modmanager, ModmanagerSettings};
|
||||
use net::omni::PeerVariant;
|
||||
use self_update::SelfUpdateManager;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use steamworks::{LobbyId, SteamAPIInitError};
|
||||
|
@ -103,7 +104,7 @@ impl App {
|
|||
fn start_server(&mut self) {
|
||||
let bind_addr = "0.0.0.0:5123".parse().unwrap();
|
||||
let peer = Peer::host(bind_addr, None).unwrap();
|
||||
let netman = net::NetManager::new(net::PeerVariant::Tangled(peer));
|
||||
let netman = net::NetManager::new(PeerVariant::Tangled(peer));
|
||||
self.set_netman_settings(&netman);
|
||||
netman.clone().start();
|
||||
self.state = AppState::Netman { netman };
|
||||
|
@ -119,7 +120,7 @@ impl App {
|
|||
}
|
||||
fn start_connect(&mut self, addr: SocketAddr) {
|
||||
let peer = Peer::connect(addr, None).unwrap();
|
||||
let netman = net::NetManager::new(net::PeerVariant::Tangled(peer));
|
||||
let netman = net::NetManager::new(PeerVariant::Tangled(peer));
|
||||
netman.clone().start();
|
||||
self.state = AppState::Netman { netman };
|
||||
}
|
||||
|
@ -129,7 +130,7 @@ impl App {
|
|||
steamworks::LobbyType::Private,
|
||||
self.steam_state.as_ref().unwrap().client.clone(),
|
||||
);
|
||||
let netman = net::NetManager::new(net::PeerVariant::Steam(peer));
|
||||
let netman = net::NetManager::new(PeerVariant::Steam(peer));
|
||||
self.set_netman_settings(&netman);
|
||||
netman.clone().start();
|
||||
self.state = AppState::Netman { netman };
|
||||
|
@ -146,7 +147,7 @@ impl App {
|
|||
id,
|
||||
self.steam_state.as_ref().unwrap().client.clone(),
|
||||
);
|
||||
let netman = net::NetManager::new(net::PeerVariant::Steam(peer));
|
||||
let netman = net::NetManager::new(PeerVariant::Steam(peer));
|
||||
netman.clone().start();
|
||||
self.state = AppState::Netman { netman };
|
||||
}
|
||||
|
|
|
@ -8,10 +8,9 @@ use std::{
|
|||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
use steamworks::{LobbyId, SteamId};
|
||||
use tracing::debug;
|
||||
|
||||
use tangled::{PeerId, PeerState, Reliability};
|
||||
use tangled::Reliability;
|
||||
use tracing::{error, info, warn};
|
||||
use tungstenite::{accept, WebSocket};
|
||||
|
||||
|
@ -26,7 +25,7 @@ pub(crate) fn ws_encode_proxy(key: &'static str, value: impl Display) -> tungste
|
|||
tungstenite::Message::Binary(buf)
|
||||
}
|
||||
|
||||
pub(crate) fn ws_encode_mod(peer: OmniPeerId, data: &[u8]) -> tungstenite::Message {
|
||||
pub(crate) fn ws_encode_mod(peer: omni::OmniPeerId, data: &[u8]) -> tungstenite::Message {
|
||||
let mut buf = Vec::new();
|
||||
buf.push(1u8);
|
||||
buf.extend_from_slice(&peer.0.to_le_bytes());
|
||||
|
@ -49,136 +48,10 @@ impl NetInnerState {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OmniPeerId(u64);
|
||||
|
||||
impl From<PeerId> for OmniPeerId {
|
||||
fn from(value: PeerId) -> Self {
|
||||
Self(value.0.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SteamId> for OmniPeerId {
|
||||
fn from(value: SteamId) -> Self {
|
||||
Self(value.raw())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OmniPeerId> for PeerId {
|
||||
fn from(value: OmniPeerId) -> Self {
|
||||
Self(
|
||||
value
|
||||
.0
|
||||
.try_into()
|
||||
.expect("Assuming PeerId was stored here, so conversion should succeed"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OmniPeerId> for SteamId {
|
||||
fn from(value: OmniPeerId) -> Self {
|
||||
Self::from_raw(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OmniPeerId {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum OmniNetworkEvent {
|
||||
PeerConnected(OmniPeerId),
|
||||
PeerDisconnected(OmniPeerId),
|
||||
Message { src: OmniPeerId, data: Vec<u8> },
|
||||
}
|
||||
|
||||
impl From<tangled::NetworkEvent> for OmniNetworkEvent {
|
||||
fn from(value: tangled::NetworkEvent) -> Self {
|
||||
match value {
|
||||
tangled::NetworkEvent::PeerConnected(id) => Self::PeerConnected(id.into()),
|
||||
tangled::NetworkEvent::PeerDisconnected(id) => Self::PeerDisconnected(id.into()),
|
||||
tangled::NetworkEvent::Message(msg) => Self::Message {
|
||||
src: msg.src.into(),
|
||||
data: msg.data,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum PeerVariant {
|
||||
Tangled(tangled::Peer),
|
||||
Steam(steam_networking::SteamPeer),
|
||||
}
|
||||
|
||||
impl PeerVariant {
|
||||
fn send(
|
||||
&self,
|
||||
peer: OmniPeerId,
|
||||
msg: Vec<u8>,
|
||||
reliability: Reliability,
|
||||
) -> Result<(), tangled::NetError> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.send(peer.into(), msg, reliability),
|
||||
PeerVariant::Steam(p) => {
|
||||
p.send_message(peer.into(), &msg, reliability);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn broadcast(&self, msg: Vec<u8>, reliability: Reliability) -> Result<(), tangled::NetError> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.broadcast(msg, reliability),
|
||||
PeerVariant::Steam(p) => Ok(p.broadcast_message(&msg, reliability)),
|
||||
}
|
||||
}
|
||||
|
||||
fn my_id(&self) -> Option<OmniPeerId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.my_id().map(OmniPeerId::from),
|
||||
PeerVariant::Steam(p) => Some(p.my_id().into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter_peer_ids(&self) -> Vec<OmniPeerId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.iter_peer_ids().map(OmniPeerId::from).collect(),
|
||||
PeerVariant::Steam(p) => p.get_peer_ids().into_iter().map(OmniPeerId::from).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn recv(&self) -> Vec<OmniNetworkEvent> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.recv().map(OmniNetworkEvent::from).collect(),
|
||||
PeerVariant::Steam(p) => p.recv(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state(&self) -> PeerState {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.state(),
|
||||
PeerVariant::Steam(p) => p.state(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host_id(&self) -> OmniPeerId {
|
||||
match self {
|
||||
PeerVariant::Tangled(_) => PeerId::HOST.into(),
|
||||
PeerVariant::Steam(p) => p.host_id().into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lobby_id(&self) -> Option<LobbyId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(_) => None,
|
||||
PeerVariant::Steam(p) => p.lobby_id(),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod omni;
|
||||
|
||||
pub struct NetManager {
|
||||
pub(crate) peer: PeerVariant,
|
||||
pub(crate) peer: omni::PeerVariant,
|
||||
pub(crate) settings: Mutex<GameSettings>,
|
||||
pub(crate) continue_running: AtomicBool, // TODO stop on drop
|
||||
pub(crate) accept_local: AtomicBool,
|
||||
|
@ -188,7 +61,7 @@ pub struct NetManager {
|
|||
}
|
||||
|
||||
impl NetManager {
|
||||
pub fn new(peer: PeerVariant) -> Arc<Self> {
|
||||
pub fn new(peer: omni::PeerVariant) -> Arc<Self> {
|
||||
Self {
|
||||
peer,
|
||||
settings: Mutex::new(GameSettings {
|
||||
|
@ -205,7 +78,7 @@ impl NetManager {
|
|||
.into()
|
||||
}
|
||||
|
||||
pub(crate) fn send(&self, peer: OmniPeerId, msg: &NetMsg, reliability: Reliability) {
|
||||
pub(crate) fn send(&self, peer: omni::OmniPeerId, msg: &NetMsg, reliability: Reliability) {
|
||||
let encoded = bitcode::encode(msg);
|
||||
self.peer.send(peer, encoded.clone(), reliability).ok(); // TODO log
|
||||
}
|
||||
|
@ -304,7 +177,7 @@ impl NetManager {
|
|||
}
|
||||
for net_event in self.peer.recv() {
|
||||
match net_event {
|
||||
OmniNetworkEvent::PeerConnected(id) => {
|
||||
omni::OmniNetworkEvent::PeerConnected(id) => {
|
||||
info!("Peer connected");
|
||||
if self.peer.my_id() == Some(self.peer.host_id()) {
|
||||
info!("Sending start game message");
|
||||
|
@ -318,10 +191,10 @@ impl NetManager {
|
|||
}
|
||||
state.try_ws_write(ws_encode_proxy("join", id));
|
||||
}
|
||||
OmniNetworkEvent::PeerDisconnected(id) => {
|
||||
omni::OmniNetworkEvent::PeerDisconnected(id) => {
|
||||
state.try_ws_write(ws_encode_proxy("leave", id));
|
||||
}
|
||||
OmniNetworkEvent::Message { src, data } => {
|
||||
omni::OmniNetworkEvent::Message { src, data } => {
|
||||
let Ok(net_msg) = bitcode::decode::<NetMsg>(&data) else {
|
||||
continue;
|
||||
};
|
||||
|
|
136
noita-proxy/src/net/omni.rs
Normal file
136
noita-proxy/src/net/omni.rs
Normal file
|
@ -0,0 +1,136 @@
|
|||
use super::steam_networking;
|
||||
use std::fmt::Display;
|
||||
use steamworks::{LobbyId, SteamId};
|
||||
use tangled::{PeerId, PeerState, Reliability};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct OmniPeerId(pub u64);
|
||||
|
||||
impl From<PeerId> for OmniPeerId {
|
||||
fn from(value: PeerId) -> Self {
|
||||
Self(value.0.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SteamId> for OmniPeerId {
|
||||
fn from(value: SteamId) -> Self {
|
||||
Self(value.raw())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OmniPeerId> for PeerId {
|
||||
fn from(value: OmniPeerId) -> Self {
|
||||
Self(
|
||||
value
|
||||
.0
|
||||
.try_into()
|
||||
.expect("Assuming PeerId was stored here, so conversion should succeed"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OmniPeerId> for SteamId {
|
||||
fn from(value: OmniPeerId) -> Self {
|
||||
Self::from_raw(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for OmniPeerId {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum OmniNetworkEvent {
|
||||
PeerConnected(OmniPeerId),
|
||||
PeerDisconnected(OmniPeerId),
|
||||
Message { src: OmniPeerId, data: Vec<u8> },
|
||||
}
|
||||
|
||||
impl From<tangled::NetworkEvent> for OmniNetworkEvent {
|
||||
fn from(value: tangled::NetworkEvent) -> Self {
|
||||
match value {
|
||||
tangled::NetworkEvent::PeerConnected(id) => Self::PeerConnected(id.into()),
|
||||
tangled::NetworkEvent::PeerDisconnected(id) => Self::PeerDisconnected(id.into()),
|
||||
tangled::NetworkEvent::Message(msg) => Self::Message {
|
||||
src: msg.src.into(),
|
||||
data: msg.data,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum PeerVariant {
|
||||
Tangled(tangled::Peer),
|
||||
Steam(steam_networking::SteamPeer),
|
||||
}
|
||||
|
||||
impl PeerVariant {
|
||||
pub(crate) fn send(
|
||||
&self,
|
||||
peer: OmniPeerId,
|
||||
msg: Vec<u8>,
|
||||
reliability: Reliability,
|
||||
) -> Result<(), tangled::NetError> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.send(peer.into(), msg, reliability),
|
||||
PeerVariant::Steam(p) => {
|
||||
p.send_message(peer.into(), &msg, reliability);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn broadcast(
|
||||
&self,
|
||||
msg: Vec<u8>,
|
||||
reliability: Reliability,
|
||||
) -> Result<(), tangled::NetError> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.broadcast(msg, reliability),
|
||||
PeerVariant::Steam(p) => Ok(p.broadcast_message(&msg, reliability)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn my_id(&self) -> Option<OmniPeerId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.my_id().map(OmniPeerId::from),
|
||||
PeerVariant::Steam(p) => Some(p.my_id().into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter_peer_ids(&self) -> Vec<OmniPeerId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.iter_peer_ids().map(OmniPeerId::from).collect(),
|
||||
PeerVariant::Steam(p) => p.get_peer_ids().into_iter().map(OmniPeerId::from).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn recv(&self) -> Vec<OmniNetworkEvent> {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.recv().map(OmniNetworkEvent::from).collect(),
|
||||
PeerVariant::Steam(p) => p.recv(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn state(&self) -> PeerState {
|
||||
match self {
|
||||
PeerVariant::Tangled(p) => p.state(),
|
||||
PeerVariant::Steam(p) => p.state(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn host_id(&self) -> OmniPeerId {
|
||||
match self {
|
||||
PeerVariant::Tangled(_) => PeerId::HOST.into(),
|
||||
PeerVariant::Steam(p) => p.host_id().into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn lobby_id(&self) -> Option<LobbyId> {
|
||||
match self {
|
||||
PeerVariant::Tangled(_) => None,
|
||||
PeerVariant::Steam(p) => p.lobby_id(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,7 +8,7 @@ use steamworks::{
|
|||
use tangled::{PeerState, Reliability};
|
||||
use tracing::{info, warn};
|
||||
|
||||
use super::OmniNetworkEvent;
|
||||
use super::omni::{self, OmniNetworkEvent};
|
||||
|
||||
enum SteamEvent {
|
||||
LobbyCreated(LobbyId),
|
||||
|
@ -150,10 +150,10 @@ impl SteamPeer {
|
|||
self.inner.lock().unwrap().state = PeerState::Disconnected;
|
||||
}
|
||||
SteamEvent::PeerConnected(id) => {
|
||||
returned_events.push(OmniNetworkEvent::PeerConnected(id.into()))
|
||||
returned_events.push(omni::OmniNetworkEvent::PeerConnected(id.into()))
|
||||
}
|
||||
SteamEvent::PeerDisconnected(id) => {
|
||||
returned_events.push(OmniNetworkEvent::PeerDisconnected(id.into()))
|
||||
returned_events.push(omni::OmniNetworkEvent::PeerDisconnected(id.into()))
|
||||
}
|
||||
SteamEvent::PeerStateChanged => self.update_remote_peers(),
|
||||
SteamEvent::AcceptConnection(id) => {
|
||||
|
@ -167,7 +167,7 @@ impl SteamPeer {
|
|||
let mut empty_array = vec![0; size];
|
||||
let mut buffer = empty_array.as_mut_slice();
|
||||
if let Some((sender, _)) = networking.read_p2p_packet(&mut buffer) {
|
||||
returned_events.push(OmniNetworkEvent::Message {
|
||||
returned_events.push(omni::OmniNetworkEvent::Message {
|
||||
src: sender.into(),
|
||||
data: empty_array,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue