Ejemplo n.º 1
0
bool BaseEvents::loadFromXml()
{
	if (m_loaded) {
		std::cout << "[Error - BaseEvents::loadFromXml] It's already loaded." << std::endl;
		return false;
	}

	std::string scriptsName = getScriptBaseName();
	std::string basePath = "data/" + scriptsName + "/";
	if (getScriptInterface().loadFile(basePath + "lib/" + scriptsName + ".lua") == -1) {
		std::cout << "[Warning - BaseEvents::loadFromXml] Can not load " << scriptsName << " lib/" << scriptsName << ".lua" << std::endl;
	}

	std::string filename = basePath + scriptsName + ".xml";

	pugi::xml_document doc;
	pugi::xml_parse_result result = doc.load_file(filename.c_str());
	if (!result) {
		std::cout << "[Error - BaseEvents::loadFromXml] Failed to load " << filename << ": " << result.description() << std::endl;
		return false;
	}

	m_loaded = true;

	for (pugi::xml_node node = doc.child(scriptsName.c_str()).first_child(); node; node = node.next_sibling()) {
		Event* event = getEvent(node.name());
		if (!event) {
			continue;
		}

		if (!event->configureEvent(node)) {
			std::cout << "[Warning - BaseEvents::loadFromXml] Failed to configure event" << std::endl;
			delete event;
			continue;
		}

		bool success;

		pugi::xml_attribute scriptAttribute = node.attribute("script");
		if (scriptAttribute) {
			std::string scriptFile = "scripts/" + std::string(scriptAttribute.as_string());
			success = event->checkScript(basePath, scriptsName, scriptFile) && event->loadScript(basePath + scriptFile);
		} else {
			pugi::xml_attribute functionAttribute = node.attribute("function");
			if (functionAttribute) {
				success = event->loadFunction(functionAttribute.as_string());
			} else {
				success = false;
			}
		}

		if (!success || !registerEvent(event, node)) {
			delete event;
		}
	}
	return true;
}
Ejemplo n.º 2
0
void Npc::onPlayerEndTrade(const Player* player, int32_t buyCallback,
		int32_t sellCallback)
{
    lua_State* L = getScriptInterface()->getLuaState();
    if(buyCallback != -1)
		luaL_unref(L, LUA_REGISTRYINDEX, buyCallback);
	if(sellCallback != -1)
		luaL_unref(L, LUA_REGISTRYINDEX, sellCallback);

	//Tell the script it
	m_npcEventHandler->onPlayerEndTrade(player);
}
Ejemplo n.º 3
0
void Npc::onPlayerEndTrade(Player* player, int32_t buyCallback, int32_t sellCallback)
{
    lua_State* L = getScriptInterface()->getLuaState();

    if (buyCallback != -1) {
        luaL_unref(L, LUA_REGISTRYINDEX, buyCallback);
    }

    if (sellCallback != -1) {
        luaL_unref(L, LUA_REGISTRYINDEX, sellCallback);
    }

    removeShopPlayer(player);

    if (npcEventHandler) {
        npcEventHandler->onPlayerEndTrade(player);
    }
}
Ejemplo n.º 4
0
bool BaseEvents::loadFromXml()
{
	std::string scriptsName = getScriptBaseName();
	if(m_loaded)
	{
		std::cout << "[Error - BaseEvents::loadFromXml] " << scriptsName << " interface already loaded!" << std::endl;
		return false;
	}

	if(getScriptInterface().loadFile(getFilePath(FILE_TYPE_OTHER, std::string(scriptsName + "/lib/" + scriptsName + ".lua"))) == -1)
		std::cout << "[Warning - BaseEvents::loadFromXml] Cannot load " << scriptsName << "/lib/" << scriptsName << ".lua" << std::endl;

	xmlDocPtr doc = xmlParseFile(getFilePath(FILE_TYPE_OTHER, std::string(scriptsName + "/" + scriptsName + ".xml")).c_str());
	if(!doc)
	{
		std::cout << "[Warning - BaseEvents::loadFromXml] Cannot open " << scriptsName << ".xml file." << std::endl;
		std::cout << getLastXMLError() << std::endl;
		return false;
	}

	xmlNodePtr p, root = xmlDocGetRootElement(doc);
	if(xmlStrcmp(root->name,(const xmlChar*)scriptsName.c_str()))
	{
		std::cout << "[Error - BaseEvents::loadFromXml] Malformed " << scriptsName << ".xml file." << std::endl;
		xmlFreeDoc(doc);
		return false;
	}

	std::string scriptsPath = getFilePath(FILE_TYPE_OTHER, std::string(scriptsName + "/scripts/"));
	p = root->children;
	while(p)
	{
		parseEventNode(p, scriptsPath, false);
		p = p->next;
	}

	xmlFreeDoc(doc);
	m_loaded = true;
	return m_loaded;
}
Ejemplo n.º 5
0
bool BaseEvents::loadFromXml(const std::string& datadir)
{
	if(m_loaded){
		std::cout << "Error: [BaseEvents::loadFromXml] loaded == true" << std::endl;
		return false;
	}
	m_datadir = datadir;
	Event* event = NULL;

	
	std::string scriptsName = getScriptBaseName();
	
	if(getScriptInterface().loadFile(std::string(m_datadir + scriptsName + "/lib/" + scriptsName + ".lua")) == -1){
		std::cout << "Warning: [BaseEvents::loadFromXml] Can not load " << scriptsName << " lib/" << scriptsName << ".lua" << std::endl;
	}
	
	std::string filename = m_datadir + scriptsName + "/" + scriptsName + ".xml";
	xmlDocPtr doc = xmlParseFile(filename.c_str());
	
	if(doc){
		m_loaded = true;
		xmlNodePtr root, p;
		root = xmlDocGetRootElement(doc);
		
		if(xmlStrcmp(root->name,(const xmlChar*)scriptsName.c_str()) != 0){
			xmlFreeDoc(doc);
			return false;
		}

		p = root->children;
		while(p){
			if(p->name){
				std::string nodeName = (const char*)p->name;
				if((event = getEvent(nodeName))){
					if(event->configureEvent(p)){
						bool success = true;
						std::string scriptfile;
						if(readXMLString(p, "script", scriptfile)){
							if(!event->loadScript(m_datadir + scriptsName + "/scripts/" + scriptfile)){
								success = false;
							}
						}
						else if(readXMLString(p, "function", scriptfile)){
							if(!event->loadFunction(scriptfile)){
								success = false;
							}
						}
						else{
							success = false;
						}
						
						if(success){
							if(!registerEvent(event, p)){
								success = false;
								delete event;
							}
						}
						else{
							delete event;
						}
					}
					else{
						std::cout << "Warning: [BaseEvents::loadFromXml] Can not configure event" << std::endl;
						delete event;
					}
					event = NULL;
				}
			}
			p = p->next;
		}
		
		xmlFreeDoc(doc);
	}
	else{
		std::cout << "Warning: [BaseEvents::loadFromXml] Can not open " << scriptsName << ".xml" << std::endl;
	}
	
	return m_loaded;
}