tibia-client/src/otclient/thingtypemanager.cpp

252 lines
7.7 KiB
C++
Raw Normal View History

2012-06-21 19:54:20 +02:00
/*
* Copyright (c) 2010-2012 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 "thingtypemanager.h"
#include "spritemanager.h"
#include "thing.h"
2012-07-19 23:22:52 +02:00
#include "thingtype.h"
#include "itemtype.h"
2012-06-21 19:54:20 +02:00
#include <framework/core/resourcemanager.h>
#include <framework/core/filestream.h>
2012-06-24 15:05:44 +02:00
#include <framework/core/binarytree.h>
#include <framework/xml/tinyxml.h>
2012-06-21 19:54:20 +02:00
ThingTypeManager g_things;
void ThingTypeManager::init()
{
2012-07-19 23:22:52 +02:00
m_nullThingType = ThingTypePtr(new ThingType);
m_nullItemType = ItemTypePtr(new ItemType);
2012-06-21 19:54:20 +02:00
m_datSignature = 0;
m_otbMinorVersion = 0;
m_otbMajorVersion = 0;
m_datLoaded = false;
m_xmlLoaded = false;
m_otbLoaded = false;
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].resize(1, m_nullThingType);
m_itemTypes.resize(1, m_nullItemType);
2012-06-21 19:54:20 +02:00
}
void ThingTypeManager::terminate()
{
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].clear();
m_itemTypes.clear();
2012-07-19 23:22:52 +02:00
m_nullThingType = nullptr;
m_nullItemType = nullptr;
2012-06-21 19:54:20 +02:00
}
bool ThingTypeManager::loadDat(const std::string& file)
{
2012-07-26 08:10:28 +02:00
m_datLoaded = false;
m_datSignature = 0;
2012-06-21 19:54:20 +02:00
try {
FileStreamPtr fin = g_resources.openFile(file);
m_datSignature = fin->getU32();
int numThings[ThingLastCategory];
for(int category = 0; category < ThingLastCategory; ++category) {
2012-06-21 19:54:20 +02:00
int count = fin->getU16() + 1;
2012-07-26 08:10:28 +02:00
m_thingTypes[category].clear();
m_thingTypes[category].resize(count, m_nullThingType);
2012-06-21 19:54:20 +02:00
}
for(int category = 0; category < ThingLastCategory; ++category) {
2012-06-21 19:54:20 +02:00
uint16 firstId = 1;
if(category == ThingCategoryItem)
2012-06-21 19:54:20 +02:00
firstId = 100;
for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) {
2012-07-19 23:22:52 +02:00
ThingTypePtr type(new ThingType);
type->unserialize(id, (ThingCategory)category, fin);
m_thingTypes[category][id] = type;
2012-06-21 19:54:20 +02:00
}
}
m_datLoaded = true;
return true;
} catch(stdext::exception& e) {
g_logger.error(stdext::format("Failed to read dat '%s': %s'", file, e.what()));
return false;
}
}
2012-07-15 01:20:38 +02:00
void ThingTypeManager::loadOtb(const std::string& file)
2012-06-21 19:54:20 +02:00
{
2012-07-15 01:20:38 +02:00
FileStreamPtr fin = g_resources.openFile(file);
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
uint signature = fin->getU32();
if(signature != 0)
stdext::throw_exception("invalid otb file");
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
BinaryTreePtr root = fin->getBinaryTree();
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
signature = root->getU32();
if(signature != 0)
stdext::throw_exception("invalid otb file");
2012-06-24 17:44:33 +02:00
2012-07-15 01:20:38 +02:00
root->getU32(); // flags
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
m_otbMajorVersion = root->getU32();
m_otbMinorVersion = root->getU32();
root->getU32(); // build number
root->skip(128); // description
2012-06-21 19:54:20 +02:00
m_itemTypes.resize(root->getChildren().size(), m_nullItemType);
2012-07-15 01:20:38 +02:00
for(const BinaryTreePtr& node : root->getChildren()) {
2012-07-26 08:10:28 +02:00
ItemTypePtr itemType(new ItemType);
itemType->unserialize(node);
addItemType(itemType);
2012-06-21 19:54:20 +02:00
}
2012-07-15 01:20:38 +02:00
m_otbLoaded = true;
2012-06-21 19:54:20 +02:00
}
2012-07-15 01:20:38 +02:00
void ThingTypeManager::loadXml(const std::string& file)
2012-06-21 19:54:20 +02:00
{
/// Read XML
TiXmlDocument doc;
doc.Parse(g_resources.loadFile(file).c_str());
if(doc.Error())
stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc()));
2012-07-15 01:20:38 +02:00
TiXmlElement* root = doc.FirstChildElement();
2012-07-14 19:29:42 +02:00
if(!root || root->ValueTStr() != "items")
2012-07-15 01:20:38 +02:00
stdext::throw_exception("invalid root tag name");
for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
2012-07-14 19:29:42 +02:00
if(element->ValueTStr() != "item")
2012-07-15 01:20:38 +02:00
continue;
uint16 id = element->readType<uint16>("id");
if(id > 20000 && id < 20100) {
id -= 20000;
2012-07-19 23:22:52 +02:00
ItemTypePtr newType(new ItemType);
newType->setServerId(id);
2012-07-19 23:22:52 +02:00
addItemType(newType);
}
2012-07-15 01:20:38 +02:00
if(id != 0)
parseItemType(id, element);
else {
uint16 fromId = element->readType<uint16>("fromid"), toId = element->readType<uint16>("toid");
for(uint16 i = fromId; i < toId; ++i)
parseItemType(i, element);
}
}
2012-07-15 01:20:38 +02:00
doc.Clear();
m_xmlLoaded = true;
g_logger.debug("items.xml read successfully.");
}
2012-06-21 19:54:20 +02:00
void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem)
{
uint16 serverId = id;
if(serverId > 20000 && id < 20100) {
serverId -= 20000;
2012-06-21 19:54:20 +02:00
2012-07-19 23:22:52 +02:00
ItemTypePtr newType(new ItemType);
newType->setServerId(serverId);
2012-07-19 23:22:52 +02:00
addItemType(newType);
}
2012-06-21 19:54:20 +02:00
2012-07-26 08:10:28 +02:00
ItemTypePtr itemType = getItemType(serverId);
itemType->setName(elem->Attribute("name"));
for(TiXmlElement* attrib = elem->FirstChildElement(); attrib; attrib = attrib->NextSiblingElement()) {
if(attrib->ValueStr() != "attribute")
break;
2012-06-22 01:58:18 +02:00
if(attrib->Attribute("key") == "description") {
2012-07-26 08:10:28 +02:00
itemType->setDesc(attrib->Attribute("value"));
break;
2012-07-15 01:20:38 +02:00
}
2012-06-22 01:58:18 +02:00
}
2012-06-21 19:54:20 +02:00
}
2012-07-26 08:10:28 +02:00
void ThingTypeManager::addItemType(const ItemTypePtr& itemType)
2012-06-21 19:54:20 +02:00
{
2012-07-26 08:10:28 +02:00
uint16 id = itemType->getServerId();
if(m_itemTypes.size() <= id)
m_itemTypes.resize(id+1, m_nullItemType);
2012-07-26 08:10:28 +02:00
m_itemTypes[id] = itemType;
2012-06-21 19:54:20 +02:00
}
2012-07-19 23:22:52 +02:00
const ItemTypePtr& ThingTypeManager::findOtbForClientId(uint16 id)
2012-06-22 01:58:18 +02:00
{
if(m_itemTypes.empty())
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-23 23:30:54 +02:00
2012-07-26 08:10:28 +02:00
for(const ItemTypePtr& itemType : m_itemTypes) {
if(itemType->getClientId() == id)
return itemType;
2012-06-22 01:58:18 +02:00
}
2012-06-23 23:30:54 +02:00
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-22 01:58:18 +02:00
}
const ThingTypePtr& ThingTypeManager::getThingType(uint16 id, ThingCategory category)
2012-06-21 19:54:20 +02:00
{
if(category >= ThingLastCategory || id >= m_thingTypes[category].size()) {
2012-06-21 19:54:20 +02:00
g_logger.error(stdext::format("invalid thing type client id %d in category %d", id, category));
2012-07-19 23:22:52 +02:00
return m_nullThingType;
2012-06-21 19:54:20 +02:00
}
return m_thingTypes[category][id];
2012-06-21 19:54:20 +02:00
}
2012-07-19 23:22:52 +02:00
const ItemTypePtr& ThingTypeManager::getItemType(uint16 id)
2012-06-21 19:54:20 +02:00
{
if(id >= m_itemTypes.size()) {
2012-06-21 19:54:20 +02:00
g_logger.error(stdext::format("invalid thing type server id %d", id));
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-21 19:54:20 +02:00
}
return m_itemTypes[id];
2012-06-21 19:54:20 +02:00
}
ThingTypeList ThingTypeManager::findThingTypeByAttr(ThingAttr attr, ThingCategory category)
{
ThingTypeList ret;
for(const ThingTypePtr& type : m_thingTypes[category])
if(type->hasAttr(attr))
ret.push_back(type);
return ret;
}
ItemTypeList ThingTypeManager::findItemTypeByCategory(ItemCategory category)
{
ItemTypeList ret;
for(const ItemTypePtr& type : m_itemTypes)
if(type->getCategory() == category)
ret.push_back(type);
return ret;
}
const ThingTypeList& ThingTypeManager::getThingTypes(ThingCategory category)
{
ThingTypeList ret;
if(category >= ThingLastCategory)
stdext::throw_exception(stdext::format("invalid thing type category %d", category));
return m_thingTypes[category];
}