mirror of
https://github.com/djibux/BoilR.git
synced 2026-09-01 05:53:41 +02:00
Modular platforms (#241)
This commit is contained in:
+79
-30
@@ -1,17 +1,18 @@
|
||||
use std::{collections::HashMap, error::Error};
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
use crate::heroic::HeroicGameType;
|
||||
use std::{collections::HashMap, error::Error, time::Duration};
|
||||
|
||||
use eframe::{egui, App, Frame};
|
||||
use egui::{ImageButton, Rounding, Stroke, TextureHandle};
|
||||
use steam_shortcuts_util::shortcut::ShortcutOwned;
|
||||
use tokio::{
|
||||
runtime::Runtime,
|
||||
sync::watch::{self, Receiver},
|
||||
};
|
||||
|
||||
use crate::{egs::ManifestItem, settings::Settings, sync::SyncProgress, config::get_renames_file};
|
||||
use crate::{
|
||||
config::get_renames_file,
|
||||
platforms::{get_platforms, GamesPlatform, Platforms, ShortcutToImport},
|
||||
settings::{save_settings, Settings},
|
||||
sync::{self, SyncProgress},
|
||||
};
|
||||
|
||||
use super::{
|
||||
ui_colors::{
|
||||
@@ -31,58 +32,78 @@ struct UiImages {
|
||||
save_button: Option<egui::TextureHandle>,
|
||||
logo_32: Option<egui::TextureHandle>,
|
||||
}
|
||||
type GamesToSync = Vec<(
|
||||
String,
|
||||
Receiver<FetcStatus<eyre::Result<Vec<ShortcutToImport>>>>,
|
||||
)>;
|
||||
|
||||
pub(crate) fn all_ready(games: &GamesToSync) -> bool {
|
||||
games.iter().all(|(_name, rx)| rx.borrow().is_some())
|
||||
}
|
||||
|
||||
pub(crate) fn get_all_games(games: &GamesToSync) -> Vec<(String, Vec<ShortcutToImport>)> {
|
||||
games
|
||||
.iter()
|
||||
.filter_map(|(name, rx)| {
|
||||
if let FetcStatus::Fetched(Ok(data)) = &*rx.borrow() {
|
||||
Some((name.to_owned(), data.to_owned()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub struct MyEguiApp {
|
||||
selected_menu: Menues,
|
||||
pub(crate) settings: Settings,
|
||||
pub(crate) rt: Runtime,
|
||||
ui_images: UiImages,
|
||||
pub(crate) games_to_sync: Receiver<FetcStatus<Vec<(String, Vec<ShortcutOwned>)>>>,
|
||||
pub(crate) games_to_sync: GamesToSync,
|
||||
pub(crate) status_reciever: Receiver<SyncProgress>,
|
||||
pub(crate) epic_manifests: Option<Vec<ManifestItem>>,
|
||||
#[cfg(target_family = "unix")]
|
||||
pub(crate) heroic_games: Option<Vec<HeroicGameType>>,
|
||||
pub(crate) image_selected_state: ImageSelectState,
|
||||
pub(crate) backup_state: BackupState,
|
||||
pub(crate) disconect_state: DiconnectState,
|
||||
pub(crate) rename_map : HashMap<u32,String>,
|
||||
pub(crate) current_edit : Option<u32>,
|
||||
pub(crate) rename_map: HashMap<u32, String>,
|
||||
pub(crate) current_edit: Option<u32>,
|
||||
pub(crate) platforms: Platforms,
|
||||
}
|
||||
|
||||
impl MyEguiApp {
|
||||
pub fn new() -> Self {
|
||||
let runtime = Runtime::new().unwrap();
|
||||
let mut runtime = Runtime::new().unwrap();
|
||||
let settings = Settings::new().expect("We must be able to load our settings");
|
||||
let platforms = get_platforms();
|
||||
let games_to_sync = create_games_to_sync(&mut runtime, &platforms);
|
||||
Self {
|
||||
selected_menu: Menues::Import,
|
||||
settings: Settings::new().expect("We must be able to load our settings"),
|
||||
settings,
|
||||
rt: runtime,
|
||||
games_to_sync: watch::channel(FetcStatus::NeedsFetched).1,
|
||||
games_to_sync,
|
||||
ui_images: UiImages::default(),
|
||||
status_reciever: watch::channel(SyncProgress::NotStarted).1,
|
||||
epic_manifests: None,
|
||||
#[cfg(target_family = "unix")]
|
||||
heroic_games: None,
|
||||
image_selected_state: ImageSelectState::default(),
|
||||
backup_state: BackupState::default(),
|
||||
disconect_state: DiconnectState::default(),
|
||||
rename_map: get_rename_map(),
|
||||
current_edit: Option::None
|
||||
current_edit: Option::None,
|
||||
platforms,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rename_map() -> HashMap<u32,String>{
|
||||
fn get_rename_map() -> HashMap<u32, String> {
|
||||
try_get_rename_map().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn try_get_rename_map() -> Result<HashMap<u32,String>,Box<dyn Error>>{
|
||||
fn try_get_rename_map() -> Result<HashMap<u32, String>, Box<dyn Error>> {
|
||||
let rename_map = get_renames_file();
|
||||
let file_content = std::fs::read_to_string(rename_map)?;
|
||||
let deserialized = serde_json::from_str(&file_content)?;
|
||||
Ok(deserialized)
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Clone)]
|
||||
enum Menues {
|
||||
Import,
|
||||
Settings,
|
||||
@@ -97,6 +118,23 @@ impl Default for Menues {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_games_to_sync(rt: &mut Runtime, platforms: &[Box<dyn GamesPlatform>]) -> GamesToSync {
|
||||
let mut to_sync = vec![];
|
||||
for platform in platforms {
|
||||
if platform.enabled() {
|
||||
let (tx, rx) = watch::channel(FetcStatus::NeedsFetched);
|
||||
to_sync.push((platform.name().to_string(), rx));
|
||||
let platform = platform.clone();
|
||||
rt.spawn_blocking(move || {
|
||||
let _ = tx.send(FetcStatus::Fetching);
|
||||
let games_to_sync = sync::get_platform_shortcuts(platform);
|
||||
let _ = tx.send(FetcStatus::Fetched(games_to_sync));
|
||||
});
|
||||
}
|
||||
}
|
||||
to_sync
|
||||
}
|
||||
|
||||
impl App for MyEguiApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut Frame) {
|
||||
let frame = egui::Frame::default()
|
||||
@@ -111,6 +149,8 @@ impl App for MyEguiApp {
|
||||
ui.image(texture, size);
|
||||
ui.add_space(SECTION_SPACING);
|
||||
|
||||
let menu_before = self.selected_menu.clone();
|
||||
|
||||
let mut changed = ui
|
||||
.selectable_value(&mut self.selected_menu, Menues::Import, "Import Games")
|
||||
.changed();
|
||||
@@ -138,9 +178,12 @@ impl App for MyEguiApp {
|
||||
if changed {
|
||||
self.backup_state.available_backups = None;
|
||||
}
|
||||
if changed && self.selected_menu == Menues::Settings {
|
||||
if changed
|
||||
&& menu_before == Menues::Settings
|
||||
&& self.selected_menu == Menues::Import
|
||||
{
|
||||
//We reset games here, since user might change settings
|
||||
self.games_to_sync = watch::channel(FetcStatus::NeedsFetched).1;
|
||||
self.games_to_sync = create_games_to_sync(&mut self.rt, &self.platforms);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -153,11 +196,11 @@ impl App for MyEguiApp {
|
||||
let save_button = ImageButton::new(texture, size * 0.5);
|
||||
|
||||
if ui.add(save_button).on_hover_text("Save settings").clicked() {
|
||||
MyEguiApp::save_settings_to_file(&self.settings.clone());
|
||||
save_settings(&self.settings, &self.platforms);
|
||||
}
|
||||
});
|
||||
}
|
||||
if self.games_to_sync.borrow().is_some() {
|
||||
if self.selected_menu == Menues::Import {
|
||||
egui::TopBottomPanel::new(egui::panel::TopBottomSide::Bottom, "Bottom Panel")
|
||||
.frame(frame)
|
||||
.show(ctx, |ui| {
|
||||
@@ -186,14 +229,16 @@ impl App for MyEguiApp {
|
||||
ui.label(&status_string);
|
||||
}
|
||||
}
|
||||
let all_ready = all_ready(&self.games_to_sync);
|
||||
|
||||
let texture = self.get_import_image(ui);
|
||||
let size = texture.size_vec2();
|
||||
let image_button = ImageButton::new(texture, size * 0.5);
|
||||
if ui
|
||||
.add(image_button)
|
||||
.on_hover_text("Import your games into steam")
|
||||
.clicked()
|
||||
if all_ready
|
||||
&& ui
|
||||
.add(image_button)
|
||||
.on_hover_text("Import your games into steam")
|
||||
.clicked()
|
||||
&& !syncing
|
||||
{
|
||||
self.run_sync(false);
|
||||
@@ -293,6 +338,10 @@ fn setup(ctx: &egui::Context) {
|
||||
}
|
||||
pub fn run_sync() {
|
||||
let mut app = MyEguiApp::new();
|
||||
while !all_ready(&app.games_to_sync) {
|
||||
println!("Finding games, trying again in 500ms");
|
||||
std::thread::sleep(Duration::from_secs_f32(0.5));
|
||||
}
|
||||
app.run_sync(true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user