mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
make otml simpler and easier to use, improve error handling/exceptions
This commit is contained in:
@@ -9,27 +9,23 @@ bool Configs::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())
|
||||
for(const OTMLNodePtr& child : doc->children())
|
||||
m_confsMap[child->tag()] = child->value();
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: could not load configurations: ", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Configs::save()
|
||||
{
|
||||
if(!m_fileName.empty()) {
|
||||
OTMLDocumentPtr doc = OTMLDocument::create();
|
||||
doc->write(m_confsMap);
|
||||
return doc->save(m_fileName);
|
||||
OTMLDocumentPtr doc = OTMLDocument::create();
|
||||
for(auto it : m_confsMap) {
|
||||
OTMLNodePtr node = OTMLNode::create(it.first, it.second);
|
||||
doc->addChild(node);
|
||||
}
|
||||
return false;
|
||||
return doc->save(m_fileName);
|
||||
}
|
||||
|
@@ -6,61 +6,57 @@
|
||||
|
||||
void Module::discover(const OTMLNodePtr& moduleNode)
|
||||
{
|
||||
m_description = moduleNode->readAt<std::string>("description");
|
||||
m_author = moduleNode->readAt<std::string>("author");
|
||||
m_website = moduleNode->readAt<std::string>("website");
|
||||
m_version = moduleNode->readAt<std::string>("version");
|
||||
const static std::string none = "none";
|
||||
m_description = moduleNode->valueAt("description", none);
|
||||
m_author = moduleNode->valueAt("author", none);
|
||||
m_website = moduleNode->valueAt("website", none);
|
||||
m_version = moduleNode->valueAt("version", none);
|
||||
m_autoLoad = moduleNode->valueAt<bool>("autoLoad", false);
|
||||
|
||||
if(OTMLNodePtr node = moduleNode->get("dependencies")) {
|
||||
for(const OTMLNodePtr& tmp : node->childNodes())
|
||||
for(const OTMLNodePtr& tmp : node->children())
|
||||
m_dependencies.push_back(tmp->value());
|
||||
}
|
||||
|
||||
// set onLoad callback
|
||||
if(OTMLNodePtr node = moduleNode->get("onLoad")) {
|
||||
g_lua.loadFunction(node->read<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
|
||||
g_lua.loadFunction(node->value<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
|
||||
g_lua.useValue();
|
||||
m_loadCallback = g_lua.polymorphicPop<BooleanCallback>();
|
||||
}
|
||||
|
||||
// set onUnload callback
|
||||
if(OTMLNodePtr node = moduleNode->get("onUnload")) {
|
||||
g_lua.loadFunction(node->read<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
|
||||
g_lua.loadFunction(node->value<std::string>(), "@" + node->source() + "[" + node->tag() + "]");
|
||||
g_lua.useValue();
|
||||
m_unloadCallback = g_lua.polymorphicPop<SimpleCallback>();
|
||||
}
|
||||
|
||||
// load if autoLoad is set
|
||||
m_autoLoad = moduleNode->readAt<bool>("autoLoad", false);
|
||||
}
|
||||
|
||||
bool Module::load()
|
||||
{
|
||||
for(const std::string& depName : m_dependencies) {
|
||||
ModulePtr dep = g_modules.getModule(depName);
|
||||
if(!dep) {
|
||||
logError("ERROR: failed to load module '",m_name,"': could not find module dependency '",depName,"'");
|
||||
return false;
|
||||
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"));
|
||||
}
|
||||
|
||||
if(!dep->isLoaded()) {
|
||||
if(!dep->load()) {
|
||||
logError("ERROR: failed to load module '",m_name,"': a dependency has failed to load");
|
||||
return false;
|
||||
}
|
||||
if(m_loadCallback) {
|
||||
m_loaded = m_loadCallback();
|
||||
if(!m_loaded)
|
||||
throw std::runtime_error("module onLoad event returned false");
|
||||
}
|
||||
|
||||
logInfo("Loaded module '", m_name, "'");
|
||||
return true;
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: failed to load module '", m_name, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
if(m_loadCallback) {
|
||||
m_loaded = m_loadCallback();
|
||||
if(!m_loaded) {
|
||||
logError("ERROR: failed to load module '",m_name, "': onLoad returned false");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
logInfo("Loaded module '", m_name, "'");
|
||||
return true;
|
||||
}
|
||||
|
||||
void Module::unload()
|
||||
|
@@ -30,7 +30,7 @@ bool ModuleManager::discoverModule(const std::string& file)
|
||||
OTMLDocumentPtr doc = OTMLDocument::parse(file);
|
||||
OTMLNodePtr moduleNode = doc->at("Module");
|
||||
|
||||
std::string name = moduleNode->readAt<std::string>("name");
|
||||
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?");
|
||||
|
||||
@@ -38,7 +38,7 @@ bool ModuleManager::discoverModule(const std::string& file)
|
||||
module->discover(moduleNode);
|
||||
m_modules.push_back(module);
|
||||
} catch(std::exception& e) {
|
||||
logError("ERROR: failed to load module from '", file, "':\n", e.what());
|
||||
logError("ERROR: failed to load module from '", file, "': ", e.what());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
@@ -30,14 +30,14 @@ void ResourceManager::init(const char* argv0)
|
||||
}
|
||||
|
||||
if(!found)
|
||||
throw std::runtime_error("could not find modules directory");
|
||||
logFatal("FATAL ERROR: could not find modules directory");
|
||||
|
||||
// setup write directory
|
||||
std::string dir = g_platform.getAppUserDir();
|
||||
if(g_resources.setWriteDir(dir))
|
||||
g_resources.addToSearchPath(dir);
|
||||
else
|
||||
throw std::runtime_error("could not setup write directory");
|
||||
logError("ERROR: could not setup write directory");
|
||||
}
|
||||
|
||||
void ResourceManager::terminate()
|
||||
@@ -156,12 +156,11 @@ std::string ResourceManager::resolvePath(const std::string& path)
|
||||
{
|
||||
std::string fullPath;
|
||||
if(boost::starts_with(path, "/"))
|
||||
fullPath = path.substr(1);
|
||||
fullPath = path;
|
||||
else {
|
||||
std::string scriptPath = g_lua.currentSourcePath();
|
||||
if(!scriptPath.empty()) {
|
||||
std::string scriptPath = "/" + g_lua.currentSourcePath();
|
||||
if(!scriptPath.empty())
|
||||
fullPath += scriptPath + "/";
|
||||
}
|
||||
fullPath += path;
|
||||
}
|
||||
return fullPath;
|
||||
|
Reference in New Issue
Block a user