Skip to content

Commit 9750cac

Browse files
committed
docs/refactor: clarify database loading logic
1 parent 91d97de commit 9750cac

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/wasm/src/dispatcher.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, error::Error, path::Path, rc::Rc};
1+
use std::{cell::RefCell, path::Path, rc::Rc};
22

33
use msfs::{commbus::*, network::NetworkRequestState, sys::sGaugeDrawData, MSFSEvent};
44
use navigation_database::database::Database;
@@ -114,33 +114,34 @@ impl<'a> Dispatcher<'a> {
114114
self.downloader.acknowledge_download();
115115
}
116116
}
117-
118117
fn load_database(&mut self) {
119118
println!("[NAVIGRAPH] Loading database");
120119

120+
// Go through logic to determine which database to load
121+
121122
// Are we bundled? None means we haven't installed anything yet
122123
let is_bundled = meta::get_internal_state()
123124
.map(|internal_state| Some(internal_state.is_bundled))
124125
.unwrap_or(None);
125126

126-
// Get the installed cycle
127+
// Get the installed cycle (if it exists)
127128
let installed_cycle = match meta::get_installed_cycle_from_json(
128129
&Path::new(consts::NAVIGATION_DATA_WORK_LOCATION).join("cycle.json"),
129130
) {
130131
Ok(cycle) => Some(cycle.cycle),
131132
Err(_) => None,
132133
};
133134

134-
// Get the bundled cycle
135+
// Get the bundled cycle (if it exists)
135136
let bundled_cycle = match meta::get_installed_cycle_from_json(
136137
&Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json"),
137138
) {
138139
Ok(cycle) => Some(cycle.cycle),
139140
Err(_) => None,
140141
};
141142

143+
// Determine if we are bundled ONLY and the bundled cycle is newer than the installed (old bundled) cycle
142144
let bundled_updated = if is_bundled.is_some() && is_bundled.unwrap() {
143-
// If we are bundled, we need to check if the bundled cycle is newer than the installed cycle
144145
if installed_cycle.is_some() && bundled_cycle.is_some() {
145146
bundled_cycle.unwrap() > installed_cycle.unwrap()
146147
} else {
@@ -150,16 +151,17 @@ impl<'a> Dispatcher<'a> {
150151
false
151152
};
152153

154+
// If there is no addon config, we can assume that we need to copy the bundled database to the work location
153155
let need_to_copy = is_bundled.is_none();
154156

155157
// If we are bundled and the installed cycle is older than the bundled cycle, we need to copy the bundled database to the work location. Or if we haven't installed anything yet, we need to copy the bundled database to the work location
156158
if bundled_updated || need_to_copy {
157-
// we need to copy to the work location
158159
match util::copy_files_to_folder(
159160
&Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION),
160161
&Path::new(consts::NAVIGATION_DATA_WORK_LOCATION),
161162
) {
162163
Ok(_) => {
164+
// Set the internal state to bundled
163165
let res = meta::set_internal_state(InternalState { is_bundled: true });
164166
if let Err(e) = res {
165167
println!("[NAVIGRAPH] Failed to set internal state: {}", e);
@@ -176,22 +178,17 @@ impl<'a> Dispatcher<'a> {
176178
}
177179

178180
// Finally, set the active database
179-
let found_work = self
180-
.set_database_if_exists(consts::NAVIGATION_DATA_WORK_LOCATION)
181-
.is_ok();
182-
183-
if found_work {
184-
println!("[NAVIGRAPH] Loaded database");
185-
} else {
186-
println!("[NAVIGRAPH] Failed to load database");
187-
}
188-
}
189-
190-
fn set_database_if_exists(&mut self, path: &str) -> Result<(), Box<dyn Error>> {
191-
if path_exists(&Path::new(path)) {
192-
self.database.set_active_database(path.to_owned())
181+
if path_exists(&Path::new(consts::NAVIGATION_DATA_WORK_LOCATION)) {
182+
match self.database.set_active_database(consts::NAVIGATION_DATA_WORK_LOCATION.to_owned()) {
183+
Ok(_) => {
184+
println!("[NAVIGRAPH] Loaded database");
185+
},
186+
Err(e) => {
187+
println!("[NAVIGRAPH] Failed to load database: {}", e);
188+
},
189+
}
193190
} else {
194-
Err("Path does not exist".into())
191+
println!("[NAVIGRAPH] Failed to load database: there is no installed database");
195192
}
196193
}
197194

0 commit comments

Comments
 (0)