1
0
Fork 0

config manager using yaml

logger improvements
more documentation
master
Eduardo Bart vor 14 Jahren
Ursprung f43e2bde48
Commit d121154932

@ -1,11 +1,16 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(otclient)
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
# find needed packages
SET(Boost_USE_STATIC_LIBS ON)
FIND_PACKAGE(Boost COMPONENTS thread filesystem REQUIRED)
FIND_PACKAGE(Boost REQUIRED)
FIND_PACKAGE(OpenGL REQUIRED)
FIND_PACKAGE(Lua51 REQUIRED)
FIND_PACKAGE(YamlCpp REQUIRED)
FIND_PACKAGE(PhysFS REQUIRED)
FIND_PACKAGE(PNG REQUIRED)
# choose a default build type if not specified
IF(NOT CMAKE_BUILD_TYPE)
@ -15,19 +20,26 @@ MESSAGE(STATUS "BUILD TYPE: " ${CMAKE_BUILD_TYPE})
# setup compiler options
IF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wl,--as-needed")
SET(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-parameter")
SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -Wl,--as-needed")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
INCLUDE_DIRECTORIES(
${LUA_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS})
${Boost_INCLUDE_DIRS}
${LUA_INCLUDE_DIRS}
${YAMLCPP_INCLUDE_DIRS}
${PHYSFS_INCLUDE_DIRS}
${PNG_INCLUDE_DIRS})
LINK_DIRECTORIES(
${Boost_LIBRARY_DIRS}
${LUA_LIBRARY_DIRS})
${Boost_LIBRARY_DIRS}
${LUA_LIBRARY_DIRS}
${PHYSFS_LIBRARY_DIRS}
${YAMLCPP_LIBRARY_DIRS}
${PNG_LIBRARY_DIRS})
# setup definitions
ADD_DEFINITIONS(-D_REENTRANT)
@ -38,11 +50,13 @@ ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
# find sources
SET(SOURCES
src/main.cpp
src/engine.cpp
src/graphics.cpp
src/logger.cpp
src/util.cpp)
src/configmanager.cpp
src/resourcemanager.cpp
src/main.cpp
src/engine.cpp
src/graphics.cpp
src/logger.cpp
src/util.cpp)
IF(WIN32)
SET(SOURCES ${SOURCES} src/win32platform.cpp)
@ -55,7 +69,9 @@ ADD_EXECUTABLE(otclient ${SOURCES})
# target link libraries
TARGET_LINK_LIBRARIES(otclient
${Boost_LIBRARIES}
${OPENGL_LIBRARY}
${LUA51_LIBRARY})
${Boost_LIBRARIES}
${OPENGL_LIBRARY}
${LUA51_LIBRARY}
${YAMLCPP_LIBRARY}
${PHYSFS_LIBRARY}
${PNG_LIBRARY})

@ -0,0 +1,40 @@
# - Try to find yaml-cpp
# Once done, this will define
#
# YAMLCPP_LIBRARY, link these to use yaml-cpp
# YAMLCPP_FOUND - system has yaml-cpp
# YAMLCPP_INCLUDE_DIR, the yaml-cpp include directories
FIND_PATH(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
HINTS
$ENV{PHYSFSDIR}
PATH_SUFFIXES include/yaml-cpp include
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw # Fink
/opt/local # DarwinPorts
/opt/csw # Blastwave
/opt
)
FIND_LIBRARY(YAMLCPP_LIBRARY
NAMES yaml-cpp
HINTS
$ENV{PHYSFSDIR}
PATH_SUFFIXES lib64 lib
PATHS
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt
)
INCLUDE("${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake")
FIND_PACKAGE_HANDLE_STANDARD_ARGS(YamlCpp DEFAULT_MSG YAMLCPP_LIBRARY YAMLCPP_INCLUDE_DIR)

