From f2a4553fa9d64cc39d138531d0a8d662af733ef8 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 3 May 2023 15:13:14 -0400 Subject: Connection details are saved locally --- CMakeLists.txt | 1 + connection_dialog.cpp | 11 ++++++++--- main.cpp | 3 +++ tracker_config.cpp | 33 +++++++++++++++++++++++++++++++++ tracker_config.h | 19 +++++++++++++++++++ tracker_frame.cpp | 6 ++++++ 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 tracker_config.cpp create mode 100644 tracker_config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d616e5..6e29d18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ add_executable(lingo_ap_tracker connection_dialog.cpp eye_indicator.cpp tracker_state.cpp + tracker_config.cpp ) set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD 20) set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/connection_dialog.cpp b/connection_dialog.cpp index 55f68eb..9dd9984 100644 --- a/connection_dialog.cpp +++ b/connection_dialog.cpp @@ -1,10 +1,15 @@ #include "connection_dialog.h" +#include "tracker_config.h" + ConnectionDialog::ConnectionDialog() : wxDialog(nullptr, wxID_ANY, "Connect to Archipelago") { - server_box_ = new wxTextCtrl(this, -1, "", wxDefaultPosition, {300, -1}); - player_box_ = new wxTextCtrl(this, -1, "", wxDefaultPosition, {300, -1}); - password_box_ = new wxTextCtrl(this, -1, "", wxDefaultPosition, {300, -1}); + server_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_server, wxDefaultPosition, + {300, -1}); + player_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_player, wxDefaultPosition, + {300, -1}); + password_box_ = new wxTextCtrl(this, -1, GetTrackerConfig().ap_password, + wxDefaultPosition, {300, -1}); wxFlexGridSizer* form_sizer = new wxFlexGridSizer(2, 10, 10); diff --git a/main.cpp b/main.cpp index 4fbbe72..fe9aceb 100644 --- a/main.cpp +++ b/main.cpp @@ -4,11 +4,14 @@ #include #endif +#include "tracker_config.h" #include "tracker_frame.h" class TrackerApp : public wxApp { public: virtual bool OnInit() { + GetTrackerConfig().Load(); + TrackerFrame *frame = new TrackerFrame(); frame->Show(true); return true; diff --git a/tracker_config.cpp b/tracker_config.cpp new file mode 100644 index 0000000..96bb60a --- /dev/null +++ b/tracker_config.cpp @@ -0,0 +1,33 @@ +#include "tracker_config.h" + +#include +#include + +constexpr const char* CONFIG_FILE_NAME = "config.yaml"; + +void TrackerConfig::Load() { + try { + YAML::Node file = YAML::LoadFile(CONFIG_FILE_NAME); + + ap_server = file["ap_server"].as(); + ap_player = file["ap_player"].as(); + ap_password = file["ap_password"].as(); + } catch (const std::exception&) { + // It's fine if the file can't be loaded. + } +} + +void TrackerConfig::Save() { + YAML::Node output; + output["ap_server"] = ap_server; + output["ap_player"] = ap_player; + output["ap_password"] = ap_password; + + std::ofstream filewriter(CONFIG_FILE_NAME); + filewriter << output; +} + +TrackerConfig& GetTrackerConfig() { + static TrackerConfig* instance = new TrackerConfig(); + return *instance; +} diff --git a/tracker_config.h b/tracker_config.h new file mode 100644 index 0000000..e2e6cd7 --- /dev/null +++ b/tracker_config.h @@ -0,0 +1,19 @@ +#ifndef TRACKER_CONFIG_H_36CDD648 +#define TRACKER_CONFIG_H_36CDD648 + +#include + +class TrackerConfig { + public: + void Load(); + + void Save(); + + std::string ap_server; + std::string ap_player; + std::string ap_password; +}; + +TrackerConfig& GetTrackerConfig(); + +#endif /* end of include guard: TRACKER_CONFIG_H_36CDD648 */ diff --git a/tracker_frame.cpp b/tracker_frame.cpp index 37a8281..774b710 100644 --- a/tracker_frame.cpp +++ b/tracker_frame.cpp @@ -2,6 +2,7 @@ #include "ap_state.h" #include "connection_dialog.h" +#include "tracker_config.h" #include "tracker_panel.h" enum TrackerFrameIds { ID_CONNECT = 1 }; @@ -64,6 +65,11 @@ void TrackerFrame::OnConnect(wxCommandEvent &event) { ConnectionDialog dlg; if (dlg.ShowModal() == wxID_OK) { + GetTrackerConfig().ap_server = dlg.GetServerValue(); + GetTrackerConfig().ap_player = dlg.GetPlayerValue(); + GetTrackerConfig().ap_password = dlg.GetPasswordValue(); + GetTrackerConfig().Save(); + GetAPState().Connect(dlg.GetServerValue(), dlg.GetPlayerValue(), dlg.GetPasswordValue()); } -- cgit 1.4.1