From b5605c839dc91fa88a58e1e564b91c6872f9b492 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 4 Dec 2015 15:36:34 +0100 Subject: [PATCH] Developer Console that lets you bind variables to be changed at runtime --- include/Engine/Core/DeveloperConsole.h | 135 +++++++++++++++++++++++++ src/Engine/Core/DeveloperConsole.cpp | 5 + 2 files changed, 140 insertions(+) create mode 100644 include/Engine/Core/DeveloperConsole.h create mode 100644 src/Engine/Core/DeveloperConsole.cpp diff --git a/include/Engine/Core/DeveloperConsole.h b/include/Engine/Core/DeveloperConsole.h new file mode 100644 index 00000000..a1901186 --- /dev/null +++ b/include/Engine/Core/DeveloperConsole.h @@ -0,0 +1,135 @@ +#ifndef DeveloperConsole_h__ +#define DeveloperConsole_h__ + +#include +#include +#include +#include "../Common.h" +#include "ResourceManager.h" +#include "ConfigFile.h" + +class DeveloperConsole +{ +public: + DeveloperConsole() = delete; + + static void MergeConfig(std::string configFile) + { + auto config = ResourceManager::Load(configFile); + m_ConfigFiles.push_back(config); + } + + template + static typename std::enable_if::value, void>::type + BindVariable(std::string path, T& variable) + { + BindVariable::type>(path, variable); + } + + template + static typename std::enable_if::value, void>::type + BindVariable(std::string path, T& variable) + { + m_VariableBindingSetters[path] = [&variable](std::string value) { + variable = static_cast(boost::lexical_cast(value)); + }; + + m_VariableBindingGetters[path] = [&variable]() { + return boost::lexical_cast(static_cast(variable)); + }; + } + + static void Consume(std::istream& stream) + { + std::string input; + std::getline(stream, input); + + if (input.empty()) { + return; + } + + boost::char_separator argumentSeparator(" "); + tokenizer tokens(input, argumentSeparator); + + for (tokenizer::const_iterator it = tokens.begin(); it != tokens.end(); it++) { + consumeToken(it, tokens.end()); + } + + /*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> tokenizer; + + static std::vector m_ConfigFiles; + static std::map> m_VariableBindingSetters; + static std::map> m_VariableBindingGetters; + + 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; + + for (auto& config : m_ConfigFiles) { + value = config->Get(key, ""); + } + + 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 \ No newline at end of file diff --git a/src/Engine/Core/DeveloperConsole.cpp b/src/Engine/Core/DeveloperConsole.cpp new file mode 100644 index 00000000..0677ad6f --- /dev/null +++ b/src/Engine/Core/DeveloperConsole.cpp @@ -0,0 +1,5 @@ +#include "Core/DeveloperConsole.h" + +std::vector DeveloperConsole::m_ConfigFiles; +std::map> DeveloperConsole::m_VariableBindingSetters; +std::map> DeveloperConsole::m_VariableBindingGetters;