@@ -38,9 +38,11 @@ use wowsunpack::{
3838use zip:: ZipArchive ;
3939
4040use crate :: {
41+ app:: TimedMessage ,
4142 build_tracker,
4243 error:: ToolkitError ,
4344 game_params:: load_game_params,
45+ plaintext_viewer:: PlaintextFileViewer ,
4446 twitch:: { self , Token , TwitchState , TwitchUpdate } ,
4547 ui:: {
4648 mod_manager:: { ModInfo , ModManagerIndex } ,
@@ -58,7 +60,7 @@ pub struct DownloadProgress {
5860}
5961
6062pub struct BackgroundTask {
61- pub receiver : mpsc:: Receiver < Result < BackgroundTaskCompletion , ToolkitError > > ,
63+ pub receiver : Option < mpsc:: Receiver < Result < BackgroundTaskCompletion , ToolkitError > > > ,
6264 pub kind : BackgroundTaskKind ,
6365}
6466
@@ -87,12 +89,18 @@ pub enum BackgroundTaskKind {
8789 rx : mpsc:: Receiver < DownloadProgress > ,
8890 last_progress : Option < DownloadProgress > ,
8991 } ,
92+ UpdateTimedMessage ( TimedMessage ) ,
93+ OpenFileViewer ( PlaintextFileViewer ) ,
9094}
9195
9296impl BackgroundTask {
9397 /// TODO: has a bug currently where if multiple tasks are running at the same time, the message looks a bit wonky
9498 pub fn build_description ( & mut self , ui : & mut egui:: Ui ) -> Option < Result < BackgroundTaskCompletion , ToolkitError > > {
95- match self . receiver . try_recv ( ) {
99+ if self . receiver . is_none ( ) {
100+ return Some ( Ok ( BackgroundTaskCompletion :: NoReceiver ) ) ;
101+ }
102+
103+ match self . receiver . as_ref ( ) . unwrap ( ) . try_recv ( ) {
96104 Ok ( result) => Some ( result) ,
97105 Err ( TryRecvError :: Empty ) => {
98106 match & mut self . kind {
@@ -166,6 +174,9 @@ impl BackgroundTask {
166174 ui. add ( egui:: ProgressBar :: new ( progress. downloaded as f32 / progress. total as f32 ) . text ( format ! ( "Uninstalling {}" , mod_info. meta. name( ) ) ) ) ;
167175 }
168176 }
177+ BackgroundTaskKind :: UpdateTimedMessage ( _) | BackgroundTaskKind :: OpenFileViewer ( _) => {
178+ // do nothing
179+ }
169180 }
170181 None
171182 }
@@ -190,6 +201,7 @@ pub enum BackgroundTaskCompletion {
190201 ModDownloaded ( ModInfo ) ,
191202 ModInstalled ( ModInfo ) ,
192203 ModUninstalled ( ModInfo ) ,
204+ NoReceiver ,
193205}
194206
195207impl std:: fmt:: Debug for BackgroundTaskCompletion {
@@ -209,6 +221,7 @@ impl std::fmt::Debug for BackgroundTaskCompletion {
209221 Self :: ModDownloaded ( modi) => f. debug_struct ( "ModDownloaded" ) . field ( "0" , modi) . finish ( ) ,
210222 Self :: ModInstalled ( modi) => f. debug_struct ( "ModInstalled" ) . field ( "0" , modi) . finish ( ) ,
211223 Self :: ModUninstalled ( modi) => f. debug_struct ( "ModUninstalled" ) . field ( "0" , modi) . finish ( ) ,
224+ Self :: NoReceiver => f. debug_struct ( "NoReceiver" ) . finish ( ) ,
212225 }
213226 }
214227}
@@ -466,7 +479,7 @@ pub fn start_download_update_task(runtime: &Runtime, release: &Asset) -> Backgro
466479 } ) ;
467480
468481 BackgroundTask {
469- receiver : rx,
482+ receiver : rx. into ( ) ,
470483 kind : BackgroundTaskKind :: Updating {
471484 rx : progress_rx,
472485 last_progress : None ,
@@ -783,7 +796,7 @@ pub fn start_populating_player_inspector(
783796 } ) ;
784797
785798 BackgroundTask {
786- receiver : rx,
799+ receiver : rx. into ( ) ,
787800 kind : BackgroundTaskKind :: PopulatePlayerInspectorFromReplays ,
788801 }
789802}
@@ -805,30 +818,30 @@ pub fn load_constants(constants: Vec<u8>) -> BackgroundTask {
805818 std:: thread:: spawn ( move || {
806819 let result = serde_json:: from_slice ( & constants)
807820 . context ( "failed to deserialize constants file" )
808- . map ( |constants| BackgroundTaskCompletion :: ConstantsLoaded ( constants ) )
821+ . map ( BackgroundTaskCompletion :: ConstantsLoaded )
809822 . map_err ( |err| err. into ( ) ) ;
810823
811824 tx. send ( result) ;
812825 } ) ;
813826 BackgroundTask {
814- receiver : rx,
827+ receiver : rx. into ( ) ,
815828 kind : BackgroundTaskKind :: LoadingConstants ,
816829 }
817830}
818831
819832pub fn load_mods_db ( ) -> BackgroundTask {
820833 let ( tx, rx) = mpsc:: channel ( ) ;
821834 std:: thread:: spawn ( move || {
822- let mods_db = std:: fs:: read_to_string ( "..\\ mods.toml" ) . unwrap ( ) ;
835+ let mods_db = std:: fs:: read_to_string ( "../ mods.toml" ) . unwrap ( ) ;
823836 let result = toml:: from_str :: < ModManagerIndex > ( & mods_db)
824837 . context ( "failed to deserialize mods db" )
825- . map ( |constants| BackgroundTaskCompletion :: ModDatabaseLoaded ( constants ) )
838+ . map ( BackgroundTaskCompletion :: ModDatabaseLoaded )
826839 . map_err ( |err| err. into ( ) ) ;
827840
828841 tx. send ( result) ;
829842 } ) ;
830843 BackgroundTask {
831- receiver : rx,
844+ receiver : rx. into ( ) ,
832845 kind : BackgroundTaskKind :: LoadingModDatabase ,
833846 }
834847}
@@ -975,7 +988,7 @@ fn install_mod(runtime: Arc<Runtime>, wows_data: Arc<RwLock<WorldOfWarshipsData>
975988 let ( download_task_tx, download_task_rx) = mpsc:: channel ( ) ;
976989 let ( download_progress_tx, download_progress_rx) = mpsc:: channel ( ) ;
977990 let _ = tx. send ( BackgroundTask {
978- receiver : download_task_rx,
991+ receiver : download_task_rx. into ( ) ,
979992 kind : BackgroundTaskKind :: DownloadingMod {
980993 mod_info : mod_info. clone ( ) ,
981994 rx : download_progress_rx,
@@ -993,7 +1006,7 @@ fn install_mod(runtime: Arc<Runtime>, wows_data: Arc<RwLock<WorldOfWarshipsData>
9931006 let ( install_task_tx, install_task_rx) = mpsc:: channel ( ) ;
9941007 let ( install_progress_tx, install_progress_rx) = mpsc:: channel ( ) ;
9951008 let _ = tx. send ( BackgroundTask {
996- receiver : install_task_rx,
1009+ receiver : install_task_rx. into ( ) ,
9971010 kind : if mod_info. enabled {
9981011 BackgroundTaskKind :: InstallingMod {
9991012 mod_info : mod_info. clone ( ) ,
@@ -1025,7 +1038,7 @@ fn uninstall_mod(runtime: Arc<Runtime>, wows_data: Arc<RwLock<WorldOfWarshipsDat
10251038 let ( uninstall_task_tx, uninstall_task_rx) = mpsc:: channel ( ) ;
10261039 let ( uninstall_progress_tx, uninstall_progress_rx) = mpsc:: channel ( ) ;
10271040 let _ = tx. send ( BackgroundTask {
1028- receiver : uninstall_task_rx,
1041+ receiver : uninstall_task_rx. into ( ) ,
10291042 kind : BackgroundTaskKind :: UninstallingMod {
10301043 mod_info : mod_info. clone ( ) ,
10311044 rx : uninstall_progress_rx,
@@ -1058,22 +1071,23 @@ fn uninstall_mod(runtime: Arc<Runtime>, wows_data: Arc<RwLock<WorldOfWarshipsDat
10581071 Ok ( ( ) )
10591072}
10601073
1061- pub fn start_mod_manager_thread ( runtime : Arc < Runtime > , wows_data : Arc < RwLock < WorldOfWarshipsData > > , receiver : mpsc:: Receiver < ModInfo > ) -> mpsc:: Receiver < BackgroundTask > {
1062- let ( tx, rx) = mpsc:: channel ( ) ;
1063-
1074+ pub fn start_mod_manager_thread (
1075+ runtime : Arc < Runtime > ,
1076+ wows_data : Arc < RwLock < WorldOfWarshipsData > > ,
1077+ receiver : mpsc:: Receiver < ModInfo > ,
1078+ background_task_sender : mpsc:: Sender < BackgroundTask > ,
1079+ ) {
10641080 std:: thread:: spawn ( move || {
1065- while let Some ( mod_info) = receiver. recv ( ) . ok ( ) {
1081+ while let Ok ( mod_info) = receiver. recv ( ) {
10661082 eprintln ! ( "mod was changed: {:?}" , mod_info. meta. name( ) ) ;
10671083
10681084 if mod_info. enabled {
10691085 eprintln ! ( "installing mod: {:?}" , mod_info. meta. name( ) ) ;
1070- install_mod ( runtime. clone ( ) , wows_data. clone ( ) , mod_info. clone ( ) , tx . clone ( ) ) ;
1086+ install_mod ( runtime. clone ( ) , wows_data. clone ( ) , mod_info. clone ( ) , background_task_sender . clone ( ) ) ;
10711087 } else {
10721088 eprintln ! ( "uninstalling mod: {:?}" , mod_info. meta. name( ) ) ;
1073- uninstall_mod ( runtime. clone ( ) , wows_data. clone ( ) , mod_info. clone ( ) , tx . clone ( ) ) ;
1089+ uninstall_mod ( runtime. clone ( ) , wows_data. clone ( ) , mod_info. clone ( ) , background_task_sender . clone ( ) ) ;
10741090 }
10751091 }
10761092 } ) ;
1077-
1078- rx
10791093}
0 commit comments