2010-05-02 03:27:16 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
2010-05-02 03:27:16 -06:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2011-04-10 02:19:15 -06:00
|
|
|
|
|
|
|
#include <string>
|
2011-04-13 17:42:03 -06:00
|
|
|
#include <sstream>
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <vector>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <map>
|
2011-07-24 22:35:50 -06:00
|
|
|
#include <set>
|
2011-04-10 02:19:15 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "dfhack/Types.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "dfhack/Process.h"
|
|
|
|
#include "dfhack/Vector.h"
|
2010-05-26 00:42:09 -06:00
|
|
|
#include "dfhack/modules/Materials.h"
|
|
|
|
#include "dfhack/modules/Items.h"
|
2011-05-12 11:09:49 -06:00
|
|
|
#include "dfhack/modules/Creatures.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-06-17 07:02:43 -06:00
|
|
|
#include <dfhack/Core.h>
|
2010-05-02 03:27:16 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
Module* DFHack::createItems()
|
2011-03-18 01:53:59 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -06:00
|
|
|
return new Items();
|
2011-03-18 01:53:59 -06:00
|
|
|
}
|
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
enum accessor_type {ACCESSOR_CONSTANT, ACCESSOR_INDIRECT, ACCESSOR_DOUBLE_INDIRECT};
|
|
|
|
|
|
|
|
/* this is used to store data about the way accessors work */
|
2011-04-27 21:10:37 -06:00
|
|
|
class Accessor
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2011-04-10 12:44:01 -06:00
|
|
|
public:
|
|
|
|
enum DataWidth {
|
|
|
|
Data32 = 0,
|
|
|
|
DataSigned16,
|
|
|
|
DataUnsigned16
|
|
|
|
};
|
2010-06-24 23:11:26 -06:00
|
|
|
private:
|
|
|
|
accessor_type type;
|
|
|
|
int32_t constant;
|
2011-04-10 11:51:21 -06:00
|
|
|
int32_t offset1;
|
|
|
|
int32_t offset2;
|
2010-06-24 23:11:26 -06:00
|
|
|
Process * p;
|
2011-04-10 12:44:01 -06:00
|
|
|
DataWidth dataWidth;
|
2011-04-13 17:42:03 -06:00
|
|
|
uint32_t method;
|
2010-06-24 23:11:26 -06:00
|
|
|
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);
|
2011-04-13 17:42:03 -06:00
|
|
|
std::string dump();
|
2011-07-20 02:41:13 -06:00
|
|
|
int32_t getValue(t_item * objectPtr);
|
2010-06-24 23:11:26 -06:00
|
|
|
bool isConstant();
|
2010-05-02 03:27:16 -06:00
|
|
|
};
|
2011-04-27 21:10:37 -06:00
|
|
|
class ItemImprovementDesc
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2010-06-24 23:11:26 -06:00
|
|
|
private:
|
|
|
|
Accessor * AType;
|
|
|
|
Process * p;
|
|
|
|
public:
|
|
|
|
ItemImprovementDesc(uint32_t VTable, Process * p);
|
|
|
|
bool getImprovement(uint32_t descptr, t_improvement & imp);
|
|
|
|
uint32_t vtable;
|
|
|
|
uint32_t maintype;
|
|
|
|
};
|
|
|
|
|
2011-04-27 21:10:37 -06:00
|
|
|
class ItemDesc
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2010-06-24 23:11:26 -06:00
|
|
|
private:
|
|
|
|
Accessor * AMainType;
|
|
|
|
Accessor * ASubType;
|
|
|
|
Accessor * ASubIndex;
|
|
|
|
Accessor * AIndex;
|
|
|
|
Accessor * AQuality;
|
2011-05-11 15:53:16 -06:00
|
|
|
Accessor * AQuantity;
|
2011-04-10 12:42:25 -06:00
|
|
|
Accessor * AWear;
|
2010-06-24 23:11:26 -06:00
|
|
|
Process * p;
|
|
|
|
bool hasDecoration;
|
2011-05-09 04:04:39 -06:00
|
|
|
int idFieldOffset;
|
2010-06-24 23:11:26 -06:00
|
|
|
public:
|
2011-07-20 02:41:13 -06:00
|
|
|
ItemDesc(void * VTable, Process * p);
|
|
|
|
bool readItem(t_item * itemptr, dfh_item & item);
|
2011-04-13 17:42:03 -06:00
|
|
|
std::string dumpAccessors();
|
2010-06-24 23:11:26 -06:00
|
|
|
std::string className;
|
2011-07-20 02:41:13 -06:00
|
|
|
void * vtable;
|
2010-06-24 23:11:26 -06:00
|
|
|
uint32_t mainType;
|
|
|
|
std::vector<ItemImprovementDesc> improvement;
|
|
|
|
};
|
|
|
|
|
2011-04-10 11:51:21 -06:00
|
|
|
inline bool do_match(uint32_t &ptr, uint64_t val, int size, uint64_t mask, uint64_t check)
|
|
|
|
{
|
|
|
|
if ((val & mask) == check) {
|
|
|
|
ptr += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool match_MEM_ACCESS(uint32_t &ptr, uint64_t v, int isize, int in_reg, int &out_reg, int &offset)
|
|
|
|
{
|
|
|
|
// ESP & EBP are hairy
|
|
|
|
if (in_reg == 4 || in_reg == 5)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ((v & 7) != in_reg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out_reg = (v>>3) & 7;
|
|
|
|
|
|
|
|
switch ((v>>6)&3) {
|
|
|
|
case 0: // MOV REG2, [REG]
|
|
|
|
offset = 0;
|
|
|
|
ptr += isize+1;
|
|
|
|
return true;
|
|
|
|
case 1: // MOV REG2, [REG+offset8]
|
|
|
|
offset = (signed char)(v >> 8);
|
|
|
|
ptr += isize+2;
|
|
|
|
return true;
|
|
|
|
case 2: // MOV REG2, [REG+offset32]
|
|
|
|
offset = (signed int)(v >> 8);
|
|
|
|
ptr += isize+5;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-10 12:44:01 -06:00
|
|
|
static bool match_MOV_MEM(uint32_t &ptr, uint64_t v, int in_reg, int &out_reg, int &offset, Accessor::DataWidth &size)
|
2011-04-10 11:51:21 -06:00
|
|
|
{
|
|
|
|
int prefix = 0;
|
2011-04-10 12:44:01 -06:00
|
|
|
size = Accessor::Data32;
|
2011-04-10 11:51:21 -06:00
|
|
|
if ((v & 0xFF) == 0x8B) { // MOV
|
|
|
|
v >>= 8;
|
|
|
|
prefix = 1;
|
|
|
|
}
|
|
|
|
else if ((v & 0xFFFF) == 0x8B66) { // MOV 16-bit
|
|
|
|
v >>= 16;
|
|
|
|
prefix = 2;
|
2011-04-10 12:44:01 -06:00
|
|
|
size = Accessor::DataUnsigned16;
|
2011-04-10 11:51:21 -06:00
|
|
|
}
|
|
|
|
else if ((v & 0xFFFF) == 0xBF0F) { // MOVSX
|
|
|
|
v >>= 16;
|
|
|
|
prefix = 2;
|
2011-04-10 12:44:01 -06:00
|
|
|
size = Accessor::DataSigned16;
|
2011-04-10 11:51:21 -06:00
|
|
|
}
|
|
|
|
else if ((v & 0xFFFF) == 0xB70F) { // MOVZ
|
|
|
|
v >>= 16;
|
|
|
|
prefix = 2;
|
2011-04-10 12:44:01 -06:00
|
|
|
size = Accessor::DataUnsigned16;
|
2011-04-10 11:51:21 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2011-04-10 11:51:21 -06:00
|
|
|
return match_MEM_ACCESS(ptr, v, prefix, in_reg, out_reg, offset);
|
|
|
|
}
|
2010-05-02 02:35:23 -06:00
|
|
|
|
|
|
|
Accessor::Accessor(uint32_t function, Process *p)
|
|
|
|
{
|
2010-06-24 23:11:26 -06:00
|
|
|
this->p = p;
|
|
|
|
this->type = ACCESSOR_CONSTANT;
|
2011-04-11 14:13:06 -06:00
|
|
|
if(!p)
|
|
|
|
{
|
|
|
|
this->constant = 0;
|
|
|
|
return;
|
|
|
|
}
|
2011-04-13 17:42:03 -06:00
|
|
|
method = function;
|
2011-04-14 07:16:12 -06:00
|
|
|
uint32_t temp = function;
|
2011-04-10 11:51:21 -06:00
|
|
|
int data_reg = -1;
|
2011-04-14 07:16:12 -06:00
|
|
|
uint64_t v = p->readQuad(temp);
|
2011-04-10 11:51:21 -06:00
|
|
|
|
2011-04-14 07:16:12 -06:00
|
|
|
if (do_match(temp, v, 2, 0xFFFF, 0xC033) ||
|
|
|
|
do_match(temp, v, 2, 0xFFFF, 0xC031)) // XOR EAX, EAX
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-04-10 11:51:21 -06:00
|
|
|
data_reg = 0;
|
|
|
|
this->constant = 0;
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
2011-04-14 07:16:12 -06:00
|
|
|
else if (do_match(temp, v, 3, 0xFFFFFF, 0xFFC883)) // OR EAX, -1
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-04-10 11:51:21 -06:00
|
|
|
data_reg = 0;
|
2010-06-24 23:11:26 -06:00
|
|
|
this->constant = -1;
|
|
|
|
}
|
2011-04-14 07:16:12 -06:00
|
|
|
else if (do_match(temp, v, 5, 0xFF, 0xB8)) // MOV EAX,imm
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-04-10 11:51:21 -06:00
|
|
|
data_reg = 0;
|
|
|
|
this->constant = (v>>8) & 0xFFFFFFFF;
|
2010-10-19 04:07:07 -06:00
|
|
|
}
|
2011-04-10 11:51:21 -06:00
|
|
|
else
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-04-10 12:44:01 -06:00
|
|
|
DataWidth xsize;
|
2011-04-10 11:51:21 -06:00
|
|
|
int ptr_reg = 1, tmp; // ECX
|
|
|
|
|
|
|
|
// MOV REG,[ESP+4]
|
2011-04-14 07:16:12 -06:00
|
|
|
if (do_match(temp, v, 4, 0xFFFFC7FFU, 0x0424448B))
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2011-04-10 11:51:21 -06:00
|
|
|
ptr_reg = (v>>11)&7;
|
2011-04-14 07:16:12 -06:00
|
|
|
v = p->readQuad(temp);
|
2011-04-10 11:51:21 -06:00
|
|
|
}
|
|
|
|
|
2011-04-14 07:16:12 -06:00
|
|
|
if (match_MOV_MEM(temp, v, ptr_reg, tmp, this->offset1, xsize)) {
|
2011-04-10 11:51:21 -06:00
|
|
|
data_reg = tmp;
|
|
|
|
this->type = ACCESSOR_INDIRECT;
|
2011-04-10 12:44:01 -06:00
|
|
|
this->dataWidth = xsize;
|
2011-04-10 11:51:21 -06:00
|
|
|
|
2011-04-10 12:44:01 -06:00
|
|
|
if (xsize == Data32)
|
2011-04-10 11:51:21 -06:00
|
|
|
{
|
2011-04-14 07:16:12 -06:00
|
|
|
v = p->readQuad(temp);
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2011-04-14 07:16:12 -06:00
|
|
|
if (match_MOV_MEM(temp, v, data_reg, tmp, this->offset2, xsize)) {
|
2011-04-10 11:51:21 -06:00
|
|
|
data_reg = tmp;
|
|
|
|
this->type = ACCESSOR_DOUBLE_INDIRECT;
|
2011-04-10 12:44:01 -06:00
|
|
|
this->dataWidth = xsize;
|
2011-04-10 11:51:21 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
|
|
|
}
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2011-04-14 07:16:12 -06:00
|
|
|
v = p->readQuad(temp);
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2011-04-14 07:16:12 -06:00
|
|
|
if (data_reg == 0 && do_match(temp, v, 1, 0xFF, 0xC3)) // RET
|
2010-10-19 04:07:07 -06:00
|
|
|
return;
|
2011-04-10 11:51:21 -06:00
|
|
|
else
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-04-10 11:51:21 -06:00
|
|
|
this->type = ACCESSOR_CONSTANT;
|
|
|
|
this->constant = 0;
|
|
|
|
printf("bad accessor @0x%x\n", function);
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
2010-05-02 02:35:23 -06:00
|
|
|
}
|
|
|
|
|
2010-05-02 04:50:51 -06:00
|
|
|
bool Accessor::isConstant()
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2010-06-24 23:11:26 -06:00
|
|
|
if(this->type == ACCESSOR_CONSTANT)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
2010-05-02 04:50:51 -06:00
|
|
|
}
|
2010-05-02 03:27:16 -06:00
|
|
|
|
2011-04-13 17:42:03 -06:00
|
|
|
string Accessor::dump()
|
|
|
|
{
|
|
|
|
stringstream sstr;
|
|
|
|
sstr << hex << "method @0x" << method << dec << " ";
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ACCESSOR_CONSTANT:
|
|
|
|
sstr << "Constant: " << dec << constant;
|
|
|
|
break;
|
|
|
|
case ACCESSOR_INDIRECT:
|
|
|
|
switch(dataWidth)
|
|
|
|
{
|
|
|
|
case Data32:
|
|
|
|
sstr << "int32_t ";
|
|
|
|
break;
|
|
|
|
case DataSigned16:
|
|
|
|
sstr << "int16_t ";
|
|
|
|
break;
|
|
|
|
case DataUnsigned16:
|
|
|
|
sstr << "uint16_t ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sstr << "unknown ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sstr << hex << "[obj + 0x" << offset1 << " ]";
|
|
|
|
break;
|
|
|
|
case ACCESSOR_DOUBLE_INDIRECT:
|
|
|
|
switch(dataWidth)
|
|
|
|
{
|
|
|
|
case Data32:
|
|
|
|
sstr << "int32_t ";
|
|
|
|
break;
|
|
|
|
case DataSigned16:
|
|
|
|
sstr << "int16_t ";
|
|
|
|
break;
|
|
|
|
case DataUnsigned16:
|
|
|
|
sstr << "uint16_t ";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sstr << "unknown ";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sstr << hex << "[ [obj + 0x" << offset1 << " ] + 0x" << offset2 << " ]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sstr.str();
|
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
int32_t Accessor::getValue(t_item * objectPtr)
|
2010-05-02 04:50:51 -06:00
|
|
|
{
|
2011-04-10 12:44:01 -06:00
|
|
|
int32_t offset = this->offset1;
|
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
switch(this->type)
|
|
|
|
{
|
|
|
|
case ACCESSOR_CONSTANT:
|
|
|
|
return this->constant;
|
|
|
|
break;
|
|
|
|
case ACCESSOR_DOUBLE_INDIRECT:
|
2011-07-20 02:41:13 -06:00
|
|
|
objectPtr = (t_item *) p->readDWord((uint32_t)objectPtr + this->offset1);
|
2011-04-10 12:44:01 -06:00
|
|
|
offset = this->offset2;
|
|
|
|
// fallthrough
|
|
|
|
case ACCESSOR_INDIRECT:
|
2010-06-24 23:11:26 -06:00
|
|
|
switch(this->dataWidth)
|
|
|
|
{
|
2011-04-10 12:44:01 -06:00
|
|
|
case Data32:
|
2011-07-20 02:41:13 -06:00
|
|
|
return p->readDWord((uint32_t)objectPtr + offset);
|
2011-04-10 12:44:01 -06:00
|
|
|
case DataSigned16:
|
2011-07-20 02:41:13 -06:00
|
|
|
return (int16_t) p->readWord((uint32_t)objectPtr + offset);
|
2011-04-10 12:44:01 -06:00
|
|
|
case DataUnsigned16:
|
2011-07-20 02:41:13 -06:00
|
|
|
return (uint16_t) p->readWord((uint32_t)objectPtr + offset);
|
2010-06-24 23:11:26 -06:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-02 02:35:23 -06:00
|
|
|
}
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
// FIXME: turn into a proper factory with caching
|
2011-07-20 02:41:13 -06:00
|
|
|
Accessor * buildAccessor (OffsetGroup * I, Process * p, const char * name, void * vtable)
|
2011-04-11 14:13:06 -06:00
|
|
|
{
|
|
|
|
int32_t offset;
|
2011-04-13 17:42:03 -06:00
|
|
|
if(I->getSafeOffset(name,offset))
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
return new Accessor( p->readDWord( (uint32_t)vtable + offset ), p);
|
2011-04-13 17:42:03 -06:00
|
|
|
}
|
2011-04-11 14:13:06 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Missing offset for item accessor \"%s\"\n", name);
|
|
|
|
return new Accessor(-1,0); // dummy accessor. always returns -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
ItemDesc::ItemDesc(void * VTable, Process *p)
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2010-08-29 16:08:17 -06:00
|
|
|
OffsetGroup * Items = p->getDescriptor()->getGroup("Items");
|
2011-04-11 14:13:06 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: and what about types, different sets of methods depending on class?
|
|
|
|
* what about more complex things than constants and integers?
|
|
|
|
* If this is to be generally useful, it needs much more power.
|
|
|
|
*/
|
|
|
|
AMainType = buildAccessor(Items, p, "item_type_accessor", VTable);
|
|
|
|
ASubType = buildAccessor(Items, p, "item_subtype_accessor", VTable);
|
|
|
|
ASubIndex = buildAccessor(Items, p, "item_subindex_accessor", VTable);
|
|
|
|
AIndex = buildAccessor(Items, p, "item_index_accessor", VTable);
|
|
|
|
AQuality = buildAccessor(Items, p, "item_quality_accessor", VTable);
|
|
|
|
AWear = buildAccessor(Items, p, "item_wear_accessor", VTable);
|
2011-05-11 15:53:16 -06:00
|
|
|
AQuantity = buildAccessor(Items, p, "item_quantity_accessor", VTable);
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2011-05-09 04:04:39 -06:00
|
|
|
idFieldOffset = Items->getOffset("id");
|
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
this->vtable = VTable;
|
|
|
|
this->p = p;
|
2011-07-06 23:00:36 -06:00
|
|
|
this->className = p->readClassName((void *) VTable).substr(5);
|
2010-06-24 23:11:26 -06:00
|
|
|
this->className.resize(this->className.size()-2);
|
2011-04-11 14:13:06 -06:00
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
this->hasDecoration = false;
|
2011-04-11 14:13:06 -06:00
|
|
|
if(AMainType->isConstant())
|
|
|
|
mainType = this->AMainType->getValue(0);
|
2010-06-24 23:11:26 -06:00
|
|
|
else
|
|
|
|
{
|
2011-05-15 14:24:40 -06:00
|
|
|
cerr << "Bad item main type accessor: " << AMainType->dump() << endl;
|
2011-04-11 14:13:06 -06:00
|
|
|
mainType = 0;
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
2010-05-02 02:35:23 -06:00
|
|
|
}
|
|
|
|
|
2011-04-13 17:42:03 -06:00
|
|
|
string ItemDesc::dumpAccessors()
|
|
|
|
{
|
|
|
|
std::stringstream outss;
|
|
|
|
outss << "MainType :" << AMainType->dump() << endl;
|
|
|
|
outss << "ASubType :" << ASubType->dump() << endl;
|
|
|
|
outss << "ASubIndex :" << ASubIndex->dump() << endl;
|
|
|
|
outss << "AIndex :" << AIndex->dump() << endl;
|
|
|
|
outss << "AQuality :" << AQuality->dump() << endl;
|
2011-05-11 15:53:16 -06:00
|
|
|
outss << "AQuantity :" << AQuantity->dump() << endl;
|
2011-04-13 17:42:03 -06:00
|
|
|
outss << "AWear :" << AWear->dump() << endl;
|
|
|
|
return outss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
bool ItemDesc::readItem(t_item * itemptr, DFHack::dfh_item &item)
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
item.base = itemptr;
|
2011-04-11 14:13:06 -06:00
|
|
|
item.matdesc.itemType = AMainType->getValue(itemptr);
|
|
|
|
item.matdesc.subType = ASubType->getValue(itemptr);
|
|
|
|
item.matdesc.subIndex = ASubIndex->getValue(itemptr);
|
|
|
|
item.matdesc.index = AIndex->getValue(itemptr);
|
|
|
|
item.quality = AQuality->getValue(itemptr);
|
2011-05-11 15:53:16 -06:00
|
|
|
item.quantity = AQuantity->getValue(itemptr);
|
2011-04-11 14:13:06 -06:00
|
|
|
// FIXME: use templates. seriously.
|
2011-04-10 12:42:25 -06:00
|
|
|
// Note: this accessor returns a 32-bit value with the higher
|
|
|
|
// half sometimes containing garbage, so the cast is essential:
|
|
|
|
item.wear_level = (int16_t)this->AWear->getValue(itemptr);
|
2010-06-24 23:11:26 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Items::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DFContextShared *d;
|
|
|
|
Process * owner;
|
|
|
|
std::map<int32_t, ItemDesc *> descType;
|
2011-07-20 02:41:13 -06:00
|
|
|
std::map<void *, ItemDesc *> descVTable;
|
|
|
|
std::map<int32_t, t_item *> idLookupTable;
|
2011-04-11 04:32:53 -06:00
|
|
|
uint32_t refVectorOffset;
|
2011-05-09 04:04:39 -06:00
|
|
|
uint32_t idFieldOffset;
|
2011-05-09 04:49:44 -06:00
|
|
|
uint32_t itemVectorAddress;
|
2011-07-24 22:35:50 -06:00
|
|
|
|
2011-05-09 03:48:54 -06:00
|
|
|
ClassNameCheck isOwnerRefClass;
|
2011-05-12 10:30:42 -06:00
|
|
|
ClassNameCheck isContainerRefClass;
|
|
|
|
ClassNameCheck isContainsRefClass;
|
2011-07-24 22:35:50 -06:00
|
|
|
|
|
|
|
// Similar to isOwnerRefClass. Value is unique to each creature, but
|
|
|
|
// different than the creature's id.
|
|
|
|
ClassNameCheck isUnitHolderRefClass;
|
|
|
|
|
|
|
|
// One of these is present for each creature contained in a cage.
|
|
|
|
// The value is similar to that for isUnitHolderRefClass, different
|
|
|
|
// than the creature's ID but unique for each creature.
|
|
|
|
ClassNameCheck isCagedUnitRefClass;
|
|
|
|
|
|
|
|
// ID of bulding containing/holding the item.
|
|
|
|
ClassNameCheck isBuildingHolderRefClass;
|
|
|
|
|
|
|
|
// Building ID of lever/etc which triggers bridge/etc holding
|
|
|
|
// this mechanism.
|
|
|
|
ClassNameCheck isTriggeredByRefClass;
|
|
|
|
|
|
|
|
// Building ID of bridge/etc which is triggered by lever/etc holding
|
|
|
|
// this mechanism.
|
|
|
|
ClassNameCheck isTriggerTargetRefClass;
|
|
|
|
|
|
|
|
// Civilization ID of owner of item, for items not owned by the
|
|
|
|
// fortress.
|
|
|
|
ClassNameCheck isEntityOwnerRefClass;
|
|
|
|
|
|
|
|
// Item has been offered to the caravan. The value is the
|
|
|
|
// civilization ID of
|
|
|
|
ClassNameCheck isOfferedRefClass;
|
|
|
|
|
|
|
|
// Item is in a depot for trade. Purpose of value is unknown, but is
|
|
|
|
// different for each item, even in the same depot at the same time.
|
|
|
|
ClassNameCheck isTradingRefClass;
|
|
|
|
|
|
|
|
// Item is flying or falling through the air. The value seems to
|
|
|
|
// be the ID for a "projectile information" object.
|
|
|
|
ClassNameCheck isProjectileRefClass;
|
|
|
|
|
|
|
|
std::set<std::string> knownItemRefTypes;
|
2010-06-24 23:11:26 -06:00
|
|
|
};
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
Items::Items()
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
2011-06-17 07:02:43 -06:00
|
|
|
Core & c = Core::getInstance();
|
2010-06-24 23:11:26 -06:00
|
|
|
d = new Private;
|
2011-06-17 07:02:43 -06:00
|
|
|
d->owner = c.p;
|
2011-05-12 10:30:42 -06:00
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
DFHack::OffsetGroup* itemGroup = c.vinfo->getGroup("Items");
|
2011-05-09 04:49:44 -06:00
|
|
|
d->itemVectorAddress = itemGroup->getAddress("items_vector");
|
|
|
|
d->idFieldOffset = itemGroup->getOffset("id");
|
2011-05-12 10:30:42 -06:00
|
|
|
d->refVectorOffset = itemGroup->getOffset("item_ref_vector");
|
2011-07-24 22:35:50 -06:00
|
|
|
|
|
|
|
d->isOwnerRefClass = ClassNameCheck("general_ref_unit_itemownerst");
|
|
|
|
d->isContainerRefClass = ClassNameCheck("general_ref_contained_in_itemst");
|
|
|
|
d->isContainsRefClass = ClassNameCheck("general_ref_contains_itemst");
|
|
|
|
d->isUnitHolderRefClass = ClassNameCheck("general_ref_unit_holderst");
|
|
|
|
d->isCagedUnitRefClass = ClassNameCheck("general_ref_contains_unitst");
|
|
|
|
d->isBuildingHolderRefClass
|
|
|
|
= ClassNameCheck("general_ref_building_holderst");
|
|
|
|
d->isTriggeredByRefClass = ClassNameCheck("general_ref_building_triggerst");
|
|
|
|
d->isTriggerTargetRefClass
|
|
|
|
= ClassNameCheck("general_ref_building_triggertargetst");
|
|
|
|
d->isEntityOwnerRefClass = ClassNameCheck("general_ref_entity_itemownerst");
|
|
|
|
d->isOfferedRefClass = ClassNameCheck("general_ref_entity_offeredst");
|
|
|
|
d->isTradingRefClass = ClassNameCheck("general_ref_unit_tradebringerst");
|
|
|
|
d->isProjectileRefClass = ClassNameCheck("general_ref_projectilest");
|
|
|
|
|
|
|
|
std::vector<std::string> known_names;
|
|
|
|
ClassNameCheck::getKnownClassNames(known_names);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < known_names.size(); i++)
|
|
|
|
{
|
|
|
|
if (known_names[i].find("general_ref_") == 0)
|
|
|
|
d->knownItemRefTypes.insert(known_names[i]);
|
|
|
|
}
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
2010-11-03 03:45:29 -06:00
|
|
|
|
|
|
|
bool Items::Start()
|
|
|
|
{
|
2011-05-09 04:49:44 -06:00
|
|
|
d->idLookupTable.clear();
|
2010-11-03 03:45:29 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Items::Finish()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
bool Items::readItemVector(std::vector<t_item *> &items)
|
2011-05-09 04:49:44 -06:00
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
std::vector <t_item *> *p_items = (std::vector <t_item *> *) d->itemVectorAddress;
|
2011-05-09 04:49:44 -06:00
|
|
|
|
|
|
|
d->idLookupTable.clear();
|
2011-07-20 02:41:13 -06:00
|
|
|
items.resize(p_items->size());
|
2011-05-09 04:49:44 -06:00
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
for (unsigned i = 0; i < p_items->size(); i++)
|
|
|
|
{
|
|
|
|
t_item * ptr = p_items->at(i);
|
2011-05-09 04:49:44 -06:00
|
|
|
items[i] = ptr;
|
2011-07-20 02:41:13 -06:00
|
|
|
d->idLookupTable[ptr->id] = ptr;
|
2011-05-09 04:49:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
t_item * Items::findItemByID(int32_t id)
|
2011-05-09 04:49:44 -06:00
|
|
|
{
|
|
|
|
if (id < 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
if (d->idLookupTable.empty())
|
|
|
|
{
|
|
|
|
std::vector<t_item *> tmp;
|
2011-05-09 04:49:44 -06:00
|
|
|
readItemVector(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->idLookupTable[id];
|
|
|
|
}
|
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
Items::~Items()
|
|
|
|
{
|
|
|
|
Finish();
|
2011-07-20 02:41:13 -06:00
|
|
|
std::map<void *, ItemDesc *>::iterator it;
|
2010-06-24 23:11:26 -06:00
|
|
|
it = d->descVTable.begin();
|
|
|
|
while (it != d->descVTable.end())
|
|
|
|
{
|
|
|
|
delete (*it).second;
|
2010-10-28 16:22:56 -06:00
|
|
|
++it;
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
|
|
|
d->descType.clear();
|
|
|
|
d->descVTable.clear();
|
|
|
|
delete d;
|
2010-05-02 02:35:23 -06:00
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
bool Items::readItem(t_item * itembase, DFHack::dfh_item &item)
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
std::map<void *, ItemDesc *>::iterator it;
|
2010-06-24 23:11:26 -06:00
|
|
|
Process * p = d->owner;
|
|
|
|
ItemDesc * desc;
|
2010-05-02 02:35:23 -06:00
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
void * vtable = itembase->vptr;
|
2010-11-03 03:45:29 -06:00
|
|
|
it = d->descVTable.find(vtable);
|
|
|
|
if(it == d->descVTable.end())
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
|
|
|
desc = new ItemDesc(vtable, p);
|
|
|
|
d->descVTable[vtable] = desc;
|
|
|
|
d->descType[desc->mainType] = desc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
desc = it->second;
|
2010-05-02 02:35:23 -06:00
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
return desc->readItem(itembase, item);
|
2010-05-02 02:35:23 -06:00
|
|
|
}
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
int32_t Items::getItemOwnerID(const DFHack::dfh_item &item)
|
2011-04-11 04:32:53 -06:00
|
|
|
{
|
2011-05-12 10:30:42 -06:00
|
|
|
std::vector<int32_t> vals;
|
|
|
|
if (readItemRefs(item, d->isOwnerRefClass, vals))
|
|
|
|
return vals[0];
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-11 04:32:53 -06:00
|
|
|
|
2011-05-12 10:30:42 -06:00
|
|
|
int32_t Items::getItemContainerID(const DFHack::dfh_item &item)
|
|
|
|
{
|
|
|
|
std::vector<int32_t> vals;
|
|
|
|
if (readItemRefs(item, d->isContainerRefClass, vals))
|
|
|
|
return vals[0];
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-11 04:32:53 -06:00
|
|
|
|
2011-05-12 10:30:42 -06:00
|
|
|
bool Items::getContainedItems(const DFHack::dfh_item &item, std::vector<int32_t> &items)
|
|
|
|
{
|
|
|
|
return readItemRefs(item, d->isContainsRefClass, items);
|
|
|
|
}
|
2011-04-11 04:32:53 -06:00
|
|
|
|
2011-05-12 10:30:42 -06:00
|
|
|
bool Items::readItemRefs(const dfh_item &item, const ClassNameCheck &classname, std::vector<int32_t> &values)
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
std::vector <t_itemref *> &p_refs = item.base->itemrefs;
|
2011-05-12 10:30:42 -06:00
|
|
|
values.clear();
|
|
|
|
|
|
|
|
for (uint32_t i=0; i<p_refs.size(); i++)
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
if (classname(d->owner, p_refs[i]->vptr))
|
|
|
|
values.push_back(int32_t(p_refs[i]->value));
|
2011-04-11 04:32:53 -06:00
|
|
|
}
|
|
|
|
|
2011-05-12 10:30:42 -06:00
|
|
|
return !values.empty();
|
2011-04-11 04:32:53 -06:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:35:50 -06:00
|
|
|
bool Items::unknownRefs(const dfh_item &item, std::vector<std::string>& names,
|
|
|
|
std::vector<int32_t>& values)
|
|
|
|
{
|
|
|
|
names.clear();
|
|
|
|
values.clear();
|
|
|
|
|
|
|
|
std::vector <t_itemref *> &p_refs = item.base->itemrefs;
|
|
|
|
|
|
|
|
for (uint32_t i=0; i<p_refs.size(); i++)
|
|
|
|
{
|
|
|
|
std::string name = p_refs[i]->getClassName();
|
|
|
|
|
|
|
|
if (d->knownItemRefTypes.find(name) == d->knownItemRefTypes.end())
|
|
|
|
{
|
|
|
|
names.push_back(name);
|
|
|
|
values.push_back(p_refs[i]->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (names.size() > 0);
|
|
|
|
}
|
|
|
|
|
2011-05-12 11:09:49 -06:00
|
|
|
bool Items::removeItemOwner(dfh_item &item, Creatures *creatures)
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
std::vector <t_itemref *> &p_refs = item.base->itemrefs;
|
2011-05-12 11:09:49 -06:00
|
|
|
for (uint32_t i=0; i<p_refs.size(); i++)
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
if (!d->isOwnerRefClass(d->owner, p_refs[i]->vptr))
|
|
|
|
continue;
|
2011-05-12 11:09:49 -06:00
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
int32_t & oid = p_refs[i]->value;
|
2011-05-12 11:09:49 -06:00
|
|
|
int32_t ix = creatures->FindIndexById(oid);
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
if (ix < 0 || !creatures->RemoveOwnedItemIdx(ix, item.base->id))
|
2011-05-28 02:18:50 -06:00
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
cerr << "RemoveOwnedItemIdx: CREATURE " << ix << " ID " << item.base->id << " FAILED!" << endl;
|
2011-05-12 11:09:49 -06:00
|
|
|
return false;
|
2011-05-28 02:18:50 -06:00
|
|
|
}
|
2011-07-20 02:41:13 -06:00
|
|
|
p_refs.erase(p_refs.begin() + i--);
|
2011-05-12 11:09:49 -06:00
|
|
|
}
|
|
|
|
|
2011-07-20 02:41:13 -06:00
|
|
|
item.base->flags.owned = 0;
|
2011-05-12 11:09:49 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-16 15:57:04 -06:00
|
|
|
std::string Items::getItemClass(const dfh_item & item)
|
|
|
|
{
|
|
|
|
return getItemClass(item.matdesc.itemType);
|
|
|
|
}
|
|
|
|
|
2010-05-02 04:50:51 -06:00
|
|
|
std::string Items::getItemClass(int32_t index)
|
|
|
|
{
|
2010-06-24 23:11:26 -06:00
|
|
|
std::map<int32_t, ItemDesc *>::iterator it;
|
|
|
|
std::string out;
|
2010-05-02 04:50:51 -06:00
|
|
|
|
2010-06-24 23:11:26 -06:00
|
|
|
it = d->descType.find(index);
|
2010-11-03 03:45:29 -06:00
|
|
|
if(it == d->descType.end())
|
2010-06-24 23:11:26 -06:00
|
|
|
{
|
|
|
|
/* these are dummy values for mood decoding */
|
|
|
|
switch(index)
|
|
|
|
{
|
|
|
|
case 0: return "bar";
|
|
|
|
case 1: return "cut gem";
|
|
|
|
case 2: return "block";
|
|
|
|
case 3: return "raw gem";
|
|
|
|
case 4: return "raw stone";
|
|
|
|
case 5: return "log";
|
|
|
|
case 54: return "leather";
|
|
|
|
case 57: return "cloth";
|
|
|
|
case -1: return "probably bone or shell, but I really don't know";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = it->second->className;
|
|
|
|
return out;
|
2010-05-02 04:50:51 -06:00
|
|
|
}
|
|
|
|
|
2011-04-11 14:13:06 -06:00
|
|
|
std::string Items::getItemDescription(const dfh_item & item, Materials * Materials)
|
2010-05-02 02:35:23 -06:00
|
|
|
{
|
2011-04-13 17:42:03 -06:00
|
|
|
std::stringstream outss;
|
2010-06-24 23:11:26 -06:00
|
|
|
switch(item.quality)
|
|
|
|
{
|
2011-04-13 17:42:03 -06:00
|
|
|
case 0:
|
|
|
|
outss << "Ordinary ";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
outss << "Well crafted ";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
outss << "Finely crafted ";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
outss << "Superior quality ";
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
outss << "Exceptionnal ";
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
outss << "Masterful ";
|
|
|
|
break;
|
|
|
|
default: outss << "Crazy quality " << item.quality << " "; break;
|
2010-06-24 23:11:26 -06:00
|
|
|
}
|
2011-04-13 17:42:03 -06:00
|
|
|
outss << Materials->getDescription(item.matdesc) << " " << getItemClass(item.matdesc.itemType);
|
|
|
|
return outss.str();
|
2011-02-16 11:29:16 -07:00
|
|
|
}
|
2011-04-13 17:42:03 -06:00
|
|
|
|
|
|
|
/// dump offsets used by accessors of a valid item to a string
|
|
|
|
std::string Items::dumpAccessors(const dfh_item & item)
|
|
|
|
{
|
2011-07-20 02:41:13 -06:00
|
|
|
std::map< void *, ItemDesc* >::const_iterator it = d->descVTable.find(item.base->vptr);
|
2011-04-16 13:02:20 -06:00
|
|
|
if(it != d->descVTable.end())
|
|
|
|
return it->second->dumpAccessors();
|
|
|
|
return "crud";
|
2011-07-24 22:35:50 -06:00
|
|
|
}
|