Skip to content

Commit

Permalink
Plugin event rewrite (#513)
Browse files Browse the repository at this point in the history
* simplify all

* fmt

* Consider using derive macro

* Add new events

* Add events call

* minor fixes

* minor fixes

* fix

* optimize code

* big changes

* fix skill issue

* fix

* Consider not specify patch version

* make logger shared

* i want test one thing

* Add one useful function

* add little amouth of docs

* use config instead

* revert changes

* fmt

---------

Co-authored-by: Alexander Medvedev <[email protected]>
  • Loading branch information
suprohub and Snowiiii authored Feb 1, 2025
1 parent 466d438 commit 186e846
Show file tree
Hide file tree
Showing 25 changed files with 679 additions and 139 deletions.
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,18 @@ tokio = { version = "1.43", features = [
"signal",
] }

thiserror = "2"
thiserror = "2.0"

bytes = "1.9"

# Concurrency/Parallelism and Synchronization
rayon = "1.10.0"
parking_lot = { version = "0.12.3", features = ["send_guard"] }
crossbeam = "0.8.4"
rayon = "1.10"
parking_lot = { version = "0.12", features = ["send_guard"] }
crossbeam = "0.8"

uuid = { version = "1.12.1", features = ["serde", "v3", "v4"] }
derive_more = { version = "1.0.0", features = ["full"] }
uuid = { version = "1.12", features = ["serde", "v3", "v4"] }
derive_more = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

async-trait = "0.1"
6 changes: 3 additions & 3 deletions pumpkin-api-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition.workspace = true
proc-macro = true

[dependencies]
syn = { version = "2.0.89", features = ["full"] }
quote = "1.0.37"
proc-macro2 = "1.0.92"
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
proc-macro2 = "1.0"
2 changes: 1 addition & 1 deletion pumpkin-api-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn with_runtime(attr: TokenStream, item: TokenStream) -> TokenStream {

method.block = if use_global {
parse_quote!({
GLOBAL_RUNTIME.block_on(async move {
crate::GLOBAL_RUNTIME.block_on(async move {
#original_body
})
})
Expand Down
1 change: 1 addition & 0 deletions pumpkin-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct BasicConfiguration {
pub encryption: bool,
/// The server's description displayed on the status screen.
pub motd: String,
/// The server's ticks per second.
pub tps: f32,
/// The default game mode for players.
pub default_gamemode: GameMode,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pumpkin-util = { path = "../pumpkin-util" }
serde.workspace = true
serde_json.workspace = true

heck = "0.5.0"
heck = "0.5"
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
97 changes: 90 additions & 7 deletions pumpkin-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse::{Nothing, Parser},
parse_macro_input, Field, Fields, ItemStruct,
parse_macro_input, Block, Expr, Field, Fields, ItemStruct, Stmt,
};

extern crate proc_macro;

#[proc_macro_attribute]
pub fn event(args: TokenStream, item: TokenStream) -> TokenStream {
#[proc_macro_derive(Event)]
pub fn event(item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemStruct);
let name = &input.ident;
let _ = parse_macro_input!(args as Nothing);

quote! {
#input

impl crate::plugin::Event for #name {
fn get_name_static() -> &'static str {
stringify!(#name)
Expand Down Expand Up @@ -46,7 +43,10 @@ pub fn cancellable(args: TokenStream, input: TokenStream) -> TokenStream {
if let Fields::Named(ref mut fields) = item_struct.fields {
fields.named.push(
Field::parse_named
.parse2(quote! { pub cancelled: bool })
.parse2(quote! {
/// A boolean indicating cancel state of the event.
pub cancelled: bool
})
.unwrap(),
);
}
Expand All @@ -67,6 +67,89 @@ pub fn cancellable(args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

#[proc_macro]
pub fn send_cancellable(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Block);

let mut event = None;
let mut after_block = None;
let mut cancelled_block = None;

for stmt in input.stmts {
if let Stmt::Expr(expr, _) = stmt {
if event.is_none() {
event = Some(expr);
} else if let Expr::Block(b) = expr {
if let Some(ref label) = b.label {
if label.name.ident == "after" {
after_block = Some(b);
} else if label.name.ident == "cancelled" {
cancelled_block = Some(b);
}
}
}
}
}

if let Some(event) = event {
if let Some(after_block) = after_block {
if let Some(cancelled_block) = cancelled_block {
quote! {
let event = crate::PLUGIN_MANAGER
.lock()
.await
.fire(#event)
.await;

if !event.cancelled {
#after_block
} else {
#cancelled_block
}
}
.into()
} else {
quote! {
let event = crate::PLUGIN_MANAGER
.lock()
.await
.fire(#event)
.await;

if !event.cancelled {
#after_block
}
}
.into()
}
} else if let Some(cancelled_block) = cancelled_block {
quote! {
let event = crate::PLUGIN_MANAGER
.lock()
.await
.fire(#event)
.await;

if event.cancelled {
#cancelled_block
}
}
.into()
} else {
quote! {
let event = crate::PLUGIN_MANAGER
.lock()
.await
.fire(#event)
.await;
}
.into()
}
} else {
panic!("Event must be specified");
}
}

#[proc_macro_attribute]
pub fn client_packet(input: TokenStream, item: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(item.clone()).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion pumpkin-nbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition.workspace = true
serde.workspace = true
thiserror.workspace = true
bytes.workspace = true
cesu8 = "1.1.0"

cesu8 = "1.1"
6 changes: 3 additions & 3 deletions pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ tokio.workspace = true
bytes.workspace = true

# encryption
aes = "0.8.4"
cfb8 = "0.8.1"
aes = "0.8"
cfb8 = "0.8"

# compression
libdeflater = "1.23.0"
libdeflater = "1.23"
2 changes: 1 addition & 1 deletion pumpkin-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pumpkin-protocol = { path = "../pumpkin-protocol" }
pumpkin-nbt = { path = "../pumpkin-nbt" }
pumpkin-util = { path = "../pumpkin-util" }

indexmap = { version = "2.7.1", features = ["serde"] }
indexmap = { version = "2.7", features = ["serde"] }

serde.workspace = true
serde_json.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions pumpkin-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ uuid.workspace = true

num-traits = "0.2"

colored = "3"
md5 = "0.7.0"
colored = "3.0"
md5 = "0.7"
19 changes: 9 additions & 10 deletions pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,28 @@ serde.workspace = true
serde_json.workspace = true
log.workspace = true

num-derive = "0.4.2"
num-derive = "0.4"

dashmap = "6.1.0"
dashmap = "6.1"

num-traits = "0.2"

# Compression
flate2 = "1.0"
lz4 = "1.28.1"
lz4 = "1.28"

file-guard = "0.2.0"
indexmap = "2.7.1"
file-guard = "0.2"
indexmap = "2.7"

enum_dispatch = "0.3.13"
enum_dispatch = "0.3"

fastnbt = { git = "https://github.com/owengage/fastnbt.git" }

noise = "0.9.0"

rand = "0.8.5"
noise = "0.9"
rand = "0.8"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
criterion = { version = "0.5", features = ["html_reports"] }

[[bench]]
name = "chunk_noise"
Expand Down
3 changes: 2 additions & 1 deletion pumpkin-world/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub enum CompressionError {
LZ4Error(std::io::Error),
}

#[derive(Clone)]
pub struct ChunkData {
/// See description in `Subchunks`
pub subchunks: Subchunks,
Expand All @@ -92,7 +93,7 @@ pub struct ChunkData {
/// chunk, what filled only air or only water.
///
/// Multi means a normal chunk, what contains 24 subchunks.
#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone)]
pub enum Subchunks {
Single(u16),
Multi(Box<[Subchunk; SUBCHUNKS_COUNT]>),
Expand Down
7 changes: 5 additions & 2 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Level {
pub fn fetch_chunks(
&self,
chunks: &[Vector2<i32>],
channel: mpsc::Sender<Arc<RwLock<ChunkData>>>,
channel: mpsc::Sender<(Arc<RwLock<ChunkData>>, bool)>,
rt: &Handle,
) {
chunks.par_iter().for_each(|at| {
Expand All @@ -246,11 +246,14 @@ impl Level {
let level_folder = self.level_folder.clone();
let world_gen = self.world_gen.clone();
let chunk_pos = *at;
let mut first_load = false;

let chunk = loaded_chunks
.get(&chunk_pos)
.map(|entry| entry.value().clone())
.unwrap_or_else(|| {
first_load = true;

let loaded_chunk =
match Self::load_chunk_from_save(chunk_reader, &level_folder, chunk_pos) {
Ok(chunk) => {
Expand Down Expand Up @@ -299,7 +302,7 @@ impl Level {

rt.spawn(async move {
let _ = channel
.send(chunk)
.send((chunk, first_load))
.await
.inspect_err(|err| log::error!("unable to send chunk to channel: {}", err));
});
Expand Down
37 changes: 18 additions & 19 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,57 +33,56 @@ uuid.workspace = true
tokio.workspace = true
rayon.workspace = true
thiserror.workspace = true
async-trait.workspace = true

# config
serde.workspace = true
serde_json.workspace = true

bytes.workspace = true

rand = "0.8.5"
rand = "0.8"

num-bigint = "0.4"

# Console line reading
rustyline = "15.0.0"
rustyline = "15.0"

# encryption
rsa = "0.9.7"
rsa-der = "0.3.0"
rsa = "0.9"
rsa-der = "0.3"

# authentication
reqwest = { version = "0.12.12", default-features = false, features = [
reqwest = { version = "0.12", default-features = false, features = [
"http2",
"json",
"macos-system-configuration",
"rustls-tls",
] }

sha1 = "0.10.6"
sha1 = "0.10"

# velocity en
hmac = "0.12.1"
sha2 = "0.10.8"
hmac = "0.12"
sha2 = "0.10"

base64 = "0.22.1"
base64 = "0.22"

# icon loading
png = "0.17.16"
png = "0.17"

# logging
simple_logger = { version = "5.0.0", features = ["threads"] }
simple_logger = { version = "5.0", features = ["threads"] }
# Remove time in favor of chrono?
time = "0.3.37"
time = "0.3"

chrono = { version = "0.4.39", features = ["serde"]}

# commands
async-trait = "0.1.85"
chrono = { version = "0.4", features = ["serde"]}

# plugins
libloading = "0.8.6"
libloading = "0.8"

[build-dependencies]
git-version = "0.3.9"
git-version = "0.3"
# This makes it so the entire project doesn't recompile on each build on linux.
[target.'cfg(target_os = "windows")'.build-dependencies]
tauri-winres= "0.2.0"
tauri-winres= "0.3"
Loading

0 comments on commit 186e846

Please sign in to comment.