You've already forked AstralRinth
forked from didirus/AstralRinth
Profile bindings (#55)
* basic framework. still has errors * added functionality for main endpoints + some structuring * formatting * unused code * mimicked CLI function with wait_for process * made PR changes, added playground * cargo fmt * removed missed println * misc tests fixes * cargo fmt * added windows support * cargo fmt * all OS use dunce * restructured profile slightly; fixed mac bug * profile changes, new main.rs * fixed requested pr + canonicaliation bug * fixed regressed bug in ui * fixed regressed bugs * fixed git error * typo * ran prettier * clippy * playground clippy * ported profile loading fix * profile change for real, url println and clippy * PR changes --------- Co-authored-by: Wyatt <wyatt@modrinth.com>
This commit is contained in:
66
theseus_gui/src-tauri/src/api/mod.rs
Normal file
66
theseus_gui/src-tauri/src/api/mod.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use serde::ser::SerializeStruct;
|
||||
use serde::{Serialize, Serializer};
|
||||
use thiserror::Error;
|
||||
|
||||
pub mod profile;
|
||||
pub mod profile_create;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, TheseusGuiError>;
|
||||
|
||||
// Main returnable Theseus GUI error
|
||||
// Needs to be Serializable to be returned to the JavaScript side
|
||||
#[derive(Error, Debug, Serialize)]
|
||||
pub enum TheseusGuiError {
|
||||
#[error(transparent)]
|
||||
Serializable(TheseusSerializableError),
|
||||
}
|
||||
|
||||
// Serializable error intermediary, so TheseusGuiError can be Serializable (eg: so that we can return theseus::Errors in Tauri directly)
|
||||
#[derive(Error, Debug)]
|
||||
pub enum TheseusSerializableError {
|
||||
#[error("Theseus API error: {0}")]
|
||||
Theseus(#[from] theseus::Error),
|
||||
|
||||
#[error("IO error: {0}")]
|
||||
IO(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
// Generic implementation of From<T> for ErrorTypeA
|
||||
impl<T> From<T> for TheseusGuiError
|
||||
where
|
||||
TheseusSerializableError: From<T>,
|
||||
{
|
||||
fn from(error: T) -> Self {
|
||||
TheseusGuiError::Serializable(TheseusSerializableError::from(error))
|
||||
}
|
||||
}
|
||||
|
||||
// This is a very simple macro that implements a very basic Serializable for each variant of TheseusSerializableError,
|
||||
// where the field is the string. (This allows easy extension to errors without many match arms)
|
||||
macro_rules! impl_serialize {
|
||||
($($variant:ident),* $(,)?) => {
|
||||
impl Serialize for TheseusSerializableError {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self {
|
||||
$(
|
||||
TheseusSerializableError::$variant(message) => {
|
||||
let mut state = serializer.serialize_struct(stringify!($variant), 2)?;
|
||||
state.serialize_field("field_name", stringify!($variant))?;
|
||||
state.serialize_field("message", &message.to_string())?;
|
||||
state.end()
|
||||
},
|
||||
)*
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Use the macro to implement Serialize for TheseusSerializableError
|
||||
impl_serialize! {
|
||||
Theseus,
|
||||
IO,
|
||||
}
|
||||
124
theseus_gui/src-tauri/src/api/profile.rs
Normal file
124
theseus_gui/src-tauri/src/api/profile.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use crate::api::Result;
|
||||
use std::path::{Path, PathBuf};
|
||||
use theseus::prelude::*;
|
||||
|
||||
// Add a profile to the in-memory state
|
||||
// invoke('profile_add',profile)
|
||||
#[tauri::command]
|
||||
pub async fn profile_add(profile: Profile) -> Result<()> {
|
||||
let res = profile::add(profile).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Add a path as a profile in-memory
|
||||
// invoke('profile_add_path',path)
|
||||
#[tauri::command]
|
||||
pub async fn profile_add_path(path: &Path) -> Result<()> {
|
||||
let res = profile::add_path(path).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Remove a profile
|
||||
// invoke('profile_add_path',path)
|
||||
#[tauri::command]
|
||||
pub async fn profile_remove(path: &Path) -> Result<()> {
|
||||
let res = profile::remove(path).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Get a profile by path
|
||||
// invoke('profile_add_path',path)
|
||||
#[tauri::command]
|
||||
pub async fn profile_get(path: &Path) -> Result<Option<Profile>> {
|
||||
let res = profile::get(path).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Check if a profile is already managed by Theseus
|
||||
// invoke('profile_is_managed',profile)
|
||||
#[tauri::command]
|
||||
pub async fn profile_is_managed(profile: &Path) -> Result<bool> {
|
||||
let res = profile::is_managed(profile).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Check if a profile is loaded
|
||||
// invoke('profile_is_loaded',profile)
|
||||
#[tauri::command]
|
||||
pub async fn profile_is_loaded(profile: &Path) -> Result<bool> {
|
||||
let res = profile::is_loaded(profile).await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Get a copy of the profile set
|
||||
// invoke('profile_list')
|
||||
#[tauri::command]
|
||||
pub async fn profile_list(
|
||||
) -> Result<std::collections::HashMap<PathBuf, Option<Profile>>> {
|
||||
let res = profile::list().await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Run Minecraft using a profile
|
||||
// Returns a u32 representing the PID, which can be used to poll
|
||||
// for the actual Child in the state.
|
||||
// invoke('profile_run')
|
||||
#[tauri::command]
|
||||
pub async fn profile_run(
|
||||
path: &Path,
|
||||
credentials: theseus::auth::Credentials,
|
||||
) -> Result<u32> {
|
||||
let proc_lock = profile::run(path, &credentials).await?;
|
||||
let pid = proc_lock.read().await.id().ok_or_else(|| {
|
||||
theseus::Error::from(theseus::ErrorKind::LauncherError(format!(
|
||||
"Process failed to stay open."
|
||||
)))
|
||||
})?;
|
||||
Ok(pid)
|
||||
}
|
||||
|
||||
// Run Minecraft using a profile, and wait for the result
|
||||
// invoke('profile_wait_for', path, credentials)
|
||||
#[tauri::command]
|
||||
pub async fn profile_run_wait(
|
||||
path: &Path,
|
||||
credentials: theseus::auth::Credentials,
|
||||
) -> Result<()> {
|
||||
let proc_lock = profile::run(path, &credentials).await?;
|
||||
let mut proc = proc_lock.write().await;
|
||||
Ok(profile::wait_for(&mut proc).await?)
|
||||
}
|
||||
|
||||
// Wait for a running minecraft process (a Child)
|
||||
// invoke('profile_wait_for', pid)
|
||||
#[tauri::command]
|
||||
pub async fn profile_wait_for(pid: u32) -> Result<()> {
|
||||
let st = State::get().await?;
|
||||
if let Some(proc_lock) = st.children.blocking_read().get(&pid) {
|
||||
let mut proc = proc_lock.write().await;
|
||||
return Ok(profile::wait_for(&mut proc).await?);
|
||||
}
|
||||
// If child is gone from state, it's not tracked or already finished
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Tries to kill a running minecraft process (if PID is still stored)
|
||||
// invoke('profile_kill', pid)
|
||||
#[tauri::command]
|
||||
pub async fn profile_kill(pid: u32) -> Result<()> {
|
||||
let st = State::get().await?;
|
||||
let st = State::get().await?;
|
||||
if let Some(proc_lock) = st.children.blocking_read().get(&pid) {
|
||||
let mut proc = proc_lock.write().await;
|
||||
return Ok(profile::kill(&mut proc).await?);
|
||||
}
|
||||
// If child is gone from state, it's not tracked or already finished
|
||||
Ok(())
|
||||
}
|
||||
34
theseus_gui/src-tauri/src/api/profile_create.rs
Normal file
34
theseus_gui/src-tauri/src/api/profile_create.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::api::Result;
|
||||
use std::path::PathBuf;
|
||||
use theseus::prelude::*;
|
||||
|
||||
// Generic basic profile creation tool.
|
||||
// Creates an essentially empty dummy profile with profile_create
|
||||
#[tauri::command]
|
||||
pub async fn profile_create_empty() -> Result<PathBuf> {
|
||||
let res = profile_create::profile_create_empty().await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
// Creates a profile at the given filepath and adds it to the in-memory state
|
||||
// invoke('profile_add',profile)
|
||||
#[tauri::command]
|
||||
pub async fn profile_create(
|
||||
name: String, // the name of the profile, and relative path
|
||||
game_version: String, // the game version of the profile
|
||||
modloader: ModLoader, // the modloader to use
|
||||
loader_version: String, // the modloader version to use, set to "latest", "stable", or the ID of your chosen loader
|
||||
icon: Option<PathBuf>, // the icon for the profile
|
||||
) -> Result<PathBuf> {
|
||||
let res = profile_create::profile_create(
|
||||
name,
|
||||
game_version,
|
||||
modloader,
|
||||
loader_version,
|
||||
icon,
|
||||
)
|
||||
.await?;
|
||||
State::sync().await?;
|
||||
Ok(res)
|
||||
}
|
||||
Reference in New Issue
Block a user