Skip to content

Commit 042a112

Browse files
committed
feat: refactor tables
1 parent 7a45d6d commit 042a112

File tree

7 files changed

+1097
-308
lines changed

7 files changed

+1097
-308
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ http-body-util = "0.1.2"
6767
toml = "0.8.19"
6868
tar = "0.4.43"
6969
scopeguard = "1.2.0"
70+
egui_table = "0.2.0"
7071

7172
# native:
7273
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
@@ -95,7 +96,7 @@ debug = true
9596

9697
[patch.crates-io]
9798
#serde-pickle = { path = "../serde-pickle" } #git = "https://github.com/landaire/serde-pickle.git" }
98-
wows_replays = { path = "../wows-replays/parser" }
99+
# wows_replays = { path = "../wows-replays/parser" }
99100
# wowsunpack = { path = "../wowsunpack" }
100101

101102
# If you want to use the bleeding edge version of egui and eframe:

src/app.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use core::cell::RefCell;
12
use std::{
23
borrow::Borrow,
34
collections::{HashMap, HashSet},
45
env,
56
error::Error,
67
path::{Path, PathBuf},
78
process::Command,
9+
rc::Rc,
810
sync::{
911
atomic::{AtomicBool, Ordering},
1012
mpsc::{self, Receiver, Sender, TryRecvError},
@@ -329,6 +331,7 @@ pub enum NotifyFileEvent {
329331
TempArenaInfoCreated(PathBuf),
330332
}
331333

334+
#[derive(Clone)]
332335
pub struct TimedMessage {
333336
pub message: String,
334337
pub expiration: Instant,
@@ -419,7 +422,7 @@ pub struct TabState {
419422
pub markdown_cache: CommonMarkCache,
420423

421424
#[serde(default)]
422-
pub replay_sort: std::sync::Mutex<replay_parser::SortOrder>,
425+
pub replay_sort: Arc<parking_lot::Mutex<replay_parser::SortOrder>>,
423426

424427
#[serde(skip)]
425428
pub game_constants: Arc<RwLock<serde_json::Value>>,
@@ -435,13 +438,16 @@ pub struct TabState {
435438
pub mod_action_receiver: Option<Receiver<ModInfo>>,
436439

437440
#[serde(skip)]
438-
pub mod_update_receiver: Option<Receiver<BackgroundTask>>,
441+
pub background_task_receiver: Receiver<BackgroundTask>,
442+
#[serde(skip)]
443+
pub background_task_sender: Sender<BackgroundTask>,
439444
}
440445

441446
impl Default for TabState {
442447
fn default() -> Self {
443448
let default_constants = serde_json::from_str(include_str!("../embedded_resources/constants.json")).expect("failed to parse constants JSON");
444449
let (mod_action_sender, mod_action_receiver) = mpsc::channel();
450+
let (background_task_sender, background_task_receiver) = mpsc::channel();
445451
Self {
446452
world_of_warships_data: None,
447453
filter: Default::default(),
@@ -472,7 +478,8 @@ impl Default for TabState {
472478
mod_manager_info: Default::default(),
473479
mod_action_sender,
474480
mod_action_receiver: Some(mod_action_receiver),
475-
mod_update_receiver: None,
481+
background_task_receiver,
482+
background_task_sender,
476483
}
477484
}
478485
}
@@ -502,7 +509,13 @@ impl TabState {
502509
if let Some(wows_data) = self.world_of_warships_data.as_ref() {
503510
update_background_task!(
504511
self.background_tasks,
505-
load_replay(Arc::clone(&self.game_constants), Arc::clone(wows_data), replay)
512+
load_replay(
513+
Arc::clone(&self.game_constants),
514+
Arc::clone(wows_data),
515+
replay,
516+
Arc::clone(&self.replay_sort),
517+
self.background_task_sender.clone()
518+
)
506519
);
507520
}
508521
}
@@ -639,7 +652,7 @@ impl TabState {
639652
});
640653

641654
BackgroundTask {
642-
receiver: rx,
655+
receiver: rx.into(),
643656
kind: BackgroundTaskKind::LoadingData,
644657
}
645658
}
@@ -755,7 +768,7 @@ impl WowsToolkitApp {
755768

756769
pub fn build_bottom_panel(&mut self, ui: &mut Ui) {
757770
// Try to update mod update tasks
758-
if let Some(new_task) = self.tab_state.mod_update_receiver.as_ref().and_then(|rx| rx.try_recv().ok()) {
771+
if let Some(new_task) = self.tab_state.background_task_receiver.try_recv().ok() {
759772
self.tab_state.background_tasks.push(new_task);
760773
}
761774

@@ -807,10 +820,19 @@ impl WowsToolkitApp {
807820
BackgroundTaskKind::DownloadingMod { mod_info, rx, last_progress } => {
808821
// do nothing
809822
}
823+
BackgroundTaskKind::UpdateTimedMessage(timed_message) => {
824+
self.tab_state.timed_message.write().replace(timed_message.clone());
825+
}
826+
BackgroundTaskKind::OpenFileViewer(plaintext_file_viewer) => {
827+
self.tab_state.file_viewer.lock().push(plaintext_file_viewer.clone());
828+
}
810829
}
811830

812831
match result {
813832
Ok(data) => match data {
833+
BackgroundTaskCompletion::NoReceiver => {
834+
// do nothing
835+
}
814836
BackgroundTaskCompletion::DataLoaded { new_dir, wows_data, replays } => {
815837
let replays_dir = wows_data.replays_dir.clone();
816838
if let Some(old_wows_data) = &self.tab_state.world_of_warships_data {
@@ -819,11 +841,12 @@ impl WowsToolkitApp {
819841
let wows_data = Arc::new(RwLock::new(wows_data));
820842
self.tab_state.world_of_warships_data = Some(Arc::clone(&wows_data));
821843

822-
self.tab_state.mod_update_receiver = Some(task::start_mod_manager_thread(
844+
task::start_mod_manager_thread(
823845
Arc::clone(&self.runtime),
824846
wows_data,
825847
self.tab_state.mod_action_receiver.take().unwrap(),
826-
));
848+
self.tab_state.background_task_sender.clone(),
849+
);
827850
}
828851
self.tab_state.update_wows_dir(&new_dir, &replays_dir);
829852
self.tab_state.replay_files = replays;
@@ -1089,7 +1112,9 @@ impl WowsToolkitApp {
10891112
parse_replay(
10901113
Arc::clone(&self.tab_state.game_constants),
10911114
Arc::clone(wows_data),
1092-
self.tab_state.settings.current_replay_path.clone()
1115+
self.tab_state.settings.current_replay_path.clone(),
1116+
Arc::clone(&self.tab_state.replay_sort),
1117+
self.tab_state.background_task_sender.clone(),
10931118
)
10941119
);
10951120
}

src/plaintext_viewer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum FileType {
1010
Image { ext: String, contents: Vec<u8> },
1111
}
1212

13+
#[derive(Clone)]
1314
pub struct PlaintextFileViewer {
1415
pub title: Arc<String>,
1516
pub file_info: Arc<Mutex<FileType>>,

src/task.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ use wowsunpack::{
3838
use zip::ZipArchive;
3939

4040
use 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

6062
pub 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

9296
impl 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

195207
impl 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

819832
pub 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

Comments
 (0)