diff --git a/dfhack/APIPrivate.cpp b/dfhack/APIPrivate.cpp index 74df2b358..95da0cf04 100644 --- a/dfhack/APIPrivate.cpp +++ b/dfhack/APIPrivate.cpp @@ -31,6 +31,7 @@ APIPrivate::APIPrivate() vegetation = 0; buildings = 0; constructions = 0; + items = 0; } APIPrivate::~APIPrivate() diff --git a/dfhack/DFHackAPI.cpp b/dfhack/DFHackAPI.cpp index 407a6b595..9aea26544 100644 --- a/dfhack/DFHackAPI.cpp +++ b/dfhack/DFHackAPI.cpp @@ -37,6 +37,7 @@ distribution. #include "modules/Maps.h" #include "modules/Materials.h" +#include "modules/Items.h" #include "modules/Position.h" #include "modules/Gui.h" #include "modules/Creatures.h" @@ -148,6 +149,11 @@ bool API::Detach() delete d->materials; d->materials = 0; } + if(d->items) + { + delete d->items; + d->items = 0; + } if(d->gui) { delete d->gui; @@ -265,6 +271,13 @@ Materials * API::getMaterials() return d->materials; } +Items * API::getItems() +{ + if(!d->items) + d->items = new Items(d); + return d->items; +} + Translation * API::getTranslation() { if(!d->translation) diff --git a/dfhack/include/DFHackAPI.h b/dfhack/include/DFHackAPI.h index f802e4b64..188f295ca 100644 --- a/dfhack/include/DFHackAPI.h +++ b/dfhack/include/DFHackAPI.h @@ -52,6 +52,7 @@ namespace DFHack class Vegetation; class Buildings; class Constructions; + class Items; class DFHACK_EXPORT API { @@ -103,6 +104,9 @@ namespace DFHack // get the materials module Materials * getMaterials(); + + // get the items module + Items * getItems(); // get the translation module Translation * getTranslation(); diff --git a/dfhack/include/DFTypes.h b/dfhack/include/DFTypes.h index c1cf2186e..fea2247b2 100644 --- a/dfhack/include/DFTypes.h +++ b/dfhack/include/DFTypes.h @@ -169,34 +169,6 @@ union t_itemflags }; //cooked -struct t_item -{ - uint32_t origin; - uint32_t vtable; - - uint32_t x; - uint32_t y; - uint32_t z; - - t_itemflags flags; - uint32_t ID; - uint32_t type; - t_matglossPair material; - /* - uint8_t matType; - uint8_t material; - */ - // vector bytes; used for item research - // FIXME: not complete, we need building presence bitmaps for stuff like farm plots and stockpiles, orientation (N,E,S,W) and state (open/closed) -}; - -// can add more here later, but this is fine for now -struct t_itemType -{ - char id[128]; - char name[128]; -}; - struct t_viewscreen { int32_t type; diff --git a/dfhack/include/modules/Items.h b/dfhack/include/modules/Items.h index 30f4bd8d8..181ee68ee 100644 --- a/dfhack/include/modules/Items.h +++ b/dfhack/include/modules/Items.h @@ -1,66 +1,66 @@ -#ifndef CL_MOD_ITEMS -#define CL_MOD_ITEMS -/* -* Creatures -*/ -#include "Export.h" -namespace DFHack -{ - -enum accessor_type {ACCESSOR_CONSTANT, ACCESSOR_INDIRECT, ACCESSOR_DOUBLE_INDIRECT}; - -/* this is used to store data about the way accessors work */ -class DFHACK_EXPORT Accessor -{ -private: - accessor_type type; - int32_t constant; - uint32_t offset1; - uint32_t offset2; - Process * p; - uint32_t dataWidth; -public: - Accessor(uint32_t function, Process * p); - Accessor(accessor_type type, int32_t constant, uint32_t offset1, uint32_t offset2, uint32_t dataWidth, Process * p); - int32_t getValue(uint32_t objectPtr); -}; - -struct t_item -{ - t_material matdesc; - int32_t quantity; - int32_t quality; -}; - -class DFHACK_EXPORT ItemDesc -{ -private: - Accessor * AMainType; - Accessor * ASubType; - Accessor * ASubIndex; - Accessor * AIndex; - Accessor * AQuality; - Process * p; - bool hasDecoration; -public: - ItemDesc(uint32_t VTable, Process * p); - bool getItem(uint32_t itemptr, t_item & item); - std::string className; - uint32_t vtable; -}; - -class DFHACK_EXPORT Items -{ -public: - Items(DFHack::APIPrivate * _d); - ~Items(); - std::string getItemDescription(uint32_t itemptr); - bool getItemData(uint32_t itemptr, t_item & item); +#ifndef CL_MOD_ITEMS +#define CL_MOD_ITEMS +/* +* Creatures +*/ +#include "Export.h" +namespace DFHack +{ + +enum accessor_type {ACCESSOR_CONSTANT, ACCESSOR_INDIRECT, ACCESSOR_DOUBLE_INDIRECT}; + +/* this is used to store data about the way accessors work */ +class DFHACK_EXPORT Accessor +{ +private: + accessor_type type; + int32_t constant; + uint32_t offset1; + uint32_t offset2; + Process * p; + uint32_t dataWidth; +public: + Accessor(uint32_t function, Process * p); + Accessor(accessor_type type, int32_t constant, uint32_t offset1, uint32_t offset2, uint32_t dataWidth, Process * p); + int32_t getValue(uint32_t objectPtr); +}; + +struct t_item +{ + t_material matdesc; + int32_t quantity; + int32_t quality; +}; + +class DFHACK_EXPORT ItemDesc +{ +private: + Accessor * AMainType; + Accessor * ASubType; + Accessor * ASubIndex; + Accessor * AIndex; + Accessor * AQuality; + Process * p; + bool hasDecoration; +public: + ItemDesc(uint32_t VTable, Process * p); + bool getItem(uint32_t itemptr, t_item & item); + std::string className; + uint32_t vtable; +}; + +class DFHACK_EXPORT Items +{ +public: + Items(DFHack::APIPrivate * _d); + ~Items(); + std::string getItemDescription(uint32_t itemptr); + bool getItemData(uint32_t itemptr, t_item & item); private: class Private; - Private* d; - /*std::map descType; might be useful later */ - std::map descVTable; -}; -} -#endif + Private* d; + /*std::map descType; might be useful later */ + std::map descVTable; +}; +} +#endif diff --git a/dfhack/modules/Items.cpp b/dfhack/modules/Items.cpp index 093236d97..ca9be516a 100644 --- a/dfhack/modules/Items.cpp +++ b/dfhack/modules/Items.cpp @@ -1,37 +1,37 @@ -/* -www.sourceforge.net/projects/dfhack -Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any -damages arising from the use of this software. - -Permission is granted to anyone to use this software for any -purpose, including commercial applications, and to alter it and -redistribute it freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must -not claim that you wrote the original software. If you use this -software in a product, an acknowledgment in the product documentation -would be appreciated but is not required. - -2. Altered source versions must be plainly marked as such, and -must not be misrepresented as being the original software. - -3. This notice may not be removed or altered from any source -distribution. -*/ - -#include "DFCommonInternal.h" -#include "../private/APIPrivate.h" -#include "modules/Materials.h" -#include "modules/Items.h" -#include "DFMemInfo.h" -#include "DFProcess.h" -#include "DFVector.h" - -using namespace DFHack; - +/* +www.sourceforge.net/projects/dfhack +Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "DFCommonInternal.h" +#include "../private/APIPrivate.h" +#include "modules/Materials.h" +#include "modules/Items.h" +#include "DFMemInfo.h" +#include "DFProcess.h" +#include "DFVector.h" + +using namespace DFHack; + class Items::Private { public: @@ -41,8 +41,8 @@ class Items::Private bool Inited; bool Started; */ -}; - +}; + Items::Items(APIPrivate * d_) { d = new Private; @@ -98,6 +98,13 @@ Accessor::Accessor(uint32_t function, Process *p) return; } } + if( (funcText&0xFFFFFF0000FFFFFFLL) == 0xC30000000081BF0FLL ) + { + /* movsx eax, word ptr [ecx+xx]; ret */ + this->type = ACCESSOR_INDIRECT; + this->offset1 = (funcText>>24) & 0xffff; + return; + } if( (funcText&0xFFFFFFFF0000FFFFLL) == 0xCCC300000000818BLL ) { /* mov eax, [ecx+xx]; ret; */ @@ -111,6 +118,8 @@ Accessor::Accessor(uint32_t function, Process *p) int32_t Accessor::getValue(uint32_t objectPtr) { + int32_t sout; + switch(this->type) { case ACCESSOR_CONSTANT: @@ -120,7 +129,7 @@ int32_t Accessor::getValue(uint32_t objectPtr) switch(this->dataWidth) { case 2: - return p->readWord(objectPtr + this->offset1); + return (int16_t) p->readWord(objectPtr + this->offset1); case 4: return p->readDWord(objectPtr + this->offset1); default: @@ -131,7 +140,7 @@ int32_t Accessor::getValue(uint32_t objectPtr) switch(this->dataWidth) { case 2: - return p->readWord(p->readDWord(objectPtr + this->offset1) + this->offset2); + return (int16_t) p->readWord(p->readDWord(objectPtr + this->offset1) + this->offset2); case 4: return p->readDWord(p->readDWord(objectPtr + this->offset1) + this->offset2); default: @@ -167,6 +176,8 @@ bool ItemDesc::getItem(uint32_t itemptr, DFHack::t_item &item) item.matdesc.subType = this->ASubType->getValue(itemptr); item.matdesc.subIndex = this->ASubIndex->getValue(itemptr); item.matdesc.index = this->AIndex->getValue(itemptr); + if(item.matdesc.index == 0xffff) + printf("wtf"); item.quality = this->AQuality->getValue(itemptr); item.quantity = 1; /* TODO */ return true; diff --git a/dfhack/private/APIPrivate.h b/dfhack/private/APIPrivate.h index c331886de..259730d58 100644 --- a/dfhack/private/APIPrivate.h +++ b/dfhack/private/APIPrivate.h @@ -36,6 +36,7 @@ namespace DFHack class Position; class Maps; class Creatures; + class Items; class Translation; class Buildings; class ProcessEnumerator; @@ -71,6 +72,7 @@ namespace DFHack Position * position; Gui * gui; Materials * materials; + Items * items; Translation * translation; Vegetation * vegetation; Buildings * buildings; diff --git a/dfhack/python/DF_Helpers.cpp b/dfhack/python/DF_Helpers.cpp index c646b9c55..046022bfc 100644 --- a/dfhack/python/DF_Helpers.cpp +++ b/dfhack/python/DF_Helpers.cpp @@ -148,12 +148,12 @@ static PyObject* BuildAttribute(DFHack::t_attrib& at) return attrObj; } - +/* static PyObject* BuildItemType(DFHack::t_itemType& item) { return Py_BuildValue("ss", item.id, item.name); } - +*/ static PyObject* BuildLike(DFHack::t_like& like) { PyObject* item; diff --git a/examples/creaturedump.cpp b/examples/creaturedump.cpp index 02378a3b5..33a653819 100644 --- a/examples/creaturedump.cpp +++ b/examples/creaturedump.cpp @@ -27,7 +27,6 @@ enum likeType }; DFHack::Materials * Materials; -vector< vector > itemTypes; DFHack::memory_info *mem; vector< vector > englishWords; vector< vector > foreignWords; diff --git a/examples/dfitemdump.cpp b/examples/dfitemdump.cpp index 9ec127a20..3848c32ad 100644 --- a/examples/dfitemdump.cpp +++ b/examples/dfitemdump.cpp @@ -18,6 +18,7 @@ using namespace std; #include #include #include +#include DFHack::Materials * Materials; @@ -69,6 +70,7 @@ int main () DFHack::API DF("Memory.xml"); DFHack::Process * p; unsigned int i,j; + DFHack::Items * Items; try { @@ -89,6 +91,7 @@ int main () p = DF.getProcess(); DFHack::DfVector p_items (p, p->getDescriptor()->getAddress ("items_vector")); uint32_t size = p_items.size(); + Items = DF.getItems(); printf("type\tvtable\tname\tquality\tdecorate\n"); @@ -115,6 +118,9 @@ int main () uint32_t quality = 0; bool hasDecorations; string desc = p->readClassName(vtable); + DFHack::t_item itm; + + Items->getItemData(p_items[i], itm); if ( (funct0&0xFFFFFFFFFF000000LL) != 0xCCCCC30000000000LL ) { @@ -190,6 +196,8 @@ int main () printf("%d\t%p\t%s\t%d\t[%d,%d,%d -> %s]", type, (void*)vtable, desc.c_str(), quality, typeB, typeC, typeD, getMatDesc(typeB, typeC, typeD).c_str()); // printf("\t%p\t%.16LX", (void *) funcD, funcDt); + if( (type!=itm.matdesc.itemType) || (typeB!=itm.matdesc.subType) || (typeC!=itm.matdesc.subIndex) || (typeD!=itm.matdesc.index) ) + printf("\tbad[%d,%d,%d,%d]", itm.matdesc.itemType, itm.matdesc.subType, itm.matdesc.subIndex, itm.matdesc.index); if (hasDecorations) { bool sep = false; diff --git a/tools/moodump.cpp b/tools/moodump.cpp index 1f243211f..9b61c0701 100644 --- a/tools/moodump.cpp +++ b/tools/moodump.cpp @@ -16,7 +16,6 @@ using namespace std; #include DFHack::Materials * Materials; -vector< vector > itemTypes; DFHack::memory_info *mem; vector< vector > englishWords; vector< vector > foreignWords;