Skip to content

Commit d51e5cb

Browse files
committed
Merge branch 'bundled-navigation-data' into chore/ui-improvements
2 parents 41e2fea + b8f6902 commit d51e5cb

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If you bundle outdated navigation data in your aircraft and you want this module
3636

3737
## Where is the Navigation Data Stored?
3838

39-
The default location for navigation data is `work/NavigationData`. If you have bundled navigation data, its located in the `NavigationData` folder in the root of your project.
39+
The default location for navigation data is `work/NavigationData`. If you have bundled navigation data, its located in the `NavigationData` folder in the root of your project. (although it gets copied into the `work` directory at runtime)
4040

4141
## Building the Sample Aircraft
4242

src/test/setup.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ let wasmFunctionTable: WebAssembly.Table // The table of callback functions in t
157157
* Maps request ids to a tuple of the returned data's pointer, and the data's size
158158
*/
159159
const promiseResults = new Map<bigint, [number, number]>()
160-
const failedRequests: bigint[] = [];
160+
const failedRequests: bigint[] = []
161161

162162
wasmInstance = new WebAssembly.Instance(wasmModule, {
163163
wasi_snapshot_preview1: wasiSystem.wasiImport,
@@ -223,20 +223,20 @@ wasmInstance = new WebAssembly.Instance(wasmModule, {
223223
func(requestId, 200, ctx)
224224
})
225225
.catch(err => {
226-
failedRequests.push(requestId);
226+
failedRequests.push(requestId)
227227
})
228228

229229
return requestId
230230
},
231231
fsNetworkHttpRequestGetState: (requestId: bigint) => {
232-
if(failedRequests.includes(requestId)) {
232+
if (failedRequests.includes(requestId)) {
233233
return 4 // FS_NETWORK_HTTP_REQUEST_STATE_FAILED
234234
}
235-
if(promiseResults.has(requestId)) {
235+
if (promiseResults.has(requestId)) {
236236
return 3 // FS_NETWORK_HTTP_REQUEST_STATE_DATA_READY
237237
}
238238
return 2 // FS_NETWORK_HTTP_REQUEST_STATE_WAITING_FOR_DATA
239-
}
239+
},
240240
},
241241
}) as WasmInstance
242242

@@ -290,6 +290,15 @@ beforeAll(async () => {
290290
throw new Error("Please specify the env var `NAVIGATION_DATA_SIGNED_URL`")
291291
}
292292

293+
// Utility function to convert onReady to a promise
294+
const waitForReady = (navDataInterface: NavigraphNavigationDataInterface): Promise<void> => {
295+
return new Promise((resolve, _reject) => {
296+
navDataInterface.onReady(() => resolve())
297+
})
298+
}
299+
300+
await waitForReady(navigationDataInterface)
301+
293302
await navigationDataInterface.download_navigation_data(downloadUrl)
294303
}, 30000)
295304

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)