Merge commit '81ec068747a39e927c42273011252daaa58f1e14' into feature-clean

This commit is contained in:
2024-12-26 16:51:17 +03:00
361 changed files with 25873 additions and 23923 deletions

View File

@@ -474,7 +474,11 @@ impl CacheValue {
| CacheValue::DonationPlatforms(_) => DEFAULT_ID.to_string(),
CacheValue::FileHash(hash) => {
format!("{}-{}", hash.size, hash.path.replace(".disabled", ""))
format!(
"{}-{}",
hash.size,
hash.path.trim_end_matches(".disabled")
)
}
CacheValue::FileUpdate(hash) => {
format!("{}-{}-{}", hash.hash, hash.loader, hash.game_version)

View File

@@ -1,7 +1,11 @@
use crate::state::DirectoryInfo;
use sqlx::migrate::MigrateDatabase;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::sqlite::{
SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions,
};
use sqlx::{Pool, Sqlite};
use std::str::FromStr;
use std::time::Duration;
pub(crate) async fn connect() -> crate::Result<Pool<Sqlite>> {
let settings_dir = DirectoryInfo::get_initial_settings_dir().ok_or(
@@ -20,9 +24,14 @@ pub(crate) async fn connect() -> crate::Result<Pool<Sqlite>> {
Sqlite::create_database(&uri).await?;
}
let conn_options = SqliteConnectOptions::from_str(&uri)?
.busy_timeout(Duration::from_secs(30))
.journal_mode(SqliteJournalMode::Wal)
.optimize_on_close(true, None);
let pool = SqlitePoolOptions::new()
.max_connections(100)
.connect(&uri)
.connect_with(conn_options)
.await?;
sqlx::migrate!().run(&pool).await?;

View File

@@ -0,0 +1,349 @@
use crate::config::{MODRINTH_API_URL_V3, MODRINTH_SOCKET_URL};
use crate::data::ModrinthCredentials;
use crate::event::emit::emit_friend;
use crate::event::FriendPayload;
use crate::state::{ProcessManager, Profile};
use crate::util::fetch::{fetch_advanced, fetch_json, FetchSemaphore};
use async_tungstenite::tokio::{connect_async, ConnectStream};
use async_tungstenite::tungstenite::client::IntoClientRequest;
use async_tungstenite::tungstenite::Message;
use async_tungstenite::WebSocketStream;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use futures::stream::SplitSink;
use futures::{SinkExt, StreamExt};
use reqwest::header::HeaderValue;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::RwLock;
type WriteSocket =
Arc<RwLock<Option<SplitSink<WebSocketStream<ConnectStream>, Message>>>>;
pub struct FriendsSocket {
write: WriteSocket,
user_statuses: Arc<DashMap<String, UserStatus>>,
}
#[derive(Deserialize, Serialize)]
pub struct UserFriend {
pub id: String,
pub friend_id: String,
pub accepted: bool,
pub created: DateTime<Utc>,
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ClientToServerMessage {
StatusUpdate { profile_name: Option<String> },
}
#[derive(Deserialize, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerToClientMessage {
StatusUpdate { status: UserStatus },
UserOffline { id: String },
FriendStatuses { statuses: Vec<UserStatus> },
FriendRequest { from: String },
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct UserStatus {
pub user_id: String,
pub profile_name: Option<String>,
pub last_update: DateTime<Utc>,
}
impl Default for FriendsSocket {
fn default() -> Self {
Self::new()
}
}
impl FriendsSocket {
pub fn new() -> Self {
Self {
write: Arc::new(RwLock::new(None)),
user_statuses: Arc::new(DashMap::new()),
}
}
#[tracing::instrument(skip_all)]
pub async fn connect(
&self,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore,
process_manager: &ProcessManager,
) -> crate::Result<()> {
let credentials =
ModrinthCredentials::get_and_refresh(exec, semaphore).await?;
if let Some(credentials) = credentials {
let mut request = format!(
"{MODRINTH_SOCKET_URL}_internal/launcher_socket?code={}",
credentials.session
)
.into_client_request()?;
let user_agent = format!(
"modrinth/theseus/{} (support@modrinth.com)",
env!("CARGO_PKG_VERSION")
);
request.headers_mut().insert(
"User-Agent",
HeaderValue::from_str(&user_agent).unwrap(),
);
let res = connect_async(request).await;
match res {
Ok((socket, _)) => {
tracing::info!("Connected to friends socket");
let (write, read) = socket.split();
{
let mut write_lock = self.write.write().await;
*write_lock = Some(write);
}
if let Some(process) = process_manager.get_all().first() {
let profile =
Profile::get(&process.profile_path, exec).await?;
if let Some(profile) = profile {
let _ =
self.update_status(Some(profile.name)).await;
}
}
let write_handle = self.write.clone();
let statuses = self.user_statuses.clone();
tokio::spawn(async move {
let mut read_stream = read;
while let Some(msg_result) = read_stream.next().await {
match msg_result {
Ok(msg) => {
let server_message = match msg {
Message::Text(text) => {
serde_json::from_str::<
ServerToClientMessage,
>(
&text
)
.ok()
}
Message::Binary(bytes) => {
serde_json::from_slice::<
ServerToClientMessage,
>(
&bytes
)
.ok()
}
Message::Ping(bytes) => {
if let Some(write) = write_handle
.write()
.await
.as_mut()
{
let _ = write
.send(Message::Pong(bytes))
.await;
}
continue;
}
Message::Pong(_)
| Message::Frame(_) => continue,
Message::Close(_) => break,
};
if let Some(server_message) = server_message
{
match server_message {
ServerToClientMessage::StatusUpdate { status } => {
statuses.insert(status.user_id.clone(), status.clone());
let _ = emit_friend(FriendPayload::StatusUpdate { user_status: status }).await;
},
ServerToClientMessage::UserOffline { id } => {
statuses.remove(&id);
let _ = emit_friend(FriendPayload::UserOffline { id }).await;
}
ServerToClientMessage::FriendStatuses { statuses: new_statuses } => {
statuses.clear();
new_statuses.into_iter().for_each(|status| {
statuses.insert(status.user_id.clone(), status);
});
let _ = emit_friend(FriendPayload::StatusSync).await;
}
ServerToClientMessage::FriendRequest { from } => {
let _ = emit_friend(FriendPayload::FriendRequest { from }).await;
}
}
}
}
Err(e) => {
tracing::error!("Error handling message from websocket server: {:?}", e);
}
}
}
let mut w = write_handle.write().await;
*w = None;
});
}
Err(e) => {
tracing::error!(
"Error connecting to friends socket: {e:?}"
);
return Err(crate::Error::from(e));
}
}
}
Ok(())
}
#[tracing::instrument(skip_all)]
pub async fn socket_loop() -> crate::Result<()> {
let state = crate::State::get().await?;
tokio::task::spawn(async move {
let mut last_connection = Utc::now();
let mut last_ping = Utc::now();
loop {
let connected = {
let read = state.friends_socket.write.read().await;
read.is_some()
};
if !connected
&& Utc::now().signed_duration_since(last_connection)
> chrono::Duration::seconds(30)
{
last_connection = Utc::now();
last_ping = Utc::now();
let _ = state
.friends_socket
.connect(
&state.pool,
&state.api_semaphore,
&state.process_manager,
)
.await;
} else if connected
&& Utc::now().signed_duration_since(last_ping)
> chrono::Duration::seconds(10)
{
last_ping = Utc::now();
let mut write = state.friends_socket.write.write().await;
if let Some(write) = write.as_mut() {
let _ = write.send(Message::Ping(Vec::new())).await;
}
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
});
Ok(())
}
#[tracing::instrument(skip(self))]
pub async fn disconnect(&self) -> crate::Result<()> {
let mut write_lock = self.write.write().await;
if let Some(ref mut write_half) = *write_lock {
write_half.close().await?;
*write_lock = None;
}
Ok(())
}
#[tracing::instrument(skip(self))]
pub async fn update_status(
&self,
profile_name: Option<String>,
) -> crate::Result<()> {
let mut write_lock = self.write.write().await;
if let Some(ref mut write_half) = *write_lock {
write_half
.send(Message::Text(serde_json::to_string(
&ClientToServerMessage::StatusUpdate { profile_name },
)?))
.await?;
}
Ok(())
}
#[tracing::instrument(skip_all)]
pub async fn friends(
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore,
) -> crate::Result<Vec<UserFriend>> {
fetch_json(
Method::GET,
&format!("{MODRINTH_API_URL_V3}friends"),
None,
None,
semaphore,
exec,
)
.await
}
#[tracing::instrument(skip(self))]
pub fn friend_statuses(&self) -> Vec<UserStatus> {
self.user_statuses
.iter()
.map(|x| x.value().clone())
.collect()
}
#[tracing::instrument(skip(exec, semaphore))]
pub async fn add_friend(
user_id: &str,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore,
) -> crate::Result<()> {
fetch_advanced(
Method::POST,
&format!("{MODRINTH_API_URL_V3}friend/{user_id}"),
None,
None,
None,
None,
semaphore,
exec,
)
.await?;
Ok(())
}
#[tracing::instrument(skip(exec, semaphore))]
pub async fn remove_friend(
user_id: &str,
exec: impl sqlx::Executor<'_, Database = sqlx::Sqlite> + Copy,
semaphore: &FetchSemaphore,
) -> crate::Result<()> {
fetch_advanced(
Method::DELETE,
&format!("{MODRINTH_API_URL_V3}friend/{user_id}"),
None,
None,
None,
None,
semaphore,
exec,
)
.await?;
Ok(())
}
}

View File

@@ -87,7 +87,7 @@ pub async fn init_watcher() -> crate::Result<FileWatcher> {
pub(crate) async fn watch_profiles_init(
watcher: &FileWatcher,
dirs: &DirectoryInfo,
) -> crate::Result<()> {
) {
if let Ok(profiles_dir) = std::fs::read_dir(dirs.profiles_dir()) {
for profile_dir in profiles_dir {
if let Ok(file_name) = profile_dir.map(|x| x.file_name()) {
@@ -96,20 +96,18 @@ pub(crate) async fn watch_profiles_init(
continue;
};
watch_profile(file_name, watcher, dirs).await?;
watch_profile(file_name, watcher, dirs).await;
}
}
}
}
Ok(())
}
pub(crate) async fn watch_profile(
profile_path: &str,
watcher: &FileWatcher,
dirs: &DirectoryInfo,
) -> crate::Result<()> {
) {
let profile_path = dirs.profiles_dir().join(profile_path);
if profile_path.exists() && profile_path.is_dir() {
@@ -120,15 +118,25 @@ pub(crate) async fn watch_profile(
let path = profile_path.join(folder);
if !path.exists() && !path.is_symlink() {
crate::util::io::create_dir_all(&path).await?;
if let Err(e) = crate::util::io::create_dir_all(&path).await {
tracing::error!(
"Failed to create directory for watcher {path:?}: {e}"
);
return;
}
}
let mut watcher = watcher.write().await;
watcher.watcher().watch(&path, RecursiveMode::Recursive)?;
if let Err(e) =
watcher.watcher().watch(&path, RecursiveMode::Recursive)
{
tracing::error!(
"Failed to watch directory for watcher {path:?}: {e}"
);
return;
}
}
}
Ok(())
}
fn crash_task(path: String) {

View File

@@ -31,6 +31,9 @@ pub use self::minecraft_auth::*;
mod cache;
pub use self::cache::*;
mod friends;
pub use self::friends::*;
pub mod db;
pub mod fs_watcher;
mod mr_auth;
@@ -60,6 +63,9 @@ pub struct State {
/// Process manager
pub process_manager: ProcessManager,
/// Friends socket
pub friends_socket: FriendsSocket,
pub(crate) pool: SqlitePool,
pub(crate) file_watcher: FileWatcher,
@@ -81,6 +87,16 @@ impl State {
if let Err(e) = res {
tracing::error!("Error running discord RPC: {e}");
}
let _ = state
.friends_socket
.connect(
&state.pool,
&state.api_semaphore,
&state.process_manager,
)
.await;
let _ = FriendsSocket::socket_loop().await;
});
Ok(())
@@ -89,7 +105,10 @@ impl State {
/// Get the current launcher state, waiting for initialization
pub async fn get() -> crate::Result<Arc<Self>> {
if !LAUNCHER_STATE.initialized() {
while !LAUNCHER_STATE.initialized() {}
tracing::error!("Attempted to get state before it is initialized - this should never happen!");
while !LAUNCHER_STATE.initialized() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
}
Ok(Arc::clone(
@@ -103,10 +122,12 @@ impl State {
#[tracing::instrument]
async fn initialize_state() -> crate::Result<Arc<Self>> {
tracing::info!("Connecting to app database");
let pool = db::connect().await?;
legacy_converter::migrate_legacy_data(&pool).await?;
tracing::info!("Fetching app settings");
let mut settings = Settings::get(&pool).await?;
let fetch_semaphore =
@@ -116,6 +137,7 @@ impl State {
let api_semaphore =
FetchSemaphore(Semaphore::new(settings.max_concurrent_downloads));
tracing::info!("Initializing directories");
DirectoryInfo::move_launcher_directory(
&mut settings,
&pool,
@@ -126,8 +148,13 @@ impl State {
let discord_rpc = DiscordGuard::init()?;
tracing::info!("Initializing file watcher");
let file_watcher = fs_watcher::init_watcher().await?;
fs_watcher::watch_profiles_init(&file_watcher, &directories).await?;
fs_watcher::watch_profiles_init(&file_watcher, &directories).await;
let process_manager = ProcessManager::new();
let friends_socket = FriendsSocket::new();
Ok(Arc::new(Self {
directories,
@@ -135,7 +162,8 @@ impl State {
io_semaphore,
api_semaphore,
discord_rpc,
process_manager: ProcessManager::new(),
process_manager,
friends_socket,
pool,
file_watcher,
}))

View File

@@ -1,4 +1,4 @@
use crate::config::MODRINTH_API_URL;
use crate::config::{MODRINTH_API_URL, MODRINTH_URL};
use crate::state::{CacheBehaviour, CachedEntry};
use crate::util::fetch::{fetch_advanced, FetchSemaphore};
use chrono::{DateTime, Duration, TimeZone, Utc};
@@ -190,8 +190,8 @@ impl ModrinthCredentials {
}
}
pub fn get_login_url() -> &'static str {
"https:/modrinth.com/auth/sign-in?launcher=true"
pub fn get_login_url() -> String {
format!("{MODRINTH_URL}auth/sign-in?launcher=true")
}
pub async fn finish_login_flow(

View File

@@ -206,6 +206,8 @@ impl Process {
let _ = state.discord_rpc.clear_to_default(true).await;
let _ = state.friends_socket.update_status(None).await;
// If in tauri, window should show itself again after process exists if it was hidden
#[cfg(feature = "tauri")]
{

View File

@@ -193,7 +193,7 @@ impl ProjectType {
ProjectType::Mod => "mod",
ProjectType::DataPack => "datapack",
ProjectType::ResourcePack => "resourcepack",
ProjectType::ShaderPack => "shaderpack",
ProjectType::ShaderPack => "shader",
}
}
@@ -664,7 +664,7 @@ impl Profile {
path: format!(
"{}/{folder}/{}",
self.path,
file_name.replace(".disabled", "")
file_name.trim_end_matches(".disabled")
),
file_name: file_name.to_string(),
project_type,
@@ -725,8 +725,9 @@ impl Profile {
let info_index = file_info.iter().position(|x| x.hash == hash.hash);
let file = info_index.map(|x| file_info.remove(x));
if let Some(initial_file_index) =
keys.iter().position(|x| x.path == hash.path)
if let Some(initial_file_index) = keys
.iter()
.position(|x| x.path == hash.path.trim_end_matches(".disabled"))
{
let initial_file = keys.remove(initial_file_index);
@@ -890,7 +891,7 @@ impl Profile {
let path = crate::api::profile::get_full_path(profile_path).await?;
let new_path = if project_path.ends_with(".disabled") {
project_path.replace(".disabled", "")
project_path.trim_end_matches(".disabled").to_string()
} else {
format!("{project_path}.disabled")
};

View File

@@ -1,5 +1,7 @@
//! Theseus settings file
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// Types
/// Global Theseus settings
@@ -13,10 +15,10 @@ pub struct Settings {
pub collapsed_navigation: bool,
pub advanced_rendering: bool,
pub native_decorations: bool,
pub toggle_sidebar: bool,
pub telemetry: bool,
pub discord_rpc: bool,
pub developer_mode: bool,
pub personalized_ads: bool,
pub onboarded: bool,
@@ -32,6 +34,16 @@ pub struct Settings {
pub custom_dir: Option<String>,
pub prev_custom_dir: Option<String>,
pub migrated: bool,
pub developer_mode: bool,
pub feature_flags: HashMap<FeatureFlag, bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, Hash, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FeatureFlag {
PagePath,
ProjectBackground,
}
impl Settings {
@@ -48,7 +60,7 @@ impl Settings {
json(extra_launch_args) extra_launch_args, json(custom_env_vars) custom_env_vars,
mc_memory_max, mc_force_fullscreen, mc_game_resolution_x, mc_game_resolution_y, hide_on_process_start,
hook_pre_launch, hook_wrapper, hook_post_exit,
custom_dir, prev_custom_dir, migrated
custom_dir, prev_custom_dir, migrated, json(feature_flags) feature_flags, toggle_sidebar
FROM settings
"
)
@@ -63,6 +75,7 @@ impl Settings {
collapsed_navigation: res.collapsed_navigation == 1,
advanced_rendering: res.advanced_rendering == 1,
native_decorations: res.native_decorations == 1,
toggle_sidebar: res.toggle_sidebar == 1,
telemetry: res.telemetry == 1,
discord_rpc: res.discord_rpc == 1,
developer_mode: res.developer_mode == 1,
@@ -95,6 +108,11 @@ impl Settings {
custom_dir: res.custom_dir,
prev_custom_dir: res.prev_custom_dir,
migrated: res.migrated == 1,
feature_flags: res
.feature_flags
.as_ref()
.and_then(|x| serde_json::from_str(x).ok())
.unwrap_or_default(),
})
}
@@ -108,6 +126,7 @@ impl Settings {
let default_page = self.default_page.as_str();
let extra_launch_args = serde_json::to_string(&self.extra_launch_args)?;
let custom_env_vars = serde_json::to_string(&self.custom_env_vars)?;
let feature_flags = serde_json::to_string(&self.feature_flags)?;
sqlx::query!(
"
@@ -143,7 +162,10 @@ impl Settings {
custom_dir = $23,
prev_custom_dir = $24,
migrated = $25
migrated = $25,
toggle_sidebar = $26,
feature_flags = $27
",
max_concurrent_writes,
max_concurrent_downloads,
@@ -169,7 +191,9 @@ impl Settings {
self.hooks.post_exit,
self.custom_dir,
self.prev_custom_dir,
self.migrated
self.migrated,
self.toggle_sidebar,
feature_flags
)
.execute(exec)
.await?;
@@ -185,6 +209,7 @@ pub enum Theme {
Dark,
Light,
Oled,
System,
}
impl Theme {
@@ -193,6 +218,7 @@ impl Theme {
Theme::Dark => "dark",
Theme::Light => "light",
Theme::Oled => "oled",
Theme::System => "system",
}
}
@@ -201,6 +227,7 @@ impl Theme {
"dark" => Theme::Dark,
"light" => Theme::Light,
"oled" => Theme::Oled,
"system" => Theme::System,
_ => Theme::Dark,
}
}