use exceptions in FML

This commit is contained in:
Eduardo Bart
2011-05-21 19:24:10 -03:00
parent c3e67fceb9
commit d7bc083014
14 changed files with 306 additions and 175 deletions

View File

@@ -75,10 +75,8 @@ UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr&
return UIElementPtr();
}
m_currentFile = filePath;
FML::Parser parser(fin);
if(!parser.hasError()) {
try {
FML::Parser parser(fin, filePath);
FML::Node* doc = parser.getDocument();
// get element id
@@ -105,8 +103,8 @@ UIElementPtr UILoader::loadFromFile(std::string filePath, const UIContainerPtr&
// report onLoad events
element->onLoad();
return element;
} else {
flogError("ERROR: Failed to load ui %s: %s", filePath.c_str() % parser.getErrorMessage());
} catch(FML::Exception e) {
flogError("ERROR: Failed to load ui %s: %s", filePath.c_str() % e.what());
}
return UIElementPtr();
@@ -192,14 +190,14 @@ void UILoader::loadElement(const UIElementPtr& element, FML::Node* node)
// load events
if(FML::Node* cnode = node->at("onLoad")) {
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onLoad")))
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, cnode)))
g_lua.setScriptableField(element, "onLoad");
else
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
}
if(FML::Node* cnode = node->at("onDestroy")) {
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, "onDestroy")))
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(element, cnode)))
g_lua.setScriptableField(element, "onDestroy");
else
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
@@ -254,19 +252,19 @@ void UILoader::loadButton(const UIButtonPtr& button, FML::Node* node)
// set on click event
if(FML::Node* cnode = node->at("onClick")) {
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(button, "onClick")))
if(g_lua.loadBufferAsFunction(cnode->value(), getElementSourceDesc(button, cnode)))
g_lua.setScriptableField(button, "onClick");
else
logError(cnode->generateErrorMessage("failed to parse inline lua script"));
}
}
std::string UILoader::getElementSourceDesc(const UIElementPtr& element, const std::string& key)
std::string UILoader::getElementSourceDesc(const UIElementPtr& element, const FML::Node *node)
{
std::string desc;
desc += g_resources.resolvePath(m_currentFile) + ":" + element->getId();
if(key.length() > 0)
desc += "[" + key + "]";
desc += g_resources.resolvePath(node->what()) + ":" + element->getId();
if(!node->tag().empty())
desc += "[" + node->tag() + "]";
return desc;
}

View File

@@ -55,9 +55,7 @@ private:
// specific elements loading
void loadButton(const UIButtonPtr& button, FML::Node* node);
std::string getElementSourceDesc(const UIElementPtr& element, const std::string& key = "");
std::string m_currentFile;
std::string getElementSourceDesc(const UIElementPtr& element, const FML::Node *node);
};
extern UILoader g_uiLoader;

View File

@@ -43,11 +43,11 @@ void UISkins::load(const std::string& skinName)
if(!g_resources.loadFile(skinName + ".yml", fin))
flogFatal("FATAL ERROR: Could not load skin \"%s", skinName.c_str());
FML::Parser parser(fin);
if(!parser.hasError()) {
try {
FML::Parser parser(fin, skinName);
FML::Node* doc = parser.getDocument();
m_defaultFont = g_fonts.get(doc->readAt<std::string>("default font"));
m_defaultFont = g_fonts.get(doc->valueAt("default font"));
if(!m_defaultFont)
logFatal("FATAL ERROR: Could not load skin default font");
@@ -57,48 +57,30 @@ void UISkins::load(const std::string& skinName)
if(!defaultTextureName.empty())
m_defaultTexture = g_textures.get(defaultTextureName);
if(FML::Node* node = doc->at("buttons")) {
foreach(FML::Node* node, *doc) {
UIElementSkinPtr skin;
foreach(FML::Node* cnode, *node) {
UIElementSkinPtr skin = UIElementSkinPtr(new UIButtonSkin(cnode->tag()));
if(node->tag() == "buttons")
skin = UIElementSkinPtr(new UIButtonSkin(cnode->tag()));
else if(node->tag() == "panels")
skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::Panel));
else if(node->tag() == "windows")
skin = UIElementSkinPtr(new UIWindowSkin(cnode->tag()));
else if(node->tag() == "labels")
skin = UIElementSkinPtr(new UILabelSkin(cnode->tag()));
else if(node->tag() == "text edits")
skin = UIElementSkinPtr(new UITextEditSkin(cnode->tag()));
else if(node->tag() == "line decorations")
skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::LineDecoration));
else {
break;
}
skin->load(cnode);
m_elementSkins.push_back(skin);
}
}
if(FML::Node* node = doc->at("panels")) {
foreach(FML::Node* cnode, *node) {
UIElementSkinPtr skin = UIElementSkinPtr(new UIElementSkin(cnode->tag(), UI::Panel));
skin->load(cnode);
m_elementSkins.push_back(skin);
}
}
if(FML::Node* node = doc->at("windows")) {
foreach(FML::Node* cnode, *node) {
UIElementSkinPtr skin = UIElementSkinPtr(new UIWindowSkin(cnode->tag()));
skin->load(cnode);
m_elementSkins.push_back(skin);
}
}
if(FML::Node* node = doc->at("labels")) {
foreach(FML::Node* cnode, *node) {
UIElementSkinPtr skin = UIElementSkinPtr(new UILabelSkin(cnode->tag()));
skin->load(cnode);
m_elementSkins.push_back(skin);
}
}
if(FML::Node* node = doc->at("text edits")) {
foreach(FML::Node* cnode, *node) {
UIElementSkinPtr skin = UIElementSkinPtr(new UITextEditSkin(cnode->tag()));
skin->load(cnode);
m_elementSkins.push_back(skin);
}
}
} else {
flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinName.c_str() % parser.getErrorMessage());
} catch(FML::Exception e) {
flogFatal("FATAL ERROR: Malformed skin file \"%s\":\n %s", skinName.c_str() % e.what());
}
g_resources.popCurrentPath();