You've already forked AstralRinth
forked from didirus/AstralRinth
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:
@@ -19,7 +19,7 @@ theseus = { path = "../../theseus", features = ["tauri"] }
|
||||
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
tauri = { version = "1.2", features = ["protocol-asset", "window-close", "window-create"] }
|
||||
tauri = { version = "1.2", features = ["protocol-asset", "window-close", "window-create", "dialog"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
thiserror = "1.0"
|
||||
tokio-stream = { version = "0.1", features = ["fs"] }
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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?)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -58,17 +58,18 @@ fn main() {
|
||||
api::jre::jre_validate_globals,
|
||||
api::jre::jre_get_optimal_jre_key,
|
||||
api::jre::jre_get_optimal_jre_key_by_path,
|
||||
api::process::process_get_all_pids,
|
||||
api::process::process_get_all_running_pids,
|
||||
api::process::process_get_pids_by_profile_path,
|
||||
api::jre::jre_get_jre,
|
||||
api::process::process_get_all_uuids,
|
||||
api::process::process_get_all_running_uuids,
|
||||
api::process::process_get_uuids_by_profile_path,
|
||||
api::process::process_get_all_running_profile_paths,
|
||||
api::process::process_get_all_running_profiles,
|
||||
api::process::process_get_exit_status_by_pid,
|
||||
api::process::process_has_finished_by_pid,
|
||||
api::process::process_get_stderr_by_pid,
|
||||
api::process::process_get_stdout_by_pid,
|
||||
api::process::process_kill_by_pid,
|
||||
api::process::process_wait_for_by_pid,
|
||||
api::process::process_get_exit_status_by_uuid,
|
||||
api::process::process_has_finished_by_uuid,
|
||||
api::process::process_get_stderr_by_uuid,
|
||||
api::process::process_get_stdout_by_uuid,
|
||||
api::process::process_kill_by_uuid,
|
||||
api::process::process_wait_for_by_uuid,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
@@ -44,6 +44,12 @@ export async function validate_globals() {
|
||||
return await invoke('jre_validate_globals')
|
||||
}
|
||||
|
||||
// Gets java version from a specific path by trying to run 'java -version' on it.
|
||||
// This also validates it, as it returns null if no valid java version is found at the path
|
||||
export async function get_jre(path) {
|
||||
return await invoke('jre_get_jre', { path })
|
||||
}
|
||||
|
||||
// Gets key for the optimal JRE to use, for a given profile path
|
||||
// The key can be used in the hashmap contained by JavaGlobals in Settings (if it exists)
|
||||
export async function get_optimal_jre_key_by_path(path) {
|
||||
@@ -52,7 +58,7 @@ export async function get_optimal_jre_key_by_path(path) {
|
||||
|
||||
// Gets key for the optimal JRE to use, for a given profile
|
||||
// The key can be used in the hashmap contained by JavaGlobals in Settings (if it exists)
|
||||
export async function get_optimal_jre_ke(path) {
|
||||
export async function get_optimal_jre_key(path) {
|
||||
return await invoke('jre_get_optimal_jre_key', { path })
|
||||
}
|
||||
|
||||
|
||||
@@ -5,34 +5,34 @@
|
||||
*/
|
||||
import { invoke } from '@tauri-apps/api/tauri'
|
||||
|
||||
/// Gets if a process has finished by PID
|
||||
/// Gets if a process has finished by UUID
|
||||
/// Returns bool
|
||||
export async function has_finished_by_pid(pid) {
|
||||
return await invoke('process_has_finished_by_pid', { pid })
|
||||
export async function has_finished_by_uuid(uuid) {
|
||||
return await invoke('process_has_finished_by_uuid', { uuid })
|
||||
}
|
||||
|
||||
/// Gets process exit status by PID
|
||||
/// Gets process exit status by UUID
|
||||
/// Returns u32
|
||||
export async function get_exit_status_by_pid(pid) {
|
||||
return await invoke('process_get_exit_status_by_pid', { pid })
|
||||
export async function get_exit_status_by_uuid(uuid) {
|
||||
return await invoke('process_get_exit_status_by_uuid', { uuid })
|
||||
}
|
||||
|
||||
/// Gets all process IDs
|
||||
/// Returns [u32]
|
||||
export async function get_all_pids() {
|
||||
return await invoke('process_get_all_pids')
|
||||
export async function get_all_uuids() {
|
||||
return await invoke('process_get_all_uuids')
|
||||
}
|
||||
|
||||
/// Gets all running process IDs
|
||||
/// Returns [u32]
|
||||
export async function get_all_running_pids() {
|
||||
return await invoke('process_get_all_running_pids')
|
||||
export async function get_all_running_uuids() {
|
||||
return await invoke('process_get_all_running_uuids')
|
||||
}
|
||||
|
||||
/// Gets all running process IDs with a given profile path
|
||||
/// Returns [u32]
|
||||
export async function get_pids_by_profile_path(profile_path) {
|
||||
return await invoke('process_get_pids_by_profile_path', { profile_path })
|
||||
export async function get_uuids_by_profile_path(profile_path) {
|
||||
return await invoke('process_get_uuids_by_profile_path', { profile_path })
|
||||
}
|
||||
|
||||
/// Gets all running process IDs with a given profile path
|
||||
@@ -47,19 +47,19 @@ export async function get_all_running_profiles(profile_path) {
|
||||
return await invoke('process_get_all_running_profiles', { profile_path })
|
||||
}
|
||||
|
||||
/// Gets process stderr by PID
|
||||
/// Gets process stderr by UUID
|
||||
/// Returns String
|
||||
export async function get_stderr_by_pid(pid) {
|
||||
return await invoke('process_get_stderr_by_pid', { pid })
|
||||
export async function get_stderr_by_uuid(uuid) {
|
||||
return await invoke('process_get_stderr_by_uuid', { uuid })
|
||||
}
|
||||
|
||||
/// Gets process stdout by PID
|
||||
/// Gets process stdout by UUID
|
||||
/// Returns String
|
||||
export async function get_stdout_by_pid(pid) {
|
||||
return await invoke('process_get_stdout_by_pid', { pid })
|
||||
export async function get_stdout_by_uuid(uuid) {
|
||||
return await invoke('process_get_stdout_by_uuid', { uuid })
|
||||
}
|
||||
|
||||
/// Kills a process by PID
|
||||
export async function kill_by_pid(pid) {
|
||||
return await invoke('process_kill_by_pid', { pid })
|
||||
/// Kills a process by UUID
|
||||
export async function kill_by_uuid(uuid) {
|
||||
return await invoke('process_kill_by_uuid', { uuid })
|
||||
}
|
||||
|
||||
@@ -6,12 +6,21 @@
|
||||
import { invoke } from '@tauri-apps/api/tauri'
|
||||
|
||||
// Add empty default instance
|
||||
export async function addDefaultInstance() {
|
||||
export async function create_empty() {
|
||||
return await invoke('profile_create_empty')
|
||||
}
|
||||
|
||||
/// Creates instance
|
||||
/// Returns a path to the profile created
|
||||
/// Add instance
|
||||
/*
|
||||
name: String, // the name of the profile, and relative path to create
|
||||
game_version: String, // the game version of the profile
|
||||
modloader: ModLoader, // the modloader to use
|
||||
- ModLoader is an enum, with the following variants: Vanilla, Forge, Fabric, Quilt
|
||||
loader_version: String, // the modloader version to use, set to "latest", "stable", or the ID of your chosen loader
|
||||
icon: Path, // the icon for the profile
|
||||
- icon is a path to an image file, which will be copied into the profile directory
|
||||
*/
|
||||
|
||||
export async function create(name, game_version, modloader, loader_version, icon) {
|
||||
return await invoke('profile_create', { name, game_version, modloader, loader_version, icon })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user