fix(instance): apply edited memory config immediately
This commit is contained in:
1 parent
e2caf66c19
commit
b3da19cbaf
6 files changed
+238
-21
No files matched your search
+2
-10
@@ -249,18 +249,10 @@ fn apply_config_update(
|
|||||||
) -> CliResult {
|
) -> CliResult {
|
||||||
match key {
|
match key {
|
||||||
"memory-max" => {
|
"memory-max" => {
|
||||||
config.memory_max = if value.is_empty() {
|
config.memory_max = crate::instance::normalize_memory_value(value);
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(value.to_string())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"memory-min" => {
|
"memory-min" => {
|
||||||
config.memory_min = if value.is_empty() {
|
config.memory_min = crate::instance::normalize_memory_value(value);
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(value.to_string())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
"java-path" => {
|
"java-path" => {
|
||||||
config.java_path = if value.is_empty() {
|
config.java_path = if value.is_empty() {
|
||||||
|
|||||||
@@ -387,6 +387,33 @@ mod tests {
|
|||||||
std::fs::remove_dir_all(&tmp).ok();
|
std::fs::remove_dir_all(&tmp).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_load_all_accepts_numeric_memory() {
|
||||||
|
let (manager, tmp) = test_manager();
|
||||||
|
let instance_dir = tmp.join("test-memory");
|
||||||
|
std::fs::create_dir_all(&instance_dir).ok();
|
||||||
|
std::fs::write(
|
||||||
|
instance_dir.join("instance.json"),
|
||||||
|
r#"{
|
||||||
|
"name": "test-memory",
|
||||||
|
"game_version": "1.7.10",
|
||||||
|
"loader": "forge",
|
||||||
|
"loader_version": "10.13.4.1614",
|
||||||
|
"created": "2026-04-20T18:04:25.567993893Z",
|
||||||
|
"memory_max": 8,
|
||||||
|
"memory_min": 512
|
||||||
|
}"#,
|
||||||
|
)
|
||||||
|
.expect("write config");
|
||||||
|
|
||||||
|
let all = manager.load_all();
|
||||||
|
assert_eq!(all.len(), 1);
|
||||||
|
assert_eq!(all[0].memory_max.as_deref(), Some("8G"));
|
||||||
|
assert_eq!(all[0].memory_min.as_deref(), Some("512M"));
|
||||||
|
|
||||||
|
std::fs::remove_dir_all(&tmp).ok();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_one_not_found() {
|
fn test_load_one_not_found() {
|
||||||
let (manager, tmp) = test_manager();
|
let (manager, tmp) = test_manager();
|
||||||
|
|||||||
+1
-1
@@ -14,4 +14,4 @@ pub use content::{scan_mods, toggle_entry, ContentEntry, scan_resource_packs, sc
|
|||||||
pub use launch::LaunchError;
|
pub use launch::LaunchError;
|
||||||
pub use loader::{get_installer, GameVersion, ModLoaderInstaller, VanillaInstaller};
|
pub use loader::{get_installer, GameVersion, ModLoaderInstaller, VanillaInstaller};
|
||||||
pub use manager::{InstanceError, InstanceManager};
|
pub use manager::{InstanceError, InstanceManager};
|
||||||
pub use models::{InstanceConfig, ModLoader};
|
pub use models::{normalize_memory_value, InstanceConfig, ModLoader};
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
@@ -34,14 +35,92 @@ pub struct InstanceConfig {
|
|||||||
pub loader: ModLoader,
|
pub loader: ModLoader,
|
||||||
pub loader_version: Option<String>,
|
pub loader_version: Option<String>,
|
||||||
pub created: DateTime<Utc>,
|
pub created: DateTime<Utc>,
|
||||||
|
#[serde(default)]
|
||||||
pub last_played: Option<DateTime<Utc>>,
|
pub last_played: Option<DateTime<Utc>>,
|
||||||
|
#[serde(default)]
|
||||||
pub java_path: Option<String>,
|
pub java_path: Option<String>,
|
||||||
|
#[serde(default, deserialize_with = "deserialize_optional_memory")]
|
||||||
pub memory_max: Option<String>,
|
pub memory_max: Option<String>,
|
||||||
|
#[serde(default, deserialize_with = "deserialize_optional_memory")]
|
||||||
pub memory_min: Option<String>,
|
pub memory_min: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
pub jvm_args: Vec<String>,
|
pub jvm_args: Vec<String>,
|
||||||
|
#[serde(default)]
|
||||||
pub resolution: Option<(u32, u32)>,
|
pub resolution: Option<(u32, u32)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn normalize_memory_value(raw: &str) -> Option<String> {
|
||||||
|
let trimmed = raw.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (digits, suffix) = match trimmed.chars().last() {
|
||||||
|
Some(c) if c.is_ascii_alphabetic() => {
|
||||||
|
(&trimmed[..trimmed.len() - c.len_utf8()], Some(c))
|
||||||
|
}
|
||||||
|
Some(_) => (trimmed, None),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
if digits.is_empty() || !digits.chars().all(|c| c.is_ascii_digit()) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = digits.parse::<u64>().ok()?;
|
||||||
|
if value == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
match suffix.map(|c| c.to_ascii_uppercase()) {
|
||||||
|
Some(unit @ ('K' | 'M' | 'G')) => Some(format!("{value}{unit}")),
|
||||||
|
Some(_) => None,
|
||||||
|
None => memory_number_to_string(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_optional_memory<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
let value = Option::<Value>::deserialize(deserializer)?;
|
||||||
|
match value {
|
||||||
|
None | Some(Value::Null) => Ok(None),
|
||||||
|
Some(Value::String(raw)) => Ok(normalize_memory_value(&raw)),
|
||||||
|
Some(Value::Number(number)) => {
|
||||||
|
let number = if let Some(value) = number.as_u64() {
|
||||||
|
value
|
||||||
|
} else if let Some(value) = number.as_i64() {
|
||||||
|
let Ok(value) = u64::try_from(value) else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
value
|
||||||
|
} else {
|
||||||
|
let Some(value) = number.as_f64() else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
if !value.is_finite() || value.fract() != 0.0 || value < 0.0 {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
value as u64
|
||||||
|
};
|
||||||
|
Ok(memory_number_to_string(number))
|
||||||
|
}
|
||||||
|
Some(_) => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn memory_number_to_string(value: u64) -> Option<String> {
|
||||||
|
if value == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if value < 128 {
|
||||||
|
Some(format!("{value}G"))
|
||||||
|
} else {
|
||||||
|
Some(format!("{value}M"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -68,4 +147,58 @@ mod tests {
|
|||||||
assert_eq!(parsed.loader, config.loader);
|
assert_eq!(parsed.loader, config.loader);
|
||||||
assert_eq!(parsed.resolution, config.resolution);
|
assert_eq!(parsed.resolution, config.resolution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn instance_config_accepts_numeric_memory() {
|
||||||
|
let json = r#"
|
||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"game_version": "1.7.10",
|
||||||
|
"loader": "forge",
|
||||||
|
"loader_version": "10.13.4.1614",
|
||||||
|
"created": "2026-04-20T18:04:25.567993893Z",
|
||||||
|
"memory_max": 8,
|
||||||
|
"memory_min": 512
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
let parsed: InstanceConfig = serde_json::from_str(json).expect("deserialize");
|
||||||
|
assert_eq!(parsed.memory_max.as_deref(), Some("8G"));
|
||||||
|
assert_eq!(parsed.memory_min.as_deref(), Some("512M"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn normalize_memory_value_handles_bare_numbers() {
|
||||||
|
assert_eq!(normalize_memory_value("8").as_deref(), Some("8G"));
|
||||||
|
assert_eq!(normalize_memory_value("4096").as_deref(), Some("4096M"));
|
||||||
|
assert_eq!(normalize_memory_value("8G").as_deref(), Some("8G"));
|
||||||
|
assert_eq!(normalize_memory_value("2048m").as_deref(), Some("2048M"));
|
||||||
|
assert_eq!(normalize_memory_value(""), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn instance_config_ignores_invalid_memory_values() {
|
||||||
|
let json = r#"
|
||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"game_version": "1.7.10",
|
||||||
|
"loader": "forge",
|
||||||
|
"loader_version": "10.13.4.1614",
|
||||||
|
"created": "2026-04-20T18:04:25.567993893Z",
|
||||||
|
"memory_max": ["8G"],
|
||||||
|
"memory_min": "8GB"
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
let parsed: InstanceConfig = serde_json::from_str(json).expect("deserialize");
|
||||||
|
assert_eq!(parsed.memory_max, None);
|
||||||
|
assert_eq!(parsed.memory_min, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn normalize_memory_value_rejects_invalid_values() {
|
||||||
|
assert_eq!(normalize_memory_value("0"), None);
|
||||||
|
assert_eq!(normalize_memory_value("-1"), None);
|
||||||
|
assert_eq!(normalize_memory_value("1.5G"), None);
|
||||||
|
assert_eq!(normalize_memory_value("8GB"), None);
|
||||||
|
assert_eq!(normalize_memory_value("banana"), None);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+62
-10
@@ -54,8 +54,10 @@ impl App {
|
|||||||
terminal.draw(|frame| self.render_frame(frame))?;
|
terminal.draw(|frame| self.render_frame(frame))?;
|
||||||
self.handle_events().wrap_err("handle events failed")?;
|
self.handle_events().wrap_err("handle events failed")?;
|
||||||
|
|
||||||
if let Some(path) = self.pending_editor.take() {
|
if let Some(path) = self.pending_editor.take()
|
||||||
Self::run_editor(terminal, &path);
|
&& Self::run_editor(terminal, &path)
|
||||||
|
{
|
||||||
|
self.reload_edited_config(&path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -149,7 +151,7 @@ impl App {
|
|||||||
// spawns $EDITOR/$VISUAL to edit a file. for terminal editors (vim, nano, etc)
|
// spawns $EDITOR/$VISUAL to edit a file. for terminal editors (vim, nano, etc)
|
||||||
// gotta leave the alternate screen and restore it after, otherwise the
|
// gotta leave the alternate screen and restore it after, otherwise the
|
||||||
// editor fights with ratatui for the terminal. GUI editors just get spawned detached.
|
// editor fights with ratatui for the terminal. GUI editors just get spawned detached.
|
||||||
fn run_editor(terminal: &mut ratatui::DefaultTerminal, path: &std::path::Path) {
|
fn run_editor(terminal: &mut ratatui::DefaultTerminal, path: &std::path::Path) -> bool {
|
||||||
use ratatui::crossterm::{
|
use ratatui::crossterm::{
|
||||||
terminal::{
|
terminal::{
|
||||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||||
@@ -199,20 +201,70 @@ impl App {
|
|||||||
|
|
||||||
if let Err(e) = result {
|
if let Err(e) = result {
|
||||||
tracing::error!("Failed to open editor: {}", e);
|
tracing::error!("Failed to open editor: {}", e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
} else { if let Err(e) = std::process::Command::new(&editor)
|
true
|
||||||
.arg(path)
|
} else {
|
||||||
.stdout(std::process::Stdio::null())
|
if let Err(e) = std::process::Command::new(&editor)
|
||||||
.stderr(std::process::Stdio::null())
|
.arg(path)
|
||||||
.spawn() {
|
.stdout(std::process::Stdio::null())
|
||||||
tracing::error!("Failed to open editor: {}", e);
|
.stderr(std::process::Stdio::null())
|
||||||
}}
|
.spawn()
|
||||||
|
{
|
||||||
|
tracing::error!("Failed to open editor: {}", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reload_edited_config(&mut self, path: &std::path::Path) {
|
||||||
|
if path.file_name().and_then(|n| n.to_str()) != Some("instance.json") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(name) = path
|
||||||
|
.parent()
|
||||||
|
.and_then(|p| p.file_name())
|
||||||
|
.and_then(|n| n.to_str())
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
match self.instance_manager.load_one(name) {
|
||||||
|
Ok(config) => {
|
||||||
|
self.instances_state.replace_instance(name, config);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to reload edited instance '{}': {}", name, e);
|
||||||
|
error_buffer::push_error(error_buffer::ErrorEvent {
|
||||||
|
id: 0,
|
||||||
|
level: tracing::Level::ERROR,
|
||||||
|
message: format!("Failed to reload edited instance '{name}': {e}"),
|
||||||
|
pushed_at: std::time::Instant::now(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn spawn_launch(&self, instance: crate::instance::InstanceConfig) {
|
pub(super) fn spawn_launch(&self, instance: crate::instance::InstanceConfig) {
|
||||||
use crate::instance::launch;
|
use crate::instance::launch;
|
||||||
use crate::running;
|
use crate::running;
|
||||||
|
|
||||||
|
let instance = match self.instance_manager.load_one(&instance.name) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to load instance '{}': {}", instance.name, e);
|
||||||
|
error_buffer::push_error(error_buffer::ErrorEvent {
|
||||||
|
id: 0,
|
||||||
|
level: tracing::Level::ERROR,
|
||||||
|
message: format!("Failed to load instance '{}': {e}", instance.name),
|
||||||
|
pushed_at: std::time::Instant::now(),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
running::set_state(&instance.name, running::RunState::Authenticating);
|
running::set_state(&instance.name, running::RunState::Authenticating);
|
||||||
|
|
||||||
let instances_dir = self.instance_manager.instances_dir.clone();
|
let instances_dir = self.instance_manager.instances_dir.clone();
|
||||||
|
|||||||
@@ -149,6 +149,19 @@ impl State {
|
|||||||
self.instances.push(instance);
|
self.instances.push(instance);
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn replace_instance(&mut self, old_name: &str, instance: InstanceConfig) {
|
||||||
|
if let Some(existing) = self
|
||||||
|
.instances
|
||||||
|
.iter_mut()
|
||||||
|
.find(|i| i.name == old_name || i.name == instance.name)
|
||||||
|
{
|
||||||
|
*existing = instance;
|
||||||
|
} else {
|
||||||
|
self.instances.push(instance);
|
||||||
|
}
|
||||||
|
self.update_scrollbar();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WidgetKey for State {
|
impl WidgetKey for State {
|
||||||
|
|||||||
Reference in new issue
Block a user