102 lines
2.7 KiB
Rust
102 lines
2.7 KiB
Rust
use std::{f32::consts::E, path::PathBuf, sync::Arc, thread, time::Duration};
|
|
|
|
use crossbeam_channel::Receiver;
|
|
use egui::{mutex::Mutex};
|
|
use log::debug;
|
|
|
|
use crate::{config::{self, ConfigProvider}, omori_locator};
|
|
|
|
#[derive(Clone)]
|
|
pub enum UiState {
|
|
Loading,
|
|
KeyRequired(String),
|
|
PickGame(Result<PathBuf, String>, Vec<PathBuf>),
|
|
Error(String)
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct UiStateHolder {
|
|
pub state: Arc<Mutex<UiState>>
|
|
}
|
|
|
|
pub enum UiEvent {
|
|
SetKey(Vec<u8>)
|
|
}
|
|
|
|
pub struct AppThread {
|
|
ui_state: UiStateHolder,
|
|
context: egui::Context,
|
|
ui_event_channel: Receiver<UiEvent>,
|
|
decryption_key: Vec<u8>
|
|
}
|
|
|
|
impl AppThread {
|
|
fn commit(&self, s: UiState) {
|
|
let mut state = self.ui_state.state.lock();
|
|
*state = s;
|
|
}
|
|
|
|
fn get_key(&mut self, reason: String) -> anyhow::Result<Vec<u8>> {
|
|
self.commit(UiState::KeyRequired(reason));
|
|
loop {
|
|
match self.ui_event_channel.recv()? {
|
|
UiEvent::SetKey(key) => {
|
|
self.commit(UiState::Loading);
|
|
return Ok(key);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn pick_game(&mut self, provider: &ConfigProvider) -> anyhow::Result<()> {
|
|
let steam_location = match omori_locator::get_omori_path() {
|
|
Ok(l) => Ok(l),
|
|
Err(e) => Err(String::from(format!("{:#}", e)))
|
|
};
|
|
|
|
let location_history = provider.get_game_paths()?;
|
|
|
|
self.commit(UiState::PickGame(steam_location.clone(), location_history.clone()));
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create(state_holder: &UiStateHolder, context: &egui::Context, ui_event_channel: &Receiver<UiEvent>) -> AppThread {
|
|
AppThread {
|
|
ui_state: state_holder.clone(),
|
|
context: context.clone(),
|
|
ui_event_channel: ui_event_channel.clone(),
|
|
decryption_key: Vec::new()
|
|
}
|
|
}
|
|
|
|
fn real_main(&mut self) -> anyhow::Result<()> {
|
|
debug!("I am ALIVE! HAHAHAHA!");
|
|
self.commit(UiState::Loading);
|
|
|
|
let mut config_provider = config::ConfigProvider::new()?;
|
|
|
|
self.decryption_key = match config_provider.get_key() {
|
|
Some(key) => key,
|
|
None => match omori_locator::get_omori_key() {
|
|
Ok(key) => key,
|
|
Err(reason) => self.get_key(format!("{:#}", reason))?,
|
|
}
|
|
};
|
|
|
|
config_provider.set_key(&self.decryption_key)?;
|
|
|
|
self.pick_game(&config_provider);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn main(&mut self) {
|
|
match self.real_main() {
|
|
Ok(_) => {},
|
|
Err(e) => self.commit(UiState::Error(format!("{:#}", e))),
|
|
}
|
|
}
|
|
}
|