feat: expand launcher settings

This commit is contained in:
objz committed 2026-09-04 20:57:07 +02:00
1 parent e8d67322b0
commit 53191bc23b
35 files changed
+1540 -266

No files matched your search

+140 -28
View File
@@ -6,6 +6,7 @@
use config::{Config as ConfigLoader, ConfigError, File};
use std::fs;
use std::io;
use std::path::PathBuf;
use std::sync::{LazyLock, RwLock, RwLockReadGuard};
@@ -53,7 +54,17 @@ pub fn load_config(config_path: &std::path::Path) -> Result<Config, ConfigError>
ConfigLoader::builder()
.add_source(File::from(config_path).required(false))
.build()?
.try_deserialize()
.try_deserialize::<Config>()
.map(Config::normalize)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LauncherSettingsSave {
pub restart_required: bool,
pub restart_changed: bool,
pub provider_changed: bool,
pub modpack_updates_enabled: bool,
pub content_updates_enabled: bool,
}
pub struct ConfigStore(RwLock<Config>);
@@ -65,34 +76,62 @@ impl ConfigStore {
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub fn save_launcher_settings(&self, edited: Config) -> std::io::Result<()> {
pub fn save_launcher_settings(&self, edited: Config) -> io::Result<LauncherSettingsSave> {
let path = get_config_path().join("config.toml");
let current = self.read().clone();
let mut persisted = load_config(&path).unwrap_or_else(|error| {
tracing::warn!("Failed to merge config.toml while saving settings: {error}");
current.clone()
});
persisted.defaults = edited.defaults.clone();
persisted.paths.java_path = edited.paths.java_path.clone();
let serialized = toml::to_string_pretty(&persisted).map_err(std::io::Error::other)?;
crate::storage::write_atomic(&path, serialized.as_bytes())?;
let mut runtime = current;
runtime.defaults = edited.defaults;
runtime.paths.java_path = edited.paths.java_path;
let edited = edited.normalize();
let persisted = load_config(&path).unwrap_or_else(|_| current.clone());
let restart_required = current.paths.instances_dir != edited.paths.instances_dir
|| current.paths.meta_dir != edited.paths.meta_dir
|| current.ui.image_protocol != edited.ui.image_protocol;
let restart_changed = persisted.paths.instances_dir != edited.paths.instances_dir
|| persisted.paths.meta_dir != edited.paths.meta_dir
|| persisted.ui.image_protocol != edited.ui.image_protocol;
let provider_changed = current.content.preferred_provider
!= edited.content.preferred_provider
|| current.content.preferred_provider_only != edited.content.preferred_provider_only;
let modpack_updates_enabled =
!current.general.check_modpack_updates && edited.general.check_modpack_updates;
let content_updates_enabled =
!current.general.check_content_updates && edited.general.check_content_updates;
write_config_document(&path, &edited)?;
let mut runtime = edited;
runtime
.paths
.instances_dir
.clone_from(&current.paths.instances_dir);
runtime.paths.meta_dir.clone_from(&current.paths.meta_dir);
runtime.ui.image_protocol = current.ui.image_protocol;
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = runtime;
Ok(())
crate::feedback::errors::set_max_error_events(self.read().ui.max_error_events);
Ok(LauncherSettingsSave {
restart_required,
restart_changed,
provider_changed,
modpack_updates_enabled,
content_updates_enabled,
})
}
pub fn reload(&self) -> Result<bool, ConfigError> {
pub fn reload(&self) -> Result<LauncherSettingsSave, ConfigError> {
let mut config = load_config(&get_config_path().join("config.toml"))?;
let restart_required = {
let outcome = {
let current = self.read();
let changed = current.paths.instances_dir != config.paths.instances_dir
let restart_required = current.paths.instances_dir != config.paths.instances_dir
|| current.paths.meta_dir != config.paths.meta_dir
|| current.ui.image_protocol != config.ui.image_protocol;
let provider_changed = current.content.preferred_provider
!= config.content.preferred_provider
|| current.content.preferred_provider_only
!= config.content.preferred_provider_only;
let modpack_updates_enabled =
!current.general.check_modpack_updates && config.general.check_modpack_updates;
let content_updates_enabled =
!current.general.check_content_updates && config.general.check_content_updates;
// App owns a manager and several watchers rooted at these paths.
// Keep them stable for this process; persisted path edits apply on restart.
config
@@ -101,32 +140,105 @@ impl ConfigStore {
.clone_from(&current.paths.instances_dir);
config.paths.meta_dir.clone_from(&current.paths.meta_dir);
config.ui.image_protocol = current.ui.image_protocol;
changed
LauncherSettingsSave {
restart_required,
restart_changed: restart_required,
provider_changed,
modpack_updates_enabled,
content_updates_enabled,
}
};
*self
.0
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = config;
Ok(restart_required)
crate::feedback::errors::set_max_error_events(self.read().ui.max_error_events);
Ok(outcome)
}
}
pub static SETTINGS: LazyLock<ConfigStore> = LazyLock::new(|| {
ConfigStore(RwLock::new({
let config = {
let path = ensure_config_exists();
load_config(&path).unwrap_or_else(|e| {
tracing::error!("Config load failed, using defaults: {}", e);
Config {
general: settings::General::default(),
paths: settings::Paths::default(),
defaults: settings::Defaults::default(),
ui: settings::Ui::default(),
content: settings::Content::default(),
}
Config::default()
})
}))
};
crate::feedback::errors::set_max_error_events(config.ui.max_error_events);
ConfigStore(RwLock::new(config))
});
fn write_config_document(path: &std::path::Path, config: &Config) -> io::Result<()> {
let source = match fs::read_to_string(path) {
Ok(source) => source,
Err(error) if error.kind() == io::ErrorKind::NotFound => String::new(),
Err(error) => return Err(error),
};
let mut document = source.parse::<toml_edit::DocumentMut>().map_err(|error| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("cannot update {}: {error}", path.display()),
)
})?;
let generated = toml::to_string_pretty(config)
.map_err(io::Error::other)?
.parse::<toml_edit::DocumentMut>()
.map_err(io::Error::other)?;
merge_table(document.as_table_mut(), generated.as_table());
if config.paths.java_path.is_none()
&& let Some(paths) = document
.get_mut("paths")
.and_then(|item| item.as_table_mut())
{
paths.remove("java_path");
}
let default_paths = settings::Paths::default();
if let Some(paths) = document
.get_mut("paths")
.and_then(|item| item.as_table_mut())
{
if config.paths.instances_dir == default_paths.instances_dir {
paths.remove("instances_dir");
}
if config.paths.meta_dir == default_paths.meta_dir {
paths.remove("meta_dir");
}
}
if config.defaults.resolution.is_none()
&& let Some(defaults) = document
.get_mut("defaults")
.and_then(|item| item.as_table_mut())
{
defaults.remove("resolution");
}
crate::storage::write_atomic(path, document.to_string().as_bytes())
}
fn merge_table(target: &mut toml_edit::Table, source: &toml_edit::Table) {
for (key, source_item) in source {
if let Some(target_item) = target.get_mut(key) {
if let (Some(target_table), Some(source_table)) =
(target_item.as_table_mut(), source_item.as_table())
{
merge_table(target_table, source_table);
continue;
}
if let (Some(target_value), Some(source_value)) =
(target_item.as_value_mut(), source_item.as_value())
{
let decor = target_value.decor().clone();
*target_value = source_value.clone();
*target_value.decor_mut() = decor;
continue;
}
*target_item = source_item.clone();
} else {
target.insert(key, source_item.clone());
}
}
}
#[cfg(test)]
#[path = "tests/loading.rs"]
mod tests;