From b9b84d2890442e2d98886cdc0389108472e0409d Mon Sep 17 00:00:00 2001 From: Imbris Date: Sat, 10 Oct 2020 19:23:41 -0400 Subject: [PATCH] Migrate singleplayer save folders to the new location if they have not already been generated --- Cargo.lock | 26 ++++++++++++++++++++++--- voxygen/Cargo.toml | 1 + voxygen/src/singleplayer.rs | 38 ++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d797ede87e..177e2ef47e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,7 +52,7 @@ dependencies = [ "bitflags", "line_drawing", "rusttype 0.7.9", - "walkdir", + "walkdir 2.3.1", "xdg", "xml-rs", ] @@ -635,6 +635,15 @@ dependencies = [ "url 1.7.2", ] +[[package]] +name = "copy_dir" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4281031634644843bd2f5aa9c48cf98fc48d6b083bd90bb11becf10deaf8b0" +dependencies = [ + "walkdir 0.1.8", +] + [[package]] name = "copypasta" version = "0.6.3" @@ -774,7 +783,7 @@ dependencies = [ "serde_derive", "serde_json", "tinytemplate", - "walkdir", + "walkdir 2.3.1", ] [[package]] @@ -2534,7 +2543,7 @@ dependencies = [ "libc", "mio 0.6.22", "mio-extras", - "walkdir", + "walkdir 2.3.1", "winapi 0.3.8", ] @@ -4734,6 +4743,7 @@ dependencies = [ "conrod_core", "conrod_winit", "const-tweaker", + "copy_dir", "cpal", "criterion", "crossbeam", @@ -4865,6 +4875,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +[[package]] +name = "walkdir" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c66c0b9792f0a765345452775f3adbd28dde9d33f30d13e5dcc5ae17cf6f3780" +dependencies = [ + "kernel32-sys", + "winapi 0.2.8", +] + [[package]] name = "walkdir" version = "2.3.1" diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index 49c2136acf..84a59fdafb 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -51,6 +51,7 @@ backtrace = "0.3.40" bincode = "1.2" chrono = "0.4.9" cpal = "0.11" +copy_dir = "0.1.2" crossbeam = "=0.7.2" deunicode = "1.0" # TODO: remove diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index 6f45188287..e60064ffce 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -10,7 +10,7 @@ use std::{ thread::{self, JoinHandle}, time::Duration, }; -use tracing::{info, warn}; +use tracing::{error, info, warn}; const TPS: u64 = 30; @@ -39,6 +39,42 @@ impl Singleplayer { path }; + // Copy saves from old folder if they don't exist in the new location + (|| { + let new_path = server_data_dir.join("saves"); + if new_path.exists() { + return; + } + + let working_dir = std::path::PathBuf::from("saves"); + let config_dir = directories_next::ProjectDirs::from("net", "veloren", "voxygen") + .expect("System's $HOME directory path not found!") + .config_dir() + .join("saves"); + let old_path = if working_dir.exists() { + working_dir + } else if config_dir.exists() { + config_dir + } else { + return; + }; + + info!( + "Saves folder doesn't exist, but there is one in the old saves location, copying \ + it to the new location" + ); + if let Some(parent) = new_path.parent() { + if let Err(e) = std::fs::create_dir_all(parent) { + error!(?e, "Could not create folder to hold saves folder."); + return; + } + } + + if let Err(e) = copy_dir::copy_dir(old_path, new_path) { + error!(?e, "Failed to copy saves from the old location"); + } + })(); + // Create server let settings = server::Settings::singleplayer(&server_data_dir); let editable_settings = server::EditableSettings::singleplayer(&server_data_dir);