Skip to content

Commit

Permalink
merge main more
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 2, 2023
2 parents 6d418c2 + d21e496 commit a3c7a2f
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 140 deletions.
237 changes: 151 additions & 86 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion azalea-auth/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub enum AuthError {
///
/// If you want to use your own code to cache or show the auth code to the user
/// in a different way, use [`get_ms_link_code`], [`get_ms_auth_token`],
/// [`get_minecraft_token`] and [`get_profile`] instead instead.
/// [`get_minecraft_token`] and [`get_profile`] instead.
pub async fn auth(email: &str, opts: AuthOpts) -> Result<AuthResult, AuthError> {
let cached_account = if let Some(cache_file) = &opts.cache_file {
cache::get_account_in_cache(cache_file, email).await
Expand Down
1 change: 0 additions & 1 deletion azalea-chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ azalea-buf = { path = "../azalea-buf", features = [
], version = "^0.8.0", optional = true }
azalea-language = { path = "../azalea-language", version = "0.8.0" }
simdnbt = { version = "0.2.1", optional = true, path = "../../simdnbt/simdnbt" }
log = "0.4.20"
tracing = "0.1.40"
once_cell = "1.18.0"
serde = { version = "^1.0", features = ["derive"] }
Expand Down
35 changes: 18 additions & 17 deletions azalea-chat/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ impl FormattedText {
}

#[cfg(feature = "simdnbt")]
fn parse_separator_nbt(nbt: &simdnbt::owned::NbtCompound) -> Option<FormattedText> {
fn parse_separator_nbt(nbt: &simdnbt::borrow::NbtCompound) -> Option<FormattedText> {
if let Some(separator) = nbt.get("separator") {
FormattedText::from_nbt_tag(separator.clone())
FormattedText::from_nbt_tag(separator)
} else {
None
}
Expand Down Expand Up @@ -290,17 +290,17 @@ impl simdnbt::Serialize for FormattedText {

#[cfg(feature = "simdnbt")]
impl simdnbt::FromNbtTag for FormattedText {
fn from_nbt_tag(tag: simdnbt::owned::NbtTag) -> Option<Self> {
fn from_nbt_tag(tag: &simdnbt::borrow::NbtTag) -> Option<Self> {
// we create a component that we might add siblings to
let mut component: FormattedText;

match tag {
// if it's a string, return a text component with that string
simdnbt::owned::NbtTag::String(string) => {
simdnbt::borrow::NbtTag::String(string) => {
Some(FormattedText::Text(TextComponent::new(string.to_string())))
}
// if it's a compound, make it do things with { text } and stuff
simdnbt::owned::NbtTag::Compound(compound) => {
simdnbt::borrow::NbtTag::Compound(compound) => {
if let Some(text) = compound.get("text") {
let text = text.string().unwrap_or_default().to_string();
component = FormattedText::Text(TextComponent::new(text));
Expand All @@ -313,9 +313,9 @@ impl simdnbt::FromNbtTag for FormattedText {
// if it's a string component with no styling and no siblings, just add
// a string to with_array otherwise add the
// component to the array
let c = FormattedText::from_nbt_tag(simdnbt::owned::NbtTag::Compound(
item.clone(),
))?;
let c = FormattedText::from_nbt_tag(
&simdnbt::borrow::NbtTag::Compound(item.clone()),
)?;
if let FormattedText::Text(text_component) = c {
if text_component.base.siblings.is_empty()
&& text_component.base.style.is_empty()
Expand All @@ -325,7 +325,7 @@ impl simdnbt::FromNbtTag for FormattedText {
}
}
with_array.push(StringOrComponent::FormattedText(
FormattedText::from_nbt_tag(simdnbt::owned::NbtTag::Compound(
FormattedText::from_nbt_tag(&simdnbt::borrow::NbtTag::Compound(
item.clone(),
))?,
));
Expand Down Expand Up @@ -380,7 +380,7 @@ impl simdnbt::FromNbtTag for FormattedText {
}
for extra_component in extra {
let sibling = FormattedText::from_nbt_tag(
simdnbt::owned::NbtTag::Compound(extra_component.clone()),
&simdnbt::borrow::NbtTag::Compound(extra_component.clone()),
)?;
component.append(sibling);
}
Expand All @@ -392,14 +392,14 @@ impl simdnbt::FromNbtTag for FormattedText {
Some(component)
}
// ok so it's not a compound, if it's a list deserialize every item
simdnbt::owned::NbtTag::List(list) => {
simdnbt::borrow::NbtTag::List(list) => {
let list = list.compounds()?;
let mut component = FormattedText::from_nbt_tag(simdnbt::owned::NbtTag::Compound(
list.get(0)?.clone(),
))?;
let mut component = FormattedText::from_nbt_tag(
&simdnbt::borrow::NbtTag::Compound(list.get(0)?.clone()),
)?;
for i in 1..list.len() {
component.append(FormattedText::from_nbt_tag(
simdnbt::owned::NbtTag::Compound(list.get(i)?.clone()),
&simdnbt::borrow::NbtTag::Compound(list.get(i)?.clone()),
)?);
}
Some(component)
Expand All @@ -412,8 +412,9 @@ impl simdnbt::FromNbtTag for FormattedText {
#[cfg(feature = "azalea-buf")]
impl McBufReadable for FormattedText {
fn read_from(buf: &mut std::io::Cursor<&[u8]>) -> Result<Self, BufReadError> {
let nbt = simdnbt::owned::NbtTag::read(buf)?;
FormattedText::from_nbt_tag(nbt).ok_or(BufReadError::Custom("couldn't read nbt".to_owned()))
let nbt = simdnbt::borrow::NbtTag::read(buf)?;
FormattedText::from_nbt_tag(&nbt)
.ok_or(BufReadError::Custom("couldn't read nbt".to_owned()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion azalea-chat/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ impl Style {

impl simdnbt::Deserialize for Style {
fn from_compound(
compound: simdnbt::owned::NbtCompound,
compound: &simdnbt::borrow::NbtCompound,
) -> Result<Self, simdnbt::DeserializeError> {
let bold = compound.byte("bold").map(|v| v != 0);
let italic = compound.byte("italic").map(|v| v != 0);
Expand Down
42 changes: 22 additions & 20 deletions azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use azalea_protocol::{
},
resolver, ServerAddress,
};
use azalea_world::{Instance, InstanceContainer, InstanceName};
use azalea_world::{Instance, InstanceContainer, InstanceName, PartialInstance};
use bevy_app::{App, FixedUpdate, Plugin, PluginGroup, PluginGroupBuilder, Update};
use bevy_ecs::{
bundle::Bundle,
Expand Down Expand Up @@ -425,16 +425,6 @@ impl Client {
});
}

pub fn local_player<'a>(&'a self, ecs: &'a mut World) -> &'a InstanceHolder {
self.query::<&InstanceHolder>(ecs)
}
pub fn local_player_mut<'a>(
&'a self,
ecs: &'a mut World,
) -> bevy_ecs::world::Mut<'a, InstanceHolder> {
self.query::<&mut InstanceHolder>(ecs)
}

pub fn raw_connection<'a>(&'a self, ecs: &'a mut World) -> &'a RawConnection {
self.query::<&RawConnection>(ecs)
}
Expand Down Expand Up @@ -468,17 +458,29 @@ impl Client {
self.query::<Option<&T>>(&mut self.ecs.lock()).cloned()
}

/// Get a reference to our (potentially shared) world.
/// Get an `RwLock` with a reference to our (potentially shared) world.
///
/// This gets the [`Instance`] from our world container. If it's a normal
/// client, then it'll be the same as the world the client has loaded.
/// If the client using a shared world, then the shared world will be a
/// superset of the client's world.
/// This gets the [`Instance`] from the client's [`InstanceHolder`]
/// component. If it's a normal client, then it'll be the same as the
/// world the client has loaded. If the client is using a shared world,
/// then the shared world will be a superset of the client's world.
pub fn world(&self) -> Arc<RwLock<Instance>> {
let world_name = self.component::<InstanceName>();
let ecs = self.ecs.lock();
let instance_container = ecs.resource::<InstanceContainer>();
instance_container.get(&world_name).unwrap()
let instance_holder = self.component::<InstanceHolder>();
instance_holder.instance.clone()
}

/// Get an `RwLock` with a reference to the world that this client has
/// loaded.
///
/// ```
/// # use azalea_core::position::ChunkPos;
/// # fn example(client: &azalea_client::Client) {
/// let world = client.partial_world();
/// let is_0_0_loaded = world.read().chunks.limited_get(&ChunkPos::new(0, 0)).is_some();
/// # }
pub fn partial_world(&self) -> Arc<RwLock<PartialInstance>> {
let instance_holder = self.component::<InstanceHolder>();
instance_holder.partial_instance.clone()
}

/// Returns whether we have a received the login packet yet.
Expand Down
25 changes: 18 additions & 7 deletions azalea-core/src/registry_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ impl RegistryHolder {
&self,
name: &ResourceLocation,
) -> Option<Result<T, simdnbt::DeserializeError>> {
self.map.get(name).map(|nbt| T::from_compound(nbt.clone()))
// this is suboptimal, ideally simdnbt should just have a way to get the
// owned::NbtCompound as a borrow::NbtCompound

let nbt_owned_compound = self.map.get(name)?;
let mut nbt_bytes = Vec::new();
nbt_owned_compound.write(&mut nbt_bytes);
let nbt_borrow_compound =
simdnbt::borrow::NbtCompound::read(&mut Cursor::new(&nbt_bytes)).ok()?;
Some(T::from_compound(&nbt_borrow_compound))
}

/// Get the dimension type registry, or `None` if it doesn't exist. You
Expand All @@ -51,9 +59,12 @@ impl RegistryHolder {

impl McBufReadable for RegistryHolder {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let nbt_compound = NbtCompound::read_from(buf)?;
let nbt_tag = simdnbt::borrow::NbtTag::read(buf)?;
let nbt_compound = nbt_tag
.compound()
.ok_or_else(|| BufReadError::Custom("RegistryHolder must be a compound".to_string()))?;
Ok(RegistryHolder {
map: simdnbt::Deserialize::from_compound(nbt_compound)?,
map: simdnbt::Deserialize::from_compound(&nbt_compound)?,
})
}
}
Expand Down Expand Up @@ -193,12 +204,12 @@ pub enum MonsterSpawnLightLevel {
}

impl FromNbtTag for MonsterSpawnLightLevel {
fn from_nbt_tag(tag: simdnbt::owned::NbtTag) -> Option<Self> {
fn from_nbt_tag(tag: &simdnbt::borrow::NbtTag) -> Option<Self> {
if let Some(value) = tag.int() {
Some(Self::Simple(value as u32))
} else if let Some(value) = tag.compound() {
let kind = ResourceLocation::from_nbt_tag(value.get("type")?.clone())?;
let value = MonsterSpawnLightLevelValues::from_nbt_tag(value.get("value")?.clone())?;
let kind = ResourceLocation::from_nbt_tag(value.get("type")?)?;
let value = MonsterSpawnLightLevelValues::from_nbt_tag(value.get("value")?)?;
Some(Self::Complex { kind, value })
} else {
None
Expand Down Expand Up @@ -251,7 +262,7 @@ pub enum BiomePrecipitation {
Snow,
}
impl FromNbtTag for BiomePrecipitation {
fn from_nbt_tag(tag: NbtTag) -> Option<Self> {
fn from_nbt_tag(tag: &simdnbt::borrow::NbtTag) -> Option<Self> {
match tag.string()?.to_str().as_ref() {
"none" => Some(Self::None),
"rain" => Some(Self::Rain),
Expand Down
2 changes: 1 addition & 1 deletion azalea-core/src/resource_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<'de> Deserialize<'de> for ResourceLocation {
}

impl FromNbtTag for ResourceLocation {
fn from_nbt_tag(tag: NbtTag) -> Option<Self> {
fn from_nbt_tag(tag: &simdnbt::borrow::NbtTag) -> Option<Self> {
tag.string().and_then(|s| s.to_str().parse().ok())
}
}
Expand Down
23 changes: 23 additions & 0 deletions azalea-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ impl Display for ServerAddress {
}
}

/// Serde deserialization for ServerAddress. This is useful for config file
/// usage.
impl<'de> serde::Deserialize<'de> for ServerAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let string = String::deserialize(deserializer)?;
ServerAddress::try_from(string.as_str()).map_err(serde::de::Error::custom)
}
}

/// Serde serialization for ServerAddress. This uses the Display impl, so it
/// will serialize to a string.
impl serde::Serialize for ServerAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}

#[cfg(test)]
mod tests {
use std::io::Cursor;
Expand Down
9 changes: 4 additions & 5 deletions azalea-protocol/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::net::{IpAddr, SocketAddr};
use thiserror::Error;
use trust_dns_resolver::{
config::{ResolverConfig, ResolverOpts},
TokioAsyncResolver,
Name, TokioAsyncResolver,
};

#[derive(Error, Debug)]
Expand Down Expand Up @@ -45,7 +45,7 @@ pub async fn resolve_address(address: &ServerAddress) -> Result<SocketAddr, Reso
.next()
.ok_or(ResolverError::NoSrvRecord)?;
let redirect_address = ServerAddress {
host: redirect_srv.target().to_utf8(),
host: redirect_srv.target().to_ascii(),
port: redirect_srv.port(),
};

Expand All @@ -58,13 +58,12 @@ pub async fn resolve_address(address: &ServerAddress) -> Result<SocketAddr, Reso
));
}

// debug!("redirecting to {:?}", redirect_address);

return resolve_address(&redirect_address).await;
}

// there's no redirect, try to resolve this as an ip address
let lookup_ip_result = resolver.lookup_ip(address.host.clone()).await;
let name = Name::from_ascii(&address.host).map_err(|_| ResolverError::NoIp)?;
let lookup_ip_result = resolver.lookup_ip(name).await;
let lookup_ip = lookup_ip_result.map_err(|_| ResolverError::NoIp)?;

Ok(SocketAddr::new(
Expand Down
2 changes: 1 addition & 1 deletion azalea/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![feature(let_chains)]

pub mod accept_resource_packs;
mod auto_respawn;
pub mod auto_respawn;
pub mod auto_tool;
mod bot;
pub mod container;
Expand Down

0 comments on commit a3c7a2f

Please sign in to comment.