2022-06-27 15:53:25 -07:00
|
|
|
//! Theseus state management system
|
2024-07-24 11:03:19 -07:00
|
|
|
use crate::util::fetch::{FetchSemaphore, IoSemaphore};
|
2022-06-27 15:53:25 -07:00
|
|
|
use std::sync::Arc;
|
2024-07-24 11:03:19 -07:00
|
|
|
use tokio::sync::{OnceCell, Semaphore};
|
2022-06-27 15:53:25 -07:00
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
use crate::state::fs_watcher::FileWatcher;
|
|
|
|
|
use sqlx::SqlitePool;
|
2023-05-16 15:30:04 -07:00
|
|
|
|
2022-06-27 15:53:25 -07:00
|
|
|
// Submodules
|
|
|
|
|
mod dirs;
|
|
|
|
|
pub use self::dirs::*;
|
|
|
|
|
|
2022-07-15 15:39:38 +00:00
|
|
|
mod profiles;
|
|
|
|
|
pub use self::profiles::*;
|
2022-06-27 15:53:25 -07:00
|
|
|
|
|
|
|
|
mod settings;
|
2022-07-15 15:39:38 +00:00
|
|
|
pub use self::settings::*;
|
2022-06-27 15:53:25 -07:00
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
mod process;
|
|
|
|
|
pub use self::process::*;
|
2023-04-03 16:08:53 -07:00
|
|
|
|
2023-04-07 13:31:06 -07:00
|
|
|
mod java_globals;
|
|
|
|
|
pub use self::java_globals::*;
|
|
|
|
|
|
2023-07-27 00:10:07 -07:00
|
|
|
mod discord;
|
|
|
|
|
pub use self::discord::*;
|
|
|
|
|
|
2024-04-15 13:58:20 -07:00
|
|
|
mod minecraft_auth;
|
|
|
|
|
pub use self::minecraft_auth::*;
|
|
|
|
|
|
2025-07-02 21:32:15 +01:00
|
|
|
pub mod minecraft_skins;
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
mod cache;
|
|
|
|
|
pub use self::cache::*;
|
|
|
|
|
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
mod friends;
|
|
|
|
|
pub use self::friends::*;
|
|
|
|
|
|
2025-02-28 12:52:47 -06:00
|
|
|
mod tunnel;
|
|
|
|
|
pub use self::tunnel::*;
|
|
|
|
|
|
2024-08-16 23:20:11 -07:00
|
|
|
pub mod db;
|
2024-07-24 11:03:19 -07:00
|
|
|
pub mod fs_watcher;
|
2023-08-04 23:38:34 -07:00
|
|
|
mod mr_auth;
|
2024-07-24 11:03:19 -07:00
|
|
|
|
2023-08-04 23:38:34 -07:00
|
|
|
pub use self::mr_auth::*;
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
mod legacy_converter;
|
|
|
|
|
|
2025-05-01 16:13:13 -07:00
|
|
|
pub mod attached_world_data;
|
2025-04-26 18:09:58 -07:00
|
|
|
pub mod server_join_log;
|
|
|
|
|
|
2022-06-27 15:53:25 -07:00
|
|
|
// Global state
|
2023-07-21 20:16:07 -07:00
|
|
|
// RwLock on state only has concurrent reads, except for config dir change which takes control of the State
|
2024-07-24 11:03:19 -07:00
|
|
|
static LAUNCHER_STATE: OnceCell<Arc<State>> = OnceCell::const_new();
|
2022-06-27 15:53:25 -07:00
|
|
|
pub struct State {
|
|
|
|
|
/// Information on the location of files used in the launcher
|
|
|
|
|
pub directories: DirectoryInfo,
|
2023-05-08 12:14:08 -07:00
|
|
|
|
|
|
|
|
/// Semaphore used to limit concurrent network requests and avoid errors
|
|
|
|
|
pub fetch_semaphore: FetchSemaphore,
|
2022-06-27 15:53:25 -07:00
|
|
|
/// Semaphore used to limit concurrent I/O and avoid errors
|
2023-05-08 12:14:08 -07:00
|
|
|
pub io_semaphore: IoSemaphore,
|
2024-07-24 11:03:19 -07:00
|
|
|
/// Semaphore to limit concurrent API requests. This is separate from the fetch semaphore
|
|
|
|
|
/// to keep API functionality while the app is performing intensive tasks.
|
|
|
|
|
pub api_semaphore: FetchSemaphore,
|
2023-05-16 15:30:04 -07:00
|
|
|
|
2023-07-27 00:10:07 -07:00
|
|
|
/// Discord RPC
|
|
|
|
|
pub discord_rpc: DiscordGuard,
|
|
|
|
|
|
2024-08-16 23:20:11 -07:00
|
|
|
/// Process manager
|
|
|
|
|
pub process_manager: ProcessManager,
|
|
|
|
|
|
2026-02-02 08:10:23 -05:00
|
|
|
/// App identifier string (like com.modrinth.ModrinthApp)
|
|
|
|
|
pub app_identifier: String,
|
|
|
|
|
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
/// Friends socket
|
|
|
|
|
pub friends_socket: FriendsSocket,
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
pub(crate) pool: SqlitePool,
|
|
|
|
|
|
|
|
|
|
pub(crate) file_watcher: FileWatcher,
|
2022-06-27 15:53:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State {
|
2026-02-02 08:10:23 -05:00
|
|
|
pub async fn init(app_identifier: String) -> crate::Result<()> {
|
2024-07-24 11:03:19 -07:00
|
|
|
let state = LAUNCHER_STATE
|
2026-02-02 08:10:23 -05:00
|
|
|
.get_or_try_init(move || Self::initialize_state(app_identifier))
|
2024-07-24 11:03:19 -07:00
|
|
|
.await?;
|
|
|
|
|
|
2024-08-16 23:20:11 -07:00
|
|
|
tokio::task::spawn(async move {
|
2024-08-20 17:53:14 -07:00
|
|
|
let res = tokio::try_join!(
|
|
|
|
|
state.discord_rpc.clear_to_default(true),
|
|
|
|
|
Profile::refresh_all(),
|
2025-10-30 22:48:51 +01:00
|
|
|
Settings::migrate(&state.pool),
|
2024-08-20 17:53:14 -07:00
|
|
|
ModrinthCredentials::refresh_all(),
|
|
|
|
|
);
|
2024-08-16 23:20:11 -07:00
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
|
tracing::error!("Error running discord RPC: {e}");
|
|
|
|
|
}
|
2024-12-12 13:25:25 -08:00
|
|
|
|
|
|
|
|
let _ = state
|
|
|
|
|
.friends_socket
|
|
|
|
|
.connect(
|
|
|
|
|
&state.pool,
|
|
|
|
|
&state.api_semaphore,
|
|
|
|
|
&state.process_manager,
|
|
|
|
|
)
|
|
|
|
|
.await;
|
2024-12-12 14:36:02 -08:00
|
|
|
let _ = FriendsSocket::socket_loop().await;
|
2024-08-16 23:20:11 -07:00
|
|
|
});
|
2024-07-24 11:03:19 -07:00
|
|
|
|
|
|
|
|
Ok(())
|
2023-07-21 20:16:07 -07:00
|
|
|
}
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
/// Get the current launcher state, waiting for initialization
|
|
|
|
|
pub async fn get() -> crate::Result<Arc<Self>> {
|
|
|
|
|
if !LAUNCHER_STATE.initialized() {
|
2025-05-02 04:51:17 -05:00
|
|
|
tracing::error!(
|
|
|
|
|
"Attempted to get state before it is initialized - this should never happen!"
|
|
|
|
|
);
|
2024-12-14 19:23:47 -07:00
|
|
|
while !LAUNCHER_STATE.initialized() {
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
|
|
|
|
}
|
2024-07-24 11:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Arc::clone(
|
|
|
|
|
LAUNCHER_STATE.get().expect("State is not initialized!"),
|
|
|
|
|
))
|
2023-07-21 20:16:07 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-12 09:27:03 -07:00
|
|
|
pub fn initialized() -> bool {
|
|
|
|
|
LAUNCHER_STATE.initialized()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 10:31:52 -07:00
|
|
|
#[tracing::instrument]
|
2026-02-02 08:10:23 -05:00
|
|
|
async fn initialize_state(
|
|
|
|
|
app_identifier: String,
|
|
|
|
|
) -> crate::Result<Arc<Self>> {
|
2024-12-22 20:03:58 -07:00
|
|
|
tracing::info!("Connecting to app database");
|
2026-02-02 08:10:23 -05:00
|
|
|
let pool = db::connect(&app_identifier).await?;
|
2023-07-21 20:16:07 -07:00
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
legacy_converter::migrate_legacy_data(&pool).await?;
|
2023-07-21 20:16:07 -07:00
|
|
|
|
2024-12-22 20:03:58 -07:00
|
|
|
tracing::info!("Fetching app settings");
|
2024-07-24 11:03:19 -07:00
|
|
|
let mut settings = Settings::get(&pool).await?;
|
2023-07-21 20:16:07 -07:00
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
let fetch_semaphore =
|
|
|
|
|
FetchSemaphore(Semaphore::new(settings.max_concurrent_downloads));
|
|
|
|
|
let io_semaphore =
|
|
|
|
|
IoSemaphore(Semaphore::new(settings.max_concurrent_writes));
|
|
|
|
|
let api_semaphore =
|
|
|
|
|
FetchSemaphore(Semaphore::new(settings.max_concurrent_downloads));
|
2023-07-21 20:16:07 -07:00
|
|
|
|
2024-12-22 20:03:58 -07:00
|
|
|
tracing::info!("Initializing directories");
|
2024-07-24 11:03:19 -07:00
|
|
|
DirectoryInfo::move_launcher_directory(
|
|
|
|
|
&mut settings,
|
|
|
|
|
&pool,
|
|
|
|
|
&io_semaphore,
|
2026-02-02 08:10:23 -05:00
|
|
|
&app_identifier,
|
2024-07-24 11:03:19 -07:00
|
|
|
)
|
|
|
|
|
.await?;
|
2026-02-02 08:10:23 -05:00
|
|
|
|
|
|
|
|
let directories =
|
|
|
|
|
DirectoryInfo::init(settings.custom_dir, &app_identifier).await?;
|
2023-08-04 19:51:46 -07:00
|
|
|
|
2024-08-16 23:20:11 -07:00
|
|
|
let discord_rpc = DiscordGuard::init()?;
|
2023-07-27 00:10:07 -07:00
|
|
|
|
2024-12-22 20:03:58 -07:00
|
|
|
tracing::info!("Initializing file watcher");
|
2024-07-24 11:03:19 -07:00
|
|
|
let file_watcher = fs_watcher::init_watcher().await?;
|
2024-12-17 23:23:30 -07:00
|
|
|
fs_watcher::watch_profiles_init(&file_watcher, &directories).await;
|
2023-08-04 19:51:46 -07:00
|
|
|
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
let process_manager = ProcessManager::new();
|
|
|
|
|
|
|
|
|
|
let friends_socket = FriendsSocket::new();
|
|
|
|
|
|
2024-07-24 11:03:19 -07:00
|
|
|
Ok(Arc::new(Self {
|
2023-07-21 20:16:07 -07:00
|
|
|
directories,
|
|
|
|
|
fetch_semaphore,
|
|
|
|
|
io_semaphore,
|
2024-07-24 11:03:19 -07:00
|
|
|
api_semaphore,
|
2023-07-27 00:10:07 -07:00
|
|
|
discord_rpc,
|
App redesign (#2946)
* Start of app redesign
* format
* continue progress
* Content page nearly done
* Fix recursion issues with content page
* Fix update all alignment
* Discover page progress
* Settings progress
* Removed unlocked-size hack that breaks web
* Revamp project page, refactor web project page to share code with app, fixed loading bar, misc UI/UX enhancements, update ko-fi logo, update arrow icons, fix web issues caused by floating-vue migration, fix tooltip issues, update web tooltips, clean up web hydration issues
* Ads + run prettier
* Begin auth refactor, move common messages to ui lib, add i18n extraction to all apps, begin Library refactor
* fix ads not hiding when plus log in
* rev lockfile changes/conflicts
* Fix sign in page
* Add generated
* (mostly) Data driven search
* Fix search mobile issue
* profile fixes
* Project versions page, fix typescript on UI lib and misc fixes
* Remove unused gallery component
* Fix linkfunction err
* Search filter controls at top, localization for locked filters
* Fix provided filter names
* Fix navigating from instance browse to main browse
* Friends frontend (#2995)
* Friends system frontend
* (almost) finish frontend
* finish friends, fix lint
* Fix lint
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
* Refresh macOS app icon
* Update web search UI more
* Fix link opens
* Fix frontend build
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Jai A <jaiagr+gpg@pm.me>
Co-authored-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
2024-12-11 19:54:18 -08:00
|
|
|
process_manager,
|
|
|
|
|
friends_socket,
|
2024-07-24 11:03:19 -07:00
|
|
|
pool,
|
|
|
|
|
file_watcher,
|
2026-02-02 08:10:23 -05:00
|
|
|
app_identifier,
|
2023-07-21 20:16:07 -07:00
|
|
|
}))
|
2022-06-27 15:53:25 -07:00
|
|
|
}
|
2023-05-16 15:30:04 -07:00
|
|
|
}
|