summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 13ef5186f25d6827e63177b0e0ff839bdd9e34da (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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::collections::HashSet;
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>,
    has: HashSet<String>,
}

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 mut print_texture = |tex: &String| {
            if !tex.is_empty() && self.has.insert(tex.clone()) {
                println!("{tex}");
            }
        };

        fn print_texmod(tex: &String, print_texture: &mut impl FnMut(&String)) {
            if !tex.is_empty() {
                print_texture(&format!("blank.png{tex}"));
            }
        }

        let print_obj_msg = |msg: &mt_net::ObjMsg| {
            use mt_net::ObjMsg::*;
            match msg {
                TextureMod { texture_mod } => print_texmod(texture_mod, &mut print_texture),
                Props(props) => {
                    props.textures.iter().for_each(&mut print_texture);
                    print_texmod(&props.dmg_texture_mod, &mut print_texture);
                }
                _ => {}
            }
        };

        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);
            }
            ObjMsgs { msgs } => {
                msgs.iter().map(|x| &x.msg).for_each(print_obj_msg);
            }
            ObjRemoveAdd { add, .. } => {
                add.iter()
                    .flat_map(|x| x.init_data.msgs.iter())
                    .map(|x| &x.0)
                    .for_each(print_obj_msg);
            }
            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(),
        has: HashSet::new(),
    };

    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();
}