mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 05:53:26 +02:00
add clock, change exceptions, add network exceptions, fix some crashes
This commit is contained in:
42
src/framework/core/clock.cpp
Normal file
42
src/framework/core/clock.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "clock.h"
|
||||
|
||||
Clock g_clock;
|
||||
|
||||
Clock::Clock()
|
||||
{
|
||||
m_startupTime = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
int Clock::updateTicks()
|
||||
{
|
||||
auto timeNow = std::chrono::high_resolution_clock::now();
|
||||
m_currentTicks = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - m_startupTime).count();
|
||||
return m_currentTicks;
|
||||
}
|
||||
|
||||
void Clock::sleep(int ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
48
src/framework/core/clock.h
Normal file
48
src/framework/core/clock.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 CLOCK_H
|
||||
#define CLOCK_H
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Clock
|
||||
{
|
||||
public:
|
||||
Clock();
|
||||
|
||||
int updateTicks();
|
||||
void sleep(int ms);
|
||||
|
||||
int ticks() { return m_currentTicks; }
|
||||
int ticksElapsed(int prevTicks) { return ticks() - prevTicks; }
|
||||
int ticksFor(int delay) { return ticks() + delay; }
|
||||
|
||||
private:
|
||||
int m_currentTicks;
|
||||
std::chrono::system_clock::time_point m_startupTime;
|
||||
};
|
||||
|
||||
extern Clock g_clock;
|
||||
|
||||
#endif
|
||||
|
@@ -20,14 +20,14 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "configs.h"
|
||||
#include "configmanager.h"
|
||||
#include "resourcemanager.h"
|
||||
|
||||
#include <framework/otml/otml.h>
|
||||
|
||||
Configs g_configs;
|
||||
ConfigManager g_configs;
|
||||
|
||||
bool Configs::load(const std::string& file)
|
||||
bool ConfigManager::load(const std::string& file)
|
||||
{
|
||||
m_fileName = file;
|
||||
|
||||
@@ -39,13 +39,13 @@ bool Configs::load(const std::string& file)
|
||||
for(const OTMLNodePtr& child : doc->children())
|
||||
m_confsMap[child->tag()] = child->value();
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
} catch(Exception& e) {
|
||||
logError("could not load configurations: ", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Configs::save()
|
||||
bool ConfigManager::save()
|
||||
{
|
||||
OTMLDocumentPtr doc = OTMLDocument::create();
|
||||
for(auto it : m_confsMap) {
|
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Configs
|
||||
class ConfigManager
|
||||
{
|
||||
public:
|
||||
bool load(const std::string& file);
|
||||
@@ -40,6 +40,6 @@ private:
|
||||
std::map<std::string, std::string> m_confsMap;
|
||||
};
|
||||
|
||||
extern Configs g_configs;
|
||||
extern ConfigManager g_configs;
|
||||
|
||||
#endif
|
24
src/framework/core/engine.cpp
Normal file
24
src/framework/core/engine.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 "engine.h"
|
||||
|
33
src/framework/core/engine.h
Normal file
33
src/framework/core/engine.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2011 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 ENGINE_H
|
||||
#define ENGINE_H
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Engine
|
||||
{
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -22,44 +22,42 @@
|
||||
|
||||
#include "eventdispatcher.h"
|
||||
|
||||
#include <framework/platform/platform.h>
|
||||
#include <framework/core/clock.h>
|
||||
|
||||
EventDispatcher g_dispatcher;
|
||||
|
||||
void EventDispatcher::init()
|
||||
void EventDispatcher::flush()
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
poll();
|
||||
|
||||
void EventDispatcher::terminate()
|
||||
{
|
||||
// clean scheduled events
|
||||
m_eventList.clear();
|
||||
while(!m_scheduledEventList.empty())
|
||||
m_scheduledEventList.pop();
|
||||
}
|
||||
|
||||
void EventDispatcher::poll()
|
||||
{
|
||||
while(!m_eventList.empty()) {
|
||||
m_eventList.front()();
|
||||
m_eventList.pop_front();
|
||||
}
|
||||
|
||||
while(!m_scheduledEventList.empty()) {
|
||||
if(g_platform.getTicks() < m_scheduledEventList.top().ticks)
|
||||
if(g_clock.ticks() < m_scheduledEventList.top().ticks)
|
||||
break;
|
||||
SimpleCallback callback = std::move(m_scheduledEventList.top().callback);
|
||||
m_scheduledEventList.pop();
|
||||
callback();
|
||||
}
|
||||
|
||||
while(!m_eventList.empty()) {
|
||||
m_eventList.front()();
|
||||
m_eventList.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void EventDispatcher::scheduleEvent(const SimpleCallback& callback, int delay)
|
||||
{
|
||||
m_scheduledEventList.push(ScheduledEvent(g_platform.getTicks() + delay, callback));
|
||||
assert(delay >= 0);
|
||||
m_scheduledEventList.push(ScheduledEvent(g_clock.ticksFor(delay), callback));
|
||||
}
|
||||
|
||||
void EventDispatcher::addEvent(const SimpleCallback& callback, bool pushFront /* = false */)
|
||||
void EventDispatcher::addEvent(const SimpleCallback& callback, bool pushFront)
|
||||
{
|
||||
if(pushFront)
|
||||
m_eventList.push_front(callback);
|
||||
|
@@ -35,19 +35,10 @@ struct ScheduledEvent {
|
||||
class EventDispatcher
|
||||
{
|
||||
public:
|
||||
/// Initialize dispatcher
|
||||
void init();
|
||||
|
||||
/// Cleanup scheduled events
|
||||
void terminate();
|
||||
|
||||
/// Execute scheduled events
|
||||
void flush();
|
||||
void poll();
|
||||
|
||||
/// Add an event
|
||||
void addEvent(const SimpleCallback& callback, bool pushFront = false);
|
||||
|
||||
/// Schedule an event
|
||||
void scheduleEvent(const SimpleCallback& callback, int delay);
|
||||
|
||||
private:
|
||||
|
@@ -57,28 +57,29 @@ void Module::discover(const OTMLNodePtr& moduleNode)
|
||||
|
||||
bool Module::load()
|
||||
{
|
||||
try {
|
||||
for(const std::string& depName : m_dependencies) {
|
||||
ModulePtr dep = g_modules.getModule(depName);
|
||||
if(!dep)
|
||||
throw std::runtime_error(Fw::mkstr("could not find module dependency '", depName ,"'"));
|
||||
|
||||
if(!dep->isLoaded() && !dep->load())
|
||||
throw std::runtime_error(Fw::mkstr("dependency '", depName, "' has failed to load"));
|
||||
for(const std::string& depName : m_dependencies) {
|
||||
ModulePtr dep = g_modules.getModule(depName);
|
||||
if(!dep) {
|
||||
logError("Unable to load module '", m_name, "' because dependency '", depName, "' was not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(m_loadCallback) {
|
||||
m_loaded = m_loadCallback();
|
||||
if(!m_loaded)
|
||||
throw std::runtime_error("module onLoad event returned false");
|
||||
if(!dep->isLoaded() && !dep->load()) {
|
||||
logError("Unable to load module '", m_name, "' because dependency '", depName, "' has failed to laod");
|
||||
return false;
|
||||
}
|
||||
|
||||
logInfo("Loaded module '", m_name, "'");
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("failed to load module '", m_name, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
if(m_loadCallback) {
|
||||
m_loaded = m_loadCallback();
|
||||
if(!m_loaded) {
|
||||
logError("Unable to load module '", m_name, "' because its onLoad event returned false");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
logInfo("Loaded module '", m_name, "'");
|
||||
return true;
|
||||
}
|
||||
|
||||
void Module::unload()
|
||||
|
@@ -36,14 +36,14 @@ public:
|
||||
bool load();
|
||||
void unload();
|
||||
|
||||
bool isLoaded() const { return m_loaded; }
|
||||
bool isLoaded() { return m_loaded; }
|
||||
|
||||
std::string getDescription() const { return m_description; }
|
||||
std::string getName() const { return m_name; }
|
||||
std::string getAuthor() const { return m_author; }
|
||||
std::string getWebsite() const { return m_website; }
|
||||
std::string getVersion() const { return m_version; }
|
||||
bool autoLoad() const { return m_autoLoad; }
|
||||
std::string getDescription() { return m_description; }
|
||||
std::string getName() { return m_name; }
|
||||
std::string getAuthor() { return m_author; }
|
||||
std::string getWebsite() { return m_website; }
|
||||
std::string getVersion() { return m_version; }
|
||||
bool autoLoad() { return m_autoLoad; }
|
||||
|
||||
private:
|
||||
bool m_loaded;
|
||||
|
@@ -56,13 +56,13 @@ bool ModuleManager::discoverModule(const std::string& file)
|
||||
|
||||
std::string name = moduleNode->valueAt("name");
|
||||
if(getModule(name))
|
||||
throw OTMLException(moduleNode, "a module with the same name is already discovered, did you duplicate module names?");
|
||||
Fw::throwException("module '", name, "' already exists, cannot have duplicate module names");
|
||||
|
||||
module = ModulePtr(new Module(name));
|
||||
module->discover(moduleNode);
|
||||
m_modules.push_back(module);
|
||||
} catch(std::exception& e) {
|
||||
logError("failed to load module from '", file, "': ", e.what());
|
||||
} catch(Exception& e) {
|
||||
logError("Unable to discover module from file '", file, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@@ -107,7 +107,7 @@ void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
|
||||
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
|
||||
if(!file) {
|
||||
out.clear(std::ios::failbit);
|
||||
throw std::runtime_error(Fw::mkstr("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError()));
|
||||
Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError());
|
||||
} else {
|
||||
int fileSize = PHYSFS_fileLength(file);
|
||||
if(fileSize > 0) {
|
||||
|
Reference in New Issue
Block a user