1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Use u8string in Path/File; replace Path::Append with Path::Combine

This commit is contained in:
Gymnasiast
2022-01-27 13:10:59 +01:00
parent 149b164ee8
commit f8c74fe2f8
13 changed files with 114 additions and 143 deletions

View File

@@ -779,10 +779,9 @@ namespace OpenRCT2
"install directory does not exist or invalid directory selected, %s", gConfigGeneral.rct2_path.c_str());
if (!config_find_or_browse_install_directory())
{
utf8 path[MAX_PATH];
config_get_default_path(path, sizeof(path));
auto path = config_get_default_path();
Console::Error::WriteLine(
"An RCT2 install directory must be specified! Please edit \"game_path\" in %s.\n", path);
"An RCT2 install directory must be specified! Please edit \"game_path\" in %s.\n", path.c_str());
return std::string();
}
}

View File

@@ -356,14 +356,11 @@ static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator* enumerator)
// Check if g1.dat exists (naive but good check)
Console::WriteLine("Checking g1.dat...");
utf8 pathG1Check[MAX_PATH];
String::Set(pathG1Check, sizeof(pathG1Check), path.c_str());
Path::Append(pathG1Check, sizeof(pathG1Check), "Data");
Path::Append(pathG1Check, sizeof(pathG1Check), "g1.dat");
auto pathG1Check = Path::Combine(path, "Data", "g1.dat");
if (!File::Exists(pathG1Check))
{
Console::Error::WriteLine("RCT2 path not valid.");
Console::Error::WriteLine("Unable to find %s.", pathG1Check);
Console::Error::WriteLine("Unable to find %s.", pathG1Check.c_str());
return EXITCODE_FAIL;
}
@@ -372,7 +369,7 @@ static exitcode_t HandleCommandSetRCT2(CommandLineArgEnumerator* enumerator)
auto configPath = env->GetFilePath(OpenRCT2::PATHID::CONFIG);
config_set_defaults();
config_open(configPath.c_str());
gConfigGeneral.rct2_path = std::string(path);
gConfigGeneral.rct2_path = path;
if (config_save(configPath.c_str()))
{
Console::WriteFormat("Updating RCT2 path to '%s'.", path.c_str());

View File

@@ -802,17 +802,16 @@ void config_release()
SafeFree(gConfigFonts.font_name);
}
void config_get_default_path(utf8* outPath, size_t size)
u8string config_get_default_path()
{
platform_get_user_directory(outPath, nullptr, size);
Path::Append(outPath, size, "config.ini");
auto env = GetContext()->GetPlatformEnvironment();
return Path::Combine(env->GetDirectoryPath(DIRBASE::USER), "config.ini");
}
bool config_save_default()
{
utf8 path[MAX_PATH];
config_get_default_path(path, sizeof(path));
return config_save(path);
auto path = config_get_default_path();
return config_save(path.c_str());
}
bool config_find_or_browse_install_directory()

View File

@@ -248,7 +248,7 @@ extern PluginConfiguration gConfigPlugin;
bool config_open(const utf8* path);
bool config_save(const utf8* path);
void config_get_default_path(utf8* outPath, size_t size);
u8string config_get_default_path();
void config_set_defaults();
void config_release();
bool config_save_default();

View File

