Add utility to help format the code (#247)

Signed-off-by: Aisuko <urakiny@gmail.com>
This commit is contained in:
Aisuko
2022-10-11 06:17:10 +02:00
committed by GitHub
parent fac58803cc
commit 40c55e542d
39 changed files with 415 additions and 421 deletions
+72 -74
View File
@@ -1,7 +1,7 @@
use serde::Deserialize;
use super::{HeroicGame, HeroicGameType, HeroicSettings};
use crate::platforms::{GamesPlatform, FromSettingsString, load_settings};
use crate::platforms::{load_settings, FromSettingsString, GamesPlatform};
use crate::platforms::{to_shortcuts, NeedsPorton, ShortcutToImport};
use std::collections::HashMap;
use std::path::Path;
@@ -86,14 +86,14 @@ impl HeroicPlatform {
}
}
impl NeedsPorton<HeroicPlatform> for HeroicGameType{
impl NeedsPorton<HeroicPlatform> for HeroicGameType {
#[cfg(not(target_family = "unix"))]
fn needs_proton(&self, _platform: &HeroicPlatform) -> bool {
fn needs_proton(&self, _platform: &HeroicPlatform) -> bool {
false
}
#[cfg(target_family = "unix")]
fn needs_proton(&self, _platform: &HeroicPlatform) -> bool {
fn needs_proton(&self, _platform: &HeroicPlatform) -> bool {
match self {
HeroicGameType::Epic(_game) => true,
HeroicGameType::Gog(_, is_windows) => *is_windows,
@@ -107,30 +107,33 @@ impl NeedsPorton<HeroicPlatform> for HeroicGameType{
}
impl HeroicPlatform {
pub fn get_epic_games(&self, install_modes: &[InstallationMode]) -> eyre::Result<Vec<HeroicGameType>> {
pub fn get_epic_games(
&self,
install_modes: &[InstallationMode],
) -> eyre::Result<Vec<HeroicGameType>> {
let mut shortcuts = vec![];
for install_mode in install_modes {
if let Ok(mut games) = get_shortcuts_from_install_mode(install_mode) {
games.sort_by_key(|m| {
format!("{}-{}-{}", m.launch_parameters, m.executable, &m.app_name)
});
games.dedup_by_key(|m| {
format!("{}-{}-{}", m.launch_parameters, m.executable, &m.app_name)
});
games.sort_by_key(|m| {
format!("{}-{}-{}", m.launch_parameters, m.executable, &m.app_name)
});
games.dedup_by_key(|m| {
format!("{}-{}-{}", m.launch_parameters, m.executable, &m.app_name)
});
for game in games {
if self.settings.is_heroic_launch(&game.app_name) {
shortcuts.push(HeroicGameType::Heroic {
title: game.title,
app_name: game.app_name,
install_mode: *install_mode,
});
} else if game.is_installed() {
shortcuts.push(HeroicGameType::Epic(game));
for game in games {
if self.settings.is_heroic_launch(&game.app_name) {
shortcuts.push(HeroicGameType::Heroic {
title: game.title,
app_name: game.app_name,
install_mode: *install_mode,
});
} else if game.is_installed() {
shortcuts.push(HeroicGameType::Epic(game));
}
}
}
}
}
Ok(shortcuts)
}
}
@@ -199,21 +202,19 @@ fn get_gog_games(
Ok(gog_shortcuts)
}
impl FromSettingsString for HeroicPlatform{
impl FromSettingsString for HeroicPlatform {
fn from_settings_string<S: AsRef<str>>(s: S) -> Self {
HeroicPlatform{
HeroicPlatform {
heroic_games: None,
settings : load_settings(s)
settings: load_settings(s),
}
}
}
impl GamesPlatform for HeroicPlatform{
impl GamesPlatform for HeroicPlatform {
fn name(&self) -> &str {
"Heroic"
}
fn enabled(&self) -> bool {
self.settings.enabled
@@ -226,53 +227,50 @@ impl GamesPlatform for HeroicPlatform{
fn render_ui(&mut self, ui: &mut egui::Ui) {
ui.heading("Heroic");
ui.checkbox(&mut self.settings.enabled, "Import from Heroic");
ui.checkbox(&mut self.settings.default_launch_through_heroic, "Always launch games through Heroic");
let safe_mode_header = match (self.settings.default_launch_through_heroic,self.settings.launch_games_through_heroic.len()) {
(false,0) => "Force games to launch through Heroic Launcher".to_string(),
(false,1) => "One game forced to launch through Heroic Launcher".to_string(),
(false,x) => format!("{} games forced to launch through Heroic Launcher", x),
(true,0) => "Force games to launch directly".to_string(),
(true,1) => "One game forced to launch directly".to_string(),
(true,x) => format!("{} games forced to launch directly", x),
};
egui::CollapsingHeader::new(safe_mode_header)
.id_source("Heroic_Launcher_safe_launch")
.show(ui, |ui| {
if
self.settings.default_launch_through_heroic{
ui.label("Some games work best when launched directly, select those games below and BoilR will create shortcuts that launch the games directly.");
} else{
ui.label("Some games must be started from the Heroic Launcher, select those games below and BoilR will create shortcuts that opens the games through the Heroic Launcher.");
ui.checkbox(
&mut self.settings.default_launch_through_heroic,
"Always launch games through Heroic",
);
let safe_mode_header = match (
self.settings.default_launch_through_heroic,
self.settings.launch_games_through_heroic.len(),
) {
(false, 0) => "Force games to launch through Heroic Launcher".to_string(),
(false, 1) => "One game forced to launch through Heroic Launcher".to_string(),
(false, x) => format!("{} games forced to launch through Heroic Launcher", x),
(true, 0) => "Force games to launch directly".to_string(),
(true, 1) => "One game forced to launch directly".to_string(),
(true, x) => format!("{} games forced to launch directly", x),
};
egui::CollapsingHeader::new(safe_mode_header).id_source("Heroic_Launcher_safe_launch").show(ui, |ui| {
if self.settings.default_launch_through_heroic{
ui.label("Some games work best when launched directly, select those games below and BoilR will create shortcuts that launch the games directly.");
} else {
ui.label("Some games must be started from the Heroic Launcher, select those games below and BoilR will create shortcuts that opens the games through the Heroic Launcher.");
}
let manifests = self.heroic_games.get_or_insert_with(|| {
let heroic_setting = self.settings.clone();
let heroic_platform = HeroicPlatform{ settings:heroic_setting, heroic_games:None};
heroic_platform.get_heroic_games().unwrap_or_default()
});
let safe_open_games = &mut self.settings.launch_games_through_heroic;
for manifest in manifests{
let key = manifest.app_name();
let display_name = manifest.title();
let mut safe_open = safe_open_games.contains(&display_name.to_string()) || safe_open_games.contains(&key.to_string());
if ui.checkbox(&mut safe_open, display_name).clicked(){
if safe_open{
safe_open_games.push(key.to_string());
}else{
safe_open_games.retain(|m| m!= display_name && m!= key);
}
}
let manifests =self.heroic_games.get_or_insert_with(||{
let heroic_setting = self.settings.clone();
let heroic_platform = HeroicPlatform{
settings:heroic_setting,
heroic_games:None
};
heroic_platform.get_heroic_games().unwrap_or_default()
});
let safe_open_games = &mut self.settings.launch_games_through_heroic;
for manifest in manifests{
let key = manifest.app_name();
let display_name = manifest.title();
let mut safe_open = safe_open_games.contains(&display_name.to_string()) || safe_open_games.contains(&key.to_string());
if ui.checkbox(&mut safe_open, display_name).clicked(){
if safe_open{
safe_open_games.push(key.to_string());
}else{
safe_open_games.retain(|m| m!= display_name && m!= key);
}
}
}
}) ;
}
});
}
fn get_settings_serilizable(&self) -> String {
@@ -282,4 +280,4 @@ self.settings.default_launch_through_heroic{
fn code_name(&self) -> &str {
"heroic"
}
}
}