Skip to content

Commit 80b32c3

Browse files
committed
fix: remove dependency on manifest.json and layout.json
1 parent 0be8293 commit 80b32c3

File tree

3 files changed

+20
-81
lines changed

3 files changed

+20
-81
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ _PackageInt
44
.rollup.cache
55
tsconfig.tsbuildinfo
66
.vs
7+
examples/aircraft/NavigationDataInterfaceAircraftProject.xml.user
78
examples/aircraft/PackageSources/html_ui/Pages/VCockpit/Instruments/Navigraph/NavigationDataInterfaceSample
89
examples/aircraft/PackageSources/SimObjects/Airplanes/Navigraph_Navigation_Data_Interface_Aircraft/panel/msfs_navigation_data_interface.wasm
910
out
@@ -32,4 +33,4 @@ target/
3233
.DS_Store
3334

3435
test_work/
35-
dist
36+
dist

src/wasm/src/database/mod.rs

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use once_cell::sync::Lazy;
66
use serde::Deserialize;
77
use std::{
88
cmp::Ordering,
9-
fs::{self, File},
9+
fs::{self, read_dir, File},
1010
path::{Path, PathBuf},
1111
sync::Mutex,
1212
};
@@ -45,79 +45,40 @@ pub const WORK_NAVIGATION_DATA_FOLDER: &str = "\\work/NavigationData";
4545
pub const WORK_CYCLE_JSON_PATH: &str = "\\work/NavigationData/cycle.json";
4646
/// The path to the "master" SQLite DB
4747
pub const WORK_DB_PATH: &str = "\\work/NavigationData/db.s3db";
48-
/// The path to the layout.json in the addon folder
49-
pub const LAYOUT_JSON: &str = ".\\layout.json";
5048
/// The folder name for bundled navigation data
51-
pub const BUNDLED_FOLDER_NAME: &str = "NavigationData";
49+
pub const BUNDLED_FOLDER_NAME: &str = ".\\NavigationData";
5250

5351
/// The global exported database state
5452
pub static DATABASE_STATE: Lazy<Mutex<DatabaseState>> =
55-
Lazy::new(|| Mutex::new(DatabaseState::new().unwrap())); // SAFETY: the only way this function can return an error is if layout.json is corrupt (which is impossible since the package wouldn't even mount), or if copying to the work folder is failing (in which case we have more fundamental problems). So overall, unwrapping here is safe
56-
57-
/// An entry in the layout.json file
58-
#[derive(Deserialize)]
59-
struct LayoutEntry {
60-
path: String,
61-
}
62-
63-
/// The representation of the layout.json file
64-
#[derive(Deserialize)]
65-
struct LayoutJson {
66-
content: Vec<LayoutEntry>,
67-
}
53+
Lazy::new(|| Mutex::new(DatabaseState::new().unwrap())); // SAFETY: the only way this function can return an error is if there is an IO error which is impossible unless the user has messed up work folder permissions (which would make interface not work anyways)
6854

