mist 1.19.0

minimal, improved speedrun timer
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::keybinds::{Binding, Keybinds};
#[cfg(feature = "plugins")]
use crate::plugin::{plugin_task, PluginErr, PluginErrTy, PluginMsg};
use crate::render::RenderState;
use anyhow::{bail, Result};
use mist_core::{
    config::Config,
    dialogs,
    error::MistError::Str,
    timer::{
        dump::StateDump,
        state::{RunState, RunUpdate, StateChangeRequest},
        Run,
    },
};
use sdl2::{
    event::{Event, WindowEvent},
    keyboard::Keycode,
    ttf::Font,
};
#[cfg(feature = "icon")]
use sdl2::{image::ImageRWops, rwops::RWops};
#[cfg(feature = "plugins")]
use std::sync::mpsc::{self, Receiver, SyncSender};
use std::{
    cell::RefCell,
    env,
    fs::File,
    path::PathBuf,
    rc::Rc,
    thread,
    time::{Duration, Instant},
};

pub struct App<'a, 'b> {
    context: sdl2::Sdl,
    run: Rc<RefCell<Run>>,
    ren_state: RenderState<'a, 'b>,
    run_state: RunState,
    config: Config,
    msf: PathBuf,
    no_file: bool,
    #[cfg(feature = "plugins")]
    plugin_tx: SyncSender<PluginMsg>,
    #[cfg(feature = "plugins")]
    scrq_rx: Receiver<StateChangeRequest>,
    #[cfg(feature = "plugins")]
    plug_err_rx: Receiver<PluginErr>,
}

static ONE_SIXTIETH: Duration = Duration::new(0, 1_000_000_000 / 60);

impl<'a, 'b> App<'a, 'b> {
    pub fn init(
        context: sdl2::Sdl,
        timer_font: Font<'b, 'a>,
        splits_font: Font<'b, 'a>,
    ) -> Result<Self> {
        let video = context.video().map_err(Str)?;
        let mut config = match Config::open() {
            Ok(c) => c,
            Err(e) => {
                bail!(e);
            }
        };
        let mut window = video
            .window("mist", config.win_size().0, config.win_size().1)
            .position_centered()
            .resizable()
            .build()?;
        #[cfg(feature = "icon")]
        {
            let rw = RWops::from_bytes(include_bytes!("../assets/MIST.png")).unwrap(); // only fails if buffer size 0
            window.set_icon(rw.load_png().map_err(Str)?);
        }

        let mut canvas = window.into_canvas().build()?;
        let cmd_path = env::args().nth(1);
        let not_exist = if let Some(ref x) = cmd_path {
            !PathBuf::from(x).exists()
        } else {
            false
        };
        let mut path = if cmd_path.is_none() || not_exist {
            if not_exist {
                println!(
                    "File {} does not exist; falling back to configuration file!",
                    cmd_path.unwrap()
                );
            }
            if let Some(x) = config.file() {
                x.to_owned()
            } else {
                match dialogs::get_run_path() {
                    Some(x) => x.into(),
                    None => PathBuf::new(),
                }
            }
        } else {
            cmd_path.unwrap().into()
        };
        let (run, no_file) = loop {
            if path == PathBuf::new() {
                break (Run::empty(), true);
            } else {
                let msf = File::open(&path)?;
                match Run::from_reader_msf(msf) {
                    Ok(r) => {
                        config.set_file(&path);
                        break (r, false);
                    }
                    Err(e) => {
                        if !dialogs::try_again(e) {
                            break (Run::empty(), true);
                        }
                    }
                }
            }
            path = match dialogs::get_run_path() {
                Some(x) => x.into(),
                None => PathBuf::new(),
            }
        };

        #[cfg(feature = "plugins")]
        let (plugin_tx, scrq_rx, plug_err_rx) = {
            let (plugin_tx, plugin_rx) = mpsc::sync_channel(1);
            let (scrq_tx, scrq_rx) = mpsc::channel();
            let (plug_err_tx, plug_err_rx) = mpsc::channel();
            if config.plugins() {
                let r2 = run.clone();
                let _ = thread::Builder::new()
                    .name("plugins".into())
                    .spawn(move || plugin_task(r2, plugin_rx, scrq_tx, plug_err_tx));
            }
            (plugin_tx, scrq_rx, plug_err_rx)
        };

        let run = Rc::new(RefCell::new(run));

        canvas.window_mut().set_title(&format!(
            "mist: {} ({})",
            run.borrow().game_title(),
            run.borrow().category(),
        ))?;

        let app = App {
            context,
            ren_state: RenderState::new(Rc::clone(&run), canvas, &config, timer_font, splits_font)?,
            run_state: RunState::new(Rc::clone(&run)),
            config,
            msf: path,
            run,
            no_file,
            #[cfg(feature = "plugins")]
            plugin_tx,
            #[cfg(feature = "plugins")]
            scrq_rx,
            #[cfg(feature = "plugins")]
            plug_err_rx,
        };

        Ok(app)
    }

