1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
use clap::Parser;
use enumset::{EnumSet, EnumSetType};
use futures_util::future::OptionFuture;
use mt_auth::Auth;
use mt_net::{CltSender, ReceiverExt, SenderExt, ToCltPkt, ToSrvPkt};
use std::pin::Pin;
use std::time::Duration;
use tokio::time::{sleep, Sleep};
#[derive(Parser, Debug)]
#[clap(version, about, long_about = None)]
struct Args {
/// Server address. Format: address:port
#[clap(value_parser)]
address: String,
/// Quit after QUIT_AFTER seconds
#[clap(short, long, value_parser)]
quit_after_seconds: Option<f32>,
/// Quit after having received item and node definitions
#[clap(short = 'Q', long, value_parser, default_value_t = false)]
quit_after_defs: bool,
/// Player name
#[clap(short, long, value_parser, default_value = "texmodbot")]
username: String,
/// Password
#[clap(short, long, value_parser, default_value = "owo")]
password: String,
}
#[derive(EnumSetType)]
enum DefType {
NodeDef,
ItemDef,
}
struct Bot {
conn: CltSender,
quit_after_defs: bool,
auth: Auth,
pending: EnumSet<DefType>,
}
impl Bot {
fn got_def(&mut self, def: DefType) {
self.pending.remove(def);
if self.quit_after_defs && self.pending.is_empty() {
self.conn.close()
}
}
async fn handle_pkt(&mut self, pkt: ToCltPkt) {
use ToCltPkt::*;
self.auth.handle_pkt(&pkt).await;
let print_texture = |tex: &String| {
if !tex.is_empty() {
println!("{tex}");
}
};
match pkt {
NodeDefs(defs) => {
defs.0
.values()
.flat_map(|def| {
std::iter::empty()
.chain(&def.tiles)
.chain(&def.special_tiles)
.chain(&def.overlay_tiles)
})
.map(|tile| &tile.texture.name)
.for_each(print_texture);
self.got_def(DefType::NodeDef);
}
ItemDefs { defs, .. } => {
defs.iter()
.flat_map(|def| {
[
&def.inventory_image,
&def.wield_image,
&def.inventory_overlay,
&def.wield_overlay,
]
})
.for_each(print_texture);
self.got_def(DefType::ItemDef);
}
Kick(reason) => {
eprintln!("kicked: {reason}");
}
_ => {}
}
}
}
#[tokio::main]
async fn main() {
let Args {
address,
quit_after_seconds,
quit_after_defs,
username,
password,
} = Args::parse();
let (tx, mut rx, worker) = mt_net::connect(&address).await.unwrap();
let mut bot = Bot {
auth: Auth::new(tx.clone(), username, password, "en_US"),
conn: tx,
quit_after_defs,
pending: EnumSet::all(),
};
let worker = tokio::spawn(worker.run());
let mut quit_sleep: Option<Pin<Box<Sleep>>> = quit_after_seconds.and_then(|x| {
if x >= 0.0 {
Some(Box::pin(sleep(Duration::from_secs_f32(x))))
} else {
None
}
});
loop {
tokio::select! {
pkt = rx.recv() => match pkt {
None => break,
Some(Err(e)) => eprintln!("{e}"),
Some(Ok(pkt)) => bot.handle_pkt(pkt).await,
},
_ = bot.auth.poll() => {
bot.conn
.send(&ToSrvPkt::CltReady {
major: 0,
minor: 0,
patch: 0,
reserved: 0,
version: "https://github.com/LizzyFleckenstein03/texmodbot".into(),
formspec: 4,
})
.await
.unwrap();
}
Some(_) = OptionFuture::from(quit_sleep.as_mut()) => {
bot.conn.close();
}
_ = tokio::signal::ctrl_c() => {
bot.conn.close();
}
}
}
worker.await.unwrap();
}
|