@ -0,0 +1,153 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "configmanager.h"
#include "logger.h"
#include "util.h"
#include <fstream>
#include <yaml-cpp/yaml.h>
ConfigManager g_config;
ConfigManager::ConfigManager()
{
}
ConfigManager::~ConfigManager()
{
}
bool ConfigManager::load(const std::string& fileName)
{
std::ifstream fin(fileName.c_str());
if(!fin.good())
return false;
m_fileName = fileName;
try {
YAML::Parser parser(fin);
YAML::Node doc;
parser.GetNextDocument(doc);
for(YAML::Iterator it=doc.begin(); it != doc.end(); ++it) {
std::string key, value;
it.first() >> key;
it.second() >> value;
m_confsMap[key] = value;
}
} catch (YAML::ParserException& e) {
error("Malformed configuration file!");
return false;
}
return true;
}
void ConfigManager::save()
{
std::ofstream fout(m_fileName.c_str());
if(!fout.good()) {
error("Failed to save configuration file %s", m_fileName.c_str());
return;
}
YAML::Emitter out;
out << m_confsMap;
fout << out.c_str();
}
void ConfigManager::setValue(const std::string &key, const std::string &value)
{
m_confsMap[key] = value;
}
void ConfigManager::setValue(const std::string &key, const char *value)
{
m_confsMap[key] = value;
}
void ConfigManager::setValue(const std::string &key, int value)
{
setValue(key, castToString<int>(value));
}
void ConfigManager::setValue(const std::string &key, float value)
{
setValue(key, castToString<float>(value));
}
void ConfigManager::setValue(const std::string &key, bool value)
{
if(value)
setValue(key,"true");
else
setValue(key,"false");
}
const std::string &ConfigManager::getString(const std::string &key)
{
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
warning("Config value %s not found", key.c_str());
static std::string emptystr;
return emptystr;
}
return iter->second;
}
float ConfigManager::getFloat(const std::string &key)
{
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
warning("Config value %s not found", key.c_str());
return 0;
}
return castFromString<float>(iter->second);
}
bool ConfigManager::getBoolean(const std::string &key)
{
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
warning("Config value %s not found", key.c_str());
return 0;
}
return (iter->second == std::string("true"));
}
int ConfigManager::getInteger(const std::string &key)
{
std::map<std::string, std::string>::iterator iter = m_confsMap.find(key);
if(iter == m_confsMap.end()) {
warning("Config value %s not found", key.c_str());
return 0;
}
return castFromString<int>(iter->second);
}

@ -0,0 +1,61 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include <string>
#include <map>
class ConfigManager
{
public:
ConfigManager();
~ConfigManager();
/// Read configuration file and parse all settings to memory
bool load(const std::string& fileName);
/// Dump all settings to configuration file
void save();
void setValue(const std::string &key, const std::string &value);
void setValue(const std::string &key, const char *value);
void setValue(const std::string &key, float value);
void setValue(const std::string &key, bool value);
void setValue(const std::string &key, int value);
const std::string &getString(const std::string &key);
float getFloat(const std::string &key);
bool getBoolean(const std::string &key);
int getInteger(const std::string &key);
private:
std::string m_fileName;
std::map<std::string, std::string> m_confsMap;
};
extern ConfigManager g_config;
#endif // CONFIGMANAGER_H

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#ifndef VERSION_H
#define VERSION_H

