Skip to content
This repository was archived by the owner on Dec 13, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/loadout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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!(
Expand Down
28 changes: 15 additions & 13 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<PathBuf> = RefCell::new(PathBuf::new());
}
pub static LIVERY: Mutex<Option<PathBuf>> = Mutex::new(None);

#[derive(Error, Debug)]
pub enum PluginError {
Expand Down Expand Up @@ -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;
Expand All @@ -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}");
Expand All @@ -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}");
Expand All @@ -191,8 +193,8 @@ impl Plugin for PersistentLoadoutPlugin {
};

// Update "old" livery
*path.borrow_mut() = new_livery_path;
});
*mutex = new_livery_path;
};
}
}
}
Expand Down
Loading