@@ -23,20 +23,20 @@
namespace File
{
bool Exists(std::string_view path)
bool Exists(u8string_view path)
{
fs::path file = u8path(path);
log_verbose("Checking if file exists: %s", std::string(path).c_str());
log_verbose("Checking if file exists: %s", u8string(path).c_str());
std::error_code ec;
const auto result = fs::exists(file, ec);
return result && ec.value() == 0;
}
bool Copy(std::string_view srcPath, std::string_view dstPath, bool overwrite)
bool Copy(u8string_view srcPath, u8string_view dstPath, bool overwrite)
{
if (!overwrite && Exists(dstPath))
{
log_warning("File::Copy(): Not overwriting %s, because overwrite flag == false", std::string(dstPath).c_str());
log_warning("File::Copy(): Not overwriting %s, because overwrite flag == false", u8string(dstPath).c_str());
return false;
}
@@ -45,39 +45,39 @@ namespace File
return result && ec.value() == 0;
}
bool Delete(std::string_view path)
bool Delete(u8string_view path)
{
std::error_code ec;
const auto result = fs::remove(u8path(path), ec);
return result && ec.value() == 0;
}
bool Move(std::string_view srcPath, std::string_view dstPath)
bool Move(u8string_view srcPath, u8string_view dstPath)
{
std::error_code ec;
fs::rename(u8path(srcPath), u8path(dstPath), ec);
return ec.value() == 0;
}
std::vector<uint8_t> ReadAllBytes(std::string_view path)
std::vector<uint8_t> ReadAllBytes(u8string_view path)
{
#if defined(_WIN32) && !defined(__MINGW32__)
auto pathW = String::ToWideChar(path);
std::ifstream fs(pathW, std::ios::in | std::ios::binary);
#else
std::ifstream fs(std::string(path), std::ios::in | std::ios::binary);
std::ifstream fs(u8string(path), std::ios::in | std::ios::binary);
#endif
if (!fs.is_open())
{
throw IOException("Unable to open " + std::string(path));
throw IOException("Unable to open " + u8string(path));
}
std::vector<uint8_t> result;
auto fsize = Platform::GetFileSize(path);
if (fsize > SIZE_MAX)
{
std::string message = String::StdFormat(
"'%s' exceeds maximum length of %lld bytes.", std::string(path).c_str(), SIZE_MAX);
u8string message = String::StdFormat(
"'%s' exceeds maximum length of %lld bytes.", u8string(path).c_str(), SIZE_MAX);
throw IOException(message);
}
else
@@ -89,18 +89,18 @@ namespace File
return result;
}
std::string ReadAllText(std::string_view path)
u8string ReadAllText(u8string_view path)
{
auto bytes = ReadAllBytes(path);
// TODO skip BOM
std::string result(bytes.size(), 0);
u8string result(bytes.size(), 0);
std::copy(bytes.begin(), bytes.end(), result.begin());
return result;
}
std::vector<std::string> ReadAllLines(std::string_view path)
std::vector<u8string> ReadAllLines(u8string_view path)
{
std::vector<std::string> lines;
std::vector<u8string> lines;
auto data = ReadAllBytes(path);
auto lineStart = reinterpret_cast<const char*>(data.data());
auto ch = lineStart;
@@ -127,18 +127,18 @@ namespace File
return lines;
}
void WriteAllBytes(std::string_view path, const void* buffer, size_t length)
void WriteAllBytes(u8string_view path, const void* buffer, size_t length)
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_WRITE);
fs.Write(buffer, length);
}
uint64_t GetLastModified(std::string_view path)
uint64_t GetLastModified(u8string_view path)
{
return Platform::GetLastModified(path);
}
uint64_t GetSize(std::string_view path)
uint64_t GetSize(u8string_view path)
{
return Platform::GetFileSize(path);
}

View File

@@ -10,6 +10,7 @@
#pragma once
#include "../common.h"
#include "../core/String.hpp"
#include <string>
#include <string_view>
@@ -17,14 +18,14 @@
namespace File
{
bool Exists(std::string_view path);
bool Copy(std::string_view srcPath, std::string_view dstPath, bool overwrite);
bool Delete(std::string_view path);
bool Move(std::string_view srcPath, std::string_view dstPath);
std::vector<uint8_t> ReadAllBytes(std::string_view path);
std::string ReadAllText(std::string_view path);
std::vector<std::string> ReadAllLines(std::string_view path);
void WriteAllBytes(std::string_view path, const void* buffer, size_t length);
uint64_t GetLastModified(std::string_view path);
uint64_t GetSize(std::string_view path);
bool Exists(u8string_view path);
bool Copy(u8string_view srcPath, u8string_view dstPath, bool overwrite);
bool Delete(u8string_view path);
bool Move(u8string_view srcPath, u8string_view dstPath);
std::vector<uint8_t> ReadAllBytes(u8string_view path);
u8string ReadAllText(u8string_view path);
std::vector<u8string> ReadAllLines(u8string_view path);
void WriteAllBytes(u8string_view path, const void* buffer, size_t length);
uint64_t GetLastModified(u8string_view path);
uint64_t GetSize(u8string_view path);
} // namespace File

