mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-18 00:49:19 +02:00
refactor: extract user_config_path() helper
Consolidates platform-specific config-directory logic (APPDATA on Windows,
~/Library/Application Support on macOS, ~/.config on Linux) into a single
user_config_path(filename) function in utilities/utilities.{cpp,h}, removing
copy-pasted #ifdef blocks from application.cpp, editor/editorSettings.cpp,
input/{keyboard,gamepad,drivermouse,uart}input.cpp.
This commit is contained in:
@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "launcher/launchermode.h"
|
#include "launcher/launchermode.h"
|
||||||
|
|
||||||
#include "utilities/Globals.h"
|
#include "utilities/Globals.h"
|
||||||
|
#include "utilities/utilities.h"
|
||||||
#include "simulation/simulation.h"
|
#include "simulation/simulation.h"
|
||||||
#include "simulation/simulationsounds.h"
|
#include "simulation/simulationsounds.h"
|
||||||
#include "vehicle/Train.h"
|
#include "vehicle/Train.h"
|
||||||
@@ -1104,19 +1105,7 @@ int eu07_application::init_settings(int Argc, char *Argv[])
|
|||||||
{
|
{
|
||||||
Global.asVersion = VERSION_INFO;
|
Global.asVersion = VERSION_INFO;
|
||||||
|
|
||||||
fs::path iniPath;
|
fs::path iniPath = user_config_path("eu07.ini");
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
|
||||||
{
|
|
||||||
iniPath = fs::path(appdata) / "MaSzyna" / "eu07.ini";
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
{
|
|
||||||
iniPath = fs::path(home) / ".config" / "MaSzyna" / "eu07.ini";
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!iniPath.empty() && fs::exists(iniPath))
|
if (!iniPath.empty() && fs::exists(iniPath))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "editor/editorSettings.hpp"
|
#include "editor/editorSettings.hpp"
|
||||||
#include "utilities/Logs.h"
|
#include "utilities/Logs.h"
|
||||||
|
#include "utilities/utilities.h"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
@@ -23,14 +24,8 @@ namespace fs = std::filesystem;
|
|||||||
|
|
||||||
fs::path settings_path()
|
fs::path settings_path()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
fs::path p = user_config_path("eu07_editor.ini");
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
return p.empty() ? fs::path("eu07_editor.ini") : p;
|
||||||
return fs::path(appdata) / "MaSzyna" / "eu07_editor.ini";
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
return fs::path(home) / ".config" / "MaSzyna" / "eu07_editor.ini";
|
|
||||||
#endif
|
|
||||||
return fs::path("eu07_editor.ini");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *scheme_to_string(editorSettings::movement_scheme scheme)
|
const char *scheme_to_string(editorSettings::movement_scheme scheme)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "rendering/renderer.h"
|
#include "rendering/renderer.h"
|
||||||
#include "application/uilayer.h"
|
#include "application/uilayer.h"
|
||||||
#include "utilities/Logs.h"
|
#include "utilities/Logs.h"
|
||||||
|
#include "utilities/utilities.h"
|
||||||
|
|
||||||
auto const EU07_CONTROLLER_MOUSESLIDERSIZE{ 0.6 };
|
auto const EU07_CONTROLLER_MOUSESLIDERSIZE{ 0.6 };
|
||||||
|
|
||||||
@@ -181,21 +182,9 @@ drivermouse_input::recall_bindings() {
|
|||||||
|
|
||||||
std::string filePath = "eu07_input-mouse.ini";
|
std::string filePath = "eu07_input-mouse.ini";
|
||||||
|
|
||||||
#ifdef _WIN32
|
fs::path appPath = user_config_path("eu07_input-mouse.ini");
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
if (!appPath.empty() && fs::exists(appPath))
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(appdata) / "MaSzyna" / "eu07_input-mouse.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
filePath = appPath.string();
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(home) / ".config" / "MaSzyna" / "eu07_input-mouse.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
||||||
|
|
||||||
|
|||||||
@@ -156,21 +156,9 @@ bool
|
|||||||
gamepad_input::recall_bindings() {
|
gamepad_input::recall_bindings() {
|
||||||
std::string filePath = "eu07_input-gamepad.ini";
|
std::string filePath = "eu07_input-gamepad.ini";
|
||||||
|
|
||||||
#ifdef _WIN32
|
fs::path appPath = user_config_path("eu07_input-gamepad.ini");
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
if (!appPath.empty() && fs::exists(appPath))
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(appdata) / "MaSzyna" / "eu07_input-gamepad.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
filePath = appPath.string();
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(home) / ".config" / "MaSzyna" / "eu07_input-gamepad.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// bindingparser tworzony zawsze, z wybran<61> <20>cie<69>k<EFBFBD>
|
// bindingparser tworzony zawsze, z wybran<61> <20>cie<69>k<EFBFBD>
|
||||||
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
|
|||||||
#include "utilities/Globals.h"
|
#include "utilities/Globals.h"
|
||||||
#include "utilities/Logs.h"
|
#include "utilities/Logs.h"
|
||||||
#include "utilities/parser.h"
|
#include "utilities/parser.h"
|
||||||
|
#include "utilities/utilities.h"
|
||||||
|
|
||||||
namespace input {
|
namespace input {
|
||||||
|
|
||||||
@@ -122,19 +123,8 @@ namespace fs = std::filesystem;
|
|||||||
bool
|
bool
|
||||||
keyboard_input::recall_bindings() {
|
keyboard_input::recall_bindings() {
|
||||||
|
|
||||||
fs::path iniPath;
|
fs::path iniPath = user_config_path("eu07_input-keyboard.ini");
|
||||||
std::string path = "";
|
std::string path = "";
|
||||||
#ifdef _WIN32
|
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
|
||||||
{
|
|
||||||
iniPath = fs::path(appdata) / "MaSzyna" / "eu07_input-keyboard.ini";
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
{
|
|
||||||
iniPath = fs::path(home) / ".config" / "MaSzyna" / "eu07_input-keyboard.ini";
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!iniPath.empty() && fs::exists(iniPath))
|
if (!iniPath.empty() && fs::exists(iniPath))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "vehicle/Train.h"
|
#include "vehicle/Train.h"
|
||||||
#include "utilities/parser.h"
|
#include "utilities/parser.h"
|
||||||
#include "utilities/Logs.h"
|
#include "utilities/Logs.h"
|
||||||
|
#include "utilities/utilities.h"
|
||||||
#include "simulation/simulationtime.h"
|
#include "simulation/simulationtime.h"
|
||||||
#include "application/application.h"
|
#include "application/application.h"
|
||||||
|
|
||||||
@@ -194,21 +195,9 @@ uart_input::recall_bindings() {
|
|||||||
m_inputbindings.clear();
|
m_inputbindings.clear();
|
||||||
std::string filePath = "eu07_input-uart.ini";
|
std::string filePath = "eu07_input-uart.ini";
|
||||||
|
|
||||||
#ifdef _WIN32
|
fs::path appPath = user_config_path("eu07_input-uart.ini");
|
||||||
if (const char *appdata = std::getenv("APPDATA"))
|
if (!appPath.empty() && fs::exists(appPath))
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(appdata) / "MaSzyna" / "eu07_input-uart.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
filePath = appPath.string();
|
||||||
}
|
|
||||||
#else
|
|
||||||
if (const char *home = std::getenv("HOME"))
|
|
||||||
{
|
|
||||||
fs::path appPath = fs::path(home) / ".config" / "MaSzyna" / "eu07_input-uart.ini";
|
|
||||||
if (fs::exists(appPath))
|
|
||||||
filePath = appPath.string();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
|
||||||
if (false == bindingparser.ok())
|
if (false == bindingparser.ok())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -55,6 +55,22 @@ std::string Now()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::filesystem::path user_config_path(const std::string &filename)
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
if (const char *appdata = std::getenv("APPDATA"))
|
||||||
|
return fs::path(appdata) / "MaSzyna" / filename;
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
if (const char *home = std::getenv("HOME"))
|
||||||
|
return fs::path(home) / "Library" / "Application Support" / "MaSzyna" / filename;
|
||||||
|
#else
|
||||||
|
if (const char *home = std::getenv("HOME"))
|
||||||
|
return fs::path(home) / ".config" / "MaSzyna" / filename;
|
||||||
|
#endif
|
||||||
|
return {}; // env var missing → caller falls back to CWD-relative filename
|
||||||
|
}
|
||||||
|
|
||||||
// zwraca różnicę czasu
|
// zwraca różnicę czasu
|
||||||
// jeśli pierwsza jest aktualna, a druga rozkładowa, to ujemna oznacza opóżnienie
|
// jeśli pierwsza jest aktualna, a druga rozkładowa, to ujemna oznacza opóżnienie
|
||||||
// na dłuższą metę trzeba uwzględnić datę, jakby opóżnienia miały przekraczać 12h (towarowych)
|
// na dłuższą metę trzeba uwzględnić datę, jakby opóżnienia miały przekraczać 12h (towarowych)
|
||||||
|
|||||||
@@ -25,6 +25,10 @@ template <typename T> constexpr T sq(T v)
|
|||||||
return v * v;
|
return v * v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns the per-user config path for `filename` (platform-specific dir),
|
||||||
|
// or an empty path if the home/appdata env var is unavailable
|
||||||
|
std::filesystem::path user_config_path(const std::string &filename);
|
||||||
|
|
||||||
// TODO: Shouldn't this be in globals?
|
// TODO: Shouldn't this be in globals?
|
||||||
namespace paths
|
namespace paths
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user