mirror of
https://github.com/edubart/otclient.git
synced 2025-11-30 23:26:51 +01:00
display motd message only once, remove update loop, use g_platform.getTicks() instead
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
#include "configmanager.h"
|
||||
#include "resourcemanager.h"
|
||||
|
||||
#include <framework/otml/otml.h>
|
||||
|
||||
ConfigManager g_configs;
|
||||
|
||||
bool ConfigManager::load(const std::string& fileName)
|
||||
{
|
||||
m_fileName = fileName;
|
||||
|
||||
if(!g_resources.fileExists(fileName))
|
||||
return false;
|
||||
|
||||
try {
|
||||
OTMLDocumentPtr doc = OTMLDocument::parse(fileName);
|
||||
for(const OTMLNodePtr& child : doc->childNodes())
|
||||
m_confsMap[child->tag()] = child->value();
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: could not load configurations: ", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ConfigManager::save()
|
||||
{
|
||||
if(!m_fileName.empty()) {
|
||||
OTMLDocumentPtr doc = OTMLDocument::create();
|
||||
doc->write(m_confsMap);
|
||||
return doc->save(m_fileName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
#ifndef CONFIGMANAGER_H
|
||||
#define CONFIGMANAGER_H
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
struct ConfigValueProxy {
|
||||
ConfigValueProxy(const std::string& v) : value(v) { }
|
||||
operator std::string() const { return fw::unsafe_cast<std::string>(value); }
|
||||
operator float() const { return fw::unsafe_cast<float>(value); }
|
||||
operator int() const { return fw::unsafe_cast<int>(value); }
|
||||
operator bool() const { return fw::unsafe_cast<bool>(value); }
|
||||
std::string value;
|
||||
};
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
public:
|
||||
bool load(const std::string& fileName);
|
||||
bool save();
|
||||
|
||||
template<class T>
|
||||
void set(const std::string& key, const T& value) { m_confsMap[key] = fw::unsafe_cast<std::string>(value); }
|
||||
|
||||
ConfigValueProxy get(const std::string& key) { return ConfigValueProxy(m_confsMap[key]); }
|
||||
|
||||
private:
|
||||
std::string m_fileName;
|
||||
std::map<std::string, std::string> m_confsMap;
|
||||
};
|
||||
|
||||
extern ConfigManager g_configs;
|
||||
|
||||
#endif
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <framework/ui/ui.h>
|
||||
#include <framework/net/protocol.h>
|
||||
#include <framework/core/eventdispatcher.h>
|
||||
#include <framework/core/configs.h>
|
||||
|
||||
void LuaInterface::registerFunctions()
|
||||
{
|
||||
@@ -30,6 +31,9 @@ void LuaInterface::registerFunctions()
|
||||
g_lua.bindClassMemberFunction<UIWidget>("getChild", &UIWidget::getChildById);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("addChild", &UIWidget::addChild);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("lock", &UIWidget::lock);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("hide", &UIWidget::hide);
|
||||
g_lua.bindClassMemberFunction<UIWidget>("show", &UIWidget::show);
|
||||
|
||||
|
||||
// UILabel
|
||||
g_lua.registerClass<UILabel, UIWidget>();
|
||||
@@ -55,6 +59,11 @@ void LuaInterface::registerFunctions()
|
||||
// Protocol
|
||||
g_lua.registerClass<Protocol>();
|
||||
|
||||
// ConfigManager
|
||||
g_lua.registerClass<Configs>();
|
||||
g_lua.bindClassStaticFunction<Configs>("set", std::bind(&Configs::set, &g_configs, _1, _2));
|
||||
g_lua.bindClassStaticFunction<Configs>("get", std::bind(&Configs::get, &g_configs, _1));
|
||||
|
||||
// global functions
|
||||
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
|
||||
g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1));
|
||||
|
||||
@@ -14,8 +14,11 @@ public:
|
||||
/// Poll platform input/window events
|
||||
void poll();
|
||||
|
||||
/// Get current time in milliseconds since init
|
||||
int getTicks();
|
||||
void updateTicks();
|
||||
|
||||
/// Get current time in milliseconds since last application init
|
||||
int getTicks() { return m_lastTicks; }
|
||||
|
||||
/// Sleep in current thread
|
||||
void sleep(ulong ms);
|
||||
|
||||
@@ -58,6 +61,7 @@ public:
|
||||
|
||||
private:
|
||||
PlatformListener* m_listener;
|
||||
int m_lastTicks;
|
||||
};
|
||||
|
||||
extern Platform g_platform;
|
||||
|
||||
@@ -38,6 +38,7 @@ struct X11PlatformPrivate {
|
||||
int height;
|
||||
int x;
|
||||
int y;
|
||||
int lastTicks;
|
||||
std::string clipboardText;
|
||||
std::map<int, uchar> keyMap;
|
||||
PlatformListener* listener;
|
||||
@@ -240,7 +241,7 @@ void Platform::init(PlatformListener* platformListener, const char *appName)
|
||||
x11.atomWindowMaximizedHorz = XInternAtom(x11.display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
|
||||
|
||||
// force first tick
|
||||
Platform::getTicks();
|
||||
updateTicks();
|
||||
}
|
||||
|
||||
void Platform::terminate()
|
||||
@@ -447,7 +448,7 @@ void Platform::poll()
|
||||
}
|
||||
}
|
||||
|
||||
int Platform::getTicks()
|
||||
void Platform::updateTicks()
|
||||
{
|
||||
static timeval tv;
|
||||
static ulong firstTick = 0;
|
||||
@@ -456,7 +457,7 @@ int Platform::getTicks()
|
||||
if(!firstTick)
|
||||
firstTick = tv.tv_sec;
|
||||
|
||||
return ((tv.tv_sec - firstTick) * 1000) + (tv.tv_usec / 1000);
|
||||
m_lastTicks = ((tv.tv_sec - firstTick) * 1000) + (tv.tv_usec / 1000);
|
||||
}
|
||||
|
||||
void Platform::sleep(ulong miliseconds)
|
||||
|
||||
@@ -193,14 +193,23 @@ R safe_cast(const T& t) {
|
||||
/// Usage:
|
||||
/// R r = fw::unsafe_cast<R>(t);
|
||||
template<typename R, typename T>
|
||||
R unsafe_cast(const T& t) {
|
||||
R r;
|
||||
R unsafe_cast(const T& t, R def = R()) {
|
||||
try {
|
||||
r = safe_cast<R,T>(t);
|
||||
return safe_cast<R,T>(t);
|
||||
} catch(bad_cast& e) {
|
||||
println(e.what());
|
||||
return def;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::string tostring(const T& t) {
|
||||
return unsafe_cast<std::string, T>(t);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T fromstring(const std::string& str, T def = T()) {
|
||||
return unsafe_cast<T, std::string>(str, def);
|
||||
}
|
||||
|
||||
// an empty string to use anywhere needed
|
||||
|
||||
Reference in New Issue
Block a user