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 ;
54use std:: ffi:: CStr ;
65use std:: os:: raw:: { c_char, c_int, c_void} ;
76use std:: path:: PathBuf ;
7+ use std:: sync:: Mutex ;
88
99use thiserror:: Error ;
1010
@@ -27,9 +27,7 @@ pub static XPLANE_OUTPUT_PATH: &str = "Output";
2727pub static PLUGIN_OUTPUT_PATH : & str = "B720" ;
2828pub 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 ) ]
3533pub 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