    pub fn run(mut self) -> Result<()> {
        // framerate cap timer
        let mut frame_time: Instant;
        let mut binds = Keybinds::from_raw(self.config.binds())?;
        let mut state_change_queue = vec![];
        let mut update: RunUpdate;
        let mut ev_pump = self.context.event_pump().map_err(Str)?;

        // main loop
        'running: loop {
            frame_time = Instant::now();
            // repeat stuff in here for every event that occured between frames
            // in order to properly respond to them
            for event in ev_pump.poll_iter() {
                // print events to terminal if running in debug
                #[cfg(debug_assertions)]
                println!("{:?}", event);

                match event {
                    // quit program on esc or being told by wm to close
                    Event::Quit { .. }
                    | Event::KeyDown {
                        keycode: Some(Keycode::Escape),
                        ..
                    } if !self.config.confirm() || dialogs::confirm_exit() => break 'running,

                    Event::MouseWheel { y, .. } => {
                        self.ren_state.scroll(y);
                    }

                    Event::MouseButtonDown { mouse_btn: m, .. } => {
                        let m = m.into();
                        if binds.load_config == m && !self.run_state.is_running() {
                            match Config::open() {
                                Ok(c) => {
                                    self.config = c;
                                    self.ren_state = self.ren_state.reload_config(&self.config)?;
                                    binds = Keybinds::from_raw(self.config.binds())?;
                                }
                                Err(e) => {
                                    dialogs::fail(e);
                                }
                            }
                        } else {
                            state_change_queue.push(self.handle_input(m, &binds)?);
                        }
                    }
                    Event::KeyDown {
                        keycode: Some(k),
                        repeat: false,
                        ..
                    } => {
                        let k = k.into();
                        if binds.load_config == k && !self.run_state.is_running() {
                            match Config::open() {
                                Ok(c) => {
                                    self.config = c;
                                    self.ren_state = self.ren_state.reload_config(&self.config)?;
                                    binds = Keybinds::from_raw(self.config.binds())?;
                                }
                                Err(e) => {
                                    dialogs::fail(e);
                                }
                            }
                        } else {
                            state_change_queue.push(self.handle_input(k, &binds)?);
                        }
                    }

                    Event::Window {
                        win_event: WindowEvent::Resized(_, y),
                        ..
                    } => {
                        self.ren_state.win_resize(y as u32);
                    }
                    _ => {}
                }
            }
            #[cfg(feature = "plugins")]
            while let Ok(rq) = self.scrq_rx.try_recv() {
                state_change_queue.push(rq);
            }
            #[cfg(feature = "plugins")]
            while let Ok(e) = self.plug_err_rx.try_recv() {
                dialogs::error(&match e.ty {
                    PluginErrTy::Filesystem => format!("Could not load plugin file {}!", e.plugin),
                    PluginErrTy::WrongVersion(v) => {
                        let ve = version_to_readable(mist_pdk::pdk_version());
                        let vf = version_to_readable(v);
                        format!(
                            "Plugin {} version mismatch: expected {}.{}, found {}.{}!",
                            e.plugin, ve.0, ve.1, vf.0, vf.1
                        )
                    }
                    PluginErrTy::MissingSym(s) => format!(
                        "Could not load necessary symbol {} from plugin {}!",
                        s, e.plugin
                    ),
                });
            }
            update = self.run_state.update(&state_change_queue[..]);
            state_change_queue.clear();
            self.ren_state.update(&update)?;
            #[cfg(feature = "plugins")]
            let _ = self.plugin_tx.try_send(PluginMsg::Update(update));
            self.ren_state.render()?;
            if Instant::now().duration_since(frame_time) <= ONE_SIXTIETH {
                thread::sleep(
                    // if the entire loop pass was completed in under 1/60 second, delay to keep the framerate at ~60fps
                    ONE_SIXTIETH - Instant::now().duration_since(frame_time),
                );
            }
        }
        self.config.set_win_size(self.ren_state.win_size());
        self.config.save()?;
        // if splits were updated, prompt user to save the split file
        if (self.run_state.needs_save() || self.no_file) && dialogs::save_check() {
            if self.no_file || self.msf == PathBuf::new() {
                let p = dialogs::get_save_as();
                if let Some(s) = p {
                    self.msf = s.into();
                    let f = File::options()
                        .write(true)
                        .create(true)
                        .truncate(true)
                        .open(self.msf)?;
                    self.run.borrow().to_writer(f)?;
                    self.no_file = false;
                }
            } else {
                let f = File::options()
                    .write(true)
                    .create(true)
                    .truncate(true)
                    .open(self.msf)?;
                self.run.borrow().to_writer(f)?;
            }
        }
        Ok(())
    }

    fn handle_input(&mut self, k: Binding, binds: &Keybinds) -> Result<StateChangeRequest> {
        if binds.start_split == k {
            Ok(StateChangeRequest::Split)
        } else if binds.pause == k {
            Ok(StateChangeRequest::Pause)
        } else if binds.reset == k {
            Ok(StateChangeRequest::Reset)
        } else if binds.prev_comp == k {
            Ok(StateChangeRequest::PrevComparison)
        } else if binds.next_comp == k {
            Ok(StateChangeRequest::NextComparison)
        } else if binds.un_split == k {
            Ok(StateChangeRequest::Unsplit)
        } else if binds.skip_split == k {
            Ok(StateChangeRequest::Skip)
        } else if !self.run_state.is_running() {
            if binds.load_splits == k {
                // save the previous run if it was updated
                if (self.run_state.needs_save() || self.no_file) && dialogs::save_check() {
                    if self.no_file || self.msf == PathBuf::new() {
                        let p = dialogs::get_save_as();
                        if let Some(s) = p {
                            self.msf = s.into();
                            let f = File::options()
                                .write(true)
                                .create(true)
                                .truncate(true)
                                .open(&self.msf)?;
                            self.run.borrow().to_writer(f)?;
                            self.no_file = false;
                        }
                    } else {
                        let f = File::options()
                            .write(true)
                            .create(true)
                            .truncate(true)
                            .open(&self.msf)?;
                        self.run.borrow().to_writer(f)?;
                    }
                }
                // open a file dialog to get a new split file + run
                // if the user cancelled, do nothing
                while let Some(x) = dialogs::get_run_path() {
                    self.msf = x.into();
                    let f = File::open(&self.msf)?;
                    match Run::from_reader_msf(f) {
                        Ok(r) => {
                            #[cfg(feature = "plugins")]
                            if self.config.plugins() {
                                let _ = self.plugin_tx.send(PluginMsg::Shutdown);
                                let (plugin_tx, plugin_rx) = mpsc::sync_channel(1);
                                let (scrq_tx, scrq_rx) = mpsc::channel();
                                let (plug_err_tx, plug_err_rx) = mpsc::channel();
                                let r2 = r.clone();
                                let _ = thread::Builder::new()
                                    .name("plugins".into())
                                    .spawn(|| plugin_task(r2, plugin_rx, scrq_tx, plug_err_tx));
                                self.plugin_tx = plugin_tx;
                                self.scrq_rx = scrq_rx;
                                self.plug_err_rx = plug_err_rx;
                            };
                            self.run.replace(r);
                            self.config.set_file(&self.msf);
                            self.run_state = RunState::new(Rc::clone(&self.run));
                            self.ren_state.reload_run()?;
                            break;
                        }
                        Err(e) => {
                            if !dialogs::try_again(e) {
                                break;
                            }
                        }
                    }
                }
                Ok(StateChangeRequest::None)
            } else if binds.dump_state == k {
                if let Some(p) = dialogs::get_dump_save() {
                    let d = self.run_state.create_state_dump();
                    d.write(p)?;
                }
                Ok(StateChangeRequest::None)
            } else if binds.load_state == k {
                while let Some(p) = dialogs::get_dump_path() {
                    match StateDump::open(p) {
                        Ok(d) => {
                            self.run_state.read_dump(&d);
                            self.ren_state.read_dump(&d)?;
                            break;
                        }
                        Err(e) => {
                            if !dialogs::try_again(e) {
                                break;
                            }
                        }
                    }
                }
                Ok(StateChangeRequest::None)
            } else {
                Ok(StateChangeRequest::None)
            }
        } else {
            Ok(StateChangeRequest::None)
        }
    }
}

fn version_to_readable(v: u16) -> (u8, u8) {
    (((v >> 8) & 0xFF) as u8, (v & 0xFF) as u8)
}