Skip to content
This repository was archived by the owner on Dec 13, 2025. It is now read-only.

Commit 272ddd5

Browse files
committed
Move global livery from thread_local into a mutex
1 parent 5e2cb62 commit 272ddd5

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/loadout.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use xplm::data::borrowed::DataRef;
1212
use xplm::data::{ArrayRead, ArrayReadWrite, ReadWrite, StringRead};
1313

1414
use crate::plugin::{AircraftModel, PluginError};
15-
use crate::plugin::{
16-
GLOBAL_LIVERY, LOADOUT_FILENAME, NAME, PLUGIN_OUTPUT_PATH, XPLANE_OUTPUT_PATH,
17-
};
15+
use crate::plugin::{LIVERY, LOADOUT_FILENAME, NAME, PLUGIN_OUTPUT_PATH, XPLANE_OUTPUT_PATH};
1816

1917
// Light switch indices for different equipment configururations
2018
const AUTOTHROTTLE: usize = 49;
@@ -47,7 +45,9 @@ impl LoadoutFile {
4745
let mut output_file_path = Self::acf_livery_path()?;
4846

4947
// Update "old" livery
50-
GLOBAL_LIVERY.with(|path| *path.borrow_mut() = output_file_path.clone());
48+
if let Ok(mut mutex) = LIVERY.lock() {
49+
*mutex = Some(output_file_path.clone());
50+
}
5151

5252
output_file_path.push(LOADOUT_FILENAME);
5353

@@ -111,7 +111,7 @@ impl LoadoutFile {
111111
} else {
112112
// Set up a valid livery path.
113113
let acf_livery_path = PathBuf::from(acf_livery_path.as_str());
114-
if let Some(livery_path) = acf_livery_path.components().last() {
114+
if let Some(livery_path) = acf_livery_path.components().next_back() {
115115
output_file_path.push(livery_path)
116116
} else {
117117
debugln!(

src/plugin.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Copyright (c) 2024 telephono
22
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
33

4-
use std::cell::RefCell;
54
use std::ffi::CStr;
65
use std::os::raw::{c_char, c_int, c_void};
76
use std::path::PathBuf;
7+
use std::sync::Mutex;
88

99
use thiserror::Error;
1010

@@ -27,9 +27,7 @@ pub static XPLANE_OUTPUT_PATH: &str = "Output";
2727
pub static PLUGIN_OUTPUT_PATH: &str = "B720";
2828
pub static LOADOUT_FILENAME: &str = "persistent-loadout.json";
2929

30-
thread_local! {
31-
pub static GLOBAL_LIVERY: RefCell<PathBuf> = RefCell::new(PathBuf::new());
32-
}
30+
pub static LIVERY: Mutex<Option<PathBuf>> = Mutex::new(None);
3331

3432
#[derive(Error, Debug)]
3533
pub enum PluginError {
@@ -137,17 +135,17 @@ impl Plugin for PersistentLoadoutPlugin {
137135
return;
138136
}
139137

140-
GLOBAL_LIVERY.with(|path| {
141-
let old_livery_path = (*path.borrow()).clone();
138+
if let Ok(mut mutex) = LIVERY.lock() {
139+
let old_livery_path = mutex.as_ref().cloned();
142140

143141
// Ignore on first run...
144-
if old_livery_path.as_os_str().is_empty() {
142+
if old_livery_path.is_none() {
145143
return;
146144
}
147145

148146
// Get new livery path
149147
let new_livery_path = match LoadoutFile::acf_livery_path() {
150-
Ok(path) => path,
148+
Ok(path) => Some(path),
151149
Err(error) => {
152150
debugln!("{NAME} something went wrong: {error}");
153151
return;
@@ -156,14 +154,16 @@ impl Plugin for PersistentLoadoutPlugin {
156154

157155
// Compare old and new livery path
158156
// Nothing to do if they are the same...
159-
if old_livery_path.as_os_str() == new_livery_path.as_os_str() {
157+
if old_livery_path == new_livery_path {
160158
return;
161159
}
162160

163161
debugln!("{NAME} livery change detected");
164162

165163
// Save loadout for old livery...
166-
let old_loadout = match LoadoutFile::with_livery_path(old_livery_path.as_os_str()) {
164+
let old_loadout = match LoadoutFile::with_livery_path(
165+
old_livery_path.as_ref().unwrap().as_os_str(),
166+
) {
167167
Ok(loadout) => loadout,
168168
Err(error) => {
169169
debugln!("{NAME} something went wrong: {error}");
@@ -177,7 +177,9 @@ impl Plugin for PersistentLoadoutPlugin {
177177
}
178178

179179
// Restore loadout for new livery...
180-
let new_loadout = match LoadoutFile::with_livery_path(new_livery_path.as_os_str()) {
180+
let new_loadout = match LoadoutFile::with_livery_path(
181+
new_livery_path.as_ref().unwrap().as_os_str(),
182+
) {
181183
Ok(loadout) => loadout,
182184
Err(error) => {
183185
debugln!("{NAME} something went wrong: {error}");
@@ -191,8 +193,8 @@ impl Plugin for PersistentLoadoutPlugin {
191193
};
192194

193195
// Update "old" livery
194-
*path.borrow_mut() = new_livery_path;
195-
});
196+
*mutex = new_livery_path;
197+
};
196198
}
197199
}
198200
}

0 commit comments

Comments
 (0)