|
| 1 | +use crate::error::{AppResult, IpcResult, MutexResultExt}; |
| 2 | +use crate::mods::{ |
| 3 | + inspect_modpkg_file, install_mod_from_package, toggle_mod_enabled, uninstall_mod_by_id, |
| 4 | + InstalledMod, ModpkgInfo, |
| 5 | +}; |
| 6 | +use crate::state::SettingsState; |
| 7 | +use tauri::{AppHandle, State}; |
| 8 | + |
| 9 | +/// Get all installed mods from the mod library. |
| 10 | +#[tauri::command] |
| 11 | +pub fn get_installed_mods( |
| 12 | + app_handle: AppHandle, |
| 13 | + settings: State<SettingsState>, |
| 14 | +) -> IpcResult<Vec<InstalledMod>> { |
| 15 | + get_installed_mods_inner(&app_handle, &settings).into() |
| 16 | +} |
| 17 | + |
| 18 | +fn get_installed_mods_inner( |
| 19 | + app_handle: &AppHandle, |
| 20 | + settings: &State<SettingsState>, |
| 21 | +) -> AppResult<Vec<InstalledMod>> { |
| 22 | + let settings = settings.0.lock().mutex_err()?.clone(); |
| 23 | + |
| 24 | + crate::mods::get_installed_mods(app_handle, &settings) |
| 25 | +} |
| 26 | + |
| 27 | +/// Install a mod from a `.modpkg` or `.fantome` file into `modStoragePath`. |
| 28 | +#[tauri::command] |
| 29 | +pub fn install_mod( |
| 30 | + file_path: String, |
| 31 | + app_handle: AppHandle, |
| 32 | + settings: State<SettingsState>, |
| 33 | +) -> IpcResult<InstalledMod> { |
| 34 | + install_mod_inner(file_path, &app_handle, &settings).into() |
| 35 | +} |
| 36 | + |
| 37 | +fn install_mod_inner( |
| 38 | + file_path: String, |
| 39 | + app_handle: &AppHandle, |
| 40 | + settings: &State<SettingsState>, |
| 41 | +) -> AppResult<InstalledMod> { |
| 42 | + let settings = settings.0.lock().mutex_err()?.clone(); |
| 43 | + install_mod_from_package(app_handle, &settings, &file_path) |
| 44 | +} |
| 45 | + |
| 46 | +/// Uninstall a mod by id. |
| 47 | +#[tauri::command] |
| 48 | +pub fn uninstall_mod( |
| 49 | + mod_id: String, |
| 50 | + app_handle: AppHandle, |
| 51 | + settings: State<SettingsState>, |
| 52 | +) -> IpcResult<()> { |
| 53 | + uninstall_mod_inner(mod_id, &app_handle, &settings).into() |
| 54 | +} |
| 55 | + |
| 56 | +fn uninstall_mod_inner( |
| 57 | + mod_id: String, |
| 58 | + app_handle: &AppHandle, |
| 59 | + settings: &State<SettingsState>, |
| 60 | +) -> AppResult<()> { |
| 61 | + let settings = settings.0.lock().mutex_err()?.clone(); |
| 62 | + uninstall_mod_by_id(app_handle, &settings, &mod_id) |
| 63 | +} |
| 64 | + |
| 65 | +/// Toggle a mod's enabled state. |
| 66 | +#[tauri::command] |
| 67 | +pub fn toggle_mod( |
| 68 | + mod_id: String, |
| 69 | + enabled: bool, |
| 70 | + app_handle: AppHandle, |
| 71 | + settings: State<SettingsState>, |
| 72 | +) -> IpcResult<()> { |
| 73 | + toggle_mod_inner(mod_id, enabled, &app_handle, &settings).into() |
| 74 | +} |
| 75 | + |
| 76 | +fn toggle_mod_inner( |
| 77 | + mod_id: String, |
| 78 | + enabled: bool, |
| 79 | + app_handle: &AppHandle, |
| 80 | + settings: &State<SettingsState>, |
| 81 | +) -> AppResult<()> { |
| 82 | + let settings = settings.0.lock().mutex_err()?.clone(); |
| 83 | + toggle_mod_enabled(app_handle, &settings, &mod_id, enabled) |
| 84 | +} |
| 85 | + |
| 86 | +/// Inspect a `.modpkg` file and return its metadata. |
| 87 | +#[tauri::command] |
| 88 | +pub fn inspect_modpkg(file_path: String) -> IpcResult<ModpkgInfo> { |
| 89 | + inspect_modpkg_file(&file_path).into() |
| 90 | +} |
0 commit comments