From a7c14ec6b0d8610f93891a818433766ae331f3ca Mon Sep 17 00:00:00 2001 From: James Huang Date: Sun, 27 Aug 2023 12:10:35 -0700 Subject: [PATCH 1/2] added auto-regen for duplicate uuids --- core/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index ac49c773..01539bd9 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -46,11 +46,13 @@ use ringbuffer::{AllocRingBuffer, RingBufferWrite}; use fs3::FileExt; use semver::Version; +use serde_json::json; use sqlx::{sqlite::SqliteConnectOptions, Pool}; use std::{ collections::{HashMap, HashSet}, net::SocketAddr, path::{Path, PathBuf}, + io::{BufWriter, Write}, str::FromStr, sync::Arc, time::Duration, @@ -149,7 +151,7 @@ async fn restore_instances( continue; } }; - let dot_lodestone_config: DotLodestoneConfig = match serde_json::from_reader( + let mut dot_lodestone_config: DotLodestoneConfig = match serde_json::from_reader( dot_lodestone_config_file, ) { Ok(v) => v, @@ -160,6 +162,36 @@ async fn restore_instances( }; debug!("restoring instance: {}", path.display()); + + // regenerate duplicate UUID + let uuid = dot_lodestone_config.uuid().to_owned(); + if ret.contains_key(&uuid) { + let uuid_string = uuid.to_string(); + warn!("UUID {} is repeated.", uuid_string); + + dot_lodestone_config = DotLodestoneConfig::new( + InstanceUuid::default(), + *dot_lodestone_config.game_type() + ); + + let updated_config_json = json!(&dot_lodestone_config); + let updated_file_contents = match serde_json::to_string_pretty(&updated_config_json) { + Ok(v) => v, + Err(error) => { + error!("Error with parsing regenerated config - {}", error); + continue; + } + }; + match std::fs::write(path.join(".lodestone_config"), updated_file_contents) { + Ok(_) => { + info!("Instance with duplicate UUID {} updated to new UUID {}", uuid_string, dot_lodestone_config.uuid().to_string()); + }, + Err(e) => { + info!("Error with regenerating duplicate UUID {}, {}", uuid_string, e); + } + } + } + let uuid_instance: (InstanceUuid, GameInstance) = match dot_lodestone_config.game_type() { GameType::MinecraftJava => { let instance = match minecraft::MinecraftInstance::restore( @@ -205,12 +237,9 @@ async fn restore_instances( } GameType::MinecraftBedrock => todo!() }; - let uuid = uuid_instance.0; - let instance = uuid_instance.1; - if ret.contains_key(&uuid) { - warn!("UUID {} is repeated.", uuid.to_string()); - } - ret.insert(uuid, instance); + + ret.insert(uuid_instance.0, uuid_instance.1); + } Ok(ret) } From b969441430ed0b9a48e3114d4565cdfc37774f87 Mon Sep 17 00:00:00 2001 From: James Huang Date: Tue, 29 Aug 2023 23:47:23 -0700 Subject: [PATCH 2/2] changed to_owend and log type --- core/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index 01539bd9..0c46f6ce 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -164,8 +164,8 @@ async fn restore_instances( debug!("restoring instance: {}", path.display()); // regenerate duplicate UUID - let uuid = dot_lodestone_config.uuid().to_owned(); - if ret.contains_key(&uuid) { + let uuid = dot_lodestone_config.uuid(); + if ret.contains_key(uuid) { let uuid_string = uuid.to_string(); warn!("UUID {} is repeated.", uuid_string); @@ -184,10 +184,10 @@ async fn restore_instances( }; match std::fs::write(path.join(".lodestone_config"), updated_file_contents) { Ok(_) => { - info!("Instance with duplicate UUID {} updated to new UUID {}", uuid_string, dot_lodestone_config.uuid().to_string()); + warn!("Instance with duplicate UUID {} updated to new UUID {}", uuid_string, dot_lodestone_config.uuid().to_string()); }, Err(e) => { - info!("Error with regenerating duplicate UUID {}, {}", uuid_string, e); + error!("Error with regenerating duplicate UUID {}, {}", uuid_string, e); } } }