Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
#![deny(
|
|
|
|
rustdoc::broken_intra_doc_links,
|
|
|
|
rustdoc::private_intra_doc_links,
|
|
|
|
rustdoc::missing_crate_level_docs,
|
|
|
|
rustdoc::invalid_codeblock_attributes,
|
|
|
|
rustdoc::invalid_rust_codeblocks,
|
|
|
|
rustdoc::bare_urls,
|
|
|
|
rustdoc::invalid_html_tags
|
|
|
|
)]
|
|
|
|
#![warn(
|
|
|
|
trivial_casts,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unused_lifetimes,
|
|
|
|
unused_import_braces,
|
|
|
|
unreachable_pub,
|
|
|
|
clippy::dbg_macro
|
|
|
|
)]
|
2022-11-14 01:10:42 +11:00
|
|
|
|
|
|
|
use proc_macro::TokenStream as StdTokenStream;
|
2022-12-24 01:30:57 +11:00
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::ToTokens;
|
2022-11-14 01:10:42 +11:00
|
|
|
use syn::{
|
2023-02-26 06:21:25 +11:00
|
|
|
parse_quote, Attribute, Error, GenericParam, Generics, Lifetime, LifetimeDef, Lit, Meta,
|
|
|
|
Result, Variant,
|
2022-11-14 01:10:42 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
mod decode;
|
|
|
|
mod encode;
|
2023-04-01 08:58:47 +11:00
|
|
|
mod ident;
|
2023-02-26 06:21:25 +11:00
|
|
|
mod packet;
|
2022-11-14 01:10:42 +11:00
|
|
|
|
2022-12-24 01:30:57 +11:00
|
|
|
#[proc_macro_derive(Encode, attributes(tag))]
|
2022-11-14 01:10:42 +11:00
|
|
|
pub fn derive_encode(item: StdTokenStream) -> StdTokenStream {
|
|
|
|
match encode::derive_encode(item.into()) {
|
|
|
|
Ok(tokens) => tokens.into(),
|
|
|
|
Err(e) => e.into_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-24 01:30:57 +11:00
|
|
|
#[proc_macro_derive(Decode, attributes(tag))]
|
|
|
|
pub fn derive_decode(item: StdTokenStream) -> StdTokenStream {
|
|
|
|
match decode::derive_decode(item.into()) {
|
2022-11-14 01:10:42 +11:00
|
|
|
Ok(tokens) => tokens.into(),
|
|
|
|
Err(e) => e.into_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 06:21:25 +11:00
|
|
|
#[proc_macro_derive(Packet, attributes(packet_id))]
|
|
|
|
pub fn derive_packet(item: StdTokenStream) -> StdTokenStream {
|
|
|
|
match packet::derive_packet(item.into()) {
|
2022-12-24 01:30:57 +11:00
|
|
|
Ok(tokens) => tokens.into(),
|
|
|
|
Err(e) => e.into_compile_error().into(),
|
2022-11-14 01:10:42 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 05:16:01 +11:00
|
|
|
#[proc_macro]
|
Reorganize Project (#321)
## Description
- `valence` and `valence_protocol` have been divided into smaller crates
in order to parallelize the build and improve IDE responsiveness. In the
process, code architecture has been made clearer by removing circular
dependencies between modules. `valence` is now just a shell around the
other crates.
- `workspace.packages` and `workspace.dependencies` are now used. This
makes dependency managements and crate configuration much easier.
- `valence_protocol` is no more. Most things from `valence_protocol`
ended up in `valence_core`. We won't advertise `valence_core` as a
general-purpose protocol library since it contains too much
valence-specific stuff. Closes #308.
- Networking code (login, initial TCP connection handling, etc.) has
been extracted into the `valence_network` crate. The API has been
expanded and improved with better defaults. Player counts and initial
connections to the server are now tracked separately. Player counts
function by default without any user configuration.
- Some crates like `valence_anvil`, `valence_network`,
`valence_player_list`, `valence_inventory`, etc. are now optional. They
can be enabled/disabled with feature flags and `DefaultPlugins` just
like bevy.
- Whole-server unit tests have been moved to `valence/src/tests` in
order to avoid [cyclic
dev-dependencies](https://github.com/rust-lang/cargo/issues/4242).
- Tools like `valence_stresser` and `packet_inspector` have been moved
to a new `tools` directory. Renamed `valence_stresser` to `stresser`.
Closes #241.
- Moved all benches to `valence/benches/` to make them easier to run and
organize.
Ignoring transitive dependencies and `valence_core`, here's what the
dependency graph looks like now:
```mermaid
graph TD
network --> client
client --> instance
biome --> registry
dimension --> registry
instance --> biome
instance --> dimension
instance --> entity
player_list --> client
inventory --> client
anvil --> instance
entity --> block
```
### Issues
- Inventory tests inspect many private implementation details of the
inventory module, forcing us to mark things as `pub` and
`#[doc(hidden)]`. It would be ideal if the tests only looked at
observable behavior.
- Consider moving packets in `valence_core` elsewhere. `Particle` wants
to use `BlockState`, but that's defined in `valence_block`, so we can't
use it without causing cycles.
- Unsure what exactly should go in `valence::prelude`.
- This could use some more tests of course, but I'm holding off on that
until I'm confident this is the direction we want to take things.
## TODOs
- [x] Update examples.
- [x] Update benches.
- [x] Update main README.
- [x] Add short READMEs to crates.
- [x] Test new schedule to ensure behavior is the same.
- [x] Update tools.
- [x] Copy lints to all crates.
- [x] Fix docs, clippy, etc.
2023-04-22 07:43:59 +10:00
|
|
|
pub fn parse_ident_str(item: StdTokenStream) -> StdTokenStream {
|
|
|
|
match ident::parse_ident_str(item.into()) {
|
2023-02-19 05:16:01 +11:00
|
|
|
Ok(tokens) => tokens.into(),
|
|
|
|
Err(e) => e.into_compile_error().into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 01:10:42 +11:00
|
|
|
fn pair_variants_with_discriminants(
|
|
|
|
variants: impl IntoIterator<Item = Variant>,
|
|
|
|
) -> Result<Vec<(i32, Variant)>> {
|
|
|
|
let mut discriminant = 0;
|
|
|
|
variants
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
if let Some(i) = find_tag_attr(&v.attrs)? {
|
|
|
|
discriminant = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
let pair = (discriminant, v);
|
|
|
|
discriminant += 1;
|
|
|
|
Ok(pair)
|
|
|
|
})
|
|
|
|
.collect::<Result<_>>()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_tag_attr(attrs: &[Attribute]) -> Result<Option<i32>> {
|
|
|
|
for attr in attrs {
|
|
|
|
if let Meta::NameValue(nv) = attr.parse_meta()? {
|
|
|
|
if nv.path.is_ident("tag") {
|
|
|
|
let span = nv.lit.span();
|
|
|
|
return match nv.lit {
|
|
|
|
Lit::Int(lit) => Ok(Some(lit.base10_parse::<i32>()?)),
|
|
|
|
_ => Err(Error::new(
|
|
|
|
span,
|
|
|
|
"discriminant value must be an integer literal",
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adding our lifetime to the generics before calling `.split_for_impl()` would
|
|
|
|
/// also add it to the resulting ty_generics, which we don't want. So I'm doing
|
|
|
|
/// this hack.
|
|
|
|
fn decode_split_for_impl(
|
|
|
|
mut generics: Generics,
|
|
|
|
lifetime: Lifetime,
|
|
|
|
) -> (TokenStream, TokenStream, TokenStream) {
|
|
|
|
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
|
|
|
|
|
|
|
|
let mut impl_generics = impl_generics.to_token_stream();
|
|
|
|
let ty_generics = ty_generics.to_token_stream();
|
|
|
|
let where_clause = where_clause.to_token_stream();
|
|
|
|
|
|
|
|
if generics.lifetimes().next().is_none() {
|
|
|
|
generics
|
|
|
|
.params
|
|
|
|
.push(GenericParam::Lifetime(LifetimeDef::new(lifetime)));
|
|
|
|
|
|
|
|
impl_generics = generics.split_for_impl().0.to_token_stream();
|
|
|
|
}
|
|
|
|
|
|
|
|
(impl_generics, ty_generics, where_clause)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_trait_bounds(generics: &mut Generics, trait_: TokenStream) {
|
|
|
|
for param in &mut generics.params {
|
|
|
|
if let GenericParam::Type(type_param) = param {
|
|
|
|
type_param.bounds.push(parse_quote!(#trait_))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|