mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
add shutdown function
This commit is contained in:
parent
a87c4cf718
commit
e63f605c82
4 changed files with 83 additions and 6 deletions
47
Cargo.lock
generated
47
Cargo.lock
generated
|
@ -32,10 +32,19 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.59"
|
||||
name = "aho-corasick"
|
||||
version = "0.7.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c91f1f46651137be86f3a2b9a8359f9ab421d04d941c62b5982e1ca21113adf9"
|
||||
checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.65"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602"
|
||||
|
||||
[[package]]
|
||||
name = "async-compression"
|
||||
|
@ -321,10 +330,12 @@ dependencies = [
|
|||
name = "bot"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"azalea-client",
|
||||
"azalea-core",
|
||||
"azalea-physics",
|
||||
"azalea-protocol",
|
||||
"env_logger",
|
||||
"tokio",
|
||||
"uuid",
|
||||
]
|
||||
|
@ -579,6 +590,19 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c90bf5f19754d10198ccb95b70664fc925bd1fc090a0fd9a6ebc54acc8cd6272"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.24"
|
||||
|
@ -698,6 +722,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.2.3"
|
||||
|
@ -1154,6 +1184,8 @@ version = "1.5.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
|
@ -1334,6 +1366,15 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
|
|
|
@ -36,7 +36,9 @@ use std::{
|
|||
};
|
||||
use thiserror::Error;
|
||||
use tokio::{
|
||||
io::AsyncWriteExt,
|
||||
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
|
||||
task::JoinHandle,
|
||||
time::{self},
|
||||
};
|
||||
|
||||
|
@ -46,6 +48,7 @@ pub enum Event {
|
|||
Chat(ChatPacket),
|
||||
/// A game tick, happens 20 times per second.
|
||||
GameTick,
|
||||
Packet(Box<ClientboundGamePacket>),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -72,6 +75,7 @@ pub struct Client {
|
|||
pub player: Arc<Mutex<Player>>,
|
||||
pub dimension: Arc<Mutex<Dimension>>,
|
||||
pub physics_state: Arc<Mutex<PhysicsState>>,
|
||||
tasks: Arc<Mutex<Vec<JoinHandle<()>>>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -201,6 +205,7 @@ impl Client {
|
|||
player: Arc::new(Mutex::new(Player::default())),
|
||||
dimension: Arc::new(Mutex::new(Dimension::default())),
|
||||
physics_state: Arc::new(Mutex::new(PhysicsState::default())),
|
||||
tasks: Arc::new(Mutex::new(Vec::new())),
|
||||
};
|
||||
|
||||
// just start up the game loop and we're ready!
|
||||
|
@ -208,8 +213,14 @@ impl Client {
|
|||
// if you get an error right here that means you're doing something with locks wrong
|
||||
// read the error to see where the issue is
|
||||
// you might be able to just drop the lock or put it in its own scope to fix
|
||||
tokio::spawn(Self::protocol_loop(client.clone(), tx.clone()));
|
||||
tokio::spawn(Self::game_tick_loop(client.clone(), tx));
|
||||
{
|
||||
let mut tasks = client.tasks.lock().unwrap();
|
||||
tasks.push(tokio::spawn(Self::protocol_loop(
|
||||
client.clone(),
|
||||
tx.clone(),
|
||||
)));
|
||||
tasks.push(tokio::spawn(Self::game_tick_loop(client.clone(), tx)));
|
||||
}
|
||||
|
||||
Ok((client, rx))
|
||||
}
|
||||
|
@ -219,6 +230,16 @@ impl Client {
|
|||
self.write_conn.lock().await.write(packet).await
|
||||
}
|
||||
|
||||
/// Disconnect from the server, ending all tasks.
|
||||
pub async fn shutdown(self) -> Result<(), std::io::Error> {
|
||||
self.write_conn.lock().await.write_stream.shutdown().await?;
|
||||
let tasks = self.tasks.lock().unwrap();
|
||||
for task in tasks.iter() {
|
||||
task.abort();
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn protocol_loop(client: Client, tx: UnboundedSender<Event>) {
|
||||
loop {
|
||||
let r = client.read_conn.lock().await.read().await;
|
||||
|
@ -254,6 +275,7 @@ impl Client {
|
|||
client: &Client,
|
||||
tx: &UnboundedSender<Event>,
|
||||
) -> Result<(), HandleError> {
|
||||
tx.send(Event::Packet(Box::new(packet.clone()))).unwrap();
|
||||
match packet {
|
||||
ClientboundGamePacket::Login(p) => {
|
||||
debug!("Got login packet {:?}", p);
|
||||
|
|
|
@ -6,9 +6,11 @@ version = "0.1.0"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.65"
|
||||
azalea-client = {path = "../azalea-client"}
|
||||
azalea-core = {path = "../azalea-core"}
|
||||
azalea-physics = {path = "../azalea-physics"}
|
||||
azalea-protocol = {path = "../azalea-protocol"}
|
||||
env_logger = "0.9.1"
|
||||
tokio = "1.19.2"
|
||||
uuid = "1.1.2"
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
use azalea_client::{Account, Client, Event, MoveDirection};
|
||||
use azalea_protocol::packets::game::ClientboundGamePacket;
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let bot = Account::offline("bot");
|
||||
|
||||
let (bot, mut rx) = bot.join(&"localhost".try_into().unwrap()).await.unwrap();
|
||||
|
@ -12,7 +15,7 @@ async fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
async fn handle_event(event: Event, mut bot: Client) {
|
||||
async fn handle_event(event: Event, mut bot: Client) -> anyhow::Result<()> {
|
||||
match event {
|
||||
Event::Login => {
|
||||
// tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
|
@ -22,6 +25,15 @@ async fn handle_event(event: Event, mut bot: Client) {
|
|||
// }
|
||||
// bot.walk(MoveDirection::None);
|
||||
}
|
||||
Event::Packet(packet) => {
|
||||
if let ClientboundGamePacket::SetHealth(_) = *packet {
|
||||
println!("got set health");
|
||||
bot.shutdown().await?;
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue