diff --git a/core/src/platform.cpp b/core/src/platform.cpp new file mode 100644 index 0000000..dd3cc2b --- /dev/null +++ b/core/src/platform.cpp @@ -0,0 +1,66 @@ +#include "platform.h" +#include + +// GNU/Linux implementation +#if defined(_POSIX_VERSION) || defined(__linux) || defined(__linux__) + +#include +#include +#include +#include +#include + +std::string getProgramDataDir() { + // https://stackoverflow.com/a/26696759/16255372 + + const char *homedir; + + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + + return std::string(homedir) + "/openblocks"; +} + +void printErrorMessage(std::string message) { + fprintf(stderr, "%s\n", message.c_str()); +} + +#endif // GNU/Linux + +// Windows implementation +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) + +#include +#include +#include +#include + +std::string getProgramDataDir() { + CHAR localAppData[MAX_PATH]; + int status = SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, localAppData); + if (status != 0) { + printErrorMessage("Failed to find local appdata folder"); + abort(); + } + return localAppData + "/openblocks"; +} + +void printErrorMessage(std::string message) { + fprintf(stderr, "%s\n", message.c_str()); + MessageBoxA(NULL, message.c_str(), "Fatal Error", MB_OK); +} + +#endif // WIN32 + +std::string getProgramLogsDir() { + return getProgramDataDir() + "/logs"; +} + +void initProgramDataDir() { + std::filesystem::create_directories(getProgramDataDir()); +} + +void initProgramLogsDir() { + std::filesystem::create_directories(getProgramLogsDir()); +} \ No newline at end of file diff --git a/core/src/platform.h b/core/src/platform.h new file mode 100644 index 0000000..92a8767 --- /dev/null +++ b/core/src/platform.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +// Collection of platform-dependent APIs + +// Gets the local data directory under the user's home directory +// Windows: %localappdata%/openblocks +// Linux: ~/.local/share/openblocks +std::string getProgramDataDir(); + +// Gets the local logs directory under the program's data directory +// Windows: %localappdata%/openblocks/logs +// Linux: ~/.local/share/openblocks/logs +std::string getProgramLogsDir(); + +// Creates the local data directory +void initProgramDataDir(); +// Creates the local logs directory +void initProgramLogsDir(); + +// Displays an error message box on Windows, or prints to eprintf +void printErrorMessage(std::string message); \ No newline at end of file