Skip to content

Commit 37acbfb

Browse files
committed
stash changes
1 parent 21b7206 commit 37acbfb

File tree

27 files changed

+731
-888
lines changed

27 files changed

+731
-888
lines changed

Cargo.lock

Lines changed: 262 additions & 233 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ rkyv = '0.8.8'
103103
serde = '1.0.217'
104104
serde_json = '1.0.140'
105105
slotmap = '1.0.7'
106-
bevy = { git = "https://github.com/bevyengine/bevy"}
106+
#bevy = { git = "https://github.com/bevyengine/bevy"}
107+
bevy = "0.16.0"
108+
eyre = { version = "0.6.12" }
107109
snafu = '0.8.5'
108110
syn = '2.0.99'
109111
tango-bench = "0.6.0"

crates/hyperion-crafting/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{collections::HashMap, io::Write};
22

33
use derive_build::Build;
44
use bevy::prelude::*;
5-
use flecs_ecs::macros::Component;
65
use slotmap::{SecondaryMap, SlotMap, new_key_type};
76
use valence_protocol::{Encode, ItemKind, ItemStack, Packet};
87

crates/hyperion-utils/Cargo.toml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
[dependencies]
2-
anyhow = {workspace = true}
3-
directories = {workspace = true}
4-
sha2 = {workspace = true}
5-
hex = {workspace = true}
6-
hyperion-packet-macros = {workspace = true}
7-
tracing = {workspace = true}
8-
reqwest = {workspace = true}
9-
flate2 = {workspace = true}
10-
tar = {workspace = true}
11-
tokio-util = {workspace = true}
12-
tokio = {workspace = true}
13-
futures-util = {workspace = true}
2+
bevy = { workspace = true }
3+
bytemuck = { workspace = true }
4+
directories = { workspace = true }
5+
eyre = { workspace = true }
6+
flate2 = { workspace = true }
7+
futures-util = { workspace = true }
8+
hex = { workspace = true }
9+
hyperion-packet-macros = { workspace = true }
10+
reqwest = { workspace = true }
11+
sha2 = { workspace = true }
12+
tar = { workspace = true }
13+
tokio = { workspace = true }
14+
tokio-util = { workspace = true }
15+
tracing = { workspace = true }
1416
valence_protocol = { workspace = true }
1517

1618
[lints]

crates/hyperion-utils/src/cached_save.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::path::PathBuf;
22

3-
use anyhow::Context;
3+
use bevy::prelude::*;
44
use directories::ProjectDirs;
5-
use flecs_ecs::core::{World, WorldGet};
5+
use eyre::WrapErr;
66
use futures_util::stream::StreamExt;
77
use sha2::Digest;
88
use tar::Archive;
@@ -14,16 +14,8 @@ use crate::AppId;
1414
pub fn cached_save<U: reqwest::IntoUrl + 'static>(
1515
world: &World,
1616
url: U,
17-
) -> impl Future<Output = anyhow::Result<PathBuf>> + 'static {
18-
let project_dirs = world
19-
.get::<&AppId>(
20-
|AppId {
21-
qualifier,
22-
organization,
23-
application,
24-
}| { ProjectDirs::from(qualifier, organization, application) },
25-
)
26-
.expect("failed to get AppId");
17+
) -> impl Future<Output = eyre::Result<PathBuf>> + 'static {
18+
let project_dirs = world.resource::<AppId>();
2719

2820
let cache = project_dirs.cache_dir();
2921

@@ -43,7 +35,7 @@ pub fn cached_save<U: reqwest::IntoUrl + 'static>(
4335
// download
4436
let response = reqwest::get(url)
4537
.await
46-
.with_context(|| format!("failed to get {url_str}"))?;
38+
.wrap_err_with(|| format!("failed to get {url_str}"))?;
4739

4840
let byte_stream = response.bytes_stream();
4941
// Convert the byte stream into an AsyncRead
@@ -65,7 +57,7 @@ pub fn cached_save<U: reqwest::IntoUrl + 'static>(
6557
.unpack(&directory)
6658
.context("failed to unpack archive")?;
6759

68-
anyhow::Ok(())
60+
eyre::Ok(())
6961
});
7062

7163
handle.await??;

