From 272ddd5a7344bf5ffaebd4d592f29b8207ff2e8d Mon Sep 17 00:00:00 2001 From: telephono Date: Tue, 5 Aug 2025 22:04:13 +0200 Subject: [PATCH] Move global livery from thread_local into a mutex --- src/loadout.rs | 10 +++++----- src/plugin.rs | 28 +++++++++++++++------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/loadout.rs b/src/loadout.rs index cd7e0d0..d0a9b71 100644 --- a/src/loadout.rs +++ b/src/loadout.rs @@ -12,9 +12,7 @@ use xplm::data::borrowed::DataRef; use xplm::data::{ArrayRead, ArrayReadWrite, ReadWrite, StringRead}; use crate::plugin::{AircraftModel, PluginError}; -use crate::plugin::{ - GLOBAL_LIVERY, LOADOUT_FILENAME, NAME, PLUGIN_OUTPUT_PATH, XPLANE_OUTPUT_PATH, -}; +use crate::plugin::{LIVERY, LOADOUT_FILENAME, NAME, PLUGIN_OUTPUT_PATH, XPLANE_OUTPUT_PATH}; // Light switch indices for different equipment configururations const AUTOTHROTTLE: usize = 49; @@ -47,7 +45,9 @@ impl LoadoutFile { let mut output_file_path = Self::acf_livery_path()?; // Update "old" livery - GLOBAL_LIVERY.with(|path| *path.borrow_mut() = output_file_path.clone()); + if let Ok(mut mutex) = LIVERY.lock() { + *mutex = Some(output_file_path.clone()); + } output_file_path.push(LOADOUT_FILENAME); @@ -111,7 +111,7 @@ impl LoadoutFile { } else { // Set up a valid livery path. let acf_livery_path = PathBuf::from(acf_livery_path.as_str()); - if let Some(livery_path) = acf_livery_path.components().last() { + if let Some(livery_path) = acf_livery_path.components().next_back() { output_file_path.push(livery_path) } else { debugln!( diff --git a/src/plugin.rs b/src/plugin.rs index 3491a07..ca8d8f8 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,10 +1,10 @@ // Copyright (c) 2024 telephono // Licensed under the MIT License. See LICENSE file in the project root for full license information. -use std::cell::RefCell; use std::ffi::CStr; use std::os::raw::{c_char, c_int, c_void}; use std::path::PathBuf; +use std::sync::Mutex; use thiserror::Error; @@ -27,9 +27,7 @@ pub static XPLANE_OUTPUT_PATH: &str = "Output"; pub static PLUGIN_OUTPUT_PATH: &str = "B720"; pub static LOADOUT_FILENAME: &str = "persistent-loadout.json"; -thread_local! { - pub static GLOBAL_LIVERY: RefCell = RefCell::new(PathBuf::new()); -} +pub static LIVERY: Mutex> = Mutex::new(None); #[derive(Error, Debug)] pub enum PluginError { @@ -137,17 +135,17 @@ impl Plugin for PersistentLoadoutPlugin { return; } - GLOBAL_LIVERY.with(|path| { - let old_livery_path = (*path.borrow()).clone(); + if let Ok(mut mutex) = LIVERY.lock() { + let old_livery_path = mutex.as_ref().cloned(); // Ignore on first run... - if old_livery_path.as_os_str().is_empty() { + if old_livery_path.is_none() { return; } // Get new livery path let new_livery_path = match LoadoutFile::acf_livery_path() { - Ok(path) => path, + Ok(path) => Some(path), Err(error) => { debugln!("{NAME} something went wrong: {error}"); return; @@ -156,14 +154,16 @@ impl Plugin for PersistentLoadoutPlugin { // Compare old and new livery path // Nothing to do if they are the same... - if old_livery_path.as_os_str() == new_livery_path.as_os_str() { + if old_livery_path == new_livery_path { return; } debugln!("{NAME} livery change detected"); // Save loadout for old livery... - let old_loadout = match LoadoutFile::with_livery_path(old_livery_path.as_os_str()) { + let old_loadout = match LoadoutFile::with_livery_path( + old_livery_path.as_ref().unwrap().as_os_str(), + ) { Ok(loadout) => loadout, Err(error) => { debugln!("{NAME} something went wrong: {error}"); @@ -177,7 +177,9 @@ impl Plugin for PersistentLoadoutPlugin { } // Restore loadout for new livery... - let new_loadout = match LoadoutFile::with_livery_path(new_livery_path.as_os_str()) { + let new_loadout = match LoadoutFile::with_livery_path( + new_livery_path.as_ref().unwrap().as_os_str(), + ) { Ok(loadout) => loadout, Err(error) => { debugln!("{NAME} something went wrong: {error}"); @@ -191,8 +193,8 @@ impl Plugin for PersistentLoadoutPlugin { }; // Update "old" livery - *path.borrow_mut() = new_livery_path; - }); + *mutex = new_livery_path; + }; } } }