@ -21,11 +21,14 @@
* THE SOFTWARE.
*/
#include "engine.h"
#include "platform.h"
#include "graphics.h"
#include "const.h"
#include "input.h"
#include "configmanager.h"
#include "logger.h"
Engine g_engine;
@ -44,8 +47,8 @@ void Engine::init()
{
Platform::init();
int width = 640;
int height = 480;
int width = g_config.getInteger("width");
int height = g_config.getInteger("height");
// create the window
Platform::createWindow(width, height, 550, 450);
@ -63,6 +66,10 @@ void Engine::init()
void Engine::terminate()
{
// save configs
g_config.setValue("width", Platform::getWindowWidth());
g_config.setValue("height", Platform::getWindowHeight());
Platform::showMouseCursor();
Platform::terminate();
g_graphics.terminate();

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#ifndef ENGINE_H
#define ENGINE_H
@ -35,20 +36,24 @@ public:
void init();
void terminate();
/// Main loop
void run();
void stop();
bool isRunning() const { return m_running; }
bool isStopping() const { return m_stopping; }
unsigned long getLastFrameTicks() const { return m_lastFrameTicks; }
// events fired by platform
/// Fired by platform on window close
void onClose();
/// Fired by platform on window resize
void onResize(int width, int height);
/// Fired by platform on mouse/keyboard input
void onInputEvent(InputEvent *event);
private:
/// Called to render every frame
void render();
/// Called between renders
void update(int elapsedTicks);
bool m_stopping;

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#include "graphics.h"
#include "logger.h"

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#ifndef GRAPHICS_H
#define GRAPHICS_H
@ -33,9 +34,13 @@ public:
void init();
void terminate();
///Called after every window resize
void resize(int width, int height);
///Called before every render
void beginRender();
///Called after every render
void endRender();
};

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#ifndef INPUT_H
#define INPUT_H

@ -21,19 +21,17 @@
* THE SOFTWARE.
*/
#include "logger.h"
#include "util.h"
#include <iostream>
#include <sstream>
#include <cstdarg>
#include <cstdlib>
#include <boost/algorithm/string.hpp>
void _log(int level, const char *trace, const char *format, ...)
void Logger::log(int level, const char *trace, const char *format, ...)
{
va_list args;
std::stringstream out;
std::string strace;
va_start(args, format);
@ -51,25 +49,22 @@ void _log(int level, const char *trace, const char *format, ...)
static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
static bool colored = getenv("COLORED_OUTPUT");
if(colored)
out << colors[level];
std::cout << colors[level];
#endif
if(!strace.empty())
out << "[" << strace << "] ";
std::cout << "[" << strace << "] ";
static char const *prefixes[] = { "FATAL ERROR: ", "ERROR: ", "WARNING: ", "", "", "" };
out << prefixes[level];
out << text;
std::cout << prefixes[level];
std::cout << text;
#ifdef linux
if(colored)
out << "\033[0m";
std::cout << "\033[0m";
#endif
if(level <= LWARNING)
std::cerr << out.str() << std::endl;
else
std::cout << out.str() << std::endl;
std::cout << std::endl;
if(level == LFATAL)
exit(-1);

@ -21,11 +21,16 @@
* THE SOFTWARE.
*/
#ifndef LOGGER_H
#define LOGGER_H
#include <iostream>
#include <sstream>
#include <string>
namespace Logger {
enum ELogLevel {
LFATAL = 0,
LERROR,
@ -34,15 +39,28 @@ enum ELogLevel {
LDEBUG
};
void _log(int level, const char *trace, const char *format, ...);
void log(int level, const char *trace, const char *format, ...);
}
#define fatal(...) _log(LFATAL, NULL, __VA_ARGS__)
#define error(...) _log(LERROR, NULL, __VA_ARGS__)
#define warning(...) _log(LWARNING, NULL, __VA_ARGS__)
#define debug(...) _log(LDEBUG, NULL, __VA_ARGS__)
#define notice(...) _log(LNOTICE, NULL, __VA_ARGS__)
#define fatal(...) Logger::log(Logger::LFATAL, NULL, __VA_ARGS__)
#define error(...) Logger::log(Logger::LERROR, NULL, __VA_ARGS__)
#define warning(...) Logger::log(Logger::LWARNING, NULL, __VA_ARGS__)
#define debug(...) Logger::log(Logger::LDEBUG, NULL, __VA_ARGS__)
#define notice(...) Logger::log(Logger::LNOTICE, NULL, __VA_ARGS__)
#define trace() Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, "")
#define tdebug(...) Logger::log(Logger::LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
struct Dump {
public:
Dump() { }
~Dump() { debug(m_buf.str().c_str()); }
template<class T> Dump &operator<<(const T &x) { m_buf << x << " "; return *this; }
private:
std::ostringstream m_buf;
};
#define trace() _log(LDEBUG, __PRETTY_FUNCTION__, "")
#define tdebug(...) _log(LDEBUG, __PRETTY_FUNCTION__, __VA_ARGS__)
#define dump() Dump()
#endif

@ -21,11 +21,14 @@
* THE SOFTWARE.
*/
#include "engine.h"
#include "const.h"
#include "logger.h"
#include <csignal>
#include "configmanager.h"
#include "util.h"
// catches terminate signals to exit nicely
void signal_handler(int sig)
@ -45,6 +48,13 @@ void signal_handler(int sig)
}
}
/// Default otclient configurations
void setDefaultConfigs()
{
g_config.setValue("width", 640);
g_config.setValue("height", 480);
}
int main(int argc, const char *argv[])
{
// install our signal handler
@ -52,11 +62,17 @@ int main(int argc, const char *argv[])
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
setDefaultConfigs();
if(!g_config.load("config.yml"))
notice("Could not read configuration file, default configurations will be used.");
notice(APP_LONGNAME);
// setup the engine and run
g_engine.init();
g_engine.run();
g_engine.terminate();
g_config.save();
return 0;
}

@ -21,17 +21,22 @@
* THE SOFTWARE.
*/
#ifndef PLATFORM_H
#define PLATFORM_H
// namespace with platform specific stuff
namespace Platform
{
void init();
void terminate();
/// Poll platform input/window events
void poll();
/// Get current time in milliseconds since first frame render
unsigned long getTicks();
/// Sleep in current thread
void sleep(unsigned long miliseconds);
bool createWindow(int width, int height, int minWidth, int minHeight);
@ -40,8 +45,12 @@ namespace Platform
void setWindowTitle(const char *title);
bool isWindowFocused();
bool isWindowVisible();
int getWindowWidth();
int getWindowHeight();
/// Get GL extension function address
void *getExtensionProcAddress(const char *ext);
/// Check if GL extension is supported
bool isExtensionSupported(const char *ext);
const char *getTextFromClipboard();
@ -50,8 +59,10 @@ namespace Platform
void hideMouseCursor();
void showMouseCursor();
/// Enable/disable vertical synchronization
void setVsync(bool enable = true);
/// Swap GL buffers
void swapBuffers();
};
}
#endif // PLATFORM_H

