summaryrefslogtreecommitdiff
path: root/src/app/load.rs
blob: 24a2385f745ed5386134511b179dc1ede5243dfc (plain) (blame)
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
/*
	Copyright 2021-2023 Gabriel Jensen.

	This file is part of Luma.

	Luma is free software: you can redistribute it
	and/or modify it under the terms of the GNU
	Affero General Public License as published by
	the Free Software Foundation, either version 3
	of the License, or (at your option) any later
	version.

	Luma is distributed in the hope that it will be
	useful, but WITHOUT ANY WARRANTY; without even
	the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU
	Affero General Public License for more details.

	You should have received a copy of the GNU
	Affero General Public License along with Luma.
	If not, see <https://www.gnu.org/licenses/>.
*/

use crate::VERSION;
use crate::app::App;
use crate::state::State;

use std::fs::File;
use std::io::Read;

impl App {
	pub fn load(&mut self, state: &mut State) -> Result<(), String> {
		eprintln!("loading booatloader \"{}\"", self.bootloader);

		let mut bootloader = match File::open(&self.bootloader) {
			Ok(file) => file,
			_        => return Err("unable to open bootloader".to_string()),
		};

		if let Err(..) = bootloader.read(state.bootloader_buffer()) { return Err("unable to read bootloader".to_string()) };

		eprintln!("loading image \"{}\"", self.image);

		let mut image = match File::open(&self.image) {
			Ok(file) => file,
			_        => return Err("unable to open image".to_string()),
		};

		match image.read(state.image_buffer()) {
			Err(..) => return Err("unable to read image".to_string()),
			_       => {},
		};

		let title   = get_title(&state.image_buffer()[0xA0..0xAC]);
		let id      = get_id(&state.image_buffer()[0xAC..0xB0]);
		let version = state.image_buffer()[0xBC];

		eprintln!("loaded image \"{title}\" ({id}) v.{version}");

		self.canvas.window_mut().set_title(&format!("Luma {:X}.{:X} - {title}", VERSION.0, VERSION.1)).unwrap();

		return Ok(());
	}
}

fn get_title(data: &[u8]) -> String {
	let mut title = String::with_capacity(0xC);

	for raw in data {
		let character = match char::from_u32(*raw as u32) {
			Some('\u{0000}') => break,
			Some(character)  => character,
			None             => '?',
		};

		title.push(character);
	}

	return title;
}

fn get_id(data: &[u8]) -> String {
	let mut id = String::with_capacity(0xC);

	for raw in data {
		let character = match char::from_u32(*raw as u32) {
			Some('\u{0000}') => break,
			Some(character)  => character,
			None             => '?',
		};

		id.push(character);
	}

	return id;
}