Migrate to SQLite for Internal Launcher Data (#1300)

* initial migration

* barebones profiles

* Finish profiles

* Add back file watcher

* UI support progress

* Finish most of cache

* Fix options page

* Fix forge, finish modrinth auth

* Accounts, process cache

* Run SQLX prepare

* Finish

* Run lint + actions

* Fix version to be compat with windows

* fix lint

* actually fix lint

* actually fix lint again
This commit is contained in:
Geometrically
2024-07-24 11:03:19 -07:00
committed by GitHub
parent 90f74427d9
commit 49a20a303a
156 changed files with 9208 additions and 8547 deletions

View File

@@ -1,7 +1,6 @@
//! Downloader for Minecraft data
use crate::launcher::parse_rules;
use crate::state::CredentialsStore;
use crate::{
event::{
emit::{emit_loading, loading_try_for_each_concurrent},
@@ -19,6 +18,7 @@ use daedalus::{
modded::LoaderVersion,
};
use futures::prelude::*;
use reqwest::Method;
use tokio::sync::OnceCell;
#[tracing::instrument(skip(st, version))]
@@ -58,7 +58,7 @@ pub async fn download_minecraft(
}
#[tracing::instrument(skip_all, fields(version = version.id.as_str(), loader = ?loader))]
#[theseus_macros::debug_pin]
pub async fn download_version_info(
st: &State,
version: &GameVersion,
@@ -72,7 +72,6 @@ pub async fn download_version_info(
let path = st
.directories
.version_dir(&version_id)
.await
.join(format!("{version_id}.json"));
let res = if path.exists() && !force.unwrap_or(false) {
@@ -82,10 +81,26 @@ pub async fn download_version_info(
.and_then(|ref it| Ok(serde_json::from_slice(it)?))
} else {
tracing::info!("Downloading version info for version {}", &version.id);
let mut info = d::minecraft::fetch_version_info(version).await?;
let mut info = fetch_json(
Method::GET,
&version.url,
None,
None,
&st.api_semaphore,
&st.pool,
)
.await?;
if let Some(loader) = loader {
let partial = d::modded::fetch_partial_version(&loader.url).await?;
let partial: d::modded::PartialVersionInfo = fetch_json(
Method::GET,
&loader.url,
None,
None,
&st.api_semaphore,
&st.pool,
)
.await?;
info = d::modded::merge_partial_version(partial, info);
}
@@ -104,7 +119,7 @@ pub async fn download_version_info(
}
#[tracing::instrument(skip_all)]
#[theseus_macros::debug_pin]
pub async fn download_client(
st: &State,
version_info: &GameVersionInfo,
@@ -125,7 +140,6 @@ pub async fn download_client(
let path = st
.directories
.version_dir(version)
.await
.join(format!("{version}.jar"));
if !path.exists() || force {
@@ -133,7 +147,7 @@ pub async fn download_client(
&client_download.url,
Some(&client_download.sha1),
&st.fetch_semaphore,
&CredentialsStore(None),
&st.pool,
)
.await?;
write(&path, &bytes, &st.io_semaphore).await?;
@@ -148,7 +162,7 @@ pub async fn download_client(
}
#[tracing::instrument(skip_all)]
#[theseus_macros::debug_pin]
pub async fn download_assets_index(
st: &State,
version: &GameVersionInfo,
@@ -159,7 +173,6 @@ pub async fn download_assets_index(
let path = st
.directories
.assets_index_dir()
.await
.join(format!("{}.json", &version.asset_index.id));
let res = if path.exists() && !force {
@@ -168,7 +181,15 @@ pub async fn download_assets_index(
.await
.and_then(|ref it| Ok(serde_json::from_slice(it)?))
} else {
let index = d::minecraft::fetch_assets_index(version).await?;
let index = fetch_json(
Method::GET,
&version.asset_index.url,
None,
None,
&st.fetch_semaphore,
&st.pool,
)
.await?;
write(&path, &serde_json::to_vec(&index)?, &st.io_semaphore).await?;
tracing::info!("Fetched assets index");
Ok(index)
@@ -182,7 +203,7 @@ pub async fn download_assets_index(
}
#[tracing::instrument(skip(st, index))]
#[theseus_macros::debug_pin]
pub async fn download_assets(
st: &State,
with_legacy: bool,
@@ -204,7 +225,7 @@ pub async fn download_assets(
None,
|(name, asset)| async move {
let hash = &asset.hash;
let resource_path = st.directories.object_dir(hash).await;
let resource_path = st.directories.object_dir(hash);
let url = format!(
"https://resources.download.minecraft.net/{sub_hash}/{hash}",
sub_hash = &hash[..2]
@@ -215,7 +236,7 @@ pub async fn download_assets(
async {
if !resource_path.exists() || force {
let resource = fetch_cell
.get_or_try_init(|| fetch(&url, Some(hash), &st.fetch_semaphore, &CredentialsStore(None)))
.get_or_try_init(|| fetch(&url, Some(hash), &st.fetch_semaphore, &st.pool))
.await?;
write(&resource_path, resource, &st.io_semaphore).await?;
tracing::trace!("Fetched asset with hash {hash}");
@@ -223,13 +244,13 @@ pub async fn download_assets(
Ok::<_, crate::Error>(())
},
async {
let resource_path = st.directories.legacy_assets_dir().await.join(
let resource_path = st.directories.legacy_assets_dir().join(
name.replace('/', &String::from(std::path::MAIN_SEPARATOR))
);
if with_legacy && !resource_path.exists() || force {
let resource = fetch_cell
.get_or_try_init(|| fetch(&url, Some(hash), &st.fetch_semaphore, &CredentialsStore(None)))
.get_or_try_init(|| fetch(&url, Some(hash), &st.fetch_semaphore, &st.pool))
.await?;
write(&resource_path, resource, &st.io_semaphore).await?;
tracing::trace!("Fetched legacy asset with hash {hash}");
@@ -246,7 +267,6 @@ pub async fn download_assets(
}
#[tracing::instrument(skip(st, libraries))]
#[theseus_macros::debug_pin]
#[allow(clippy::too_many_arguments)]
pub async fn download_libraries(
st: &State,
@@ -261,8 +281,8 @@ pub async fn download_libraries(
tracing::debug!("Loading libraries");
tokio::try_join! {
io::create_dir_all(st.directories.libraries_dir().await),
io::create_dir_all(st.directories.version_natives_dir(version).await)
io::create_dir_all(st.directories.libraries_dir()),
io::create_dir_all(st.directories.version_natives_dir(version))
}?;
let num_files = libraries.len();
loading_try_for_each_concurrent(
@@ -275,38 +295,42 @@ pub async fn download_libraries(
}
}
if !library.downloadable {
tracing::trace!("Skipped non-downloadable library {}", &library.name);
return Ok(());
}
tokio::try_join! {
async {
let artifact_path = d::get_path_from_artifact(&library.name)?;
let path = st.directories.libraries_dir().await.join(&artifact_path);
let path = st.directories.libraries_dir().join(&artifact_path);
match library.downloads {
_ if path.exists() && !force => Ok(()),
Some(d::minecraft::LibraryDownloads {
artifact: Some(ref artifact),
..
}) => {
let bytes = fetch(&artifact.url, Some(&artifact.sha1), &st.fetch_semaphore, &CredentialsStore(None))
if path.exists() && !force {
return Ok(());
}
if let Some(d::minecraft::LibraryDownloads { artifact: Some(ref artifact), ..}) = library.downloads {
if !artifact.url.is_empty(){
let bytes = fetch(&artifact.url, Some(&artifact.sha1), &st.fetch_semaphore, &st.pool)
.await?;
write(&path, &bytes, &st.io_semaphore).await?;
tracing::trace!("Fetched library {} to path {:?}", &library.name, &path);
Ok::<_, crate::Error>(())
}
_ => {
let url = [
library
.url
.as_deref()
.unwrap_or("https://libraries.minecraft.net/"),
&artifact_path
].concat();
let bytes = fetch(&url, None, &st.fetch_semaphore, &CredentialsStore(None)).await?;
write(&path, &bytes, &st.io_semaphore).await?;
tracing::trace!("Fetched library {} to path {:?}", &library.name, &path);
Ok::<_, crate::Error>(())
return Ok::<_, crate::Error>(());
}
}
let url = [
library
.url
.as_deref()
.unwrap_or("https://libraries.minecraft.net/"),
&artifact_path
].concat();
let bytes = fetch(&url, None, &st.fetch_semaphore, &st.pool).await?;
write(&path, &bytes, &st.io_semaphore).await?;
tracing::trace!("Fetched library {} to path {:?}", &library.name, &path);
Ok::<_, crate::Error>(())
},
async {
// HACK: pseudo try block using or else
@@ -327,10 +351,10 @@ pub async fn download_libraries(
);
if let Some(native) = classifiers.get(&parsed_key) {
let data = fetch(&native.url, Some(&native.sha1), &st.fetch_semaphore, &CredentialsStore(None)).await?;
let data = fetch(&native.url, Some(&native.sha1), &st.fetch_semaphore, &st.pool).await?;
let reader = std::io::Cursor::new(&data);
if let Ok(mut archive) = zip::ZipArchive::new(reader) {
match archive.extract(st.directories.version_natives_dir(version).await) {
match archive.extract(st.directories.version_natives_dir(version)) {
Ok(_) => tracing::debug!("Fetched native {}", &library.name),
Err(err) => tracing::error!("Failed extracting native {}. err: {}", &library.name, err)
}

View File

@@ -1,23 +1,22 @@
//! Logic for launching Minecraft
use crate::data::ModLoader;
use crate::event::emit::{emit_loading, init_or_edit_loading};
use crate::event::{LoadingBarId, LoadingBarType};
use crate::launcher::io::IOError;
use crate::prelude::JavaVersion;
use crate::state::{Credentials, ProfileInstallStage};
use crate::state::{Credentials, JavaVersion, Process, ProfileInstallStage};
use crate::util::io;
use crate::{
process,
state::{self as st, MinecraftChild},
state::{self as st},
State,
};
use chrono::Utc;
use daedalus as d;
use daedalus::minecraft::{RuleAction, VersionInfo};
use daedalus::modded::LoaderVersion;
use st::Profile;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::process::Command;
use uuid::Uuid;
mod args;
@@ -114,86 +113,127 @@ pub async fn get_java_version_from_profile(
profile: &Profile,
version_info: &VersionInfo,
) -> crate::Result<Option<JavaVersion>> {
if let Some(java) = profile.java.clone().and_then(|x| x.override_version) {
Ok(Some(java))
} else {
let key = version_info
.java_version
.as_ref()
.map(|it| it.major_version)
.unwrap_or(8);
if let Some(java) = profile.java_path.as_ref() {
let java = crate::api::jre::check_jre(std::path::PathBuf::from(java))
.await
.ok()
.flatten();
let state = State::get().await?;
let settings = state.settings.read().await;
if let Some(java) = settings.java_globals.get(&format!("JAVA_{key}")) {
return Ok(Some(java.clone()));
if let Some(java) = java {
return Ok(Some(java));
}
}
let key = version_info
.java_version
.as_ref()
.map(|it| it.major_version)
.unwrap_or(8);
let state = State::get().await?;
let java_version = JavaVersion::get(key, &state.pool).await?;
Ok(java_version)
}
pub async fn get_loader_version_from_profile(
game_version: &str,
loader: ModLoader,
loader_version: Option<&str>,
) -> crate::Result<Option<LoaderVersion>> {
if loader == ModLoader::Vanilla {
return Ok(None);
}
let version = loader_version.unwrap_or("latest");
let filter = |it: &LoaderVersion| match version {
"latest" => true,
"stable" => it.stable,
id => it.id == *id,
};
let versions =
crate::api::metadata::get_loader_versions(loader.as_meta_str()).await?;
let loaders = versions.game_versions.into_iter().find(|x| {
x.id.replace(daedalus::modded::DUMMY_REPLACE_STRING, game_version)
== game_version
});
if let Some(loaders) = loaders {
let loader_version = loaders.loaders.iter().find(|x| filter(x)).or(
if version == "stable" {
loaders.loaders.first()
} else {
None
},
);
Ok(loader_version.cloned())
} else {
Ok(None)
}
}
#[tracing::instrument(skip(profile))]
#[theseus_macros::debug_pin]
pub async fn install_minecraft(
profile: &Profile,
existing_loading_bar: Option<LoadingBarId>,
repairing: bool,
) -> crate::Result<()> {
let sync_projects = existing_loading_bar.is_some();
let loading_bar = init_or_edit_loading(
existing_loading_bar,
LoadingBarType::MinecraftDownload {
// If we are downloading minecraft for a profile, provide its name and uuid
profile_name: profile.metadata.name.clone(),
profile_path: profile.get_profile_full_path().await?,
profile_name: profile.name.clone(),
profile_path: profile.path.clone(),
},
100.0,
"Downloading Minecraft",
)
.await?;
crate::api::profile::edit(&profile.profile_id(), |prof| {
crate::api::profile::edit(&profile.path, |prof| {
prof.install_stage = ProfileInstallStage::Installing;
async { Ok(()) }
})
.await?;
State::sync().await?;
if sync_projects {
Profile::sync_projects_task(profile.profile_id(), true);
}
let state = State::get().await?;
let instance_path =
&io::canonicalize(profile.get_profile_full_path().await?)?;
let metadata = state.metadata.read().await;
let version_index = metadata
.minecraft
let instance_path =
crate::api::profile::get_full_path(&profile.path).await?;
let minecraft = crate::api::metadata::get_minecraft_versions().await?;
let version_index = minecraft
.versions
.iter()
.position(|it| it.id == profile.metadata.game_version)
.position(|it| it.id == profile.game_version)
.ok_or(crate::ErrorKind::LauncherError(format!(
"Invalid game version: {}",
profile.metadata.game_version
profile.game_version
)))?;
let version = &metadata.minecraft.versions[version_index];
let version = &minecraft.versions[version_index];
let minecraft_updated = version_index
<= metadata
.minecraft
<= minecraft
.versions
.iter()
.position(|x| x.id == "22w16a")
.unwrap_or(0);
let version_jar = profile
.metadata
.loader_version
.as_ref()
.map_or(version.id.clone(), |it| {
let loader_version = get_loader_version_from_profile(
&profile.game_version,
profile.loader,
profile.loader_version.as_deref(),
)
.await?;
let version_jar =
loader_version.as_ref().map_or(version.id.clone(), |it| {
format!("{}-{}", version.id.clone(), it.id.clone())
});
@@ -201,14 +241,12 @@ pub async fn install_minecraft(
let mut version_info = download::download_version_info(
&state,
version,
profile.metadata.loader_version.as_ref(),
loader_version.as_ref(),
Some(repairing),
Some(&loading_bar),
)
.await?;
// TODO: check if java exists, if not install it add to install step
let key = version_info
.java_version
.as_ref()
@@ -235,13 +273,7 @@ pub async fn install_minecraft(
})?;
if set_java {
{
let mut settings = state.settings.write().await;
settings
.java_globals
.insert(format!("JAVA_{key}"), java_version.clone());
}
State::sync().await?;
java_version.upsert(&state.pool).await?;
}
// Download minecraft (5-90)
@@ -259,10 +291,9 @@ pub async fn install_minecraft(
let client_path = state
.directories
.version_dir(&version_jar)
.await
.join(format!("{version_jar}.jar"));
let libraries_dir = state.directories.libraries_dir().await;
let libraries_dir = state.directories.libraries_dir();
if let Some(ref mut data) = version_info.data {
processor_rules! {
@@ -274,7 +305,7 @@ pub async fn install_minecraft(
client => client_path.to_string_lossy(),
server => "";
"MINECRAFT_VERSION":
client => profile.metadata.game_version.clone(),
client => profile.game_version.clone(),
server => "";
"ROOT":
client => instance_path.to_string_lossy(),
@@ -356,20 +387,18 @@ pub async fn install_minecraft(
}
}
crate::api::profile::edit(&profile.profile_id(), |prof| {
crate::api::profile::edit(&profile.path, |prof| {
prof.install_stage = ProfileInstallStage::Installed;
async { Ok(()) }
})
.await?;
State::sync().await?;
emit_loading(&loading_bar, 1.0, Some("Finished installing")).await?;
Ok(())
}
#[tracing::instrument(skip_all)]
#[theseus_macros::debug_pin]
#[allow(clippy::too_many_arguments)]
pub async fn launch_minecraft(
java_args: &[String],
@@ -381,7 +410,7 @@ pub async fn launch_minecraft(
credentials: &Credentials,
post_exit_hook: Option<String>,
profile: &Profile,
) -> crate::Result<Arc<tokio::sync::RwLock<MinecraftChild>>> {
) -> crate::Result<Process> {
if profile.install_stage == ProfileInstallStage::PackInstalling
|| profile.install_stage == ProfileInstallStage::Installing
{
@@ -396,41 +425,43 @@ pub async fn launch_minecraft(
}
let state = State::get().await?;
let metadata = state.metadata.read().await;
let instance_path = profile.get_profile_full_path().await?;
let instance_path = &io::canonicalize(instance_path)?;
let instance_path =
crate::api::profile::get_full_path(&profile.path).await?;
let version_index = metadata
.minecraft
let minecraft = crate::api::metadata::get_minecraft_versions().await?;
let version_index = minecraft
.versions
.iter()
.position(|it| it.id == profile.metadata.game_version)
.position(|it| it.id == profile.game_version)
.ok_or(crate::ErrorKind::LauncherError(format!(
"Invalid game version: {}",
profile.metadata.game_version
profile.game_version
)))?;
let version = &metadata.minecraft.versions[version_index];
let version = &minecraft.versions[version_index];
let minecraft_updated = version_index
<= metadata
.minecraft
<= minecraft
.versions
.iter()
.position(|x| x.id == "22w16a")
.unwrap_or(0);
let version_jar = profile
.metadata
.loader_version
.as_ref()
.map_or(version.id.clone(), |it| {
let loader_version = get_loader_version_from_profile(
&profile.game_version,
profile.loader,
profile.loader_version.as_deref(),
)
.await?;
let version_jar =
loader_version.as_ref().map_or(version.id.clone(), |it| {
format!("{}-{}", version.id.clone(), it.id.clone())
});
let version_info = download::download_version_info(
&state,
version,
profile.metadata.loader_version.as_ref(),
loader_version.as_ref(),
None,
None,
)
@@ -458,7 +489,6 @@ pub async fn launch_minecraft(
let client_path = state
.directories
.version_dir(&version_jar)
.await
.join(format!("{version_jar}.jar"));
let args = version_info.arguments.clone().unwrap_or_default();
@@ -474,11 +504,11 @@ pub async fn launch_minecraft(
// Check if profile has a running profile, and reject running the command if it does
// Done late so a quick double call doesn't launch two instances
let existing_processes =
process::get_uuids_by_profile_path(profile.profile_id()).await?;
if let Some(uuid) = existing_processes.first() {
process::get_by_profile_path(&profile.path).await?;
if let Some(process) = existing_processes.first() {
return Err(crate::ErrorKind::LauncherError(format!(
"Profile {} is already running at UUID: {uuid}",
profile.profile_id()
"Profile {} is already running at path: {}",
profile.path, process.pid
))
.as_error());
}
@@ -487,10 +517,10 @@ pub async fn launch_minecraft(
args::get_jvm_arguments(
args.get(&d::minecraft::ArgumentType::Jvm)
.map(|x| x.as_slice()),
&state.directories.version_natives_dir(&version_jar).await,
&state.directories.libraries_dir().await,
&state.directories.version_natives_dir(&version_jar),
&state.directories.libraries_dir(),
&args::get_class_paths(
&state.directories.libraries_dir().await,
&state.directories.libraries_dir(),
version_info.libraries.as_slice(),
&client_path,
&java_version.architecture,
@@ -513,8 +543,8 @@ pub async fn launch_minecraft(
credentials,
&version.id,
&version_info.asset_index.id,
instance_path,
&state.directories.assets_dir().await,
&instance_path,
&state.directories.assets_dir(),
&version.type_,
*resolution,
&java_version.architecture,
@@ -561,13 +591,12 @@ pub async fn launch_minecraft(
io::write(&options_path, options_string).await?;
}
crate::api::profile::edit(&profile.profile_id(), |prof| {
prof.metadata.last_played = Some(Utc::now());
crate::api::profile::edit(&profile.path, |prof| {
prof.last_played = Some(Utc::now());
async { Ok(()) }
})
.await?;
State::sync().await?;
let mut censor_strings = HashMap::new();
let username = whoami::username();
@@ -603,31 +632,25 @@ pub async fn launch_minecraft(
let window = EventState::get_main_window().await?;
if let Some(window) = window {
let settings = state.settings.read().await;
if settings.hide_on_process {
let settings = crate::state::Settings::get(&state.pool).await?;
if settings.hide_on_process_start {
window.minimize()?;
}
}
}
if !*state.offline.read().await {
// Add game played to discord rich presence
let _ = state
.discord_rpc
.set_activity(&format!("Playing {}", profile.metadata.name), true)
.await;
}
let _ = state
.discord_rpc
.set_activity(&format!("Playing {}", profile.name), true)
.await;
// Create Minecraft child by inserting it into the state
// This also spawns the process and prepares the subsequent processes
let mut state_children = state.children.write().await;
state_children
.insert_new_process(
Uuid::new_v4(),
profile.profile_id(),
command,
post_exit_hook,
censor_strings,
)
.await
Process::insert_new_process(
&profile.path,
command,
post_exit_hook,
&state.pool,
)
.await
}