From f7dff377080db3034dd70871e42f4b6be52a2443 Mon Sep 17 00:00:00 2001 From: Ted John Date: Mon, 26 Jun 2017 22:00:01 +0100 Subject: [PATCH] Move copying of user files to Context.cpp --- src/openrct2/Context.cpp | 56 ++++++++++++++++++++++++++++++- src/openrct2/rct2.c | 71 +--------------------------------------- src/openrct2/rct2.h | 4 --- 3 files changed, 56 insertions(+), 75 deletions(-) diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 4527417082..c8f9b23316 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -22,9 +22,11 @@ #include "ui/UiContext.h" #include "core/Console.hpp" #include "core/File.h" +#include "core/FileScanner.h" #include "core/FileStream.hpp" #include "core/Guard.hpp" #include "core/MemoryStream.h" +#include "core/Path.hpp" #include "core/String.hpp" #include "FileClassifier.h" #include "network/network.h" @@ -235,7 +237,7 @@ namespace OpenRCT2 network_set_env(_env); chat_init(); theme_manager_initialise(); - rct2_copy_original_user_files_over(); + CopyOriginalUserFilesOver(); rct2_interop_setup_hooks(); @@ -598,6 +600,58 @@ namespace OpenRCT2 } return false; } + + /** + * Copy saved games and landscapes to user directory + */ + void CopyOriginalUserFilesOver() + { + CopyOriginalUserFilesOver(DIRID::SAVE, "*.sv6"); + CopyOriginalUserFilesOver(DIRID::LANDSCAPE, "*.sc6"); + } + + void CopyOriginalUserFilesOver(DIRID dirid, const std::string &pattern) + { + auto src = _env->GetDirectoryPath(DIRBASE::RCT2, dirid); + auto dst = _env->GetDirectoryPath(DIRBASE::USER, dirid); + CopyOriginalUserFilesOver(src, dst, pattern); + } + + void CopyOriginalUserFilesOver(const std::string &srcRoot, const std::string &dstRoot, const std::string &pattern) + { + log_verbose("CopyOriginalUserFilesOver('%s', '%s', '%s')", srcRoot.c_str(), dstRoot.c_str(), pattern.c_str()); + + auto scanPattern = Path::Combine(srcRoot, pattern); + auto scanner = Path::ScanDirectory(scanPattern, true); + while (scanner->Next()) + { + auto src = std::string(scanner->GetPath()); + auto dst = Path::Combine(dstRoot, scanner->GetPathRelative()); + auto dstDirectory = Path::GetDirectory(dst); + + // Create the directory if necessary + if (!platform_directory_exists(dstDirectory.c_str())) + { + Console::WriteLine("Creating directory '%s'", dstDirectory.c_str()); + if (!platform_ensure_directory_exists(dstDirectory.c_str())) + { + Console::Error::WriteLine("Could not create directory %s.", dstDirectory.c_str()); + break; + } + } + + // Only copy the file if it doesn't already exist + if (!File::Exists(dst)) + { + Console::WriteLine("Copying '%s' to '%s'", src.c_str(), dst.c_str()); + if (!File::Copy(src, dst, false)) + { + Console::Error::WriteLine("Failed to copy '%s' to '%s'", src.c_str(), dst.c_str()); + } + } + } + delete scanner; + } }; class PlainContext final : public Context diff --git a/src/openrct2/rct2.c b/src/openrct2/rct2.c index 882df72fcf..5d5290e93e 100644 --- a/src/openrct2/rct2.c +++ b/src/openrct2/rct2.c @@ -56,7 +56,7 @@ #include "world/sprite.h" // rct2: 0x0097F67C -const char * const RCT2FilePaths[PATH_ID_END] = { +static const char * const RCT2FilePaths[PATH_ID_END] = { "Data" PATH_SEPARATOR "g1.dat", "Data" PATH_SEPARATOR "plugin.dat", "Data" PATH_SEPARATOR "css1.dat", @@ -390,72 +390,3 @@ uint32 get_file_extension_type(const utf8 *path) if (strcicmp(extension, ".td6") == 0) return FILE_EXTENSION_TD6; return FILE_EXTENSION_UNKNOWN; } - -static void rct2_copy_files_over(const utf8 *originalDirectory, const utf8 *newDirectory, const utf8 *extension) -{ - utf8 *ch, filter[MAX_PATH], oldPath[MAX_PATH], newPath[MAX_PATH]; - sint32 fileEnumHandle; - file_info fileInfo; - - if (!platform_ensure_directory_exists(newDirectory)) { - log_error("Could not create directory %s.", newDirectory); - return; - } - - // Create filter path - safe_strcpy(filter, originalDirectory, sizeof(filter)); - ch = strchr(filter, '*'); - if (ch != NULL) - *ch = 0; - safe_strcat_path(filter, "*", sizeof(filter)); - path_append_extension(filter, extension, sizeof(filter)); - - fileEnumHandle = platform_enumerate_files_begin(filter); - while (platform_enumerate_files_next(fileEnumHandle, &fileInfo)) { - safe_strcpy(newPath, newDirectory, sizeof(newPath)); - safe_strcat_path(newPath, fileInfo.path, sizeof(newPath)); - - safe_strcpy(oldPath, originalDirectory, sizeof(oldPath)); - ch = strchr(oldPath, '*'); - if (ch != NULL) - *ch = 0; - safe_strcat_path(oldPath, fileInfo.path, sizeof(oldPath)); - - if (!platform_file_exists(newPath)) - platform_file_copy(oldPath, newPath, false); - } - platform_enumerate_files_end(fileEnumHandle); - - fileEnumHandle = platform_enumerate_directories_begin(originalDirectory); - while (platform_enumerate_directories_next(fileEnumHandle, filter)) { - safe_strcpy(newPath, newDirectory, sizeof(newPath)); - safe_strcat_path(newPath, filter, sizeof(newPath)); - - safe_strcpy(oldPath, originalDirectory, MAX_PATH); - ch = strchr(oldPath, '*'); - if (ch != NULL) - *ch = 0; - safe_strcat_path(oldPath, filter, sizeof(oldPath)); - - if (!platform_ensure_directory_exists(newPath)) { - log_error("Could not create directory %s.", newPath); - return; - } - rct2_copy_files_over(oldPath, newPath, extension); - } - platform_enumerate_directories_end(fileEnumHandle); -} - -/** - * Copy saved games and landscapes to user directory - */ -void rct2_copy_original_user_files_over() -{ - utf8 path[MAX_PATH]; - - platform_get_user_directory(path, "save", sizeof(path)); - rct2_copy_files_over((utf8*)gRCT2AddressSavedGamesPath, path, ".sv6"); - - platform_get_user_directory(path, "landscape", sizeof(path)); - rct2_copy_files_over((utf8*)gRCT2AddressLandscapesPath, path, ".sc6"); -} diff --git a/src/openrct2/rct2.h b/src/openrct2/rct2.h index 2882109d2a..eb8ec99868 100644 --- a/src/openrct2/rct2.h +++ b/src/openrct2/rct2.h @@ -153,9 +153,6 @@ enum { extern "C" { #endif - -extern const char * const RCT2FilePaths[PATH_ID_END]; - extern uint32 gCurrentDrawCount; extern uint8 gScreenFlags; @@ -183,7 +180,6 @@ void rct2_quit(); bool rct2_open_file(const char *path); uint32 get_file_extension_type(const utf8 *path); -void rct2_copy_original_user_files_over(); #ifdef __cplusplus }