6955
/// Find the bundled navigation data distribution
7056
fn get_bundled_db() -> Result<Option<DatabaseDistributionInfo>> {
71-
// Since we don't know the exact filenames of the bundled navigation data,
72-
// we need to find them through the layout.json file. In a perfect world,
73-
// we would just enumerate the bundled directory. However, fd_readdir is unreliable in the sim.
74-
let mut layout = fs::read_to_string(LAYOUT_JSON)?;
75-
let parsed = serde_json::from_str::<LayoutJson>(&mut layout)?;
76-
77-
// Filter out the files in the layout that are not in the bundled folder
78-
let bundled_files = parsed
79-
.content
80-
.iter()
81-
.filter_map(|e| {
82-
let path = Path::new(&e.path);
83-
84-
// Get parent
85-
let (Some(parent), Some(filename)) = (path.parent(), path.file_name()) else {
86-
return None;
87-
};
88-
89-
// Ensure the file is within our known bundled data path
90-
if parent != Path::new(BUNDLED_FOLDER_NAME) {
91-
return None;
92-
};
93-
94-
// Finally, return just the basename
95-
filename.to_str()
96-
})
57+
let bundled_entries = read_dir(BUNDLED_FOLDER_NAME)?
58+
.filter_map(Result::ok)
9759
.collect::<Vec<_>>();
9860

99-
// Try extracting the cycle info and DB files
100-
let cycle_info = if let Some(file) = bundled_files
61+
// Try finding cycle.json
62+
let Some(cycle_file_name) = bundled_entries
10163
.iter()
102-
.find(|f| f.to_lowercase().ends_with(".json"))
103-
{
104-
file
105-
} else {
64+
.filter_map(|e| e.file_name().to_str().map(|s| s.to_owned()))
65+
.find(|e| *e == String::from("cycle.json"))
66+
else {
10667
return Ok(None);
10768
};
10869

109-
let db_file = if let Some(file) = bundled_files
70+
// Try finding the DB (we don't know the full filename, only extension)
71+
let Some(db_file_name) = bundled_entries
11072
.iter()
111-
.find(|f| f.to_lowercase().ends_with(".s3db"))
112-
{
113-
file
114-
} else {
73+
.filter_map(|e| e.file_name().to_str().map(|s| s.to_owned()))
74+
.find(|e| e.ends_with(".s3db"))
75+
else {
11576
return Ok(None);
11677
};
11778

11879
Ok(Some(DatabaseDistributionInfo::new(
119-
Path::new(&format!(".\\{BUNDLED_FOLDER_NAME}\\{cycle_info}")), // We need to reconstruct the bundled path to include the proper syntax to reference non-work folder files
120-
Path::new(&format!(".\\{BUNDLED_FOLDER_NAME}\\{db_file}")),
80+
Path::new(&format!(".\\{BUNDLED_FOLDER_NAME}\\{cycle_file_name}")), // We need to reconstruct the bundled path to include the proper syntax to reference non-work folder files
81+
Path::new(&format!(".\\{BUNDLED_FOLDER_NAME}\\{db_file_name}")),
12182
)?))
12283
}
12384

src/wasm/src/sentry_gauge.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ use sentry::integrations::anyhow::capture_anyhow;
1515
use serde::{Deserialize, Serialize};
1616
use uuid::Uuid;
1717

18-
/// The path to the manifest.json in the addon folder
19-
const MANIFEST_FILE_PATH: &str = ".\\manifest.json";
20-
2118
/// The path to the sentry persistent state file
2219
const SENTRY_FILE: &str = "\\work/ng_sentry.json";
2320

@@ -242,39 +239,19 @@ where
242239
},
243240
));
244241

245-
// In order to track what addon the reports are coming from, we need to parse the manifest.json file to extract relevant info
246-
let manifest = {
247-
#[derive(Deserialize)]
248-
struct Manifest {
249-
title: String,
250-
creator: String,
251-
package_version: String,
252-
}
253-
let manifest_file = File::open(MANIFEST_FILE_PATH)?;
254-
serde_json::from_reader::<_, Manifest>(manifest_file)?
255-
};
256-
257242
// Get the user ID from persistent state
258243
let user_id = SENTRY_STATE
259244
.try_lock()
260245
.map_err(|_| anyhow!("Unable to lock SENTRY_STATE"))?
261246
.user_id
262247
.to_string();
263248

264-
// Configure the sentry scope to report the user ID and plugin info loaded from manifest.json
249+
// Configure the sentry scope to report the user ID
265250
sentry::configure_scope(|scope| {
266251
scope.set_user(Some(sentry::User {
267252
id: Some(user_id),
268253
..Default::default()
269254
}));
270-
271-
scope.set_tag(
272-
"plugin",
273-
format!(
274-
"{}/{}@{}",
275-
manifest.creator, manifest.title, manifest.package_version
276-
),
277-
);
278255
});
279256

280257
// Drain any pending reports. We need to structure it like this as opposed to just a top level `let Ok(state) = ...`` due to the fact we should not be holding a MutexGuard across an await point

0 commit comments

Comments
 (0)