feat: expand launcher settings
This commit is contained in:
1 parent
e8d67322b0
commit
53191bc23b
35 files changed
+1540
-266
No files matched your search
@@ -386,6 +386,13 @@ pub async fn build_launch_invocation(
|
||||
) -> Result<LaunchInvocation, LaunchError> {
|
||||
let instance_dir = instances_dir.join(&config.name);
|
||||
let minecraft_dir = instance_dir.join(crate::storage::MINECRAFT_DIR_NAME);
|
||||
let (window_mode, resolution) = {
|
||||
let defaults = &crate::config::SETTINGS.read().defaults;
|
||||
(
|
||||
config.effective_window_mode(defaults.window_mode),
|
||||
config.effective_resolution(defaults.resolution),
|
||||
)
|
||||
};
|
||||
|
||||
let metadata_paths = crate::storage::MetadataPaths::new(meta_dir);
|
||||
let meta_path = metadata_paths
|
||||
@@ -407,7 +414,7 @@ pub async fn build_launch_invocation(
|
||||
|
||||
let current_features = FeatureSet {
|
||||
is_quick_play_singleplayer: quick_play_world.map(|_| true),
|
||||
has_custom_resolution: config.resolution.map(|_| true),
|
||||
has_custom_resolution: resolution.map(|_| true),
|
||||
..Default::default()
|
||||
};
|
||||
let host_os_version = system::mojang_os_version();
|
||||
@@ -585,8 +592,8 @@ pub async fn build_launch_invocation(
|
||||
.join(&config.game_version)
|
||||
.join("natives");
|
||||
let version_type = merged_profile.type_.as_deref().unwrap_or("release");
|
||||
let resolution_width = config.resolution.map(|(width, _)| width.to_string());
|
||||
let resolution_height = config.resolution.map(|(_, height)| height.to_string());
|
||||
let resolution_width = resolution.map(|(width, _)| width.to_string());
|
||||
let resolution_height = resolution.map(|(_, height)| height.to_string());
|
||||
let template_ctx = TemplateContext {
|
||||
library_directory,
|
||||
classpath_separator: sep,
|
||||
@@ -615,10 +622,10 @@ pub async fn build_launch_invocation(
|
||||
build_game_args(&merged_profile, &rule_ctx, &template_ctx)?;
|
||||
// Modern Mojang profiles include feature-gated resolution arguments.
|
||||
// Older and third-party profiles may not, so add them when absent.
|
||||
apply_custom_resolution(&mut game_args, config.resolution);
|
||||
apply_window_mode(&mut game_args, config.window_mode);
|
||||
apply_custom_resolution(&mut game_args, resolution);
|
||||
apply_window_mode(&mut game_args, window_mode);
|
||||
|
||||
let (memory_min, memory_max) = {
|
||||
let (memory_min, memory_max, global_jvm_args, global_environment) = {
|
||||
let settings = crate::config::SETTINGS.read();
|
||||
(
|
||||
config
|
||||
@@ -629,16 +636,22 @@ pub async fn build_launch_invocation(
|
||||
.memory_max
|
||||
.clone()
|
||||
.unwrap_or_else(|| settings.defaults.memory_max.clone()),
|
||||
settings.defaults.jvm_args.clone(),
|
||||
settings.defaults.environment.clone(),
|
||||
)
|
||||
};
|
||||
let mut jvm_args: Vec<String> = vec![format!("-Xms{memory_min}"), format!("-Xmx{memory_max}")];
|
||||
jvm_args.extend(patch_jvm_args);
|
||||
jvm_args.extend(upstream_jvm_args);
|
||||
jvm_args.extend(global_jvm_args);
|
||||
jvm_args.extend(config.jvm_args.clone());
|
||||
if let Some(glfw_path) = config.glfw_path.as_deref() {
|
||||
jvm_args.push(format!("-Dorg.lwjgl.glfw.libname={glfw_path}"));
|
||||
}
|
||||
|
||||
let mut environment = global_environment;
|
||||
environment.extend(config.environment.clone());
|
||||
|
||||
Ok(LaunchInvocation {
|
||||
java,
|
||||
jvm_args,
|
||||
@@ -647,7 +660,7 @@ pub async fn build_launch_invocation(
|
||||
main_class,
|
||||
extra_args,
|
||||
game_args,
|
||||
environment: config.environment.clone(),
|
||||
environment,
|
||||
working_dir: minecraft_dir,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -262,6 +262,7 @@ impl InstanceManager {
|
||||
}
|
||||
})?;
|
||||
|
||||
let defaults = crate::config::SETTINGS.read().defaults.clone();
|
||||
let config = InstanceConfig {
|
||||
name: name.to_string(),
|
||||
game_version: game_version.to_string(),
|
||||
@@ -274,8 +275,10 @@ impl InstanceManager {
|
||||
memory_min: None,
|
||||
jvm_args: vec![],
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
resolution: None,
|
||||
window_mode: defaults.window_mode,
|
||||
inherit_window_mode: true,
|
||||
resolution: defaults.resolution,
|
||||
inherit_resolution: true,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
|
||||
+3
-1
@@ -27,4 +27,6 @@ pub use content::{
|
||||
pub use launch::LaunchError;
|
||||
pub use loader::{GameVersion, ModLoaderInstaller, VanillaInstaller, get_installer};
|
||||
pub use manager::{InstanceError, InstanceManager};
|
||||
pub use models::{InstanceConfig, LaunchCommand, ModLoader, WindowMode, normalize_memory_value};
|
||||
pub use models::{
|
||||
InstanceConfig, LaunchCommand, ModLoader, WindowMode, memory_kib, normalize_memory_value,
|
||||
};
|
||||
@@ -89,8 +89,12 @@ pub struct InstanceConfig {
|
||||
pub environment: BTreeMap<String, String>,
|
||||
#[serde(default, skip_serializing_if = "WindowMode::is_windowed")]
|
||||
pub window_mode: WindowMode,
|
||||
#[serde(default, skip_serializing_if = "is_false")]
|
||||
pub inherit_window_mode: bool,
|
||||
#[serde(default)]
|
||||
pub resolution: Option<(u32, u32)>,
|
||||
#[serde(default, skip_serializing_if = "is_false")]
|
||||
pub inherit_resolution: bool,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub preferred_account: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "LaunchCommand::is_default")]
|
||||
@@ -105,6 +109,28 @@ pub struct InstanceConfig {
|
||||
pub modpack_source: Option<crate::instance::ProviderProject>,
|
||||
}
|
||||
|
||||
impl InstanceConfig {
|
||||
pub fn effective_window_mode(&self, global: WindowMode) -> WindowMode {
|
||||
if self.inherit_window_mode {
|
||||
global
|
||||
} else {
|
||||
self.window_mode
|
||||
}
|
||||
}
|
||||
|
||||
pub fn effective_resolution(&self, global: Option<(u32, u32)>) -> Option<(u32, u32)> {
|
||||
if self.inherit_resolution {
|
||||
global
|
||||
} else {
|
||||
self.resolution
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_false(value: &bool) -> bool {
|
||||
!value
|
||||
}
|
||||
|
||||
pub fn parse_resolution(input: &str) -> Result<(u32, u32), String> {
|
||||
let (width, height) = input
|
||||
.trim()
|
||||
@@ -152,6 +178,18 @@ pub fn normalize_memory_value(raw: &str) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn memory_kib(value: &str) -> Option<u64> {
|
||||
let normalized = normalize_memory_value(value)?;
|
||||
let (number, suffix) = normalized.split_at(normalized.len().saturating_sub(1));
|
||||
let number = number.parse::<u64>().ok()?;
|
||||
match suffix {
|
||||
"K" => Some(number),
|
||||
"M" => number.checked_mul(1024),
|
||||
"G" => number.checked_mul(1024 * 1024),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_optional_memory<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
|
||||
@@ -246,7 +246,9 @@ fn instance() -> InstanceConfig {
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
|
||||
@@ -51,7 +51,9 @@ fn job(name: &str) -> ReconcileJob {
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
|
||||
@@ -191,7 +191,9 @@ async fn launch_commands_receive_instance_environment() {
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
@@ -321,7 +323,9 @@ async fn migrate_legacy_loader_profile_skips_modern_with_inherits_from() {
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
@@ -376,7 +380,9 @@ async fn migrate_legacy_loader_profile_skips_fabric() {
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
|
||||
@@ -29,7 +29,9 @@ fn dummy_config(name: &str) -> InstanceConfig {
|
||||
jvm_args: vec![],
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
|
||||
@@ -18,7 +18,9 @@ fn instance_config_roundtrips_through_json() {
|
||||
jvm_args: vec![],
|
||||
environment: Default::default(),
|
||||
window_mode: Default::default(),
|
||||
inherit_window_mode: false,
|
||||
resolution: Some((1920, 1080)),
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
@@ -55,12 +57,57 @@ fn instance_config_accepts_numeric_memory() {
|
||||
assert_eq!(parsed.memory_min.as_deref(), Some("512M"));
|
||||
assert!(parsed.environment.is_empty());
|
||||
assert_eq!(parsed.window_mode, WindowMode::Windowed);
|
||||
assert!(!parsed.inherit_window_mode);
|
||||
assert!(!parsed.inherit_resolution);
|
||||
assert_eq!(parsed.preferred_account, None);
|
||||
assert_eq!(parsed.pre_launch_command, LaunchCommand::default());
|
||||
assert_eq!(parsed.post_exit_command, LaunchCommand::default());
|
||||
assert_eq!(parsed.glfw_path, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_defaults_are_inherited_only_when_explicitly_enabled() {
|
||||
let mut config = InstanceConfig {
|
||||
name: "test".to_owned(),
|
||||
game_version: "1.21.1".to_owned(),
|
||||
loader: ModLoader::Vanilla,
|
||||
loader_version: None,
|
||||
created: Utc::now(),
|
||||
last_played: None,
|
||||
java_path: None,
|
||||
memory_max: None,
|
||||
memory_min: None,
|
||||
jvm_args: Vec::new(),
|
||||
environment: Default::default(),
|
||||
window_mode: WindowMode::Windowed,
|
||||
inherit_window_mode: false,
|
||||
resolution: None,
|
||||
inherit_resolution: false,
|
||||
preferred_account: None,
|
||||
pre_launch_command: Default::default(),
|
||||
post_exit_command: Default::default(),
|
||||
glfw_path: None,
|
||||
config_sync_profile: None,
|
||||
modpack_source: None,
|
||||
};
|
||||
assert_eq!(
|
||||
config.effective_window_mode(WindowMode::Fullscreen),
|
||||
WindowMode::Windowed
|
||||
);
|
||||
assert_eq!(config.effective_resolution(Some((1920, 1080))), None);
|
||||
|
||||
config.inherit_window_mode = true;
|
||||
config.inherit_resolution = true;
|
||||
assert_eq!(
|
||||
config.effective_window_mode(WindowMode::Fullscreen),
|
||||
WindowMode::Fullscreen
|
||||
);
|
||||
assert_eq!(
|
||||
config.effective_resolution(Some((1920, 1080))),
|
||||
Some((1920, 1080))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_memory_value_handles_bare_numbers() {
|
||||
assert_eq!(normalize_memory_value("8").as_deref(), Some("8G"));
|
||||
|
||||
Reference in new issue
Block a user