Skip to content

Commit

Permalink
Move shared logic back to main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekx committed Apr 26, 2024
1 parent b1d47e7 commit 047bc30
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
30 changes: 30 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::path::PathBuf;

use anyhow::Result;
use clap::{Parser, Subcommand};
use ini::Ini;
use steamlocate::SteamDir;

#[derive(Parser)]
#[command(version, about, propagate_version = false, subcommand_required = true)]
Expand Down Expand Up @@ -113,3 +115,31 @@ async fn main() -> Result<()> {

Ok(())
}

fn get_retro_arch_config(retro_arch_path: Option<PathBuf>) -> Result<(Ini, PathBuf)> {
let retro_arch_path = retro_arch_path
.or_else(|| {
let steam_dir = SteamDir::locate().expect("Steam not found");
let (app, library) = steam_dir.find_app(1118310).expect("RetroArch not found")?;

Some(library.resolve_app_dir(&app))
})
.expect("RetroArch not installed in Steam");

let config_file_path = retro_arch_path.join("retroarch.cfg");
Ok((Ini::load_from_file(config_file_path)?, retro_arch_path))
}

fn get_path_from_config(config: &Ini, key: &str, retro_arch_path: &PathBuf) -> Result<PathBuf> {
let path = config
.get_from(None::<String>, key)
.expect(&format!("Key {key} not found in RetroArch config"));

let result = if path.starts_with(":") {
retro_arch_path.join(path.replace(":/", "./").replace(":", ""))
} else {
PathBuf::from(path)
};

Ok(result)
}
44 changes: 9 additions & 35 deletions src/update_cores.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::{get_path_from_config, get_retro_arch_config};

use std::env::consts;
use std::fs::{remove_file, File};
use std::io::{Read, Write};
Expand All @@ -6,36 +8,16 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use futures_util::StreamExt;
use indicatif::{ProgressBar, ProgressStyle};
use ini::Ini;
use reqwest::Client;
use sevenz_rust::{Password, SevenZReader};
use steamlocate::SteamDir;
use zip::ZipArchive;

pub(crate) async fn update_cores(version: String, retro_arch_path: Option<PathBuf>) -> Result<()> {
// Get RetroArch path from Steam if not supplied via args
let retro_arch_path = retro_arch_path
.or_else(|| {
let steam_dir = SteamDir::locate().expect("Steam not found");
let (app, library) = steam_dir.find_app(1118310).expect("RetroArch not found")?;

Some(library.resolve_app_dir(&app))
})
.expect("RetroArch not installed in Steam");

// Load core and info paths from RetroArch config
let config_file_path = retro_arch_path.join("retroarch.cfg");
let config = Ini::load_from_file(config_file_path)?;

let core_path_settings = config
.get_from(None::<String>, "libretro_directory")
.expect("Couldn't find core path in RetroArch config");
let info_path_settings = config
.get_from(None::<String>, "libretro_info_path")
.expect("Couldn't find info path in RetroArch config");

let core_path = parse_retro_arch_path(core_path_settings, &retro_arch_path);
let info_path = parse_retro_arch_path(info_path_settings, &retro_arch_path);
// Get RetroArch config and load needed paths from it
let (config, retro_arch_path) = get_retro_arch_config(retro_arch_path)?;

let core_path = get_path_from_config(&config, "libretro_directory", &retro_arch_path)?;
let info_path = get_path_from_config(&config, "libretro_info_path", &retro_arch_path)?;

// Build download URL for RetroArch cores and download and extract them
let release_type = if version != "nightly" {
Expand Down Expand Up @@ -83,19 +65,11 @@ pub(crate) async fn update_cores(version: String, retro_arch_path: Option<PathBu
)?;

remove_file(info_download_file_path)?;
println!("Cores successfully updated.");

println!("Cores successfully updated.");
Ok(())
}

fn parse_retro_arch_path(path: &str, retro_arch_path: &PathBuf) -> PathBuf {
if path.starts_with(":") {
retro_arch_path.join(path.replace(":/", "./").replace(":", ""))
} else {
PathBuf::from(path)
}
}

async fn download_file(
client: &Client,
url: &str,
Expand All @@ -115,7 +89,7 @@ async fn download_file(
.unwrap()
.progress_chars("#>-"));

// download chunks
// Download file
let mut file = File::create(path)?;
let mut downloaded: u64 = 0;
let mut stream = response.bytes_stream();
Expand Down

0 comments on commit 047bc30

Please sign in to comment.