Compare commits

...

5 Commits

8 changed files with 326 additions and 47 deletions
+31 -25
View File
@@ -3,10 +3,10 @@
#include <string> #include <string>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/range/adaptors.hpp>
#include <ini_file/ini_file.hpp>
#include "../Common.h" #include "../Common.h"
#include "ResourceManager.h" #include "ResourceManager.h"
@@ -21,45 +21,51 @@ public:
template <typename T> template <typename T>
T Get(std::string key, T defaultValue); T Get(std::string key, T defaultValue);
template <typename T> template <typename T>
std::vector<std::pair<std::string, T>> GetAll(std::string key);
template <typename T>
void Set(std::string key, T value); void Set(std::string key, T value);
const ini_file::section* GetSection(std::string section);
const ini_file::section_map& GetSections() { return m_Merged; }
const ini_file::param* GetParam(std::string key);
void SaveToDisk(); void SaveToDisk();
private: private:
boost::filesystem::path m_Path; boost::filesystem::path m_Path;
boost::property_tree::ptree m_PTreeDefaults; ini_file::section_map m_Defaults;
boost::property_tree::ptree m_PTreeOverrides; ini_file::section_map m_Overrides;
boost::property_tree::ptree m_PTreeMerged; ini_file::section_map m_Merged;
// Merge ini file section map b into a
void mergeINI(ini_file::section_map& a, const ini_file::section_map& b);
// Convert an ini key delimited by periods to a section and a param
boost::optional<std::pair<std::string, std::string>> tokenizeKey(std::string key);
}; };
template <typename T> template <typename T>
T ConfigFile::Get(std::string key, T defaultValue) T ConfigFile::Get(std::string key, T defaultValue)
{ {
return m_PTreeMerged.get<T>(key, defaultValue); auto param = GetParam(key);
} if (param == nullptr) {
return defaultValue;
}
template <typename T> return boost::lexical_cast<T>(param->get_value());
std::vector<std::pair<std::string, T>> ConfigFile::GetAll(std::string key)
{
std::vector<std::pair<std::string, T>> out;
auto parent = m_PTreeMerged.find(key);
if (parent == m_PTreeMerged.not_found()) {
return out;
}
for (auto& child : parent->second) {
T value = boost::lexical_cast<T>(child.second.data());
out.push_back(std::make_pair(child.first, value));
}
return out;
} }
template <typename T> template <typename T>
void ConfigFile::Set(std::string key, T value) void ConfigFile::Set(std::string key, T value)
{ {
m_PTreeOverrides.put<T>(key, value); std::string section;
m_PTreeMerged.put<T>(key, value); std::string param;
if (auto tokens = tokenizeKey(key)) {
std::tie(section, param) = *tokens;
} else {
LOG_WARNING("%s: Malformed config key \"%s\"", m_Path.string().c_str(), key.c_str());
return;
}
m_Overrides[section][param] = boost::lexical_cast<std::string>(value);
m_Merged[section][param] = boost::lexical_cast<std::string>(value);
} }
#endif #endif
+157
View File
@@ -0,0 +1,157 @@
#ifndef DeveloperConsole_h__
#define DeveloperConsole_h__
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include "../Common.h"
#include "ConfigFile.h"
class DeveloperConsole
{
public:
DeveloperConsole() = delete;
static void MergeConfig(std::string configFile)
{
auto config = ResourceManager::Load<ConfigFile>("Config.ini");
LOG_DEBUG("Debug.LogLevel %i", config->Get<int>("Debug.LogLevel", -1));
config->Set("Debug.LogLevel", 8);
LOG_DEBUG("Debug.LogLevel %i", config->Get<int>("Debug.LogLevel", -1));
config->Set("Test.TesTest.Testies", "Hello");
config->SaveToDisk();
for (auto& section : config->GetSections()) {
for (auto& param : *section.second) {
std::cout << "// " << param.second->get_comment() << std::endl;
std::cout << section.first << "." << param.first << " " << param.second->get_value() << std::endl;
}
}
for (auto& section : config->GetSections()) {
for (auto& param : *section.second) {
std::string key = section.first + "." + param.first;
m_VariableBindingSetters[key] = [config, key](std::string value) {
config->Set<std::string>(key, value);
};
m_VariableBindingGetters[key] = [config, key]() {
return config->Get<std::string>(key, "");
};
}
}
}
template <typename T>
static typename std::enable_if<std::is_enum<T>::value, void>::type
BindVariable(std::string path, T& variable)
{
BindVariable<T, std::underlying_type<T>::type>(path, variable);
}
template <typename T, typename R = T>
static typename std::enable_if<!std::is_enum<R>::value, void>::type
BindVariable(std::string path, T& variable)
{
m_VariableBindingSetters[path] = [&variable](std::string value) {
variable = static_cast<T>(boost::lexical_cast<R>(value));
};
m_VariableBindingGetters[path] = [&variable]() {
return boost::lexical_cast<std::string>(static_cast<R>(variable));
};
}
static void Consume(const std::string& command)
{
if (command.empty()) {
return;
}
boost::char_separator<char> argumentSeparator(" ");
tokenizer tokens(command, argumentSeparator);
for (tokenizer::const_iterator it = tokens.begin(); it != tokens.end(); it++) {
consumeToken(it, tokens.end());
}
}
static void Consume(std::istream& stream)
{
std::string input;
std::getline(stream, input);
Consume(input);
/*if (stream.peek() == '\n') {
printValue(key);
} else {
std::string newValue;
stream >> newValue;
if (m_VariableBindingSetters.find(key) != m_VariableBindingSetters.end()) {
m_VariableBindingSetters.at(key)(newValue);
} else {
std::cout << "Unknown path: " << key << std::endl;
}
return;
for (auto& config : m_ConfigFiles) {
config->Set(key, newValue);
config->SaveToDisk();
LOG_DEBUG("Key %s set to new value %s and saved to disk.", key.c_str(), newValue.c_str());
}
} */
}
private:
typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
static std::map<std::string, std::function<std::string()>> m_VariableBindingGetters;
static std::map<std::string, std::function<void(std::string)>> m_VariableBindingSetters;
static std::map<std::string, std::function<std::string(std::string)>> m_VariableBindingMappers;
static std::map<std::string, std::string> m_VariableDocumentation;
static void consumeToken(tokenizer::iterator& token, const tokenizer::iterator end)
{
tokenizer::iterator next = token;
next++;
if (next == end) {
printValue(token);
} else {
setValue(token, end);
}
}
static void printValue(tokenizer::iterator token)
{
std::string key = *token;
std::string value;
if (m_VariableBindingGetters.find(key) != m_VariableBindingGetters.end()) {
std::cout << key << " = " << m_VariableBindingGetters.at(key)() << std::endl;
} else {
std::cout << "Unknown path: " << key << std::endl;
}
return;
if (!value.empty()) {
std::cout << key << " = " << value << std::endl;
} else {
std::cerr << "Unknown path: " << key << std::endl;
}
}
static void setValue(tokenizer::iterator& token, const tokenizer::iterator end)
{
std::string key = *token;
std::string value = *(++token);
if (m_VariableBindingSetters.find(key) != m_VariableBindingSetters.end()) {
m_VariableBindingSetters.at(key)(value);
} else {
std::cout << "Unknown path: " << key << std::endl;
}
}
};
#endif
+23
View File
@@ -80,4 +80,27 @@ static void _LOG(_LOG_LEVEL logLevel, const char* file, const char* func, unsign
#define LOG_DEBUG(format, ...) \ #define LOG_DEBUG(format, ...) \
LOG(LOG_LEVEL_DEBUG, format, ##__VA_ARGS__) LOG(LOG_LEVEL_DEBUG, format, ##__VA_ARGS__)
// Set the log level temporarily for the current scope
#define LOG_LEVEL_SCOPE(logLevel) \
_LOG_LEVEL_SCOPED_HELPER<logLevel> _logLevelScopedHelper;
template <_LOG_LEVEL LEVEL>
class _LOG_LEVEL_SCOPED_HELPER
{
public:
_LOG_LEVEL_SCOPED_HELPER()
{
m_OriginalLogLevel = LOG_LEVEL;
LOG_LEVEL = LEVEL;
}
~_LOG_LEVEL_SCOPED_HELPER()
{
LOG_LEVEL = m_OriginalLogLevel;
}
private:
_LOG_LEVEL m_OriginalLogLevel;
};
#endif // Logging_h__ #endif // Logging_h__
+1
View File
@@ -94,6 +94,7 @@ set(SOURCE_FILES
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui.cpp ${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui.cpp
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_draw.cpp ${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_draw.cpp
${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_demo.cpp ${CMAKE_SOURCE_DIR}/deps/include/imgui/imgui_demo.cpp
${CMAKE_SOURCE_DIR}/deps/include/ini_file/ini_file.cpp
${CMAKE_SOURCE_DIR}/deps/include/nativefiledialog/nfd_common.c ${CMAKE_SOURCE_DIR}/deps/include/nativefiledialog/nfd_common.c
) )
+90 -16
View File
@@ -7,36 +7,110 @@ ConfigFile::ConfigFile(std::string path)
boost::filesystem::path defaultFile; boost::filesystem::path defaultFile;
defaultFile = m_Path.parent_path() / ("Default" + m_Path.filename().string()); defaultFile = m_Path.parent_path() / ("Default" + m_Path.filename().string());
std::fstream file;
// Read defaults // Read defaults
if (boost::filesystem::exists(defaultFile)) { file.open(defaultFile.string());
if (file) {
try { try {
boost::property_tree::ini_parser::read_ini(defaultFile.string(), m_PTreeDefaults); file >> m_Defaults;
} catch (boost::property_tree::ptree_error& e) { } catch (ini_file::ini_exceptions::ini_file_exception& e) {
LOG_ERROR("Failed to parse \"%s\":\n%s", defaultFile.string().c_str(), e.what()); LOG_ERROR("Failed to parse \"%s\":\n%s", defaultFile.string().c_str(), "ini_file_exception");
}
} else {
LOG_WARNING("Failed to find \"%s\"! Relying on hardcoded default values!", defaultFile.string().c_str());
}
file.close();
// Read overrides
file.open(m_Path.string());
if (file) {
try {
file >> m_Overrides;
} catch (ini_file::ini_exceptions::ini_file_exception& e) {
LOG_ERROR("Failed to parse \"%s\":\n%s", defaultFile.string().c_str(), "ini_file_exception");
} }
} else { } else {
LOG_ERROR("Failed to find \"%s\"! Relying on hardcoded default values!", defaultFile.string().c_str()); LOG_ERROR("Failed to find \"%s\"! Relying on hardcoded default values!", defaultFile.string().c_str());
} }
file.close();
m_PTreeMerged = m_PTreeDefaults; // Merge
mergeINI(m_Merged, m_Defaults);
mergeINI(m_Merged, m_Overrides);
}
// Read overrides const ini_file::section* ConfigFile::GetSection(std::string section)
if (boost::filesystem::exists(m_Path)) { {
try { auto sectionIt = m_Merged.find(section);
boost::property_tree::ini_parser::read_ini(m_Path.string(), m_PTreeOverrides); if (sectionIt == m_Merged.end()) {
for (auto& topLevelNode : m_PTreeOverrides) { LOG_WARNING("%s: Unknown section \"%s\"", m_Path.string().c_str(), section.c_str());
auto& mergedTopLevelNode = m_PTreeMerged.find(topLevelNode.first); return nullptr;
for (auto& childOverrideNode : topLevelNode.second) {
mergedTopLevelNode->second.put_child(childOverrideNode.first, childOverrideNode.second);
} }
return sectionIt->second.get();
}
const ini_file::param* ConfigFile::GetParam(std::string key)
{
std::string section;
std::string param;
if (auto tokens = tokenizeKey(key)) {
std::tie(section, param) = *tokens;
} else {
LOG_WARNING("%s: Malformed config key \"%s\"", m_Path.string().c_str(), key.c_str());
return nullptr;
} }
} catch (boost::property_tree::ptree_error& e) {
LOG_ERROR("Failed to parse \"%s\":\n%s", m_Path.filename().string().c_str(), e.what()); auto sectionIt = m_Merged.find(section);
if (sectionIt == m_Merged.end()) {
LOG_WARNING("%s: Unknown section \"%s\"", m_Path.string().c_str(), section.c_str());
return nullptr;
} }
auto paramIt = sectionIt->second->find(param);
if (paramIt == sectionIt->second->end()) {
LOG_WARNING("%s: Unknown section param \"%s\"", m_Path.string().c_str(), key.c_str());
return nullptr;
} }
return paramIt->second.get();
} }
void ConfigFile::SaveToDisk() void ConfigFile::SaveToDisk()
{ {
boost::property_tree::ini_parser::write_ini(m_Path.string(), m_PTreeOverrides); std::ofstream file(m_Path.string());
file << m_Overrides;
file.close();
}
void ConfigFile::mergeINI(ini_file::section_map& to, const ini_file::section_map& from)
{
for (auto& section : from) {
for (auto& param : *section.second) {
auto& p = to[section.first][param.first];
if (!param.second->get_comment().empty()) {
p.set_comment(param.second->get_comment());
}
p.set_value(param.second->get_value());
}
}
}
boost::optional<std::pair<std::string, std::string>> ConfigFile::tokenizeKey(std::string key)
{
std::size_t delimiter = key.find_last_of('.');
if (delimiter == std::string::npos) {
return boost::none;
}
std::string section = key.substr(0, delimiter);
if (section.empty()) {
return boost::none;
}
std::string param = key.substr(delimiter + 1);
if (param.empty()) {
return boost::none;
}
return std::make_pair(section, param);
} }
+4
View File
@@ -0,0 +1,4 @@
#include "Core/DeveloperConsole.h"
std::map<std::string, std::function<void(std::string)>> DeveloperConsole::m_VariableBindingSetters;
std::map<std::string, std::function<std::string()>> DeveloperConsole::m_VariableBindingGetters;
+8 -3
View File
@@ -17,10 +17,15 @@ InputProxy::~InputProxy()
void InputProxy::LoadBindings(std::string file) void InputProxy::LoadBindings(std::string file)
{ {
auto config = ResourceManager::Load<ConfigFile>(file); auto config = ResourceManager::Load<ConfigFile>(file);
for (auto& origin : config->GetAll<std::string>("Bindings")) { auto section = config->GetSection("Bindings");
if (section == nullptr) {
return;
}
for (auto& param : *section) {
Events::BindOrigin e; Events::BindOrigin e;
e.Origin = origin.first; e.Origin = param.first;
e.Command = origin.second; e.Command = param.second->get_value();
e.Value = 1.f; e.Value = 1.f;
if (!e.Command.empty()) { if (!e.Command.empty()) {
char prefix = e.Command.at(0); char prefix = e.Command.at(0);
Regular → Executable
+9
View File
@@ -2,6 +2,7 @@
#include "Collision/TriggerSystem.h" #include "Collision/TriggerSystem.h"
#include "Collision/CollisionSystem.h" #include "Collision/CollisionSystem.h"
#include "Game/HealthSystem.h" #include "Game/HealthSystem.h"
#include "Core/DeveloperConsole.h"
#include "Core/EntityFileWriter.h" #include "Core/EntityFileWriter.h"
Game::Game(int argc, char* argv[]) Game::Game(int argc, char* argv[])
@@ -15,6 +16,14 @@ Game::Game(int argc, char* argv[])
m_Config = ResourceManager::Load<ConfigFile>("Config.ini"); m_Config = ResourceManager::Load<ConfigFile>("Config.ini");
LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1)); LOG_LEVEL = static_cast<_LOG_LEVEL>(m_Config->Get<int>("Debug.LogLevel", 1));
DeveloperConsole::MergeConfig("Config.ini");
DeveloperConsole::BindVariable("Debug.LogLevel", LOG_LEVEL);
std::string testBind = "Carlito";
DeveloperConsole::BindVariable("Test", testBind);
while (true) {
DeveloperConsole::Consume(std::cin);
}
// Create the core event broker // Create the core event broker
m_EventBroker = new EventBroker(); m_EventBroker = new EventBroker();