crates/hyperion-utils/src/lib.rs

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,53 @@
1-
use flecs_ecs::{
2-
core::World,
3-
macros::Component,
4-
prelude::{Entity, Module},
5-
};
6-
71
mod cached_save;
82
mod lifetime;
3+
use std::path::PathBuf;
4+
5+
use bevy::prelude::*;
96
pub use cached_save::cached_save;
7+
use eyre::WrapErr;
108
pub use lifetime::*;
119

12-
pub trait EntityExt {
13-
fn minecraft_id(&self) -> i32;
14-
15-
fn from_minecraft_id(id: i32) -> Self;
10+
pub trait EntityExt: Sized {
11+
fn minecraft_id(&self) -> eyre::Result<i32>;
12+
fn from_minecraft_id(id: i32) -> eyre::Result<Self>;
1613
}
1714

15+
// todo(bevy): this is probably different with bevy
16+
17+
// we need a mapping from Bevy entity id to Minecraft entity id
18+
// some
1819
impl EntityExt for Entity {
19-
fn minecraft_id(&self) -> i32 {
20-
let raw = self.0;
21-
// Convert entity id into two u32s
22-
let most_significant = (raw >> 32) as u32;
23-
24-
#[expect(
25-
clippy::cast_possible_truncation,
26-
reason = "we are getting the least significant bits, we expect truncation"
27-
)]
28-
let least_significant = raw as u32;
29-
30-
// Ensure most_significant >> 4 does not overlap with least_significant
31-
// and that least_significant AND most_significant is 0
32-
// this is the "thread" space which allows for 2^6 = 64 threads
33-
debug_assert_eq!(
34-
most_significant >> 6,
35-
0,
36-
"Entity ID is too large for Minecraft"
37-
);
38-
39-
debug_assert!(
40-
least_significant < (1 << 26),
41-
"Entity ID is too large for Minecraft (must fit in 2^26)"
42-
);
43-
44-
// Combine them into a single i32
45-
let result = (most_significant << 26) | least_significant;
46-
47-
#[expect(
48-
clippy::cast_possible_wrap,
49-
reason = "we do not care about sign changes, we expect wrap"
50-
)]
51-
let result = result as i32;
52-
53-
result
20+
fn minecraft_id(&self) -> eyre::Result<i32> {
21+
let index = self.index();
22+
let generation = self.generation();
23+
24+
let index_u16 = u16::try_from(index)
25+
.wrap_err("entity index is too large to create a Minecraft entity id.")?;
26+
27+
let generation_u16 = u16::try_from(generation)
28+
.wrap_err("entity generation is too large to create a Minecraft entity id.")?;
29+
30+
let index_u32 = u32::from(index_u16);
31+
let generation_u32 = u32::from(generation_u16);
32+
33+
let raw_u32 = index_u32 << 16 | generation_u32;
34+
35+
Ok(bytemuck::cast(raw_u32))
5436
}
5537

56-
fn from_minecraft_id(id: i32) -> Self {
57-
#[expect(clippy::cast_sign_loss, reason = "we do not care about sign changes.")]
58-
let id = id as u32;
38+
fn from_minecraft_id(id: i32) -> eyre::Result<Self> {
39+
let id: u32 = bytemuck::cast(id);
5940

60-
let least_significant = id & ((1 << 26) - 1);
61-
let most_significant = (id >> 26) & 0x3F;
41+
let index = id >> 16;
42+
let generation = id & 0xFFFF;
6243

63-
let raw = (u64::from(most_significant) << 32) | u64::from(least_significant);
64-
Self(raw)
44+
let raw_u64 = u64::from(index) | (u64::from(generation) << 32);
45+
Ok(Self::from_bits(raw_u64))
6546
}
6647
}
6748

