1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-22 23:33:04 +01:00

Remove platform_get_user_directory() (#16530)

This commit is contained in:
Michael Steenbeek
2022-01-29 13:01:05 +01:00
committed by GitHub
parent df77222390
commit f43a4344ce
12 changed files with 73 additions and 85 deletions

View File

@@ -11,8 +11,10 @@
#include <openrct2-ui/interface/Widget.h> #include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h> #include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h> #include <openrct2/Context.h>
#include <openrct2/PlatformEnvironment.h>
#include <openrct2/audio/audio.h> #include <openrct2/audio/audio.h>
#include <openrct2/core/File.h> #include <openrct2/core/File.h>
#include <openrct2/core/Path.hpp>
#include <openrct2/localisation/Formatter.h> #include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.h> #include <openrct2/localisation/Localisation.h>
#include <openrct2/object/ObjectManager.h> #include <openrct2/object/ObjectManager.h>
@@ -430,22 +432,20 @@ static void WindowInstallTrackUpdatePreview()
static void WindowInstallTrackDesign(rct_window* w) static void WindowInstallTrackDesign(rct_window* w)
{ {
utf8 destPath[MAX_PATH]; auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
auto destPath = env->GetDirectoryPath(OpenRCT2::DIRBASE::USER, OpenRCT2::DIRID::TRACK);
platform_get_user_directory(destPath, "track", sizeof(destPath)); if (!platform_ensure_directory_exists(destPath.c_str()))
if (!platform_ensure_directory_exists(destPath))
{ {
log_error("Unable to create directory '%s'", destPath); log_error("Unable to create directory '%s'", destPath.c_str());
context_show_error(STR_CANT_SAVE_TRACK_DESIGN, STR_NONE, {}); context_show_error(STR_CANT_SAVE_TRACK_DESIGN, STR_NONE, {});
return; return;
} }
safe_strcat_path(destPath, _trackName.c_str(), sizeof(destPath)); destPath = Path::Combine(destPath, _trackName + ".td6");
path_append_extension(destPath, ".td6", sizeof(destPath));
if (File::Exists(destPath)) if (File::Exists(destPath))
{ {
log_info("%s already exists, prompting user for a different track design name", destPath); log_info("%s already exists, prompting user for a different track design name", destPath.c_str());
context_show_error(STR_UNABLE_TO_INSTALL_THIS_TRACK_DESIGN, STR_NONE, {}); context_show_error(STR_UNABLE_TO_INSTALL_THIS_TRACK_DESIGN, STR_NONE, {});
WindowTextInputRawOpen( WindowTextInputRawOpen(
w, WIDX_INSTALL, STR_SELECT_NEW_NAME_FOR_TRACK_DESIGN, STR_AN_EXISTING_TRACK_DESIGN_ALREADY_HAS_THIS_NAME, {}, w, WIDX_INSTALL, STR_SELECT_NEW_NAME_FOR_TRACK_DESIGN, STR_AN_EXISTING_TRACK_DESIGN_ALREADY_HAS_THIS_NAME, {},

View File

@@ -18,6 +18,7 @@
#include <openrct2/FileClassifier.h> #include <openrct2/FileClassifier.h>
#include <openrct2/Game.h> #include <openrct2/Game.h>
#include <openrct2/GameState.h> #include <openrct2/GameState.h>
#include <openrct2/PlatformEnvironment.h>
#include <openrct2/config/Config.h> #include <openrct2/config/Config.h>
#include <openrct2/core/FileScanner.h> #include <openrct2/core/FileScanner.h>
#include <openrct2/core/Guard.hpp> #include <openrct2/core/Guard.hpp>
@@ -168,33 +169,37 @@ static utf8* GetLastDirectoryByType(int32_t type)
} }
} }
static void GetInitialDirectoryByType(const int32_t type, char* path, size_t pathSize) static u8string GetInitialDirectoryByType(const int32_t type)
{ {
const char* subdir = nullptr; std::optional<OpenRCT2::DIRID> subdir = std::nullopt;
switch (type & 0x0E) switch (type & 0x0E)
{ {
case LOADSAVETYPE_GAME: case LOADSAVETYPE_GAME:
subdir = "save"; subdir = OpenRCT2::DIRID::SAVE;
break; break;
case LOADSAVETYPE_LANDSCAPE: case LOADSAVETYPE_LANDSCAPE:
subdir = "landscape"; subdir = OpenRCT2::DIRID::LANDSCAPE;
break; break;
case LOADSAVETYPE_SCENARIO: case LOADSAVETYPE_SCENARIO:
subdir = "scenario"; subdir = OpenRCT2::DIRID::SCENARIO;
break; break;
case LOADSAVETYPE_TRACK: case LOADSAVETYPE_TRACK:
subdir = "track"; subdir = OpenRCT2::DIRID::TRACK;
break; break;
case LOADSAVETYPE_HEIGHTMAP: case LOADSAVETYPE_HEIGHTMAP:
subdir = "heightmap"; subdir = OpenRCT2::DIRID::HEIGHTMAP;
break; break;
} }
platform_get_user_directory(path, subdir, pathSize); auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
if (subdir.has_value())
return env->GetDirectoryPath(OpenRCT2::DIRBASE::USER, subdir.value());
else
return env->GetDirectoryPath(OpenRCT2::DIRBASE::USER);
} }
static const char* GetFilterPatternByType(const int32_t type, const bool isSave) static const char* GetFilterPatternByType(const int32_t type, const bool isSave)
@@ -227,9 +232,15 @@ static int32_t WindowLoadsaveGetDir(const int32_t type, char* path, size_t pathS
{ {
const char* last_save = GetLastDirectoryByType(type); const char* last_save = GetLastDirectoryByType(type);
if (last_save != nullptr && Path::DirectoryExists(last_save)) if (last_save != nullptr && Path::DirectoryExists(last_save))
{
safe_strcpy(path, last_save, pathSize); safe_strcpy(path, last_save, pathSize);
}
else else
GetInitialDirectoryByType(type, path, pathSize); {
auto result = GetInitialDirectoryByType(type);
path = String::Duplicate(result.c_str());
pathSize = sizeof(path);
}
return 1; return 1;
} }
@@ -503,7 +514,8 @@ static void WindowLoadsaveMouseup(rct_window* w, rct_widgetindex widgetIndex)
break; break;
case WIDX_DEFAULT: case WIDX_DEFAULT:
GetInitialDirectoryByType(_type, path, sizeof(path)); auto result = GetInitialDirectoryByType(_type);
safe_strcpy(path, result.c_str(), sizeof(path));
WindowLoadsavePopulateList(w, isSave, path, _extension); WindowLoadsavePopulateList(w, isSave, path, _extension);
WindowInitScrollWidgets(w); WindowInitScrollWidgets(w);
w->no_list_items = static_cast<uint16_t>(_listItems.size()); w->no_list_items = static_cast<uint16_t>(_listItems.size());

View File

@@ -1535,18 +1535,3 @@ bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc,
return false; return false;
} }
} }
/**
* This function is deprecated.
* Use IPlatformEnvironment instead.
*/
void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize)
{
auto env = GetContext()->GetPlatformEnvironment();
auto path = env->GetDirectoryPath(DIRBASE::USER);
if (!String::IsNullOrEmpty(subDirectory))
{
path = Path::Combine(path, subDirectory);
}
String::Set(outPath, outSize, path.c_str());
}