View File

@@ -141,17 +141,14 @@ public:
{
if (_recurse)
{
utf8 childPath[MAX_PATH];
String::Set(childPath, sizeof(childPath), state->Path.c_str());
Path::Append(childPath, sizeof(childPath), child->Name.c_str());
auto childPath = Path::Combine(state->Path, child->Name);
PushState(childPath);
}
}
else if (PatternMatch(child->Name))
{
String::Set(_currentPath, MAX_PATH, state->Path.c_str());
Path::Append(_currentPath, MAX_PATH, child->Name.c_str());
auto path = Path::Combine(state->Path, child->Name);
String::Set(_currentPath, MAX_PATH, path.c_str());
_currentFileInfo->Name = child->Name.c_str();
_currentFileInfo->Size = child->Size;
@@ -315,15 +312,12 @@ private:
result.Type = DIRECTORY_CHILD_TYPE::DC_FILE;
// Get the full path of the file
size_t pathSize = String::SizeOf(directory) + 1 + String::SizeOf(node->d_name) + 1;
utf8* path = Memory::Allocate<utf8>(pathSize);
String::Set(path, pathSize, directory);
Path::Append(path, pathSize, node->d_name);
auto path = Path::Combine(directory, node->d_name);
struct stat statInfo
{
};
int32_t statRes = stat(path, &statInfo);
int32_t statRes = stat(path.c_str(), &statInfo);
if (statRes != -1)
{
result.Size = statInfo.st_size;
@@ -334,8 +328,6 @@ private:
result.Type = DIRECTORY_CHILD_TYPE::DC_DIRECTORY;
}
}
Memory::Free(path);
}
return result;
}

View File

