Skip to content

Commit

Permalink
uhhh stuff i guess
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Dec 4, 2023
1 parent b8f1d7b commit 70c76f1
Show file tree
Hide file tree
Showing 34 changed files with 493 additions and 232 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
set(CMAKE_CXX_VISIBILITY_PRESET hidden)

# Enable LTO (2.5x less binary size, no noticable compile time hit)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

project(globed2 VERSION 1.0.0)

option(ENABLE_DEBUG "Debug mode" OFF)
Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 dank_meme

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ This repository contains the complete rewrite of Globed, for Geometry Dash 2.2 a
## Features

* Real-time multiplayer
* Voice chat and text messages with **full encryption and zero logging**
* blazing fast server written in pure Rust 🚀
* Voice chat and text messages with **full encryption and zero logging/storage**
* Available on three of the major platforms (Windows, Mac and Android)
* Fully open-source, including the server code

## Installation

Expand All @@ -23,7 +24,6 @@ Planned features:
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,9 +38,11 @@ camila314 - thank you for [UIBuilder](https://github.com/camila314/uibuilder)

RobTop - thank you for releasing this awesome game :)

### Used libraries
### License

* [Geode](https://geode-sdk.org/)
* [UIBuilder](https://github.com/camila314/uibuilder)
* [Opus](https://github.com/xiph/opus)
* [libsodium](https://github.com/jedisct1/libsodium)
Globed is licensed under the MIT license. All of the following libraries used in Globed have their own licenses you may want to read:

* [Geode](https://geode-sdk.org/) - [BSL 1.0](https://github.com/geode-sdk/geode/blob/main/LICENSE.txt)
* [UIBuilder](https://github.com/camila314/uibuilder) - [MIT](https://github.com/camila314/uibuilder/blob/main/LICENSE)
* [Opus](https://github.com/xiph/opus) - [BSD 3-Clause (?)](https://github.com/xiph/opus/blob/master/COPYING)
* [libsodium](https://github.com/jedisct1/libsodium) - [ISC](https://github.com/jedisct1/libsodium/blob/master/LICENSE)
71 changes: 43 additions & 28 deletions server/game-derives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#![feature(proc_macro_diagnostic)]
#![allow(clippy::missing_panics_doc)]

use darling::FromDeriveInput;
use proc_macro::{self, TokenStream};
use proc_macro::{self, Span, TokenStream};
use quote::{quote, ToTokens};
use syn::{parse_macro_input, punctuated::Punctuated, Data, DeriveInput, Meta, Token};

/// Implements `Encodable` for the given type. For `Encodable` to be successfully derived,
/// for structs, all of the members of the struct must also implement `Encodable`.
/// Implements `Encodable` for the given type, allowing you to serialize it into a regular `ByteBuffer`.
/// For `Encodable` to be successfully derived, for structs, all of the members of the struct must also implement `Encodable`.
/// The members are serialized in the same order they are laid out in the struct.
///
/// For enums, the enum must have no associated data fields (only variants), and may have a
/// For enums, the enum must derive `Copy`, must be plain (no associated data fields), and may have a
/// `#[repr(u*)]` or `#[repr(i*)]` attribute to indicate the encoded type. By default it will be `i32` if omitted.
#[proc_macro_derive(Encodable)]
pub fn derive_encodable(input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -67,8 +69,8 @@ pub fn derive_encodable(input: TokenStream) -> TokenStream {
gen.into()
}

/// Implements `KnownSize` for the given type. For `KnownSize` to be successfully derived,
/// for structs, all of the members of the struct must also implement `KnownSize`.
/// Implements `KnownSize` for the given type, allowing you to serialize it into a `FastByteBuffer`.
/// For `KnownSize` to be successfully derived, for structs, all of the members of the struct must also implement `KnownSize`.
///
/// For enums, all the same limitations apply as in `Encodable`.
#[proc_macro_derive(KnownSize)]
Expand Down Expand Up @@ -114,8 +116,9 @@ pub fn derive_known_size(input: TokenStream) -> TokenStream {
gen.into()
}

/// Implements `Decodable` for the given type. For `Decodable` to be successfully derived,
/// for structs, all of the members of the struct must also implement `Decodable`.
/// Implements `Decodable` for the given type, allowing you to deserialize it from a `ByteReader`/`ByteBuffer`.
/// For `Decodable` to be successfully derived, for structs, all of the members of the struct must also implement `Decodable`.
/// The members are deserialized in the same order they are laid out in the struct.
///
/// For enums, all the same limitations apply as in `Encodable` plus the enum must have explicitly specified values for all variants.
#[proc_macro_derive(Decodable)]
Expand Down Expand Up @@ -246,8 +249,13 @@ fn get_enum_repr_type(input: &DeriveInput) -> proc_macro2::TokenStream {
}
}

// assume i32 by default
repr_type.unwrap_or(quote! { i32 })
if repr_type.is_none() {
// if not specified, assume i32 and give a warning.
repr_type = Some(quote! { i32 });
Span::call_site().warning("enum repr type not specified - assuming i32. it is recommended to add #[repr(type)] before the enum as that makes it more explicit.").emit();
}

repr_type.unwrap()
}

#[derive(FromDeriveInput)]
Expand All @@ -263,37 +271,44 @@ struct PacketAttributes {
/// ```rust
/// #[derive(Packet, Encodable, Decodable)]
/// #[packet(id = 10000, encrypted = false)]
/// pub struct MyPacket { /* ... */ }
/// pub struct MyPacket { /* fields */ }
/// ```
#[proc_macro_derive(Packet, attributes(packet))]
pub fn packet(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input);
let opts = PacketAttributes::from_derive_input(&input).expect("wrong value passed into #[packet] derive macro");
let Ok(opts) = PacketAttributes::from_derive_input(&input) else {
return quote! {
compile_error!("invalid or missing signature for #[packet] attribute, please see documentation for `Packet` proc macro");
}
.into();
};

let DeriveInput { ident, .. } = input;

let id = opts.id;
let enc = opts.encrypted;

let output = quote! {
impl PacketMetadata for #ident {
const PACKET_ID: crate::data::packets::PacketId = #id;
const ENCRYPTED: bool = #enc;
const NAME: &'static str = stringify!(#ident);
}
let output = match &input.data {
Data::Struct(_) => {
quote! {
impl PacketMetadata for #ident {
const PACKET_ID: crate::data::packets::PacketId = #id;
const ENCRYPTED: bool = #enc;
const NAME: &'static str = stringify!(#ident);
}

impl Packet for #ident {
fn get_packet_id(&self) -> crate::data::packets::PacketId {
#id
}
impl Packet for #ident {}

fn get_encrypted(&self) -> bool {
#enc
impl #ident {
pub const fn header() -> crate::data::packets::PacketHeader {
crate::data::packets::PacketHeader::from_packet::<Self>()
}
}
}
}

impl #ident {
pub const fn header() -> crate::data::packets::PacketHeader {
crate::data::packets::PacketHeader::from_packet::<Self>()
Data::Enum(_) | Data::Union(_) => {
quote! {
compile_error!("Packet cannot be derived for enums or unions");
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions server/game/src/data/bytebufferext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub trait KnownSize {
pub const MAX_NAME_SIZE: usize = 32;
/// maximum characters in a `ServerNoticePacket` or `ServerDisconnectPacket` (164)
pub const MAX_NOTICE_SIZE: usize = 164;
/// maximum characters in a user message (256)
pub const MAX_MESSAGE_SIZE: usize = 256;
/// maximum characters in a user message (156)
pub const MAX_MESSAGE_SIZE: usize = 156;
/// max profiles that can be requested in `RequestProfilesPacket` (128)
pub const MAX_PROFILES_REQUESTED: usize = 128;

Expand Down
25 changes: 5 additions & 20 deletions server/game/src/data/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ pub mod server;
pub use client::*;
pub use server::*;

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

type PacketId = u16;
pub type PacketId = u16;

pub trait Packet: Send + Sync + PacketMetadata {
fn get_packet_id(&self) -> PacketId;
fn get_encrypted(&self) -> bool;
}
pub trait Packet: PacketMetadata {}

// god i hate this
pub trait PacketMetadata {
Expand All @@ -20,6 +17,7 @@ pub trait PacketMetadata {
const NAME: &'static str;
}

#[derive(Encodable, Decodable, KnownSize)]
pub struct PacketHeader {
pub packet_id: PacketId,
pub encrypted: bool,
Expand All @@ -34,18 +32,5 @@ impl PacketHeader {
}
}

pub const SIZE: usize = size_of_types!(PacketId, bool);
pub const SIZE: usize = Self::ENCODED_SIZE;
}

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 })
});

size_calc_impl!(PacketHeader, PacketHeader::SIZE);
2 changes: 1 addition & 1 deletion server/game/src/data/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl_primitive!(f64, read_f64, write_f64);
encode_impl!(String, buf, self, buf.write_string(self));
decode_impl!(String, buf, Ok(buf.read_string()?));

encode_impl!(&str, buf, self, buf.write_string(self));
encode_impl!(str, buf, self, buf.write_string(self));

/* Option<T> */

Expand Down
Loading

0 comments on commit 70c76f1

Please sign in to comment.