@ -0,0 +1,37 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "resourcemanager.h"
ResourceManager g_resources;
ResourceManager::ResourceManager()
{
}
ResourceManager::~ResourceManager()
{
}

@ -0,0 +1,42 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <string>
#include <list>
class ResourceManager
{
public:
ResourceManager();
~ResourceManager();
private:
};
extern ResourceManager g_resources;
#endif // RESOURCEMANAGER_H

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#include "util.h"
#include <cstdio>

@ -21,13 +21,34 @@
* THE SOFTWARE.
*/
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <sstream>
#include <cstdarg>
/// Formatting like printf for std::string, va_list input version
std::string vformat(const char *format, va_list args);
/// Formatting like printf for std::string
std::string format(const char *format, ...);
/// Convert int/float like types to std::string
template<typename T>
inline std::string castToString(const T& x) {
std::ostringstream ss;
ss << x;
return ss.str();
}
template<typename T>
inline T castFromString(const std::string& s) {
std::istringstream ss(s);
T x = 0;
ss >> x;
return x;
}
#endif

@ -21,6 +21,7 @@
* THE SOFTWARE.
*/
#include "platform.h"
#include "engine.h"
#include "input.h"
@ -708,3 +709,13 @@ bool Platform::isWindowVisible()
{
return x11.visible;
}
int Platform::getWindowWidth()
{
return x11.width;
}
int Platform::getWindowHeight()
{
return x11.height;
}

Laden…
Abbrechen
Speichern