@@ -23,86 +23,81 @@
namespace Path
{
utf8* Append(utf8* buffer, size_t bufferSize, const utf8* src)
{
return safe_strcat_path(buffer, src, bufferSize);
}
std::string Combine(std::string_view a, std::string_view b)
u8string Combine(u8string_view a, u8string_view b)
{
if (a.empty())
return std::string(b);
return u8string(b);
if (b.empty())
return std::string(a);
return u8string(a);
auto aEnd = a.back();
auto bBegin = b.front();
if (Platform::IsPathSeparator(aEnd))
{
if (Platform::IsPathSeparator(bBegin))
{
return std::string(a) + std::string(b.substr(1));
return u8string(a) + u8string(b.substr(1));
}
return std::string(a) + std::string(b);
return u8string(a) + u8string(b);
}
if (Platform::IsPathSeparator(bBegin))
{
return std::string(a) + std::string(b);
return u8string(a) + u8string(b);
}
return std::string(a) + PATH_SEPARATOR + std::string(b);
return u8string(a) + PATH_SEPARATOR + u8string(b);
}
std::string GetDirectory(std::string_view path)
u8string GetDirectory(u8string_view path)
{
return u8path(path).parent_path().u8string();
}
void CreateDirectory(std::string_view path)
void CreateDirectory(u8string_view path)
{
platform_ensure_directory_exists(std::string(path).c_str());
platform_ensure_directory_exists(u8string(path).c_str());
}
bool DirectoryExists(std::string_view path)
bool DirectoryExists(u8string_view path)
{
std::error_code ec;
const auto result = fs::is_directory(u8path(path), ec);
return result && ec.value() == 0;
}
std::string GetFileName(std::string_view path)
u8string GetFileName(u8string_view path)
{
return u8path(path).filename().u8string();
}
std::string GetFileNameWithoutExtension(std::string_view path)
u8string GetFileNameWithoutExtension(u8string_view path)
{
return u8path(path).stem().u8string();
}
std::string GetExtension(std::string_view path)
u8string GetExtension(u8string_view path)
{
return u8path(path).extension().u8string();
}
std::string GetAbsolute(std::string_view relative)
u8string GetAbsolute(u8string_view relative)
{
std::error_code ec;
return fs::absolute(u8path(relative), ec).u8string();
}
bool Equals(std::string_view a, std::string_view b)
bool Equals(u8string_view a, u8string_view b)
{
return String::Equals(a, b, Platform::ShouldIgnoreCase());
}
std::string ResolveCasing(std::string_view path)
u8string ResolveCasing(u8string_view path)
{
return Platform::ResolveCasing(path, File::Exists(path));
}
bool DeleteDirectory(std::string_view path)
bool DeleteDirectory(u8string_view path)
{
std::error_code ec;
const auto result = fs::remove_all(u8path(path), ec);

View File

@@ -16,23 +16,22 @@
namespace Path
{
utf8* Append(utf8* buffer, size_t bufferSize, const utf8* src);
std::string Combine(std::string_view a, std::string_view b);
u8string Combine(u8string_view a, u8string_view b);
template<typename... Args> static std::string Combine(std::string_view a, std::string_view b, Args... args)
template<typename... Args> static u8string Combine(u8string_view a, u8string_view b, Args... args)
{
return Combine(a, Combine(b, args...));
}
std::string GetDirectory(std::string_view path);
void CreateDirectory(std::string_view path);
bool DirectoryExists(std::string_view path);
bool DeleteDirectory(std::string_view path);
std::string GetFileName(std::string_view origPath);
std::string GetFileNameWithoutExtension(std::string_view path);
std::string GetExtension(std::string_view path);
std::string GetAbsolute(std::string_view relative);
bool Equals(std::string_view a, std::string_view b);
u8string GetDirectory(u8string_view path);
void CreateDirectory(u8string_view path);
bool DirectoryExists(u8string_view path);
bool DeleteDirectory(u8string_view path);
u8string GetFileName(u8string_view origPath);
u8string GetFileNameWithoutExtension(u8string_view path);
u8string GetExtension(u8string_view path);
u8string GetAbsolute(u8string_view relative);
bool Equals(u8string_view a, u8string_view b);
/**
* Checks if the given path is a file. If not, checks to see if
@@ -40,5 +39,5 @@ namespace Path
* one found based on a straight forward character sort.
* Note: This will not resolve the case for Windows.
*/
std::string ResolveCasing(std::string_view path);
u8string ResolveCasing(u8string_view path);
} // namespace Path

View File

@@ -104,9 +104,9 @@ using namespace OpenRCT2;
static void network_chat_show_connected_message();
static void network_chat_show_server_greeting();
static void network_get_keys_directory(utf8* buffer, size_t bufferSize);
static void network_get_private_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName);
static void network_get_public_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName, const utf8* hash);
static u8string network_get_keys_directory();
static u8string network_get_private_key_path(u8string_view playerName);
static u8string network_get_public_key_path(u8string_view playerName, u8string_view hash);
NetworkBase::NetworkBase(OpenRCT2::IContext& context)
: OpenRCT2::System(context)
@@ -276,20 +276,18 @@ bool NetworkBase::BeginClient(const std::string& host, uint16_t port)
// risk of tick collision with the server map and title screen map.
GameActions::SuspendQueue();
utf8 keyPath[MAX_PATH];
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
auto keyPath = network_get_private_key_path(gConfigNetwork.player_name);
if (!File::Exists(keyPath))
{
Console::WriteLine("Generating key... This may take a while");
Console::WriteLine("Need to collect enough entropy from the system");
_key.Generate();
Console::WriteLine("Key generated, saving private bits as %s", keyPath);
Console::WriteLine("Key generated, saving private bits as %s", keyPath.c_str());
utf8 keysDirectory[MAX_PATH];
network_get_keys_directory(keysDirectory, sizeof(keysDirectory));
if (!platform_ensure_directory_exists(keysDirectory))
const auto keysDirectory = network_get_keys_directory();
if (!platform_ensure_directory_exists(keysDirectory.c_str()))
{
log_error("Unable to create directory %s.", keysDirectory);
log_error("Unable to create directory %s.", keysDirectory.c_str());
return false;
}
@@ -300,14 +298,14 @@ bool NetworkBase::BeginClient(const std::string& host, uint16_t port)
}
catch (const std::exception&)
{
log_error("Unable to save private key at %s.", keyPath);
log_error("Unable to save private key at %s.", keyPath.c_str());
return false;
}
const std::string hash = _key.PublicKeyHash();
const utf8* publicKeyHash = hash.c_str();
network_get_public_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name, publicKeyHash);
Console::WriteLine("Key generated, saving public bits as %s", keyPath);
keyPath = network_get_public_key_path(gConfigNetwork.player_name, publicKeyHash);
Console::WriteLine("Key generated, saving public bits as %s", keyPath.c_str());
try
{
@@ -316,7 +314,7 @@ bool NetworkBase::BeginClient(const std::string& host, uint16_t port)
}
catch (const std::exception&)
{
log_error("Unable to save public key at %s.", keyPath);
log_error("Unable to save public key at %s.", keyPath.c_str());
return false;
}
}
@@ -326,13 +324,13 @@ bool NetworkBase::BeginClient(const std::string& host, uint16_t port)
bool ok = false;
try
{
log_verbose("Loading key from %s", keyPath);
log_verbose("Loading key from %s", keyPath.c_str());
auto fs = FileStream(keyPath, FILE_MODE_OPEN);
ok = _key.LoadPrivate(&fs);
}
catch (const std::exception&)
{
log_error("Unable to read private key from %s.", keyPath);
log_error("Unable to read private key from %s.", keyPath.c_str());
return false;
}
@@ -2126,11 +2124,10 @@ std::string NetworkBase::MakePlayerNameUnique(const std::string& name)
void NetworkBase::Client_Handle_TOKEN(NetworkConnection& connection, NetworkPacket& packet)
{
utf8 keyPath[MAX_PATH];
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
auto keyPath = network_get_private_key_path(gConfigNetwork.player_name);
if (!File::Exists(keyPath))
{
log_error("Key file (%s) was not found. Restart client to re-generate it.", keyPath);
log_error("Key file (%s) was not found. Restart client to re-generate it.", keyPath.c_str());
return;
}
@@ -2144,7 +2141,7 @@ void NetworkBase::Client_Handle_TOKEN(NetworkConnection& connection, NetworkPack
}
catch (const std::exception&)
{
log_error("Failed to load key %s", keyPath);
log_error("Failed to load key %s", keyPath.c_str());
connection.SetLastDisconnectReason(STR_MULTIPLAYER_VERIFICATION_FAILURE);
connection.Disconnect();
return;
@@ -3840,11 +3837,10 @@ void network_send_game_action(const GameAction* action)
void network_send_password(const std::string& password)
{
auto& network = OpenRCT2::GetContext()->GetNetwork();
utf8 keyPath[MAX_PATH];
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
const auto keyPath = network_get_private_key_path(gConfigNetwork.player_name);
if (!File::Exists(keyPath))
{
log_error("Private key %s missing! Restart the game to generate it.", keyPath);
log_error("Private key %s missing! Restart the game to generate it.", keyPath.c_str());
return;
}
try
@@ -3854,7 +3850,7 @@ void network_send_password(const std::string& password)
}
catch (const std::exception&)
{
log_error("Error reading private key from %s.", keyPath);
log_error("Error reading private key from %s.", keyPath.c_str());
return;
}
const std::string pubkey = network._key.PublicKeyString();
@@ -3885,25 +3881,21 @@ void network_append_server_log(const utf8* text)
network.AppendServerLog(text);
}
static void network_get_keys_directory(utf8* buffer, size_t bufferSize)
static u8string network_get_keys_directory()
{
platform_get_user_directory(buffer, "keys", bufferSize);
auto env = GetContext()->GetPlatformEnvironment();
return Path::Combine(env->GetDirectoryPath(DIRBASE::USER), "keys");
}
static void network_get_private_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName)
static u8string network_get_private_key_path(u8string_view playerName)
{
network_get_keys_directory(buffer, bufferSize);
Path::Append(buffer, bufferSize, playerName.c_str());
String::Append(buffer, bufferSize, ".privkey");
return Path::Combine(network_get_keys_directory(), u8string(playerName) + ".privkey");
}
static void network_get_public_key_path(utf8* buffer, size_t bufferSize, const std::string& playerName, const utf8* hash)
static u8string network_get_public_key_path(u8string_view playerName, u8string_view hash)
{
network_get_keys_directory(buffer, bufferSize);
Path::Append(buffer, bufferSize, playerName.c_str());
String::Append(buffer, bufferSize, "-");
String::Append(buffer, bufferSize, hash);
String::Append(buffer, bufferSize, ".pubkey");
const auto filename = u8string(playerName) + "-" + u8string(hash) + ".pubkey";
return Path::Combine(network_get_keys_directory(), filename);
}
const utf8* network_get_server_name()

