Stripped nonsense from the DFHack vector wrapper. Next step: removal.

develop
Petr Mrázek 2011-06-13 01:14:10 +02:00
parent 81d648dfa7
commit 511f459182
13 changed files with 48 additions and 105 deletions

@ -199,7 +199,6 @@ DFhackCExport int SDL_Init(uint32_t flags)
*******************************************************************************/
static int (*_refresh)(void) = 0;
//extern NCURSES_EXPORT(int) refresh (void);
DFhackCExport int refresh (void)
{
if(_refresh)
@ -217,7 +216,6 @@ DFhackCExport int refresh (void)
}
static int (*_endwin)(void) = 0;
//extern NCURSES_EXPORT(int) endwin (void);
DFhackCExport int endwin (void)
{
if(!errorstate)
@ -233,7 +231,6 @@ DFhackCExport int endwin (void)
}
typedef void WINDOW;
//extern NCURSES_EXPORT(WINDOW *) initscr (void);
static WINDOW * (*_initscr)(void) = 0;
DFhackCExport WINDOW * initscr (void)
{

@ -80,16 +80,6 @@ Process::Process(VersionInfoFactory * known_versions)
}
}
void Process::readSTLVector(const uint32_t address, t_vecTriplet & triplet)
{
read(address, sizeof(triplet), (uint8_t *) &triplet);
}
void Process::writeSTLVector(const uint32_t address, t_vecTriplet & triplet)
{
write(address, sizeof(triplet), (uint8_t *) &triplet);
}
string Process::doReadClassName (uint32_t vptr)
{
int typeinfo = Process::readDWord(vptr - 0x4);

@ -229,9 +229,6 @@ namespace DFHack
return str->size();
}
/// read a STL vector
void readSTLVector(const uint32_t address, t_vecTriplet & triplet);
void writeSTLVector(const uint32_t address, t_vecTriplet & triplet);
/// get class name of an object with rtti/type info
std::string doReadClassName(uint32_t vptr);

@ -33,6 +33,7 @@ distribution.
#include "dfhack/Process.h"
#include <string.h>
#include <vector>
namespace DFHack
{
@ -40,96 +41,54 @@ namespace DFHack
class DFHACK_EXPORT DfVector
{
private:
Process *_p;
uint32_t _address;
t_vecTriplet t;
t_vecTriplet t_read;
uint32_t _size;// vector size
T * data; // cached data
bool isMetadataInSync()
{
t_vecTriplet t2;
_p->readSTLVector(_address,t2);
return (t2.start == t.start || t2.end == t.end || t2.alloc_end == t.alloc_end);
}
std::vector<T> * real_vec;
public:
DfVector(Process *p, uint32_t address) : _p(p), _address(address)
{
p->readSTLVector(address,t);
t_read = t;
uint32_t byte_size = t.end - t.start;
_size = byte_size / sizeof(T);
data = new T[_size];
p->read(t.start,byte_size, (uint8_t *)data);
};
DfVector()
DfVector(uint32_t address)
{
data = 0;
real_vec = (std::vector<T> *) address;
};
~DfVector()
{
if(data)
delete [] data;
};
// get offset of the specified index
inline const T& operator[] (uint32_t index)
{
// FIXME: vector out of bounds exception
//assert(index < size);
return data[index];
return real_vec->at(index);
};
// get offset of the specified index
inline const T& at (uint32_t index)
{
//assert(index < size);
return data[index];
return real_vec->at(index);
};
// update value at index
bool set(uint32_t index, T value)
{
if (index >= _size)
if (index >= real_vec->size())
return false;
data[index] = value;
_p->write(t.start + sizeof(T)*index, sizeof(T), (uint8_t *)&data[index]);
real_vec->at(index) = value;
return true;
}
// remove value
bool remove(uint32_t index)
{
if (index >= _size || !isMetadataInSync())
if (index >= real_vec->size())
return false;
// Remove the item
_size--;
t.end -= sizeof(T);
int tail = (_size-index)*sizeof(T);
memmove(&data[index], &data[index+1], tail);
// Write back the data
if (tail)
_p->write(t.start + sizeof(T)*index, tail, (uint8_t *)&data[index]);
_p->writeSTLVector(_address,t);
real_vec->erase(real_vec->begin() + index);
return true;
}
// get vector size
inline uint32_t size ()
{
return _size;
};
// get vector start
inline uint32_t start ()
{
return t.start;
};
// get vector end
inline uint32_t end ()
{
return t.end;
return real_vec->size();
};
// get vector start
inline const uint32_t alloc_end ()
inline const T * start ()
{
return t.alloc_end;
return real_vec->data();
};
};
}

