Merge pull request #63 from IntelOrca/fix/reading-config
Fix reading / writing of config file
This commit is contained in:
commit
77676e6d46
|
@ -1,5 +1,8 @@
|
|||
lib/
|
||||
|
||||
## Visual Studio Code
|
||||
.vscode/
|
||||
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
##
|
||||
|
|
|
@ -78,7 +78,13 @@ namespace openloco::config
|
|||
if (!fs::is_directory(dir))
|
||||
{
|
||||
fs::create_directories(configPath.parent_path());
|
||||
fs::permissions(dir, fs::perms::owner_read | fs::perms::owner_write | fs::perms::group_read | fs::perms::others_read);
|
||||
// clang-format off
|
||||
fs::permissions(
|
||||
dir,
|
||||
fs::perms::owner_all |
|
||||
fs::perms::group_read | fs::perms::group_exec |
|
||||
fs::perms::others_read | fs::perms::others_exec);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
#ifdef _OPENLOCO_USE_BOOST_FS_
|
||||
|
|
|
@ -124,6 +124,8 @@ namespace openloco::environment
|
|||
* the given file and returns the first match.
|
||||
*/
|
||||
static fs::path find_similar_file(const fs::path& path)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto expectedFilename = path.filename().generic_string();
|
||||
auto directory = path.parent_path();
|
||||
|
@ -135,6 +137,12 @@ namespace openloco::environment
|
|||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
// Ignore errors when searching, most common will be that the
|
||||
// parent directory does not exist
|
||||
}
|
||||
return fs::path();
|
||||
}
|
||||
#endif // _WIN32
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "../interop/interop.hpp"
|
||||
#include "../openloco.h"
|
||||
#include "platform.h"
|
||||
#include <iostream>
|
||||
#include <pwd.h>
|
||||
#include <time.h>
|
||||
|
||||
|
@ -21,7 +22,8 @@ int main(int argc, const char** argv)
|
|||
|
||||
static std::string GetEnvironmentVariable(const std::string& name)
|
||||
{
|
||||
return getenv(name.c_str());
|
||||
auto result = getenv(name.c_str());
|
||||
return result == nullptr ? std::string() : result;
|
||||
}
|
||||
|
||||
uint32_t openloco::platform::get_time()
|
||||
|
@ -31,29 +33,44 @@ uint32_t openloco::platform::get_time()
|
|||
return spec.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
fs::path openloco::platform::get_user_directory()
|
||||
static fs::path get_home_directory()
|
||||
{
|
||||
std::string path;
|
||||
auto pw = getpwuid(getuid());
|
||||
if (pw != nullptr)
|
||||
{
|
||||
path = pw->pw_dir;
|
||||
return pw->pw_dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
path = GetEnvironmentVariable("HOME");
|
||||
return GetEnvironmentVariable("HOME");
|
||||
}
|
||||
}
|
||||
|
||||
fs::path openloco::platform::get_user_directory()
|
||||
{
|
||||
auto path = fs::path(GetEnvironmentVariable("XDG_CONFIG_HOME"));
|
||||
if (path.empty())
|
||||
{
|
||||
path = get_home_directory();
|
||||
if (path.empty())
|
||||
{
|
||||
path = "/";
|
||||
}
|
||||
return path;
|
||||
else
|
||||
{
|
||||
path = path / fs::path(".config");
|
||||
}
|
||||
}
|
||||
return path / fs::path("OpenLoco");
|
||||
}
|
||||
|
||||
#if !(defined(__APPLE__) && defined(__MACH__))
|
||||
std::string openloco::platform::prompt_directory(const std::string& title)
|
||||
{
|
||||
return "/";
|
||||
std::string input;
|
||||
std::cout << "Type your Locomotion path: ";
|
||||
std::cin >> input;
|
||||
return input;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue