Skip to content

Commit

Permalink
Move release fetching on sep. threads
Browse files Browse the repository at this point in the history
  • Loading branch information
funlennysub committed Oct 21, 2022
1 parent d0dc47d commit 6e44dfb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 49 deletions.
2 changes: 1 addition & 1 deletion bepinex_helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bepinex_helpers"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion bepinex_installer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bepinex-installer"
version = "0.2.0"
version = "1.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
78 changes: 70 additions & 8 deletions bepinex_installer/src/installer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::time::Duration;
use std::{collections::HashMap, time::Duration};

use bepinex_helpers::game::{Game, GameType};
use bepinex_helpers::game::{get_unity_games, Game, GameType};
use bepinex_sources::{
bepinex::{AssetDownloader, BepInEx, BepInExRelease, ReleaseFlavor},
builds::BuildsApi,
github::GitHubApi,
version::VersionExt,
};
use eframe::{
Expand All @@ -11,8 +13,11 @@ use eframe::{
};
use egui_extras::{Size, StripBuilder};
use egui_toast::{ToastOptions, Toasts};
use parking_lot::Once;

use crate::MIN_IL2CPP_STABLE_VERSION;
use crate::{MIN_IL2CPP_STABLE_VERSION, MIN_SUPPORTED_STABLE_VERSION};

static INIT_BIE: Once = Once::new();

#[derive(Default)]
pub struct Installer {
Expand All @@ -22,11 +27,63 @@ pub struct Installer {
pub games: Vec<Game>,
pub selected_game: Option<Game>,
pub dl_promise: Option<poll_promise::Promise<anyhow::Result<()>>>,
pub fetch_promises: HashMap<String, poll_promise::Promise<Vec<BepInExRelease>>>,
pub shown_toast: bool,
}

impl Installer {
fn show_games_select(self: &mut Installer, ui: &mut Ui) {
pub fn new() -> Self {
let mut new_app = Self::default();
let bie = BepInEx::default();

let gh_promise = poll_promise::Promise::spawn_thread("gh_fetch", || {
let mut gh = GitHubApi::new("BepInEx", "BepInEx");
gh.set_pre_releases(true);
gh.set_min_tag(Some(MIN_SUPPORTED_STABLE_VERSION.clone()));

gh.get_all()
.unwrap_or_default()
.into_iter()
.map(BepInExRelease::from)
.collect::<Vec<_>>()
});
new_app.fetch_promises.insert("gh_fetch".into(), gh_promise);

let be_promise = poll_promise::Promise::spawn_thread("be_fetch", || {
let be = BuildsApi::new("https://builds.bepinex.dev");
be.get_builds()
.unwrap_or_default()
.into_iter()
.map(BepInExRelease::from)
.collect::<Vec<_>>()
});
new_app.fetch_promises.insert("be_fetch".into(), be_promise);

let mut games = get_unity_games().unwrap_or_default();
games.sort();

new_app.games = games;
new_app.bepinex = bie;
new_app.selected_bie = new_app.bepinex.latest();

new_app
}

fn fetch(&mut self) {
let gh_promise = self.fetch_promises.get("gh_fetch").unwrap();
let be_promise = self.fetch_promises.get("be_fetch").unwrap();
if let (Some(gh_releases), Some(be_releases)) = (gh_promise.ready(), be_promise.ready()) {
INIT_BIE.call_once(|| {
let mut releases: Vec<BepInExRelease> = Vec::new();
releases.extend(gh_releases.iter().cloned());
releases.extend(be_releases.iter().cloned());

self.bepinex.releases = releases;
});
}
}

fn show_games_select(&mut self, ui: &mut Ui) {
ComboBox::from_id_source("game_selector")
.width(ui.available_width() - 8.0)
.selected_text(
Expand All @@ -42,7 +99,7 @@ impl Installer {
});
}

fn show_bie_select(self: &mut Installer, ui: &mut Ui) {
fn show_bie_select(&mut self, ui: &mut Ui) {
ComboBox::from_id_source("bie_selector")
.width(ui.available_width() - 8.0)
.selected_text(
Expand All @@ -68,7 +125,7 @@ impl Installer {
});
}

fn release_flavor_select(self: &mut Installer, ui: &mut Ui) {
fn release_flavor_select(&mut self, ui: &mut Ui) {
ui.radio_value(
&mut self.release_flavor,
ReleaseFlavor::Stable,
Expand All @@ -81,7 +138,7 @@ impl Installer {
);
}

fn install_bie(self: &mut Installer, toasts: &mut Toasts, options: ToastOptions) {
fn install_bie(&mut self, toasts: &mut Toasts, options: ToastOptions) {
if let (Some(selected_game), Some(selected_bie)) = (&self.selected_game, &self.selected_bie)
{
let supported_ver = selected_game.ty == Some(GameType::UnityIL2CPP)
Expand Down Expand Up @@ -202,6 +259,8 @@ impl App for Installer {
self.install_bie(&mut toasts, options);
}
if let Some(dl_promise) = &self.dl_promise {
// Not the best solution but it works
ctx.request_repaint();
if let Some(r) = dl_promise.ready() {
if let Err(e) = r {
toasts.error(e.to_string(), options);
Expand All @@ -222,7 +281,10 @@ impl App for Installer {
});
});

if self.bepinex.releases.is_empty() {
self.fetch()
};

toasts.show(ctx);
ctx.request_repaint();
}
}
38 changes: 1 addition & 37 deletions bepinex_installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

pub mod installer;

use bepinex_helpers::game::get_unity_games;
use bepinex_sources::{
bepinex::{BepInEx, BepInExRelease},
builds::BuildsApi,
github::GitHubApi,
};
use eframe::{egui, run_native, NativeOptions};
use lazy_static::lazy_static;
use semver::Version;
Expand All @@ -20,27 +14,6 @@ lazy_static! {
}

fn main() {
// TODO: populate Installer while the app running instead.
let mut gh = GitHubApi::new("BepInEx", "BepInEx");
gh.set_pre_releases(true);
gh.set_min_tag(Some(MIN_SUPPORTED_STABLE_VERSION.clone()));

let stable_releases = gh.get_all().unwrap_or_default();

let be = BuildsApi::new("https://builds.bepinex.dev");
let be_builds = be.get_builds().unwrap_or_default();

let mut releases: Vec<BepInExRelease> = Vec::new();
releases.extend(stable_releases.into_iter().map(|r| r.into()));
releases.extend(be_builds.into_iter().map(|r| r.into()));

let games = get_unity_games();
if games.is_err() {
return;
}
let mut games = games.unwrap();
games.sort();

let min_size = Some(egui::vec2(400.0, 450.0));
let options = NativeOptions {
follow_system_theme: true,
Expand All @@ -50,18 +23,9 @@ fn main() {
..NativeOptions::default()
};

let bepinex = BepInEx { releases };

let installer = Installer {
bepinex: bepinex.clone(),
selected_bie: bepinex.latest(),
games,
..Installer::default()
};

run_native(
"BepInEx Installer",
options,
Box::new(|_cc| Box::new(installer)),
Box::new(|_cc| Box::new(Installer::new())),
)
}
4 changes: 2 additions & 2 deletions bepinex_sources/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "bepinex_sources"
version = "0.1.0"
version = "1.0.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lazy_static.workspace = true
bepinex_helpers = { version ="0.1.0", path = "../bepinex_helpers" }
bepinex_helpers = { path = "../bepinex_helpers" }
anyhow.workspace = true
reqwest = { version = "0.11.12", features = ["blocking", "json"] }
semver = { workspace = true, features = ["serde"] }
Expand Down

0 comments on commit 6e44dfb

Please sign in to comment.