From fcf40083954bc05b1eaafb5b55e4538a5365e3f8 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Wed, 6 Jul 2016 18:22:08 +0200 Subject: [PATCH 1/3] Remember previous save/load location Works for games, landscapes, scenarios and tracks (but this is commented out due to tracks not being separated or something?) --- src/config.c | 5 ++++- src/config.h | 4 ++++ src/util/util.c | 15 ++++++++++++++ src/util/util.h | 1 + src/windows/loadsave.c | 44 +++++++++++++++++++++++++++++++++++------- 5 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/config.c b/src/config.c index a834af0a1d..7d7dd815c3 100644 --- a/src/config.c +++ b/src/config.c @@ -222,7 +222,10 @@ config_property_definition _generalDefinitions[] = { { offsetof(general_configuration, scenario_select_mode), "scenario_select_mode", CONFIG_VALUE_TYPE_UINT8, SCENARIO_SELECT_MODE_ORIGIN, NULL }, { offsetof(general_configuration, scenario_unlocking_enabled), "scenario_unlocking_enabled", CONFIG_VALUE_TYPE_BOOLEAN, true, NULL }, { offsetof(general_configuration, scenario_hide_mega_park), "scenario_hide_mega_park", CONFIG_VALUE_TYPE_BOOLEAN, true, NULL }, - + { offsetof(general_configuration, last_save_game_directory), "last_game_directory", CONFIG_VALUE_TYPE_STRING, { .value_string = NULL }, NULL }, + { offsetof(general_configuration, last_save_landscape_directory), "last_landscape_directory", CONFIG_VALUE_TYPE_STRING, { .value_string = NULL }, NULL }, + { offsetof(general_configuration, last_save_scenario_directory), "last_scenario_directory", CONFIG_VALUE_TYPE_STRING, { .value_string = NULL }, NULL }, + { offsetof(general_configuration, last_save_track_directory), "last_track_directory", CONFIG_VALUE_TYPE_STRING, { .value_string = NULL }, NULL }, }; config_property_definition _interfaceDefinitions[] = { diff --git a/src/config.h b/src/config.h index 45b9fd75cf..628a2b381f 100644 --- a/src/config.h +++ b/src/config.h @@ -192,6 +192,10 @@ typedef struct general_configuration { uint8 scenario_select_mode; uint8 scenario_unlocking_enabled; uint8 scenario_hide_mega_park; + utf8string last_save_game_directory; + utf8string last_save_landscape_directory; + utf8string last_save_scenario_directory; + utf8string last_save_track_directory; } general_configuration; typedef struct interface_configuration { diff --git a/src/util/util.c b/src/util/util.c index 72fac91556..94abb55782 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -62,6 +62,21 @@ bool filename_valid_characters(const utf8 *filename) return true; } +utf8 *path_get_directory(const utf8 *path) +{ + // Find the last slash or backslash in the path + char *filename = strrchr(path, platform_get_path_separator()); + + // If the path is invalid (e.g. just a file name), return NULL + if (filename == NULL) + return NULL; + + char *directory = strdup(path); + safe_strtrunc(directory, strlen(path) - strlen(filename) + 2); + + return directory; +} + const char *path_get_filename(const utf8 *path) { // Find last slash or backslash in the path diff --git a/src/util/util.h b/src/util/util.h index d33e4e4ddd..0819393ade 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -28,6 +28,7 @@ int mph_to_dmps(int mph); bool filename_valid_characters(const utf8 *filename); +char *path_get_directory(const utf8 *path); const char *path_get_filename(const utf8 *path); const char *path_get_extension(const utf8 *path); void path_set_extension(utf8 *path, const utf8 *newExtension); diff --git a/src/windows/loadsave.c b/src/windows/loadsave.c index c77eedc847..769eaf44c6 100644 --- a/src/windows/loadsave.c +++ b/src/windows/loadsave.c @@ -196,7 +196,11 @@ rct_window *window_loadsave_open(int type, char *defaultName) includeNewItem = (type & 0x01) == LOADSAVETYPE_SAVE; switch (type & 0x0E) { case LOADSAVETYPE_GAME: - platform_get_user_directory(path, "save"); + if (gConfigGeneral.last_save_game_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_game_directory)) + safe_strcpy(path, gConfigGeneral.last_save_game_directory, MAX_PATH); + else + platform_get_user_directory(path, "save"); + if (!platform_ensure_directory_exists(path)) { log_error("Unable to create save directory."); window_close(w); @@ -206,7 +210,11 @@ rct_window *window_loadsave_open(int type, char *defaultName) window_loadsave_populate_list(w, includeNewItem, path, ".sv6"); break; case LOADSAVETYPE_LANDSCAPE: - platform_get_user_directory(path, "landscape"); + if (gConfigGeneral.last_save_landscape_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_landscape_directory)) + safe_strcpy(path, gConfigGeneral.last_save_landscape_directory, MAX_PATH); + else + platform_get_user_directory(path, "landscape"); + if (!platform_ensure_directory_exists(path)) { log_error("Unable to create landscapes directory."); window_close(w); @@ -216,7 +224,11 @@ rct_window *window_loadsave_open(int type, char *defaultName) window_loadsave_populate_list(w, includeNewItem, path, ".sc6"); break; case LOADSAVETYPE_SCENARIO: - platform_get_user_directory(path, "scenario"); + if (gConfigGeneral.last_save_scenario_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_scenario_directory)) + safe_strcpy(path, gConfigGeneral.last_save_scenario_directory, MAX_PATH); + else + platform_get_user_directory(path, "scenario"); + if (!platform_ensure_directory_exists(path)) { log_error("Unable to create scenarios directory."); window_close(w); @@ -229,11 +241,15 @@ rct_window *window_loadsave_open(int type, char *defaultName) /* Uncomment when user tracks are separated - platform_get_user_directory(path, "tracks"); + if (gConfigGeneral.last_save_track_directory && platform_ensure_directory_exists(gConfigGeneral.last_save_track_directory)) + save_strcpy(path, gConfigGeneral.last_save_track_directory, MAX_PATH); + else + platform_get_user_directory(path, "tracks"); + if (!platform_ensure_directory_exists(path)) { - log_error("Unable to create tracks directory."); - window_close(w); - return NULL; + log_error("Unable to create tracks directory."); + window_close(w); + return NULL; } */ @@ -705,11 +721,20 @@ static void window_loadsave_invoke_callback(int result) } } +static void save_path(utf8 **config_str, const char *path) +{ + if (*config_str != NULL) + free(*config_str); + *config_str = path_get_directory(path); + config_save_default(); +} + static void window_loadsave_select(rct_window *w, const char *path) { SDL_RWops* rw; switch (_loadsaveType & 0x0F) { case (LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME) : + save_path(&gConfigGeneral.last_save_game_directory, path); if (gLoadSaveTitleSequenceSave) { utf8 newName[MAX_PATH]; char *extension = (char*)path_get_extension(path); @@ -739,6 +764,7 @@ static void window_loadsave_select(rct_window *w, const char *path) } break; case (LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME) : + save_path(&gConfigGeneral.last_save_game_directory, path); rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { int success = scenario_save(rw, gConfigGeneral.save_plugin_data ? 1 : 0); @@ -761,6 +787,7 @@ static void window_loadsave_select(rct_window *w, const char *path) } break; case (LOADSAVETYPE_LOAD | LOADSAVETYPE_LANDSCAPE) : + save_path(&gConfigGeneral.last_save_landscape_directory, path); if (editor_load_landscape(path)) { gfx_invalidate_screen(); window_loadsave_invoke_callback(MODAL_RESULT_OK); @@ -771,6 +798,7 @@ static void window_loadsave_select(rct_window *w, const char *path) } break; case (LOADSAVETYPE_SAVE | LOADSAVETYPE_LANDSCAPE) : + save_path(&gConfigGeneral.last_save_landscape_directory, path); rw = SDL_RWFromFile(path, "wb+"); if (rw != NULL) { scenario_set_filename(path); @@ -791,6 +819,7 @@ static void window_loadsave_select(rct_window *w, const char *path) break; case (LOADSAVETYPE_SAVE | LOADSAVETYPE_SCENARIO) : { + save_path(&gConfigGeneral.last_save_scenario_directory, path); int parkFlagsBackup = gParkFlags; gParkFlags &= ~PARK_FLAGS_18; gS6Info->editor_step = 255; @@ -815,6 +844,7 @@ static void window_loadsave_select(rct_window *w, const char *path) break; } case (LOADSAVETYPE_LOAD | LOADSAVETYPE_TRACK) : + save_path(&gConfigGeneral.last_save_track_directory, path); window_install_track_open(path); window_close_by_class(WC_LOADSAVE); window_loadsave_invoke_callback(MODAL_RESULT_OK); From edbca4907a013465704ba22581545d06756ccd0e Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Wed, 6 Jul 2016 18:24:42 +0200 Subject: [PATCH 2/3] Update my entry in contributors.md --- contributors.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contributors.md b/contributors.md index 622d021844..8e53a8bff0 100644 --- a/contributors.md +++ b/contributors.md @@ -26,7 +26,7 @@ Includes all git commit authors. Aliases are GitHub user names. * (vanderkleij) - Misc. * Ben Pye (benpye) - Misc. * (JeroenSack) - Misc. -* (Vijfhoek) - Misc. +* Sijmen Schoon (SijmenSchoon) - Misc. * (wolfreak99) - Misc. * Inseok Lee (dlunch) - Original command line * Lewis Fox (LRFLEW) - Misc. @@ -93,7 +93,7 @@ Includes all git commit authors. Aliases are GitHub user names. * English (UK) - Ted John (IntelOrca), (Tinytimrob) * English (US) - Ted John (IntelOrca), Michael Steenbeek (Gymnasiast); small fixes: (LRFLEW), (mike-koch), Harry Lam (daihakken) * Czech - Martin Černáč (octaroot), (Clonewayx), Tomáš Pazdiora (Aroidzap) -* Dutch - Michael Steenbeek (Gymnasiast), Yannic Geurts (xzbobzx), (mrtnptrs), Thomas den Hollander (ThomasdenH), (hostbrute), Marijn van de Werf (marijnvdwerf); reviewing and discussion: Aaron van Geffen (AaronVanGeffen), (Balletie) and Sijmen Schoon (Vijfhoek). +* Dutch - Michael Steenbeek (Gymnasiast), Yannic Geurts (xzbobzx), (mrtnptrs), Thomas den Hollander (ThomasdenH), (hostbrute), Marijn van de Werf (marijnvdwerf); reviewing and discussion: Aaron van Geffen (AaronVanGeffen), (Balletie) and Sijmen Schoon (SijmenSchoon). * Finnish - (DJHasis), (Zode), (TheWing) * French - (fbourigault), Joël Troch (JoelTroch), Michael Steenbeek (Gymnasiast), Romain Vigier (rvgr), (AziasYur), Hugo Courtial (s0r00t), David Delobel (incyclum) * German - (danidoedel), (atmaxinger), (Yepoleb), Daniel Kessel (dkessel), Leon (AllGoodNamesAreTaken), (raidcookie) From e882bfe7367c036d98e46efb1d383df06b5417b8 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Wed, 6 Jul 2016 18:50:30 +0200 Subject: [PATCH 3/3] Change strdup to _strdup --- src/util/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/util.c b/src/util/util.c index 94abb55782..bb8d08f6fe 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -71,7 +71,7 @@ utf8 *path_get_directory(const utf8 *path) if (filename == NULL) return NULL; - char *directory = strdup(path); + char *directory = _strdup(path); safe_strtrunc(directory, strlen(path) - strlen(filename) + 2); return directory;