Skip to content

Commit

Permalink
last change dump for now
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Nov 24, 2023
1 parent e27fe28 commit 66f57ed
Show file tree
Hide file tree
Showing 25 changed files with 926 additions and 813 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Known issues:

* i am silly
* voice chat is a bit silly
* needs more unsafe code to be even more blazinger faster 🚀🚀🚀

## Hosting a server

Expand All @@ -38,15 +39,15 @@ cmake --build build --config Release

## Credit

ca7x3, Firee, Croozington, Coloride, Cvolton, mat, alk, maki, xTymon - thank you for being awesome, whether it's because you helped me, suggested ideas, or if I just found you awesome in general :D
ca7x3, Firee, Croozington, Coloride, Cvolton, mat, alk, maki, xTymon - thank you for being awesome, whether it's because you helped me, suggested ideas, helped with testing, or if I just found you awesome in general :D

camila314 - thank you for [UIBuilder](https://github.com/camila314/uibuilder)

RobTop - thank you for releasing this awesome game :)

## Open source acknowledgments

* [Geode](https://github.com/geode-sdk/geode) - the one thing that made all of this possible :)
* [Geode](https://geode-sdk.org/) - the one thing that made all of this possible :)
* [UIBuilder](https://github.com/camila314/uibuilder) - something you should 100% use when creating GD mods
* [Opus](https://github.com/xiph/opus) - audio codec used for audio compression
* [libsodium](https://github.com/jedisct1/libsodium) - library used for data encryption
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"geode": "1.3.5",
"version": "v1.0.0-beta.1",
"version": "v1.0.0-alpha.1",
"id": "dankmeme.globed2",
"name": "Globed",
"developer": "dankmeme",
Expand Down
8 changes: 8 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[workspace]
members = ["central", "game", "shared"]
resolver = "2"

[profile.release]
# my observation thus far with LTO:
# compile times -> ~100% increase
# executable size -> ~30% decrease
# performance -> too lazy to benchmark but probably a very minor improvement
# so.. good enough to keep!
lto = "fat"
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::data::types::cocos;
use crate::data::{
packets::{PacketHeader, PacketMetadata},
types::cocos,
};
use anyhow::{anyhow, Result};
use bytebuffer::{ByteBuffer, ByteReader};

Expand Down Expand Up @@ -26,12 +29,14 @@ pub const MAX_ENCODED_STRING_SIZE: usize = 512 + size_of_types!(u32); // 512 cha

macro_rules! encode_impl {
($typ:ty, $buf:ident, $self:ident, $encode:expr) => {
impl crate::bytebufferext::Encodable for $typ {
impl crate::data::bytebufferext::Encodable for $typ {
#[inline]
fn encode(&$self, $buf: &mut bytebuffer::ByteBuffer) {
$encode
}

fn encode_fast(&$self, $buf: &mut crate::bytebufferext::FastByteBuffer) {
#[inline]
fn encode_fast(&$self, $buf: &mut crate::data::bytebufferext::FastByteBuffer) {
$encode
}
}
Expand All @@ -40,11 +45,13 @@ macro_rules! encode_impl {

macro_rules! decode_impl {
($typ:ty, $buf:ident, $decode:expr) => {
impl crate::bytebufferext::Decodable for $typ {
impl crate::data::bytebufferext::Decodable for $typ {
#[inline]
fn decode($buf: &mut bytebuffer::ByteBuffer) -> anyhow::Result<Self> {
$decode
}

#[inline]
fn decode_from_reader($buf: &mut bytebuffer::ByteReader) -> anyhow::Result<Self> {
$decode
}
Expand All @@ -54,15 +61,15 @@ macro_rules! decode_impl {

macro_rules! encode_unimpl {
($typ:ty) => {
impl crate::bytebufferext::Encodable for $typ {
impl crate::data::bytebufferext::Encodable for $typ {
fn encode(&self, _: &mut bytebuffer::ByteBuffer) {
panic!(
"Tried to call {}::encode when Encodable was not implemented for this type",
stringify!($typ)
);
}

fn encode_fast(&self, _: &mut crate::bytebufferext::FastByteBuffer) {
fn encode_fast(&self, _: &mut crate::data::bytebufferext::FastByteBuffer) {
panic!(
"Tried to call {}::encode_fast when Encodable was not implemented for this type",
stringify!($typ)
Expand All @@ -74,7 +81,7 @@ macro_rules! encode_unimpl {

macro_rules! decode_unimpl {
($typ:ty) => {
impl crate::bytebufferext::Decodable for $typ {
impl crate::data::bytebufferext::Decodable for $typ {
fn decode(_: &mut bytebuffer::ByteBuffer) -> anyhow::Result<Self> {
Err(anyhow::anyhow!("decoding unimplemented for {}", stringify!($typ)))
}
Expand All @@ -88,7 +95,7 @@ macro_rules! decode_unimpl {

macro_rules! size_calc_impl {
($typ:ty, $calc:expr) => {
impl crate::bytebufferext::EncodableWithKnownSize for $typ {
impl crate::data::bytebufferext::EncodableWithKnownSize for $typ {
const ENCODED_SIZE: usize = $calc;
}
};
Expand Down Expand Up @@ -124,6 +131,7 @@ pub trait ByteBufferExtWrite {
fn write_value_vec<T: Encodable>(&mut self, val: &[T]);

fn write_enum<E: Into<B>, B: Encodable>(&mut self, val: E);
fn write_packet_header<T: PacketMetadata>(&mut self);

fn write_color3(&mut self, val: cocos::Color3B);
fn write_color4(&mut self, val: cocos::Color4B);
Expand All @@ -142,6 +150,7 @@ pub trait ByteBufferExtRead {
fn read_value_vec<T: Decodable>(&mut self) -> Result<Vec<T>>;

fn read_enum<E: TryFrom<B>, B: Decodable>(&mut self) -> Result<E>;
fn read_packet_header(&mut self) -> Result<PacketHeader>;

fn read_color3(&mut self) -> Result<cocos::Color3B>;
fn read_color4(&mut self) -> Result<cocos::Color4B>;
Expand Down Expand Up @@ -270,49 +279,64 @@ impl ByteBufferExt for ByteBuffer {

macro_rules! impl_extwrite {
($encode_fn:ident) => {
#[inline]
fn write_bool(&mut self, val: bool) {
self.write_u8(u8::from(val));
}

#[inline]
fn write_byte_array(&mut self, val: &[u8]) {
self.write_u32(val.len() as u32);
self.write_bytes(val);
}

#[inline]
fn write_value<T: Encodable>(&mut self, val: &T) {
val.$encode_fn(self);
}

#[inline]
fn write_optional_value<T: Encodable>(&mut self, val: Option<&T>) {
self.write_bool(val.is_some());
if let Some(val) = val {
self.write_value(val);
}
}

#[inline]
fn write_value_array<T: Encodable, const N: usize>(&mut self, val: &[T; N]) {
val.iter().for_each(|v| self.write_value(v));
}

#[inline]
fn write_value_vec<T: Encodable>(&mut self, val: &[T]) {
self.write_u32(val.len() as u32);
for elem in val {
elem.$encode_fn(self);
}
}

#[inline]
fn write_enum<E: Into<B>, B: Encodable>(&mut self, val: E) {
self.write_value(&val.into());
}

#[inline]
fn write_packet_header<T: PacketMetadata>(&mut self) {
self.write_value(&PacketHeader::from_packet::<T>());
}

#[inline]
fn write_color3(&mut self, val: cocos::Color3B) {
self.write_value(&val);
}

#[inline]
fn write_color4(&mut self, val: cocos::Color4B) {
self.write_value(&val);
}

#[inline]
fn write_point(&mut self, val: cocos::Point) {
self.write_value(&val);
}
Expand All @@ -321,30 +345,36 @@ macro_rules! impl_extwrite {

macro_rules! impl_extread {
($decode_fn:ident) => {
#[inline]
fn read_bool(&mut self) -> Result<bool> {
Ok(self.read_u8()? != 0u8)
}

#[inline]
fn read_byte_array(&mut self) -> Result<Vec<u8>> {
let length = self.read_u32()? as usize;
Ok(self.read_bytes(length)?)
}

#[inline]
fn read_value<T: Decodable>(&mut self) -> Result<T> {
T::$decode_fn(self)
}

#[inline]
fn read_optional_value<T: Decodable>(&mut self) -> Result<Option<T>> {
Ok(match self.read_bool()? {
false => None,
true => Some(self.read_value::<T>()?),
})
}

#[inline]
fn read_value_array<T: Decodable, const N: usize>(&mut self) -> Result<[T; N]> {
array_init::try_array_init(|_| self.read_value::<T>())
}

#[inline]
fn read_value_vec<T: Decodable>(&mut self) -> Result<Vec<T>> {
let mut out = Vec::new();
let length = self.read_u32()? as usize;
Expand All @@ -355,21 +385,30 @@ macro_rules! impl_extread {
Ok(out)
}

#[inline]
fn read_enum<E: TryFrom<B>, B: Decodable>(&mut self) -> Result<E> {
let val = self.read_value::<B>()?;
let val: Result<E, _> = val.try_into();

val.map_err(|_| anyhow!("failed to decode enum"))
}

#[inline]
fn read_packet_header(&mut self) -> Result<PacketHeader> {
self.read_value()
}

#[inline]
fn read_color3(&mut self) -> Result<cocos::Color3B> {
self.read_value()
}

#[inline]
fn read_color4(&mut self) -> Result<cocos::Color4B> {
self.read_value()
}

#[inline]
fn read_point(&mut self) -> Result<cocos::Point> {
self.read_value()
}
Expand Down
5 changes: 5 additions & 0 deletions server/game/src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
pub mod bytebufferext;
pub mod packets;
pub mod types;

pub use bytebufferext::*;
pub use packets::*;
pub use types::*;
4 changes: 1 addition & 3 deletions server/game/src/data/packets/client/connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::bytebufferext::*;
use crate::data::packets::*;
use crate::data::types::CryptoPublicKey;
use crate::data::*;

/* PingPacket - 10000 */

Expand Down
6 changes: 1 addition & 5 deletions server/game/src/data/packets/client/game.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::bytebufferext::*;
use crate::data::packets::*;
use crate::data::types::EncodedAudioFrame;
use crate::data::types::PlayerData;
use crate::data::types::PlayerIconData;
use crate::data::*;

/* SyncIconsPacket - 11000 */

Expand Down
57 changes: 40 additions & 17 deletions server/game/src/data/packets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
pub mod client;
pub mod server;

use crate::bytebufferext::{Decodable, Encodable};
pub use client::*;
pub use server::*;

use crate::data::bytebufferext::*;

type PacketId = u16;

Expand Down Expand Up @@ -29,29 +32,23 @@ macro_rules! packet {
impl crate::data::packets::PacketMetadata for $packet_type {
const PACKET_ID: crate::data::packets::PacketId = $packet_id;
const ENCRYPTED: bool = $encrypted;
const NAME: &'static str = stringify!($packet_type);
}

impl $packet_type {
pub const fn header() -> crate::data::packets::PacketHeader {
crate::data::packets::PacketHeader::from_packet::<Self>()
}
}
};

($packet_type:ident, $packet_id:expr, $encrypted:expr, { $($field:ident: $field_type:ty),* $(,)? }) => {
#[derive(Clone)]
pub struct $packet_type {
$(pub $field: $field_type),*
}

impl crate::data::packets::Packet for $packet_type {
fn get_packet_id(&self) -> crate::data::packets::PacketId {
$packet_id
}

fn get_encrypted(&self) -> bool {
$encrypted
}
}

impl crate::data::packets::PacketMetadata for $packet_type {
const PACKET_ID: crate::data::packets::PacketId = $packet_id;
const ENCRYPTED: bool = $encrypted;
const NAME: &'static str = stringify!($packet_type);
}
packet!($packet_type, $packet_id, $encrypted);
};
}

Expand Down Expand Up @@ -93,4 +90,30 @@ pub trait PacketMetadata {
const NAME: &'static str;
}

pub const PACKET_HEADER_LEN: usize = std::mem::size_of::<PacketId>() + std::mem::size_of::<bool>();
pub struct PacketHeader {
pub packet_id: u16,
pub encrypted: bool,
}

impl PacketHeader {
#[inline]
pub const fn from_packet<P: PacketMetadata>() -> Self {
Self {
packet_id: P::PACKET_ID,
encrypted: P::ENCRYPTED,
}
}

pub const SIZE: usize = std::mem::size_of::<PacketId>() + std::mem::size_of::<bool>();
}

encode_impl!(PacketHeader, buf, self, {
buf.write_u16(self.packet_id);
buf.write_bool(self.encrypted);
});

decode_impl!(PacketHeader, buf, {
let packet_id = buf.read_u16()?;
let encrypted = buf.read_bool()?;
Ok(Self { packet_id, encrypted })
});
8 changes: 1 addition & 7 deletions server/game/src/data/packets/server/connection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use crate::{
bytebufferext::*,
data::{
packets::{empty_server_packet, packet},
types::CryptoPublicKey,
},
};
use crate::data::*;

/* PingResponsePacket - 20000 */

Expand Down
Loading

0 comments on commit 66f57ed

Please sign in to comment.