View File

@@ -546,9 +546,9 @@ void save_game()
} }
} }
void save_game_cmd(const utf8* name /* = nullptr */) void save_game_cmd(u8string_view name /* = {} */)
{ {
if (name == nullptr) if (name.empty())
{ {
char savePath[MAX_PATH]; char savePath[MAX_PATH];
safe_strcpy(savePath, gScenarioSavePath.c_str(), MAX_PATH); safe_strcpy(savePath, gScenarioSavePath.c_str(), MAX_PATH);
@@ -559,20 +559,18 @@ void save_game_cmd(const utf8* name /* = nullptr */)
} }
else else
{ {
char savePath[MAX_PATH]; auto env = GetContext()->GetPlatformEnvironment();
platform_get_user_directory(savePath, "save", sizeof(savePath)); auto savePath = Path::Combine(env->GetDirectoryPath(DIRBASE::USER, DIRID::SAVE), u8string(name) + ".park");
safe_strcat_path(savePath, name, sizeof(savePath));
path_append_extension(savePath, ".park", sizeof(savePath));
save_game_with_name(savePath); save_game_with_name(savePath);
} }
} }
void save_game_with_name(const utf8* name) void save_game_with_name(u8string_view name)
{ {
log_verbose("Saving to %s", name); log_verbose("Saving to %s", u8string(name).c_str());
if (scenario_save(name, 0x80000000 | (gConfigGeneral.save_plugin_data ? 1 : 0))) if (scenario_save(name, 0x80000000 | (gConfigGeneral.save_plugin_data ? 1 : 0)))
{ {
log_verbose("Saved to %s", name); log_verbose("Saved to %s", u8string(name).c_str());
gCurrentLoadedPath = name; gCurrentLoadedPath = name;
gScreenAge = 0; gScreenAge = 0;
} }
@@ -665,12 +663,12 @@ static void limit_autosave_count(const size_t numberOfFilesToKeep, bool processL
void game_autosave() void game_autosave()
{ {
const char* subDirectory = "save"; auto subDirectory = DIRID::SAVE;
const char* fileExtension = ".park"; const char* fileExtension = ".park";
uint32_t saveFlags = 0x80000000; uint32_t saveFlags = 0x80000000;
if (gScreenFlags & SCREEN_FLAGS_EDITOR) if (gScreenFlags & SCREEN_FLAGS_EDITOR)
{ {
subDirectory = "landscape"; subDirectory = DIRID::LANDSCAPE;
fileExtension = ".park"; fileExtension = ".park";
saveFlags |= 2; saveFlags |= 2;
} }
@@ -687,16 +685,13 @@ void game_autosave()
int32_t autosavesToKeep = gConfigGeneral.autosave_amount; int32_t autosavesToKeep = gConfigGeneral.autosave_amount;
limit_autosave_count(autosavesToKeep - 1, (gScreenFlags & SCREEN_FLAGS_EDITOR)); limit_autosave_count(autosavesToKeep - 1, (gScreenFlags & SCREEN_FLAGS_EDITOR));
utf8 path[MAX_PATH]; auto env = GetContext()->GetPlatformEnvironment();
utf8 backupPath[MAX_PATH]; auto autosaveDir = Path::Combine(env->GetDirectoryPath(DIRBASE::USER, subDirectory), "autosave");
platform_get_user_directory(path, subDirectory, sizeof(path)); platform_ensure_directory_exists(autosaveDir.c_str());
safe_strcat_path(path, "autosave", sizeof(path));
platform_ensure_directory_exists(path); auto path = Path::Combine(autosaveDir, timeName);
safe_strcpy(backupPath, path, sizeof(backupPath)); auto backupFileName = u8string("autosave") + fileExtension + ".bak";
safe_strcat_path(path, timeName, sizeof(path)); auto backupPath = Path::Combine(autosaveDir, backupFileName);
safe_strcat_path(backupPath, "autosave", sizeof(backupPath));
safe_strcat(backupPath, fileExtension, sizeof(backupPath));
safe_strcat(backupPath, ".bak", sizeof(backupPath));
if (File::Exists(path)) if (File::Exists(path))
{ {

View File

@@ -166,8 +166,8 @@ bool game_is_not_paused();
void save_game(); void save_game();
void* create_save_game_as_intent(); void* create_save_game_as_intent();
void save_game_as(); void save_game_as();
void save_game_cmd(const utf8* name = nullptr); void save_game_cmd(u8string_view name = {});
void save_game_with_name(const utf8* name); void save_game_with_name(u8string_view name);
void game_autosave(); void game_autosave();
void rct2_to_utf8_self(char* buffer, size_t length); void rct2_to_utf8_self(char* buffer, size_t length);
void game_fix_save_vars(); void game_fix_save_vars();

View File

@@ -1355,29 +1355,30 @@ static int32_t cc_load_park([[maybe_unused]] InteractiveConsole& console, [[mayb
return 0; return 0;
} }
char savePath[MAX_PATH]; u8string savePath = {};
if (String::IndexOf(argv[0].c_str(), '/') == SIZE_MAX && String::IndexOf(argv[0].c_str(), '\\') == SIZE_MAX) if (String::IndexOf(argv[0].c_str(), '/') == SIZE_MAX && String::IndexOf(argv[0].c_str(), '\\') == SIZE_MAX)
{ {
// no / or \ was included. File should be in save dir. // no / or \ was included. File should be in save dir.
platform_get_user_directory(savePath, "save", sizeof(savePath)); auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
safe_strcat_path(savePath, argv[0].c_str(), sizeof(savePath)); auto directory = env->GetDirectoryPath(OpenRCT2::DIRBASE::USER, OpenRCT2::DIRID::SAVE);
savePath = Path::Combine(directory, argv[0]);
} }
else else
{ {
safe_strcpy(savePath, argv[0].c_str(), sizeof(savePath)); savePath = argv[0];
} }
if (!String::EndsWith(savePath, ".sv6", true) && !String::EndsWith(savePath, ".sc6", true) if (!String::EndsWith(savePath, ".sv6", true) && !String::EndsWith(savePath, ".sc6", true)
&& !String::EndsWith(savePath, ".park", true)) && !String::EndsWith(savePath, ".park", true))
{ {
path_append_extension(savePath, ".park", sizeof(savePath)); savePath += ".park";
} }
if (context_load_park_from_file(savePath)) if (context_load_park_from_file(savePath.c_str()))
{ {
console.WriteFormatLine("Park %s was loaded successfully", savePath); console.WriteFormatLine("Park %s was loaded successfully", savePath.c_str());
} }
else else
{ {
console.WriteFormatLine("Loading Park %s failed", savePath); console.WriteFormatLine("Loading Park %s failed", savePath.c_str());
} }
return 1; return 1;
} }

View File

@@ -14,6 +14,7 @@
#include "../GameState.h" #include "../GameState.h"
#include "../Intro.h" #include "../Intro.h"
#include "../OpenRCT2.h" #include "../OpenRCT2.h"
#include "../PlatformEnvironment.h"
#include "../actions/SetCheatAction.h" #include "../actions/SetCheatAction.h"
#include "../audio/audio.h" #include "../audio/audio.h"
#include "../core/Console.hpp" #include "../core/Console.hpp"
@@ -102,9 +103,8 @@ static std::string screenshot_get_park_name()
static std::string screenshot_get_directory() static std::string screenshot_get_directory()
{ {
char screenshotPath[MAX_PATH]; auto env = GetContext()->GetPlatformEnvironment();
platform_get_user_directory(screenshotPath, "screenshot", sizeof(screenshotPath)); return env->GetDirectoryPath(DIRBASE::USER, DIRID::SCREENSHOT);
return screenshotPath;
} }
static std::pair<rct2_date, rct2_time> screenshot_get_date_time() static std::pair<rct2_date, rct2_time> screenshot_get_date_time()

View File

@@ -947,10 +947,8 @@ void NetworkBase::SaveGroups()
{ {
if (GetMode() == NETWORK_MODE_SERVER) if (GetMode() == NETWORK_MODE_SERVER)
{ {
utf8 path[MAX_PATH]; auto env = GetContext().GetPlatformEnvironment();
auto path = Path::Combine(env->GetDirectoryPath(DIRBASE::USER), "groups.json");
platform_get_user_directory(path, nullptr, sizeof(path));
safe_strcat_path(path, "groups.json", sizeof(path));
json_t jsonGroups = json_t::array(); json_t jsonGroups = json_t::array();
for (auto& group : group_list) for (auto& group : group_list)
@@ -967,7 +965,7 @@ void NetworkBase::SaveGroups()
} }
catch (const std::exception& ex) catch (const std::exception& ex)
{ {
log_error("Unable to save %s: %s", path, ex.what()); log_error("Unable to save %s: %s", path.c_str(), ex.what());
} }
} }
} }
@@ -1009,10 +1007,8 @@ void NetworkBase::LoadGroups()
{ {
group_list.clear(); group_list.clear();
utf8 path[MAX_PATH]; auto env = GetContext().GetPlatformEnvironment();
auto path = Path::Combine(env->GetDirectoryPath(DIRBASE::USER), "groups.json");
platform_get_user_directory(path, nullptr, sizeof(path));
safe_strcat_path(path, "groups.json", sizeof(path));
json_t jsonGroupConfig; json_t jsonGroupConfig;
if (File::Exists(path)) if (File::Exists(path))
@@ -1023,7 +1019,7 @@ void NetworkBase::LoadGroups()
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
log_error("Failed to read %s as JSON. Setting default groups. %s", path, e.what()); log_error("Failed to read %s as JSON. Setting default groups. %s", path.c_str(), e.what());
} }
} }

View File

@@ -2229,7 +2229,7 @@ enum : uint32_t
S6_SAVE_FLAG_AUTOMATIC = 1u << 31, S6_SAVE_FLAG_AUTOMATIC = 1u << 31,
}; };
int32_t scenario_save(const utf8* path, int32_t flags) int32_t scenario_save(u8string_view path, int32_t flags)
{ {
if (flags & S6_SAVE_FLAG_SCENARIO) if (flags & S6_SAVE_FLAG_SCENARIO)
{ {

View File

@@ -94,7 +94,6 @@ bool platform_lock_single_instance();
int32_t platform_get_drives(); int32_t platform_get_drives();
uint32_t platform_get_ticks(); uint32_t platform_get_ticks();
void platform_sleep(uint32_t ms); void platform_sleep(uint32_t ms);
void platform_get_user_directory(utf8* outPath, const utf8* subDirectory, size_t outSize);
bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize); bool platform_open_common_file_dialog(utf8* outFilename, file_dialog_desc* desc, size_t outSize);
std::string platform_get_rct1_steam_dir(); std::string platform_get_rct1_steam_dir();
std::string platform_get_rct2_steam_dir(); std::string platform_get_rct2_steam_dir();

View File

@@ -16,10 +16,12 @@
#include "../GameState.h" #include "../GameState.h"
#include "../OpenRCT2.h" #include "../OpenRCT2.h"
#include "../ParkImporter.h" #include "../ParkImporter.h"
#include "../PlatformEnvironment.h"
#include "../audio/audio.h" #include "../audio/audio.h"
#include "../config/Config.h" #include "../config/Config.h"
#include "../core/BitSet.hpp" #include "../core/BitSet.hpp"
#include "../core/Guard.hpp" #include "../core/Guard.hpp"
#include "../core/Path.hpp"
#include "../core/Random.hpp" #include "../core/Random.hpp"
#include "../entity/Duck.h" #include "../entity/Duck.h"
#include "../entity/Guest.h" #include "../entity/Guest.h"
@@ -133,11 +135,9 @@ void scenario_begin()
} }
// Set the last saved game path // Set the last saved game path
char savePath[MAX_PATH]; auto env = GetContext()->GetPlatformEnvironment();
platform_get_user_directory(savePath, "save", sizeof(savePath)); auto savePath = env->GetDirectoryPath(DIRBASE::USER, DIRID::SAVE);
safe_strcat_path(savePath, park.Name.c_str(), sizeof(savePath)); gScenarioSavePath = Path::Combine(savePath, park.Name + ".park");
path_append_extension(savePath, ".park", sizeof(savePath));
gScenarioSavePath = savePath;
gCurrentExpenditure = 0; gCurrentExpenditure = 0;
gCurrentProfit = 0; gCurrentProfit = 0;

View File

@@ -179,7 +179,7 @@ random_engine_t::result_type scenario_rand();
uint32_t scenario_rand_max(uint32_t max); uint32_t scenario_rand_max(uint32_t max);
bool scenario_prepare_for_save(); bool scenario_prepare_for_save();
int32_t scenario_save(const utf8* path, int32_t flags); int32_t scenario_save(u8string_view path, int32_t flags);
void scenario_failure(); void scenario_failure();
void scenario_success(); void scenario_success();
void scenario_success_submit_name(const char* name); void scenario_success_submit_name(const char* name);