String settings hooks (#82)

* added theme; env change

* began refactoring

* added process hook

* now singular string for each hook

* fixed splitting by comma to by space

* profile_create function updated

* prettier

* added jre validator

* restructured so that it doesnt look like a vec

* fixed merge issue

* snake case

* resolved merge issues + added process events

* clippy, fmt

* removed unnecssary func
This commit is contained in:
Wyatt Verchere
2023-04-17 12:40:27 -07:00
committed by GitHub
parent b120b5cfa8
commit 9f40640ed8
23 changed files with 473 additions and 210 deletions

View File

@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use crate::api::Result;
use theseus::prelude::JavaVersion;
@@ -60,3 +60,10 @@ pub async fn jre_get_optimal_jre_key_by_path(path: &Path) -> Result<String> {
pub async fn jre_validate_globals() -> Result<bool> {
Ok(jre::validate_globals().await?)
}
// Validates JRE at a given path
// Returns None if the path is not a valid JRE
#[tauri::command]
pub async fn jre_get_jre(path: PathBuf) -> Result<Option<JavaVersion>> {
jre::check_jre(path).await.map_err(|e| e.into())
}

View File

@@ -33,6 +33,9 @@ pub enum TheseusSerializableError {
#[error("No profile found at {0}")]
NoProfileFound(String),
#[error("Improperly formatted environment variables: {0}")]
BadEnvVars(String),
}
// Generic implementation of From<T> for ErrorTypeA
@@ -74,4 +77,5 @@ impl_serialize! {
Theseus,
IO,
NoProfileFound,
BadEnvVars,
}

View File

@@ -2,37 +2,40 @@ use std::path::{Path, PathBuf};
use crate::api::Result;
use theseus::prelude::*;
use uuid::Uuid;
// Checks if a process has finished by process PID
// Checks if a process has finished by process UUID
#[tauri::command]
pub async fn process_has_finished_by_pid(pid: u32) -> Result<bool> {
Ok(process::has_finished_by_pid(pid).await?)
pub async fn process_has_finished_by_uuid(uuid: Uuid) -> Result<bool> {
Ok(process::has_finished_by_uuid(&uuid).await?)
}
// Gets process exit status by process PID
// Gets process exit status by process UUID
#[tauri::command]
pub async fn process_get_exit_status_by_pid(pid: u32) -> Result<Option<i32>> {
Ok(process::get_exit_status_by_pid(pid).await?)
pub async fn process_get_exit_status_by_uuid(
uuid: Uuid,
) -> Result<Option<i32>> {
Ok(process::get_exit_status_by_uuid(&uuid).await?)
}
// Gets all process PIDs
// Gets all process UUIDs
#[tauri::command]
pub async fn process_get_all_pids() -> Result<Vec<u32>> {
Ok(process::get_all_pids().await?)
pub async fn process_get_all_uuids() -> Result<Vec<Uuid>> {
Ok(process::get_all_uuids().await?)
}
// Gets all running process PIDs
// Gets all running process UUIDs
#[tauri::command]
pub async fn process_get_all_running_pids() -> Result<Vec<u32>> {
Ok(process::get_all_running_pids().await?)
pub async fn process_get_all_running_uuids() -> Result<Vec<Uuid>> {
Ok(process::get_all_running_uuids().await?)
}
// Gets all process PIDs by profile path
// Gets all process UUIDs by profile path
#[tauri::command]
pub async fn process_get_pids_by_profile_path(
pub async fn process_get_uuids_by_profile_path(
profile_path: &Path,
) -> Result<Vec<u32>> {
Ok(process::get_pids_by_profile_path(profile_path).await?)
) -> Result<Vec<Uuid>> {
Ok(process::get_uuids_by_profile_path(profile_path).await?)
}
// Gets the Profile paths of each *running* stored process in the state
@@ -47,26 +50,26 @@ pub async fn process_get_all_running_profiles() -> Result<Vec<Profile>> {
Ok(process::get_all_running_profiles().await?)
}
// Gets process stderr by process PID
// Gets process stderr by process UUID
#[tauri::command]
pub async fn process_get_stderr_by_pid(pid: u32) -> Result<String> {
Ok(process::get_stderr_by_pid(pid).await?)
pub async fn process_get_stderr_by_uuid(uuid: Uuid) -> Result<String> {
Ok(process::get_stderr_by_uuid(&uuid).await?)
}
// Gets process stdout by process PID
// Gets process stdout by process UUID
#[tauri::command]
pub async fn process_get_stdout_by_pid(pid: u32) -> Result<String> {
Ok(process::get_stdout_by_pid(pid).await?)
pub async fn process_get_stdout_by_uuid(uuid: Uuid) -> Result<String> {
Ok(process::get_stdout_by_uuid(&uuid).await?)
}
// Kill a process by process PID
// Kill a process by process UUID
#[tauri::command]
pub async fn process_kill_by_pid(pid: u32) -> Result<()> {
Ok(process::kill_by_pid(pid).await?)
pub async fn process_kill_by_uuid(uuid: Uuid) -> Result<()> {
Ok(process::kill_by_uuid(&uuid).await?)
}
// Wait for a process to finish by process PID
// Wait for a process to finish by process UUID
#[tauri::command]
pub async fn process_wait_for_by_pid(pid: u32) -> Result<()> {
Ok(process::wait_for_by_pid(pid).await?)
pub async fn process_wait_for_by_uuid(uuid: Uuid) -> Result<()> {
Ok(process::wait_for_by_uuid(&uuid).await?)
}

View File

@@ -1,6 +1,7 @@
use crate::api::Result;
use std::path::{Path, PathBuf};
use theseus::prelude::*;
use uuid::Uuid;
// Remove a profile
// invoke('profile_add_path',path)
@@ -73,18 +74,14 @@ pub async fn profile_remove_project(
Ok(())
}
// Run minecraft using a profile using the default credentials
// Returns a u32 representing the PID, which can be used to poll
// Returns the UUID, which can be used to poll
// for the actual Child in the state.
// invoke('profile_run', path)
#[tauri::command]
pub async fn profile_run(path: &Path) -> Result<u32> {
let proc_lock = profile::run(path).await?;
let pid = proc_lock.read().await.child.id().ok_or_else(|| {
theseus::Error::from(theseus::ErrorKind::LauncherError(
"Process failed to stay open.".to_string(),
))
})?;
Ok(pid)
pub async fn profile_run(path: &Path) -> Result<Uuid> {
let minecraft_child = profile::run(path).await?;
let uuid = minecraft_child.read().await.uuid;
Ok(uuid)
}
// Run Minecraft using a profile using the default credentials, and wait for the result
@@ -97,21 +94,17 @@ pub async fn profile_run_wait(path: &Path) -> Result<()> {
}
// Run Minecraft using a profile using chosen credentials
// Returns a u32 representing the PID, which can be used to poll
// Returns the UUID, which can be used to poll
// for the actual Child in the state.
// invoke('profile_run_credentials', {path, credentials})')
#[tauri::command]
pub async fn profile_run_credentials(
path: &Path,
credentials: Credentials,
) -> Result<u32> {
let proc_lock = profile::run_credentials(path, &credentials).await?;
let pid = proc_lock.read().await.child.id().ok_or_else(|| {
theseus::Error::from(theseus::ErrorKind::LauncherError(
"Process failed to stay open.".to_string(),
))
})?;
Ok(pid)
) -> Result<Uuid> {
let minecraft_child = profile::run_credentials(path, &credentials).await?;
let uuid = minecraft_child.read().await.uuid;
Ok(uuid)
}
// Run Minecraft using a profile using the chosen credentials, and wait for the result

View File

@@ -2,14 +2,17 @@ use crate::api::Result;
use serde::{Deserialize, Serialize};
use theseus::prelude::*;
use super::TheseusSerializableError;
// Identical to theseus::settings::Settings except for the custom_java_args field
// This allows us to split the custom_java_args string into a Vec<String> here and join it back into a string in the backend
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FrontendSettings {
pub theme: Theme,
pub memory: MemorySettings,
pub game_resolution: WindowSize,
pub custom_java_args: String,
pub custom_env_args: Vec<(String, String)>,
pub custom_env_args: String,
pub java_globals: JavaGlobals,
pub default_user: Option<uuid::Uuid>,
pub hooks: Hooks,
@@ -23,10 +26,16 @@ pub struct FrontendSettings {
pub async fn settings_get() -> Result<FrontendSettings> {
let backend_settings = settings::get().await?;
let frontend_settings = FrontendSettings {
theme: backend_settings.theme,
memory: backend_settings.memory,
game_resolution: backend_settings.game_resolution,
custom_java_args: backend_settings.custom_java_args.join(" "),
custom_env_args: backend_settings.custom_env_args,
custom_env_args: backend_settings
.custom_env_args
.into_iter()
.map(|(s1, s2)| format!("{s1}={s2}"))
.collect::<Vec<String>>()
.join(" "),
java_globals: backend_settings.java_globals,
default_user: backend_settings.default_user,
hooks: backend_settings.hooks,
@@ -40,7 +49,25 @@ pub async fn settings_get() -> Result<FrontendSettings> {
// invoke('settings_set', settings)
#[tauri::command]
pub async fn settings_set(settings: FrontendSettings) -> Result<()> {
let custom_env_args: Vec<(String, String)> = settings
.custom_env_args
.split_whitespace()
.map(|s| s.to_string())
.map(|f| {
let mut split = f.split('=');
if let (Some(name), Some(value)) = (split.next(), split.next()) {
Ok((name.to_string(), value.to_string()))
} else {
Err(TheseusSerializableError::BadEnvVars(
"Invalid environment variable: {}".to_string(),
)
.into())
}
})
.collect::<Result<Vec<(String, String)>>>()?;
let backend_settings = Settings {
theme: settings.theme,
memory: settings.memory,
game_resolution: settings.game_resolution,
custom_java_args: settings
@@ -48,7 +75,7 @@ pub async fn settings_set(settings: FrontendSettings) -> Result<()> {
.split_whitespace()
.map(|s| s.to_string())
.collect(),
custom_env_args: settings.custom_env_args,
custom_env_args,
java_globals: settings.java_globals,
default_user: settings.default_user,
hooks: settings.hooks,