View File

@@ -11,13 +11,13 @@
# include "NetworkUser.h"
# include "../Context.h"
# include "../PlatformEnvironment.h"
# include "../core/Console.hpp"
# include "../core/File.h"
# include "../core/Guard.hpp"
# include "../core/Json.hpp"
# include "../core/Path.hpp"
# include "../core/String.hpp"
# include "../platform/Platform2.h"
# include <unordered_set>
@@ -78,8 +78,7 @@ void NetworkUserManager::DisposeUsers()
void NetworkUserManager::Load()
{
utf8 path[MAX_PATH];
GetStorePath(path, sizeof(path));
const auto path = GetStorePath();
if (File::Exists(path))
{
@@ -102,15 +101,14 @@ void NetworkUserManager::Load()
}
catch (const std::exception& ex)
{
Console::Error::WriteLine("Failed to read %s as JSON. %s", path, ex.what());
Console::Error::WriteLine("Failed to read %s as JSON. %s", path.c_str(), ex.what());
}
}
}
void NetworkUserManager::Save()
{
utf8 path[MAX_PATH];
GetStorePath(path, sizeof(path));
const auto path = GetStorePath();
json_t jsonUsers;
try
@@ -232,10 +230,10 @@ NetworkUser* NetworkUserManager::GetOrAddUser(const std::string& hash)
return networkUser;
}
void NetworkUserManager::GetStorePath(utf8* buffer, size_t bufferSize)
u8string NetworkUserManager::GetStorePath()
{
platform_get_user_directory(buffer, nullptr, bufferSize);
Path::Append(buffer, bufferSize, USER_STORE_FILENAME);
auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
return Path::Combine(env->GetDirectoryPath(OpenRCT2::DIRBASE::USER), USER_STORE_FILENAME);
}
#endif

View File

@@ -68,5 +68,5 @@ private:
std::map<std::string, NetworkUser*> _usersByHash;
void DisposeUsers();
static void GetStorePath(utf8* buffer, size_t bufferSize);
static u8string GetStorePath();
};

View File

@@ -214,9 +214,8 @@ bool ServerList::WriteFavourites(const std::vector<ServerListEntry>& entries) co
{
log_verbose("server_list_write(%d, 0x%p)", entries.size(), entries.data());
utf8 path[MAX_PATH];
platform_get_user_directory(path, nullptr, sizeof(path));
Path::Append(path, sizeof(path), "servers.cfg");
auto env = GetContext()->GetPlatformEnvironment();
auto path = Path::Combine(env->GetDirectoryPath(DIRBASE::USER), "servers.cfg");
try
{