6849
/// Represents application identification information used for caching and other system-level operations
69-
#[derive(Component)]
50+
#[derive(Resource)]
7051
pub struct AppId {
7152
/// The qualifier/category of the application (e.g. "com", "org", "hyperion")
7253
pub qualifier: String,
@@ -76,17 +57,44 @@ pub struct AppId {
7657
pub application: String,
7758
}
7859

60+
impl AppId {
61+
#[must_use]
62+
pub fn cache_dir(&self) -> PathBuf {
63+
let project_dirs = directories::ProjectDirs::from(
64+
self.qualifier.as_str(),
65+
self.organization.as_str(),
66+
self.application.as_str(),
67+
)
68+
.unwrap();
69+
project_dirs.cache_dir().to_path_buf()
70+
}
71+
}
72+
7973
#[derive(Component)]
8074
pub struct HyperionUtilsModule;
8175

82-
impl Module for HyperionUtilsModule {
83-
fn module(world: &World) {
84-
world.component::<AppId>();
85-
86-
world.set(AppId {
76+
impl Plugin for HyperionUtilsModule {
77+
fn build(&self, app: &mut App) {
78+
app.insert_resource(AppId {
8779
qualifier: "github".to_string(),
8880
organization: "hyperion-mc".to_string(),
8981
application: "generic".to_string(),
9082
});
9183
}
9284
}
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn test_entity_id() {
92+
let entity = Entity::from_raw(0xDEAD_BEEF);
93+
let id = entity.minecraft_id().unwrap();
94+
assert_eq!(id, 0xDEAD_BEEF);
95+
96+
let entity = Entity::from_raw(0xDEAD_BEEF);
97+
let id = entity.minecraft_id().unwrap();
98+
assert_eq!(id, 0xDEAD_BEEF);
99+
}
100+
}

crates/hyperion/src/common/config.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
use std::{fmt::Debug, fs::File, io::Read, path::Path};
44

5-
use bevy::log::Resource;
6-
use flecs_ecs::macros::Component;
5+
use bevy::prelude::*;
76
use serde::{Deserialize, Serialize};
87
use tracing::{info, instrument, warn};
98

crates/hyperion/src/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
time::Duration,
55
};
66

7-
use flecs_ecs::macros::Component;
7+
use bevy::prelude::*;
88
use libdeflater::CompressionLvl;
99
use valence_protocol::CompressionThreshold;
1010

crates/hyperion/src/common/runtime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! See [`AsyncRuntime`].
22
33
use std::sync::Arc;
4-
use bevy::log::Resource;
4+
use bevy::prelude::*;
55
use derive_more::{Deref, DerefMut};
6-
use flecs_ecs::{core::World, macros::Component};
76
use kanal::{Receiver, Sender};
87

98
/// Type alias for world callback functions
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
pub mod mojang;
22

3-
mod sendable;
4-
mod tracing_ext;
5-
63
pub use sendable::*;
74
pub use tracing_ext::*;

crates/hyperion/src/common/util/mojang.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use std::{sync::Arc, time::Duration};
44

5-
use anyhow::{Context, bail};
6-
use flecs_ecs::macros::Component;
5+
use eyre::{Context, bail};
6+
use bevy::prelude::*;
77
use serde_json::Value;
88
use tokio::{
99
sync::Semaphore,
@@ -101,7 +101,7 @@ impl MojangClient {
101101
}
102102

103103
/// Gets a player's UUID from their username.
104-
pub async fn get_uuid(&self, username: &str) -> anyhow::Result<Uuid> {
104+
pub async fn get_uuid(&self, username: &str) -> eyre::Result<Uuid> {
105105
let url = self.provider.username_url(username);
106106
let json_object = self.response_raw(&url).await?;
107107

@@ -115,7 +115,7 @@ impl MojangClient {
115115
}
116116

117117
/// Gets a player's username from their UUID.
118-
pub async fn get_username(&self, uuid: Uuid) -> anyhow::Result<String> {
118+
pub async fn get_username(&self, uuid: Uuid) -> eyre::Result<String> {
119119
let url = self.provider.uuid_url(&uuid);
120120
let json_object = self.response_raw(&url).await?;
121121

@@ -128,18 +128,18 @@ impl MojangClient {
128128
}
129129

130130
/// Gets player data from their UUID.
131-
pub async fn data_from_uuid(&self, uuid: &Uuid) -> anyhow::Result<Value> {
131+
pub async fn data_from_uuid(&self, uuid: &Uuid) -> eyre::Result<Value> {
132132
let url = self.provider.uuid_url(uuid);
133133
self.response_raw(&url).await
134134
}
135135

136136
/// Gets player data from their username.
137-
pub async fn data_from_username(&self, username: &str) -> anyhow::Result<Value> {
137+
pub async fn data_from_username(&self, username: &str) -> eyre::Result<Value> {
138138
let url = self.provider.username_url(username);
139139
self.response_raw(&url).await
140140
}
141141

142-
async fn response_raw(&self, url: &str) -> anyhow::Result<Value> {
142+
async fn response_raw(&self, url: &str) -> eyre::Result<Value> {
143143
self.rate_limit
144144
.acquire()
145145
.await

crates/hyperion/src/common/util/sendable.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)