@ -125,7 +125,7 @@ bool Buildings::Start(uint32_t & numbuildings)
{
if(!d->Inited)
return false;
d->p_bld = new DfVector <uint32_t> (d->owner, d->buildings_vector);
d->p_bld = new DfVector <uint32_t> (d->buildings_vector);
numbuildings = d->p_bld->size();
d->Started = true;
return true;
@ -178,7 +178,7 @@ bool Buildings::ReadCustomWorkshopTypes(map <uint32_t, string> & btypes)
return false;
Process * p = d->owner;
DfVector <uint32_t> p_matgloss (p, d->custom_workshop_vector);
DfVector <uint32_t> p_matgloss (d->custom_workshop_vector);
uint32_t size = p_matgloss.size();
btypes.clear();

@ -78,7 +78,7 @@ Constructions::~Constructions()
bool Constructions::Start(uint32_t & numconstructions)
{
d->p_cons = new DfVector <uint32_t> (d->owner, d->construction_vector);
d->p_cons = new DfVector <uint32_t> (d->construction_vector);
numconstructions = d->p_cons->size();
d->Started = true;
return true;

@ -237,7 +237,7 @@ bool Creatures::Start( uint32_t &numcreatures )
{
if(d->Ft_basic)
{
d->p_cre = new DfVector <uint32_t> (d->owner, d->creatures.vector);
d->p_cre = new DfVector <uint32_t> (d->creatures.vector);
d->Started = true;
numcreatures = d->p_cre->size();
d->IdMapReady = false;
@ -314,7 +314,7 @@ bool Creatures::ReadCreature (const int32_t index, t_creature & furball)
*/
// appearance
DfVector <uint32_t> app(p, addr_cr + offs.appearance_vector_offset);
DfVector <uint32_t> app(addr_cr + offs.appearance_vector_offset);
furball.nbcolors = app.size();
if(furball.nbcolors>MAX_COLORS)
furball.nbcolors = MAX_COLORS;
@ -346,7 +346,7 @@ bool Creatures::ReadCreature (const int32_t index, t_creature & furball)
{
furball.has_default_soul = true;
// get first soul's skills
DfVector <uint32_t> skills(p, soul + offs.soul_skills_vector_offset);
DfVector <uint32_t> skills(soul + offs.soul_skills_vector_offset);
furball.defaultSoul.numSkills = skills.size();
for (uint32_t i = 0; i < furball.defaultSoul.numSkills;i++)
@ -504,7 +504,7 @@ bool Creatures::WriteSkills(const uint32_t index, const t_soul &soul)
return false;
}
DfVector<uint32_t> skills(p, souloff + d->creatures.soul_skills_vector_offset);
DfVector<uint32_t> skills(souloff + d->creatures.soul_skills_vector_offset);
for (uint32_t i=0; i<soul.numSkills; i++)
{
@ -601,7 +601,7 @@ bool Creatures::WriteJob(const t_creature * furball, std::vector<t_material> con
unsigned int i;
Process * p = d->owner;
Private::t_offsets & off = d->creatures;
DfVector <uint32_t> cmats(p, furball->current_job.occupationPtr + off.job_materials_vector);
DfVector <uint32_t> cmats(furball->current_job.occupationPtr + off.job_materials_vector);
for(i=0;i<cmats.size();i++)
{
@ -665,7 +665,7 @@ bool Creatures::ReadJob(const t_creature * furball, vector<t_material> & mat)
Process * p = d->owner;
Private::t_offsets & off = d->creatures;
DfVector <uint32_t> cmats(p, furball->current_job.occupationPtr + off.job_materials_vector);
DfVector <uint32_t> cmats(furball->current_job.occupationPtr + off.job_materials_vector);
mat.resize(cmats.size());
for(i=0;i<cmats.size();i++)
{
@ -691,7 +691,7 @@ bool Creatures::ReadInventoryPtr(const uint32_t temp, std::vector<uint32_t> & it
if(!d->Started || !d->Ft_inventory) return false;
Process * p = d->owner;
DfVector <uint32_t> citem(p, temp + d->creatures.inventory_offset);
DfVector <uint32_t> citem(temp + d->creatures.inventory_offset);
if(citem.size() == 0)
return false;
item.resize(citem.size());
@ -713,7 +713,7 @@ bool Creatures::ReadOwnedItemsPtr(const uint32_t temp, std::vector<int32_t> & it
if(!d->Started || !d->Ft_owned_items) return false;
Process * p = d->owner;
DfVector <int32_t> citem(p, temp + d->creatures.owned_items_offset);
DfVector <int32_t> citem(temp + d->creatures.owned_items_offset);
if(citem.size() == 0)
return false;
item.resize(citem.size());
@ -738,7 +738,7 @@ bool Creatures::RemoveOwnedItemPtr(const uint32_t temp, int32_t id)
if(!d->Started || !d->Ft_owned_items) return false;
Process * p = d->owner;
DfVector <int32_t> citem(p, temp + d->creatures.owned_items_offset);
DfVector <int32_t> citem(temp + d->creatures.owned_items_offset);
for (unsigned i = 0; i < citem.size(); i++) {
if (citem[i] != id)

@ -78,7 +78,7 @@ Engravings::~Engravings()
bool Engravings::Start(uint32_t & numengravings)
{
d->p_engr = new DfVector <uint32_t> (d->owner, d->engraving_vector);
d->p_engr = new DfVector <uint32_t> (d->engraving_vector);
numengravings = d->p_engr->size();
d->Started = true;
return true;

@ -462,7 +462,7 @@ bool Items::Finish()
bool Items::readItemVector(std::vector<uint32_t> &items)
{
DFHack::DfVector <uint32_t> p_items(d->owner, d->itemVectorAddress);
DFHack::DfVector <uint32_t> p_items(d->itemVectorAddress);
d->idLookupTable.clear();
items.resize(p_items.size());
@ -565,7 +565,7 @@ bool Items::getContainedItems(const DFHack::dfh_item &item, std::vector<int32_t>
bool Items::readItemRefs(const dfh_item &item, const ClassNameCheck &classname, std::vector<int32_t> &values)
{
DFHack::DfVector<uint32_t> p_refs(d->owner, item.origin + d->refVectorOffset);
DFHack::DfVector<uint32_t> p_refs(item.origin + d->refVectorOffset);
values.clear();
@ -581,7 +581,7 @@ bool Items::readItemRefs(const dfh_item &item, const ClassNameCheck &classname,
bool Items::removeItemOwner(dfh_item &item, Creatures *creatures)
{
DFHack::DfVector<uint32_t> p_refs(d->owner, item.origin + d->refVectorOffset);
DFHack::DfVector<uint32_t> p_refs(item.origin + d->refVectorOffset);
for (uint32_t i=0; i<p_refs.size(); i++)
{

@ -711,7 +711,7 @@ bool Maps::StartFeatures()
if(loc_f_array16x16)
{
uint32_t feat_vector = loc_f_array16x16 + sizeof_16vec * sub_x + sizeof_vec * sub_y;
DfVector<uint32_t> p_features(p, feat_vector);
DfVector<uint32_t> p_features(feat_vector);
uint32_t size = p_features.size();
DFCoord pc(blockX,blockY);
std::vector<t_feature *> tempvec;
@ -766,7 +766,7 @@ bool Maps::StartFeatures()
const uint32_t global_feature_funcptr = off.global_funcptr;
const uint32_t glob_main_mat_offset = off.global_material;
const uint32_t glob_sub_mat_offset = off.global_submaterial;
DfVector<uint32_t> p_features (p,global_feature_vector);
DfVector<uint32_t> p_features (global_feature_vector);
d->v_global_feature.clear();
uint32_t size = p_features.size();
@ -945,7 +945,7 @@ bool Maps::ReadVeins(uint32_t x, uint32_t y, uint32_t z, vector <t_vein>* veins,
if (!addr) return false;
// veins are stored as a vector of pointers to veins
/*pointer is 4 bytes! we work with a 32bit program here, no matter what architecture we compile khazad for*/
DfVector <uint32_t> p_veins (p, addr + off.veinvector);
DfVector <uint32_t> p_veins (addr + off.veinvector);
uint32_t size = p_veins.size();
// read all veins
for (uint32_t i = 0; i < size;i++)
@ -1129,7 +1129,7 @@ bool Maps::ReadGeology (vector < vector <uint16_t> >& assign)
}
// read the geoblock vector
DfVector <uint32_t> geoblocks (d->d->p, geoblocks_vector_addr);
DfVector <uint32_t> geoblocks (geoblocks_vector_addr);
// iterate over 8 surrounding regions + local region
for (int i = eNorthWest; i < eBiomeCount; i++)
@ -1162,7 +1162,7 @@ bool Maps::ReadGeology (vector < vector <uint16_t> >& assign)
/// geology blocks have a vector of layer descriptors
// get the vector with pointer to layers
DfVector <uint32_t> geolayers (p, geoblock_off + off.geolayer_geoblock_offset); // let's hope
DfVector <uint32_t> geolayers (geoblock_off + off.geolayer_geoblock_offset); // let's hope
// make sure we don't load crap
assert (geolayers.size() > 0 && geolayers.size() <= 16);
@ -1222,7 +1222,7 @@ bool Maps::ReadVegetation(uint32_t x, uint32_t y, uint32_t z, std::vector<dfh_pl
plants->clear();
Private::t_offsets &off = d->offsets;
DfVector<uint32_t> vegptrs(d->owner, addr + off.vegvector);
DfVector<uint32_t> vegptrs(addr + off.vegvector);
for(size_t i = 0; i < vegptrs.size(); i++)
{
d->d->readName(shrubbery.name,vegptrs[i]);

@ -229,7 +229,7 @@ bool API::ReadInorganicMaterials (vector<t_matgloss> & inorganic)
// good for now
inline bool ReadNamesOnly(Process* p, uint32_t address, vector<t_matgloss> & names)
{
DfVector <uint32_t> p_matgloss (p, address);
DfVector <uint32_t> p_matgloss (address);
uint32_t size = p_matgloss.size();
names.clear();
names.reserve (size);
@ -245,7 +245,7 @@ inline bool ReadNamesOnly(Process* p, uint32_t address, vector<t_matgloss> & nam
bool Materials::ReadInorganicMaterials (void)
{
Process * p = d->owner;
DfVector <uint32_t> p_matgloss (p, d->vector_inorganic);
DfVector <uint32_t> p_matgloss (d->vector_inorganic);
uint32_t size = p_matgloss.size();
inorganic.clear();
inorganic.reserve (size);
@ -311,7 +311,7 @@ bool Materials::ReadDescriptorColors (void)
{
Process * p = d->owner;
OffsetGroup * OG_Descriptors = p->getDescriptor()->getGroup("Materials")->getGroup("descriptors");
DfVector <uint32_t> p_colors (p, OG_Descriptors->getAddress ("colors_vector"));
DfVector <uint32_t> p_colors (OG_Descriptors->getAddress ("colors_vector"));
uint32_t size = p_colors.size();
color.clear();
@ -340,7 +340,7 @@ bool Materials::ReadCreatureTypesEx (void)
uint32_t sizeof_string = OG_string->getHexValue ("sizeof");
OffsetGroup * OG_Mats = mem->getGroup("Materials");
DfVector <uint32_t> p_races (p, OG_Mats->getAddress ("creature_type_vector"));
DfVector <uint32_t> p_races (OG_Mats->getAddress ("creature_type_vector"));
OffsetGroup * OG_Creature = OG_Mats->getGroup("creature");
uint32_t castes_vector_offset = OG_Creature->getOffset ("caste_vector");
@ -403,7 +403,7 @@ bool Materials::ReadCreatureTypesEx (void)
mat.tilecolor.back = p->readWord( p_races[i] + tile_color_offset + 2 );
mat.tilecolor.bright = p->readWord( p_races[i] + tile_color_offset + 4 );
DfVector <uint32_t> p_castes(p, p_races[i] + castes_vector_offset);
DfVector <uint32_t> p_castes(p_races[i] + castes_vector_offset);
sizecas = p_castes.size();
for (uint32_t j = 0; j < sizecas;j++)
{
@ -419,13 +419,13 @@ bool Materials::ReadCreatureTypesEx (void)
{
/* color mod reading */
// Caste + offset > color mod vector
DfVector <uint32_t> p_colormod(p, caste_start + caste_colormod_offset);
DfVector <uint32_t> p_colormod(caste_start + caste_colormod_offset);
sizecolormod = p_colormod.size();
caste.ColorModifier.resize(sizecolormod);
for(uint32_t k = 0; k < sizecolormod;k++)
{
// color mod [0] -> color list
DfVector <uint32_t> p_colorlist(p, p_colormod[k]);
DfVector <uint32_t> p_colorlist(p_colormod[k]);
sizecolorlist = p_colorlist.size();
caste.ColorModifier[k].colorlist.resize(sizecolorlist);
for(uint32_t l = 0; l < sizecolorlist; l++)
@ -436,7 +436,7 @@ bool Materials::ReadCreatureTypesEx (void)
caste.ColorModifier[k].enddate = p->readDWord( p_colormod[k] + color_modifier_enddate_offset );
}
/* body parts */
DfVector <uint32_t> p_bodypart(p, caste_start + caste_bodypart_offset);
DfVector <uint32_t> p_bodypart(caste_start + caste_bodypart_offset);
caste.bodypart.empty();
sizebp = p_bodypart.size();
for(uint32_t k = 0; k < sizebp; k++)
@ -454,7 +454,7 @@ bool Materials::ReadCreatureTypesEx (void)
}
mat.castes.push_back(caste);
}
DfVector <uint32_t> p_extract(p, p_races[i] + extract_vector_offset);
DfVector <uint32_t> p_extract(p_races[i] + extract_vector_offset);
for(uint32_t j = 0; j < p_extract.size(); j++)
{
t_creatureextract extract;

@ -86,8 +86,8 @@ bool Translation::Start()
return false;
Process * p = d->d->p;
Finish();
DfVector <uint32_t> genericVec (p, d->genericAddress);
DfVector <uint32_t> transVec (p, d->transAddress);
DfVector <uint32_t> genericVec (d->genericAddress);
DfVector <uint32_t> transVec (d->transAddress);
DFDict & translations = d->dicts.translations;
DFDict & foreign_languages = d->dicts.foreign_languages;
@ -106,7 +106,7 @@ bool Translation::Start()
for (uint32_t i = 0; i < transVec.size();i++)
{
uint32_t transPtr = transVec.at(i);
DfVector <uint32_t> trans_names_vec (p, transPtr + d->word_table_offset);
DfVector <uint32_t> trans_names_vec (transPtr + d->word_table_offset);
for (uint32_t j = 0;j < trans_names_vec.size();j++)
{
uint32_t transNamePtr = trans_names_vec.at(j);

@ -81,7 +81,7 @@ bool Vegetation::Start(uint32_t & numplants)
{
if(!d->Inited)
return false;
d->p_veg = new DfVector <uint32_t> (d->owner, d->vegetation_vector);
d->p_veg = new DfVector <uint32_t> (d->vegetation_vector);
numplants = d->p_veg->size();
d->Started = true;
return true;