From 2adce1795211fd0a42c3b4e03ab35a90bb01bccf Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 26 Nov 2023 14:05:29 -0500 Subject: Stop relying on correctly set working directory --- src/eye_indicator.cpp | 10 ++++++---- src/game_data.cpp | 12 ++++++++---- src/global.cpp | 24 ++++++++++++++++++++++++ src/global.h | 11 +++++++++++ src/logger.cpp | 4 +++- src/tracker_config.cpp | 9 +++++---- src/tracker_config.h | 5 +++++ src/tracker_panel.cpp | 4 +++- 8 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 src/global.cpp create mode 100644 src/global.h (limited to 'src') diff --git a/src/eye_indicator.cpp b/src/eye_indicator.cpp index 03e234e..61ad780 100644 --- a/src/eye_indicator.cpp +++ b/src/eye_indicator.cpp @@ -1,5 +1,7 @@ #include "eye_indicator.h" +#include "global.h" + EyeIndicator::EyeIndicator(wxWindow* parent) : wxWindow(parent, wxID_ANY) { SetMinSize({32, 32}); @@ -17,14 +19,14 @@ void EyeIndicator::SetChecked(bool checked) { } const wxImage& EyeIndicator::GetUncheckedImage() { - static wxImage* unchecked_image = - new wxImage("assets/unchecked.png", wxBITMAP_TYPE_PNG); + static wxImage* unchecked_image = new wxImage( + GetAbsolutePath("assets/unchecked.png").c_str(), wxBITMAP_TYPE_PNG); return *unchecked_image; } const wxImage& EyeIndicator::GetCheckedImage() { - static wxImage* checked_image = - new wxImage("assets/checked.png", wxBITMAP_TYPE_PNG); + static wxImage* checked_image = new wxImage( + GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG); return *checked_image; } diff --git a/src/game_data.cpp b/src/game_data.cpp index 31e23ec..5204262 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -6,6 +6,7 @@ #include #include +#include "global.h" #include "logger.h" namespace { @@ -61,10 +62,13 @@ struct GameData { std::set malconfigured_areas_; GameData() { - YAML::Node lingo_config = YAML::LoadFile("assets/LL1.yaml"); - YAML::Node areas_config = YAML::LoadFile("assets/areas.yaml"); - YAML::Node pilgrimage_config = YAML::LoadFile("assets/pilgrimage.yaml"); - YAML::Node ids_config = YAML::LoadFile("assets/ids.yaml"); + YAML::Node lingo_config = + YAML::LoadFile(GetAbsolutePath("assets/LL1.yaml")); + YAML::Node areas_config = + YAML::LoadFile(GetAbsolutePath("assets/areas.yaml")); + YAML::Node pilgrimage_config = + YAML::LoadFile(GetAbsolutePath("assets/pilgrimage.yaml")); + YAML::Node ids_config = YAML::LoadFile(GetAbsolutePath("assets/ids.yaml")); auto init_color_id = [this, &ids_config](const std::string &color_name) { if (ids_config["special_items"] && diff --git a/src/global.cpp b/src/global.cpp new file mode 100644 index 0000000..3ee4f58 --- /dev/null +++ b/src/global.cpp @@ -0,0 +1,24 @@ +#include "global.h" + +#include + +#include +#include +#include + +const std::filesystem::path& GetExecutableDirectory() { + static const std::filesystem::path* executable_directory = []() { + int length = wai_getExecutablePath(NULL, 0, NULL); + std::string buf(length, 0); + wai_getExecutablePath(buf.data(), length, NULL); + + std::filesystem::path exec_path(buf); + return new std::filesystem::path(exec_path.parent_path()); + }(); + + return *executable_directory; +} + +std::string GetAbsolutePath(std::string_view path) { + return (GetExecutableDirectory() / path).string(); +} diff --git a/src/global.h b/src/global.h new file mode 100644 index 0000000..2eb7884 --- /dev/null +++ b/src/global.h @@ -0,0 +1,11 @@ +#ifndef GLOBAL_H_44945DBA +#define GLOBAL_H_44945DBA + +#include +#include + +const std::filesystem::path& GetExecutableDirectory(); + +std::string GetAbsolutePath(std::string_view path); + +#endif /* end of include guard: GLOBAL_H_44945DBA */ diff --git a/src/logger.cpp b/src/logger.cpp index ccb721a..4b722c8 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -4,11 +4,13 @@ #include #include +#include "global.h" + namespace { class Logger { public: - Logger() : logfile_("debug.log") {} + Logger() : logfile_(GetAbsolutePath("debug.log")) {} void LogLine(const std::string& text) { std::lock_guard guard(file_mutex_); diff --git a/src/tracker_config.cpp b/src/tracker_config.cpp index 11e1cec..b0f4ac4 100644 --- a/src/tracker_config.cpp +++ b/src/tracker_config.cpp @@ -4,11 +4,11 @@ #include -constexpr const char* CONFIG_FILE_NAME = "config.yaml"; +#include "global.h" void TrackerConfig::Load() { try { - YAML::Node file = YAML::LoadFile(CONFIG_FILE_NAME); + YAML::Node file = YAML::LoadFile(filename_); ap_server = file["ap_server"].as(); ap_player = file["ap_player"].as(); @@ -32,11 +32,12 @@ void TrackerConfig::Save() { output["hybrid_areas"] = hybrid_areas; output["show_hunt_panels"] = show_hunt_panels; - std::ofstream filewriter(CONFIG_FILE_NAME); + std::ofstream filewriter(filename_); filewriter << output; } TrackerConfig& GetTrackerConfig() { - static TrackerConfig* instance = new TrackerConfig(); + static TrackerConfig* instance = + new TrackerConfig(GetAbsolutePath("config.yaml")); return *instance; } diff --git a/src/tracker_config.h b/src/tracker_config.h index 0a29d2c..b95194e 100644 --- a/src/tracker_config.h +++ b/src/tracker_config.h @@ -5,6 +5,8 @@ class TrackerConfig { public: + explicit TrackerConfig(const std::string& filename) : filename_(filename) {} + void Load(); void Save(); @@ -16,6 +18,9 @@ class TrackerConfig { bool should_check_for_updates = false; bool hybrid_areas = false; bool show_hunt_panels = false; + + private: + std::string filename_; }; TrackerConfig& GetTrackerConfig(); diff --git a/src/tracker_panel.cpp b/src/tracker_panel.cpp index 5e035af..81cb595 100644 --- a/src/tracker_panel.cpp +++ b/src/tracker_panel.cpp @@ -3,6 +3,7 @@ #include "ap_state.h" #include "area_popup.h" #include "game_data.h" +#include "global.h" #include "tracker_config.h" #include "tracker_state.h" @@ -11,7 +12,8 @@ constexpr int AREA_BORDER_SIZE = 5; constexpr int AREA_EFFECTIVE_SIZE = AREA_ACTUAL_SIZE + AREA_BORDER_SIZE * 2; TrackerPanel::TrackerPanel(wxWindow *parent) : wxPanel(parent, wxID_ANY) { - map_image_ = wxImage("assets/lingo_map.png", wxBITMAP_TYPE_PNG); + map_image_ = wxImage(GetAbsolutePath("assets/lingo_map.png").c_str(), + wxBITMAP_TYPE_PNG); if (!map_image_.IsOk()) { return; } -- cgit 1.4.1