-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Configuration and Connection Handling for Consistency #268
Open
stevewgr
wants to merge
6
commits into
master
Choose a base branch
from
refactor-ini-code
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7a6647d
Remove redundant unused ini files.
stevewgr 1fa93c7
Load LoginServer connection string from server.ini.
stevewgr 4bd0e38
Refactor and centralize configuration loading from ini files.
stevewgr 0ad96ed
Simplify and optimize connection string handling.
stevewgr f91d896
Rename and use more concise naming convention for connection strings.
stevewgr 65b9b82
Implement consistent cross-platform ini file management.
stevewgr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "StdAfx.h" | ||
#include "Ini.h" | ||
|
||
CIni::CIni() { | ||
m_Ini.SetUnicode(); | ||
IniFileSet(fs::mktemp_file("config", ".ini")); | ||
m_bWriteDefaults = true; | ||
} | ||
|
||
CIni::CIni(const fs::path & fsFile, bool bWriteDefaults /* = true */) { | ||
m_Ini.SetUnicode(); | ||
IniFileSet(fsFile); | ||
m_bWriteDefaults = bWriteDefaults; | ||
} | ||
|
||
void CIni::IniFileSet(const fs::path & fsFile) { | ||
m_fsFile = fsFile.is_absolute() ? fsFile : (n3std::get_app_dir() / fsFile); | ||
ReloadFile(); | ||
} | ||
|
||
void CIni::ReloadFile() const { | ||
if (!fs::exists(m_fsFile)) { | ||
return; | ||
} | ||
|
||
std::unique_lock lock(m_mtxIni); | ||
|
||
// Check both last write timestamp and interval to avoid excessive reloads. | ||
// Defaults written by the app updates the file timestamp, so we also ensure | ||
// a minimum time interval before reloading. | ||
auto tpCurrentWriteTime = fs::last_write_time(m_fsFile); | ||
auto tpNow = std::chrono::steady_clock::now(); | ||
auto tpElapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>(tpNow - m_tpLastReload); | ||
bool bNeedsReload = tpCurrentWriteTime != m_tpLastWriteTime && tpElapsedSeconds >= m_dReloadIntervalSeconds; | ||
if (!bNeedsReload) { | ||
return; | ||
} | ||
|
||
m_Ini.Reset(); | ||
m_Ini.LoadFile(m_fsFile.c_str()); | ||
m_tpLastReload = tpNow; | ||
m_tpLastWriteTime = tpCurrentWriteTime; | ||
} | ||
|
||
void CIni::SetBool(std::string_view szSection, std::string_view szKey, bool bDefault) const { | ||
SetString(szSection, szKey, bDefault ? "1" : "0"); | ||
} | ||
|
||
void CIni::SetInt(std::string_view szSection, std::string_view szKey, int iDefault) const { | ||
SetString(szSection, szKey, std::to_string(iDefault)); | ||
} | ||
|
||
void CIni::SetFloat(std::string_view szSection, std::string_view szKey, float fDefault) const { | ||
SetString(szSection, szKey, std::format("{:.04f}", fDefault)); | ||
} | ||
|
||
void CIni::SetString(std::string_view szSection, std::string_view szKey, std::string_view szDefault) const { | ||
std::unique_lock lock(m_mtxIni); | ||
m_Ini.SetValue(szSection.data(), szKey.data(), szDefault.data()); | ||
m_Ini.SaveFile(m_fsFile.c_str()); | ||
} | ||
|
||
bool CIni::GetBool(std::string_view szSection, std::string_view szKey, bool bDefault) const { | ||
return GetInt(szSection, szKey, bDefault ? 1 : 0) ? true : false; | ||
} | ||
|
||
int CIni::GetInt(std::string_view szSection, std::string_view szKey, int iDefault) const { | ||
std::string szValue = GetString(szSection, szKey, std::to_string(iDefault)); | ||
|
||
int iValue = iDefault; | ||
auto errConv = std::from_chars(szValue.data(), szValue.data() + szValue.size(), iValue); | ||
if (errConv.ec == std::errc::invalid_argument || errConv.ec == std::errc::result_out_of_range) { | ||
return iDefault; | ||
} | ||
|
||
return iValue; | ||
} | ||
|
||
float CIni::GetFloat(std::string_view szSection, std::string_view szKey, float fDefault) const { | ||
std::string szValue = GetString(szSection, szKey, std::format("{:.04f}", fDefault)); | ||
|
||
float fValue = fDefault; | ||
auto errConv = std::from_chars(szValue.data(), szValue.data() + szValue.size(), fValue); | ||
if (errConv.ec == std::errc::invalid_argument || errConv.ec == std::errc::result_out_of_range) { | ||
return fDefault; | ||
} | ||
|
||
return fValue; | ||
} | ||
|
||
std::string CIni::GetString(std::string_view szSection, std::string_view szKey, std::string_view szDefault) const { | ||
ReloadFile(); | ||
if (m_bWriteDefaults && !m_Ini.KeyExists(szSection.data(), szKey.data())) { | ||
SetString(szSection, szKey, szDefault); | ||
return std::string(szDefault); | ||
} | ||
|
||
std::shared_lock lock(m_mtxIni); | ||
const char * szValue = m_Ini.GetValue(szSection.data(), szKey.data(), szDefault.data()); | ||
return szValue ? std::string(szValue) : std::string(szDefault); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
#include "N3Utils.h" | ||
|
||
#include <simpleini/SimpleIni.h> | ||
#include <shared_mutex> | ||
|
||
class CIni { | ||
public: | ||
CIni(); | ||
CIni(const fs::path & fsFile, bool bWriteDefaults = true); | ||
|
||
const fs::path & IniFile() const { return m_fsFile; } | ||
void IniFileSet(const fs::path & fsFile); | ||
|
||
void SetBool(std::string_view szSection, std::string_view szKey, bool bDefault) const; | ||
void SetInt(std::string_view szSection, std::string_view szKey, int iDefault) const; | ||
void SetFloat(std::string_view szSection, std::string_view szKey, float fDefault) const; | ||
void SetString(std::string_view szSection, std::string_view szKey, std::string_view szDefault) const; | ||
|
||
bool GetBool(std::string_view szSection, std::string_view szKey, bool bDefault) const; | ||
int GetInt(std::string_view szSection, std::string_view szKey, int iDefault) const; | ||
float GetFloat(std::string_view szSection, std::string_view szKey, float fDefault) const; | ||
std::string GetString(std::string_view szSection, std::string_view szKey, std::string_view szDefault) const; | ||
|
||
private: | ||
void ReloadFile() const; | ||
|
||
public: | ||
bool m_bWriteDefaults; // Writes defaults if a requested key does not exists. | ||
|
||
private: | ||
fs::path m_fsFile; | ||
|
||
mutable CSimpleIniA m_Ini; | ||
mutable std::shared_mutex m_mtxIni; | ||
|
||
mutable fs::file_time_type m_tpLastWriteTime; | ||
mutable std::chrono::steady_clock::time_point m_tpLastReload; // Last reload timestamp | ||
const std::chrono::seconds m_dReloadIntervalSeconds{3}; // Hot reload interval | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add
srand(static_cast<unsigned>(time(0)));
before this line to get a different value every time?