Merge branch 'master' of git://github.com/peterix/dfhack into errorhandling

develop
mizipzor 2010-02-28 02:29:29 +01:00
commit decb20ef67
5 changed files with 356 additions and 188 deletions

@ -26,6 +26,9 @@ distribution.
#include "DFError.h" #include "DFError.h"
using namespace DFHack; using namespace DFHack;
/*
FIXME: memset to 0?
*/
class API::Private class API::Private
{ {
public: public:
@ -96,9 +99,9 @@ public:
uint32_t hotkey_xyz_offset; uint32_t hotkey_xyz_offset;
uint32_t hotkey_size; uint32_t hotkey_size;
uint32_t settlement_name_offset; uint32_t settlement_name_offset;
uint32_t settlement_world_xy_offset; uint32_t settlement_world_xy_offset;
uint32_t settlement_local_xy_offset; uint32_t settlement_local_xy_offset;
uint32_t dwarf_lang_table_offset; uint32_t dwarf_lang_table_offset;
@ -117,8 +120,7 @@ public:
bool itemsInited; bool itemsInited;
bool notesInited; bool notesInited;
bool hotkeyInited; bool hotkeyInited;
bool settlementsInited; bool settlementsInited;
bool nameTablesInited; bool nameTablesInited;
uint32_t tree_offset; uint32_t tree_offset;
@ -128,8 +130,8 @@ public:
DfVector *p_veg; DfVector *p_veg;
DfVector *p_itm; DfVector *p_itm;
DfVector *p_notes; DfVector *p_notes;
DfVector *p_settlements; DfVector *p_settlements;
DfVector *p_current_settlement; DfVector *p_current_settlement;
}; };
API::API (const string path_to_xml) API::API (const string path_to_xml)
@ -147,7 +149,6 @@ API::API (const string path_to_xml)
d->itemsInited = false; d->itemsInited = false;
d->notesInited = false; d->notesInited = false;
d->hotkeyInited = false; d->hotkeyInited = false;
d->pm = NULL; d->pm = NULL;
} }
@ -234,7 +235,6 @@ bool API::DestroyMap()
delete [] d->block; delete [] d->block;
d->block = NULL; d->block = NULL;
} }
return true; return true;
} }
@ -283,7 +283,7 @@ bool API::WriteDirtyBit(uint32_t x, uint32_t y, uint32_t z, bool dirtybit)
{ {
uint32_t addr_of_struct = g_pProcess->readDWord(addr); uint32_t addr_of_struct = g_pProcess->readDWord(addr);
uint32_t dirtydword = g_pProcess->readDWord(addr_of_struct); uint32_t dirtydword = g_pProcess->readDWord(addr_of_struct);
dirtydword &= 0xFFFFFFF0; dirtydword &= 0xFFFFFFFE;
dirtydword |= (uint32_t) dirtybit; dirtydword |= (uint32_t) dirtybit;
g_pProcess->writeDWord (addr_of_struct, dirtydword); g_pProcess->writeDWord (addr_of_struct, dirtydword);
return true; return true;
@ -332,7 +332,7 @@ bool API::WriteTileTypes (uint32_t x, uint32_t y, uint32_t z, uint16_t *buffer)
bool API::getCurrentCursorCreatures (vector<uint32_t> &addresses) bool API::getCurrentCursorCreatures (vector<uint32_t> &addresses)
{ {
assert (d->cursorWindowInited); if(d->cursorWindowInited) return false;
DfVector creUnderCursor = d->p->readVector (d->current_cursor_creature_offset, 4); DfVector creUnderCursor = d->p->readVector (d->current_cursor_creature_offset, 4);
if (creUnderCursor.getSize() == 0) if (creUnderCursor.getSize() == 0)
{ {
@ -371,7 +371,7 @@ bool API::WriteOccupancy (uint32_t x, uint32_t y, uint32_t z, uint32_t *buffer)
return false; return false;
} }
// FIXME: this is bad. determine the real size!
//16 of them? IDK... there's probably just 7. Reading more doesn't cause errors as it's an array nested inside a block //16 of them? IDK... there's probably just 7. Reading more doesn't cause errors as it's an array nested inside a block
// 16 * sizeof(uint8_t) // 16 * sizeof(uint8_t)
bool API::ReadRegionOffsets (uint32_t x, uint32_t y, uint32_t z, uint8_t *buffer) bool API::ReadRegionOffsets (uint32_t x, uint32_t y, uint32_t z, uint8_t *buffer)
@ -641,7 +641,7 @@ bool API::ReadCreatureMatgloss (vector<t_matgloss> & creatures)
bool API::ReadGeology (vector < vector <uint16_t> >& assign) bool API::ReadGeology (vector < vector <uint16_t> >& assign)
{ {
memory_info * minfo = d->offset_descriptor; memory_info * minfo = d->offset_descriptor;
// get needed addresses and offsets // get needed addresses and offsets. Now this is what I call crazy.
int region_x_offset = minfo->getAddress ("region_x"); int region_x_offset = minfo->getAddress ("region_x");
int region_y_offset = minfo->getAddress ("region_y"); int region_y_offset = minfo->getAddress ("region_y");
int region_z_offset = minfo->getAddress ("region_z"); int region_z_offset = minfo->getAddress ("region_z");
@ -755,9 +755,9 @@ bool API::InitReadBuildings ( uint32_t& numbuildings )
// read one building // read one building
bool API::ReadBuilding (const int32_t &index, t_building & building) bool API::ReadBuilding (const int32_t index, t_building & building)
{ {
assert (d->buildingsInited); if(!d->buildingsInited) return false;
t_building_df40d bld_40d; t_building_df40d bld_40d;
@ -787,8 +787,11 @@ bool API::ReadBuilding (const int32_t &index, t_building & building)
void API::FinishReadBuildings() void API::FinishReadBuildings()
{ {
delete d->p_bld; if(d->p_bld)
d->p_bld = NULL; {
delete d->p_bld;
d->p_bld = NULL;
}
d->buildingsInited = false; d->buildingsInited = false;
} }
@ -814,9 +817,9 @@ bool API::InitReadConstructions(uint32_t & numconstructions)
} }
bool API::ReadConstruction (const int32_t &index, t_construction & construction) bool API::ReadConstruction (const int32_t index, t_construction & construction)
{ {
assert (d->constructionsInited); if(!d->constructionsInited) return false;
t_construction_df40d c_40d; t_construction_df40d c_40d;
// read pointer from vector at position // read pointer from vector at position
@ -837,8 +840,11 @@ bool API::ReadConstruction (const int32_t &index, t_construction & construction)
void API::FinishReadConstructions() void API::FinishReadConstructions()
{ {
delete d->p_cons; if(d->p_cons)
d->p_cons = NULL; {
delete d->p_cons;
d->p_cons = NULL;
}
d->constructionsInited = false; d->constructionsInited = false;
} }
@ -863,10 +869,10 @@ bool API::InitReadVegetation(uint32_t & numplants)
} }
bool API::ReadVegetation (const int32_t &index, t_tree_desc & shrubbery) bool API::ReadVegetation (const int32_t index, t_tree_desc & shrubbery)
{ {
assert (d->vegetationInited); if(!d->vegetationInited)
// uint32_t temp; return false;
// read pointer from vector at position // read pointer from vector at position
uint32_t temp = * (uint32_t *) d->p_veg->at (index); uint32_t temp = * (uint32_t *) d->p_veg->at (index);
//read construction from memory //read construction from memory
@ -879,8 +885,11 @@ bool API::ReadVegetation (const int32_t &index, t_tree_desc & shrubbery)
void API::FinishReadVegetation() void API::FinishReadVegetation()
{ {
delete d->p_veg; if(d->p_veg)
d->p_veg = NULL; {
delete d->p_veg;
d->p_veg = 0;
}
d->vegetationInited = false; d->vegetationInited = false;
} }
@ -978,10 +987,9 @@ bool API::InitReadNotes( uint32_t &numnotes )
return false; return false;
} }
} }
bool API::ReadNote (const int32_t &index, t_note & note) bool API::ReadNote (const int32_t index, t_note & note)
{ {
if(!d->notesInited) if(!d->notesInited) return false;
return false;
// read pointer from vector at position // read pointer from vector at position
uint32_t temp = * (uint32_t *) d->p_notes->at (index); uint32_t temp = * (uint32_t *) d->p_notes->at (index);
note.symbol = g_pProcess->readByte(temp); note.symbol = g_pProcess->readByte(temp);
@ -1019,12 +1027,11 @@ bool API::InitReadSettlements( uint32_t & numsettlements )
return false; return false;
} }
} }
bool API::ReadSettlement(const int32_t &index, t_settlement & settlement) bool API::ReadSettlement(const int32_t index, t_settlement & settlement)
{ {
if(!d->settlementsInited) if(!d->settlementsInited) return false;
return false; if(!d->p_settlements->getSize()) return false;
if(!d->p_settlements->getSize())
return false;
// read pointer from vector at position // read pointer from vector at position
uint32_t temp = * (uint32_t *) d->p_settlements->at (index); uint32_t temp = * (uint32_t *) d->p_settlements->at (index);
settlement.origin = temp; settlement.origin = temp;
@ -1035,10 +1042,9 @@ bool API::ReadSettlement(const int32_t &index, t_settlement & settlement)
} }
bool API::ReadCurrentSettlement(t_settlement & settlement) bool API::ReadCurrentSettlement(t_settlement & settlement)
{ {
if(!d->settlementsInited) if(!d->settlementsInited) return false;
return false; if(!d->p_current_settlement->getSize()) return false;
if(!d->p_current_settlement->getSize())
return false;
uint32_t temp = * (uint32_t *) d->p_current_settlement->at(0); uint32_t temp = * (uint32_t *) d->p_current_settlement->at(0);
settlement.origin = temp; settlement.origin = temp;
g_pProcess->read(temp + d->settlement_name_offset, 2 * sizeof(int32_t), (uint8_t *) &settlement.name); g_pProcess->read(temp + d->settlement_name_offset, 2 * sizeof(int32_t), (uint8_t *) &settlement.name);
@ -1049,10 +1055,16 @@ bool API::ReadCurrentSettlement(t_settlement & settlement)
void API::FinishReadSettlements() void API::FinishReadSettlements()
{ {
delete d->p_settlements; if(d->p_settlements)
delete d->p_current_settlement; {
d->p_settlements = NULL; delete d->p_settlements;
d->p_current_settlement = NULL; d->p_settlements = NULL;
}
if(d->p_current_settlement)
{
delete d->p_current_settlement;
d->p_current_settlement = NULL;
}
d->settlementsInited = false; d->settlementsInited = false;
} }
@ -1078,8 +1090,7 @@ bool API::InitReadHotkeys( )
} }
bool API::ReadHotkeys(t_hotkey hotkeys[]) bool API::ReadHotkeys(t_hotkey hotkeys[])
{ {
if (!d->hotkeyInited) if (!d->hotkeyInited) return false;
return false;
uint32_t currHotkey = d->hotkey_start; uint32_t currHotkey = d->hotkey_start;
for(uint32_t i = 0 ; i < NUM_HOTKEYS ;i++) for(uint32_t i = 0 ; i < NUM_HOTKEYS ;i++)
{ {
@ -1092,11 +1103,11 @@ bool API::ReadHotkeys(t_hotkey hotkeys[])
} }
// returns index of creature actually read or -1 if no creature can be found // returns index of creature actually read or -1 if no creature can be found
int32_t API::ReadCreatureInBox (int32_t index, t_creature & furball, int32_t API::ReadCreatureInBox (int32_t index, t_creature & furball,
const uint16_t &x1, const uint16_t &y1, const uint16_t &z1, const uint16_t x1, const uint16_t y1, const uint16_t z1,
const uint16_t &x2, const uint16_t &y2, const uint16_t &z2) const uint16_t x2, const uint16_t y2, const uint16_t z2)
{ {
if (!d->creaturesInited) return -1;
uint16_t coords[3]; uint16_t coords[3];
assert (d->creaturesInited);
uint32_t size = d->p_cre->getSize(); uint32_t size = d->p_cre->getSize();
while (uint32_t(index) < size) while (uint32_t(index) < size)
{ {
@ -1119,11 +1130,11 @@ int32_t API::ReadCreatureInBox (int32_t index, t_creature & furball,
return -1; return -1;
} }
void API::getItemIndexesInBox(vector<uint32_t> &indexes, bool API::getItemIndexesInBox(vector<uint32_t> &indexes,
const uint16_t &x1, const uint16_t &y1, const uint16_t &z1, const uint16_t x1, const uint16_t y1, const uint16_t z1,
const uint16_t &x2, const uint16_t &y2, const uint16_t &z2) const uint16_t x2, const uint16_t y2, const uint16_t z2)
{ {
assert(d->itemsInited); if(!d->itemsInited) return false;
indexes.clear(); indexes.clear();
uint32_t size = d->p_itm->getSize(); uint32_t size = d->p_itm->getSize();
struct temp2{ struct temp2{
@ -1149,9 +1160,9 @@ void API::getItemIndexesInBox(vector<uint32_t> &indexes,
} }
} }
bool API::ReadCreature (const int32_t &index, t_creature & furball) bool API::ReadCreature (const int32_t index, t_creature & furball)
{ {
assert (d->creaturesInited); if(!d->creaturesInited) return false;
// read pointer from vector at position // read pointer from vector at position
uint32_t temp = * (uint32_t *) d->p_cre->at (index); uint32_t temp = * (uint32_t *) d->p_cre->at (index);
furball.origin = temp; furball.origin = temp;
@ -1195,13 +1206,13 @@ bool API::ReadCreature (const int32_t &index, t_creature & furball)
if (jobIdAddr) if (jobIdAddr)
{ {
furball.current_job.active = true; furball.current_job.active = true;
furball.current_job.jobId = g_pProcess->readByte (jobIdAddr + d->creature_current_job_id_offset); furball.current_job.jobId = g_pProcess->readByte (jobIdAddr + d->creature_current_job_id_offset);
} }
else else
{ {
furball.current_job.active = false; furball.current_job.active = false;
} }
//likes //likes
DfVector likes(d->p->readVector(temp+d->creature_likes_offset,4)); DfVector likes(d->p->readVector(temp+d->creature_likes_offset,4));
@ -1223,7 +1234,7 @@ bool API::ReadCreature (const int32_t &index, t_creature & furball)
return true; return true;
} }
void API::WriteLabors(const uint32_t &index, uint8_t labors[NUM_CREATURE_LABORS]) void API::WriteLabors(const uint32_t index, uint8_t labors[NUM_CREATURE_LABORS])
{ {
uint32_t temp = * (uint32_t *) d->p_cre->at (index); uint32_t temp = * (uint32_t *) d->p_cre->at (index);
WriteRaw(temp + d->creature_labors_offset, NUM_CREATURE_LABORS, labors); WriteRaw(temp + d->creature_labors_offset, NUM_CREATURE_LABORS, labors);
@ -1271,7 +1282,7 @@ bool API::InitReadNameTables (map< string, vector<string> > & nameTable)
string API::TranslateName (const int names[], int size, const map<string, vector<string> > & nameTable, const string & language) string API::TranslateName (const int names[], int size, const map<string, vector<string> > & nameTable, const string & language)
{ {
string trans; string trans;
assert (d->nameTablesInited); assert (d->nameTablesInited);
map<string, vector<string> >::const_iterator it; map<string, vector<string> >::const_iterator it;
it = nameTable.find (language); it = nameTable.find (language);
@ -1343,22 +1354,28 @@ void API::FinishReadNameTables()
void API::FinishReadCreatures() void API::FinishReadCreatures()
{ {
delete d->p_cre; if(d->p_cre)
d->p_cre = NULL; {
delete d->p_cre;
d->p_cre = 0;
}
d->creaturesInited = false; d->creaturesInited = false;
//FinishReadNameTables(); //FinishReadNameTables();
} }
void API::FinishReadNotes() void API::FinishReadNotes()
{ {
delete d->p_notes; if(d->p_notes)
d->p_notes = NULL; {
delete d->p_notes;
d->p_notes = 0;
}
d->notesInited = false; d->notesInited = false;
} }
bool API::Attach() bool API::Attach()
{ {
// detach all processes, destroy manager // detach all processes, destroy manager
if (d->pm == NULL) if (d->pm == 0)
{ {
d->pm = new ProcessEnumerator (d->xml); // FIXME: handle bad XML better d->pm = new ProcessEnumerator (d->xml); // FIXME: handle bad XML better
} }
@ -1430,12 +1447,12 @@ bool API::isSuspended()
return d->p->isSuspended(); return d->p->isSuspended();
} }
void API::ReadRaw (const uint32_t &offset, const uint32_t &size, uint8_t *target) void API::ReadRaw (const uint32_t offset, const uint32_t size, uint8_t *target)
{ {
g_pProcess->read (offset, size, target); g_pProcess->read (offset, size, target);
} }
void API::WriteRaw (const uint32_t &offset, const uint32_t &size, uint8_t *source) void API::WriteRaw (const uint32_t offset, const uint32_t size, uint8_t *source)
{ {
g_pProcess->write (offset, size, source); g_pProcess->write (offset, size, source);
} }
@ -1481,25 +1498,25 @@ bool API::InitViewSize()
bool API::getViewCoords (int32_t &x, int32_t &y, int32_t &z) bool API::getViewCoords (int32_t &x, int32_t &y, int32_t &z)
{ {
assert (d->cursorWindowInited); if (!d->cursorWindowInited) return false;
g_pProcess->readDWord (d->window_x_offset, (uint32_t &) x); g_pProcess->readDWord (d->window_x_offset, (uint32_t &) x);
g_pProcess->readDWord (d->window_y_offset, (uint32_t &) y); g_pProcess->readDWord (d->window_y_offset, (uint32_t &) y);
g_pProcess->readDWord (d->window_z_offset, (uint32_t &) z); g_pProcess->readDWord (d->window_z_offset, (uint32_t &) z);
return true; return true;
} }
//FIXME: confine writing of coords to map bounds? //FIXME: confine writing of coords to map bounds?
bool API::setViewCoords (const int32_t &x, const int32_t &y, const int32_t &z) bool API::setViewCoords (const int32_t x, const int32_t y, const int32_t z)
{ {
assert (d->cursorWindowInited); if (!d->cursorWindowInited) return false;
g_pProcess->writeDWord (d->window_x_offset, (uint32_t &) x); g_pProcess->writeDWord (d->window_x_offset, (uint32_t) x);
g_pProcess->writeDWord (d->window_y_offset, (uint32_t &) y); g_pProcess->writeDWord (d->window_y_offset, (uint32_t) y);
g_pProcess->writeDWord (d->window_z_offset, (uint32_t &) z); g_pProcess->writeDWord (d->window_z_offset, (uint32_t) z);
return true; return true;
} }
bool API::getCursorCoords (int32_t &x, int32_t &y, int32_t &z) bool API::getCursorCoords (int32_t &x, int32_t &y, int32_t &z)
{ {
assert (d->cursorWindowInited); if(!d->cursorWindowInited) return false;
int32_t coords[3]; int32_t coords[3];
g_pProcess->read (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords); g_pProcess->read (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords);
x = coords[0]; x = coords[0];
@ -1509,16 +1526,17 @@ bool API::getCursorCoords (int32_t &x, int32_t &y, int32_t &z)
return true; return true;
} }
//FIXME: confine writing of coords to map bounds? //FIXME: confine writing of coords to map bounds?
bool API::setCursorCoords (const int32_t &x, const int32_t &y, const int32_t &z) bool API::setCursorCoords (const int32_t x, const int32_t y, const int32_t z)
{ {
assert (d->cursorWindowInited); if (!d->cursorWindowInited) return false;
int32_t coords[3] = {x, y, z}; int32_t coords[3] = {x, y, z};
g_pProcess->write (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords); g_pProcess->write (d->cursor_xyz_offset, 3*sizeof (int32_t), (uint8_t *) coords);
return true; return true;
} }
bool API::getWindowSize (int32_t &width, int32_t &height) bool API::getWindowSize (int32_t &width, int32_t &height)
{ {
assert (d->viewSizeInited); if(! d->viewSizeInited) return false;
int32_t coords[2]; int32_t coords[2];
g_pProcess->read (d->window_dims_offset, 2*sizeof (int32_t), (uint8_t *) coords); g_pProcess->read (d->window_dims_offset, 2*sizeof (int32_t), (uint8_t *) coords);
width = coords[0]; width = coords[0];
@ -1569,9 +1587,10 @@ bool API::InitReadItems(uint32_t & numitems)
return false; return false;
} }
} }
bool API::ReadItem (const uint32_t &index, t_item & item) bool API::ReadItem (const uint32_t index, t_item & item)
{ {
assert (d->itemsInited); //should change to the generic init rather than buildings if (!d->itemsInited) return false;
t_item_df40d item_40d; t_item_df40d item_40d;
// read pointer from vector at position // read pointer from vector at position
@ -1602,14 +1621,18 @@ bool API::ReadItem (const uint32_t &index, t_item & item)
} }
void API::FinishReadItems() void API::FinishReadItems()
{ {
delete d->p_itm; if(d->p_itm)
d->p_itm = NULL; {
delete d->p_itm;
d->p_itm = NULL;
}
d->itemsInited = false; d->itemsInited = false;
} }
bool API::ReadPauseState() bool API::ReadPauseState()
{ {
assert (d->cursorWindowInited); // replace with an exception
if(!d->cursorWindowInited) return false;
uint32_t pauseState = g_pProcess->readDWord (d->pause_state_offset); uint32_t pauseState = g_pProcess->readDWord (d->pause_state_offset);
return pauseState & 1; return pauseState & 1;
@ -1617,13 +1640,15 @@ bool API::ReadPauseState()
uint32_t API::ReadMenuState() uint32_t API::ReadMenuState()
{ {
assert (d->cursorWindowInited); if(d->cursorWindowInited)
return(g_pProcess->readDWord(d->current_menu_state_offset)); return(g_pProcess->readDWord(d->current_menu_state_offset));
return false;
} }
bool API::ReadViewScreen (t_viewscreen &screen) bool API::ReadViewScreen (t_viewscreen &screen)
{ {
assert (d->cursorWindowInited); if (!d->cursorWindowInited) return false;
uint32_t last = g_pProcess->readDWord (d->view_screen_offset); uint32_t last = g_pProcess->readDWord (d->view_screen_offset);
uint32_t screenAddr = g_pProcess->readDWord (last); uint32_t screenAddr = g_pProcess->readDWord (last);
uint32_t nextScreenPtr = g_pProcess->readDWord (last + 4); uint32_t nextScreenPtr = g_pProcess->readDWord (last + 4);
@ -1635,13 +1660,15 @@ bool API::ReadViewScreen (t_viewscreen &screen)
} }
return d->offset_descriptor->resolveObjectToClassID (last, screen.type); return d->offset_descriptor->resolveObjectToClassID (last, screen.type);
} }
bool API::ReadItemTypes(vector< vector< t_itemType > > & itemTypes) bool API::ReadItemTypes(vector< vector< t_itemType > > & itemTypes)
{ {
memory_info * minfo = d->offset_descriptor; memory_info * minfo = d->offset_descriptor;
int matgloss_address = minfo->getAddress("matgloss"); int matgloss_address = minfo->getAddress("matgloss");
int matgloss_skip = minfo->getHexValue("matgloss_skip"); int matgloss_skip = minfo->getHexValue("matgloss_skip");
int item_type_name_offset = minfo->getOffset("item_type_name"); int item_type_name_offset = minfo->getOffset("item_type_name");
for(int i = 8;i<20;i++){ for(int i = 8;i<20;i++)
{
DfVector p_temp = d->p->readVector(matgloss_address + i*matgloss_skip,4); DfVector p_temp = d->p->readVector(matgloss_address + i*matgloss_skip,4);
vector< t_itemType > typesForVec; vector< t_itemType > typesForVec;
for(uint32_t j =0; j<p_temp.getSize();j++) for(uint32_t j =0; j<p_temp.getSize();j++)
@ -1657,57 +1684,4 @@ bool API::ReadItemTypes(vector< vector< t_itemType > > & itemTypes)
itemTypes.push_back(typesForVec); itemTypes.push_back(typesForVec);
} }
return true; return true;
} }
// FIXME: Too dangerous. Contains hardcoded addresses and some arbitrary stuff
/*
bool API::ReadAllMatgloss(vector< vector< string > > & all)
{
memory_info * minfo = d->offset_descriptor;
int matgloss_address = minfo->getAddress("matgloss");
int matgloss_skip = minfo->getHexValue("matgloss_skip");
for(int i = 0;i<7;i++){
DfVector p_temp = d->p->readVector(matgloss_address + i*matgloss_skip,4);
vector< string > stringsForVec;
for(uint32_t j =0; j<p_temp.getSize();j++)
{
uint32_t temp = *(uint32_t *) p_temp[j];
string tempStr = d->p->readSTLString(temp);
stringsForVec.push_back(tempStr);
}
all.push_back(stringsForVec);
}
for(int i = 7;i<22;i++){
DfVector p_temp = d->p->readVector(matgloss_address + i*matgloss_skip,4);
vector< string > stringsForVec;
for(uint32_t j =0; j<p_temp.getSize();j++)
{
uint32_t temp = *(uint32_t *) p_temp[j];
string tempStr = d->p->readSTLString(temp+4);
stringsForVec.push_back(tempStr);
}
all.push_back(stringsForVec);
}
for(int i = 22;i<25;i++){
DfVector p_temp = d->p->readVector(matgloss_address + i*matgloss_skip,4);
vector< string > stringsForVec;
for(uint32_t j =0; j<p_temp.getSize();j++)
{
uint32_t temp = *(uint32_t *) p_temp[j];
string tempStr = d->p->readSTLString(temp);
stringsForVec.push_back(tempStr);
}
all.push_back(stringsForVec);
}
DfVector p_temp = d->p->readVector(0x01604104,4);
vector< string > stringsForVec;
for(uint32_t j =0; j<p_temp.getSize();j++)
{
uint32_t temp = *(uint32_t *) p_temp[j];
string tempStr = d->p->readSTLString(temp);
stringsForVec.push_back(tempStr);
}
all.push_back(stringsForVec);
return true;
}
*/

@ -133,7 +133,7 @@ namespace DFHack
/** /**
* Get the address of a block or 0 if block is not valid * Get the address of a block or 0 if block is not valid
*/ */
uint32_t getBlockPtr (uint32_t x, uint32_t y, uint32_t z); uint32_t getBlockPtr (uint32_t blockx, uint32_t blocky, uint32_t blockz);
bool ReadTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer); // 256 * sizeof(uint16_t) bool ReadTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer); // 256 * sizeof(uint16_t)
bool WriteTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer); // 256 * sizeof(uint16_t) bool WriteTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer); // 256 * sizeof(uint16_t)
@ -157,36 +157,36 @@ namespace DFHack
* Buildings, constructions, plants, all pretty straighforward. InitReadBuildings returns all the building types as a mapping between a numeric values and strings * Buildings, constructions, plants, all pretty straighforward. InitReadBuildings returns all the building types as a mapping between a numeric values and strings
*/ */
bool InitReadConstructions( uint32_t & numconstructions ); bool InitReadConstructions( uint32_t & numconstructions );
bool ReadConstruction(const int32_t &index, t_construction & construction); bool ReadConstruction(const int32_t index, t_construction & construction);
void FinishReadConstructions(); void FinishReadConstructions();
bool InitReadBuildings ( uint32_t & numbuildings ); bool InitReadBuildings ( uint32_t & numbuildings );
bool ReadBuilding(const int32_t &index, t_building & building); bool ReadBuilding(const int32_t index, t_building & building);
void FinishReadBuildings(); void FinishReadBuildings();
bool InitReadVegetation( uint32_t & numplants ); bool InitReadVegetation( uint32_t & numplants );
bool ReadVegetation(const int32_t &index, t_tree_desc & shrubbery); bool ReadVegetation(const int32_t index, t_tree_desc & shrubbery);
void FinishReadVegetation(); void FinishReadVegetation();
bool InitReadCreatures( uint32_t & numcreatures ); bool InitReadCreatures( uint32_t & numcreatures );
/// returns index of creature actually read or -1 if no creature can be found /// returns index of creature actually read or -1 if no creature can be found
int32_t ReadCreatureInBox(int32_t index, t_creature & furball, int32_t ReadCreatureInBox(const int32_t index, t_creature & furball,
const uint16_t &x1, const uint16_t &y1,const uint16_t &z1, const uint16_t x1, const uint16_t y1,const uint16_t z1,
const uint16_t &x2, const uint16_t &y2,const uint16_t &z2); const uint16_t x2, const uint16_t y2,const uint16_t z2);
bool ReadCreature(const int32_t &index, t_creature & furball); bool ReadCreature(const int32_t index, t_creature & furball);
void FinishReadCreatures(); void FinishReadCreatures();
void ReadRaw (const uint32_t &offset, const uint32_t &size, uint8_t *target); void ReadRaw (const uint32_t offset, const uint32_t size, uint8_t *target);
void WriteRaw (const uint32_t &offset, const uint32_t &size, uint8_t *source); void WriteRaw (const uint32_t offset, const uint32_t size, uint8_t *source);
bool InitViewAndCursor(); bool InitViewAndCursor();
bool InitReadNotes( uint32_t & numnotes ); bool InitReadNotes( uint32_t & numnotes );
bool ReadNote(const int32_t &index, t_note & note); bool ReadNote(const int32_t index, t_note & note);
void FinishReadNotes(); void FinishReadNotes();
bool InitReadSettlements( uint32_t & numsettlements ); bool InitReadSettlements( uint32_t & numsettlements );
bool ReadSettlement(const int32_t &index, t_settlement & settlement); bool ReadSettlement(const int32_t index, t_settlement & settlement);
bool ReadCurrentSettlement(t_settlement & settlement); bool ReadCurrentSettlement(t_settlement & settlement);
void FinishReadSettlements(); void FinishReadSettlements();
@ -194,10 +194,10 @@ namespace DFHack
bool ReadHotkeys(t_hotkey hotkeys[]); bool ReadHotkeys(t_hotkey hotkeys[]);
bool getViewCoords (int32_t &x, int32_t &y, int32_t &z); bool getViewCoords (int32_t &x, int32_t &y, int32_t &z);
bool setViewCoords (const int32_t &x, const int32_t &y, const int32_t &z); bool setViewCoords (const int32_t x, const int32_t y, const int32_t z);
bool getCursorCoords (int32_t &x, int32_t &y, int32_t &z); bool getCursorCoords (int32_t &x, int32_t &y, int32_t &z);
bool setCursorCoords (const int32_t &x, const int32_t &y, const int32_t &z); bool setCursorCoords (const int32_t x, const int32_t y, const int32_t z);
/// This returns false if there is nothing under the cursor, it puts the addresses in a vector if there is /// This returns false if there is nothing under the cursor, it puts the addresses in a vector if there is
bool getCurrentCursorCreatures(std::vector<uint32_t> &addresses); bool getCurrentCursorCreatures(std::vector<uint32_t> &addresses);
@ -208,9 +208,9 @@ namespace DFHack
bool setWindowSize(const int32_t & width, const int32_t & height); bool setWindowSize(const int32_t & width, const int32_t & height);
*/ */
void getItemIndexesInBox(std::vector<uint32_t> &indexes, bool getItemIndexesInBox(std::vector<uint32_t> &indexes,
const uint16_t &x1, const uint16_t &y1, const uint16_t &z1, const uint16_t x1, const uint16_t y1, const uint16_t z1,
const uint16_t &x2, const uint16_t &y2, const uint16_t &z2); const uint16_t x2, const uint16_t y2, const uint16_t z2);
/* /*
// FIXME: add a real creature class, move these // FIXME: add a real creature class, move these
string getLastName(const uint32_t &index, bool); string getLastName(const uint32_t &index, bool);
@ -228,10 +228,10 @@ namespace DFHack
std::string TranslateName(const t_squadname & squad, const std::map< std::string, std::vector< std::string > > &nameTable,const std::string & language="GENERIC"); std::string TranslateName(const t_squadname & squad, const std::map< std::string, std::vector< std::string > > &nameTable,const std::string & language="GENERIC");
std::string TranslateName (const int names[], int size, const std::map<std::string, std::vector<std::string> > &nameTable, const std::string & language="GENERIC"); std::string TranslateName (const int names[], int size, const std::map<std::string, std::vector<std::string> > &nameTable, const std::string & language="GENERIC");
void WriteLabors(const uint32_t &index, uint8_t labors[NUM_CREATURE_LABORS]); void WriteLabors(const uint32_t index, uint8_t labors[NUM_CREATURE_LABORS]);
bool InitReadItems(uint32_t & numitems); bool InitReadItems(uint32_t & numitems);
bool ReadItem(const uint32_t &index, t_item & item); bool ReadItem(const uint32_t index, t_item & item);
void FinishReadItems(); void FinishReadItems();
memory_info *getMemoryInfo(); memory_info *getMemoryInfo();

@ -1,27 +0,0 @@
%module dfhack
%include "std_string.i"
%include "std_vector.i"
%include "stdint.i"
%{
/* Includes the header in the wrapper code */
#define LINUX_BUILD
#define SWIG_WRAPPER
#include "DFTypes.h"
#include "DFHackAPI.h"
using namespace std;
using namespace DFHack;
%}
/* templates have to be instantiated for swig */
using namespace std;
namespace std
{
%template(MatglossVector) vector<DFHack::t_matgloss>;
%template(PlantMatglossVector) vector<DFHack::t_matglossPlant>;
%template(VeinVector) std::vector <DFHack::t_vein>;
%template(IceVeinVector) std::vector <DFHack::t_frozenliquidvein>;
}
/* Parse the header file to generate wrappers */
%include "DFTypes.h"
%include "DFHackAPI.h"

@ -0,0 +1,219 @@
%module pydfhack
%include "std_string.i"
%include "std_vector.i"
%include "std_map.i"
%include "stdint.i"
%include "typemaps.i"
/* This goes to the header of the wrapper code */
%{
#define LINUX_BUILD
#define SWIG_WRAPPER
#include "DFTypes.h"
#include "DFHackAPI.h"
using namespace std;
using namespace DFHack;
%}
/* make swig not react to the macro */
%define DFHACK_EXPORT
/* Parse the header file to generate wrappers */
%include "DFTypes.h"
/* templates have to be instantiated for swig */
%template(MatglossVector) std::vector<DFHack::t_matgloss>;
%template(PlantMatglossVector) std::vector<DFHack::t_matglossPlant>;
%template(VeinVector) std::vector <DFHack::t_vein>;
%template(IceVeinVector) std::vector <DFHack::t_frozenliquidvein>;
/*
This causes swig to generate BS uncompilable code
%template(GeologyAssign) std::vector < std::vector <uint16_t> >;
*/
/*
SWIG typemaps recipe
%apply double *OUTPUT { double *result }; // 'double *result' is to be treated as OUTPUT
%clear double *result; // Remove all typemaps for double *result
INPUT
int *INPUT
short *INPUT
long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
double *INPUT
float *INPUT
OUTPUT
int *OUTPUT
short *OUTPUT
long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
double *OUTPUT
float *OUTPUT
INOUT
int *INOUT
short *INOUT
long *INOUT
unsigned int *INOUT
unsigned short *INOUT
unsigned long *INOUT
double *INOUT
float *INOUT
*/
namespace DFHack
{
class memory_info;
class Process;
class API
{
class Private;
Private * const d;
public:
API(const std::string path_to_xml);
~API();
bool Attach();
bool Detach();
bool isAttached();
bool ReadPauseState();
bool ReadViewScreen(t_viewscreen &);
uint32_t ReadMenuState();
bool Suspend();
bool AsyncSuspend();
bool Resume();
bool ForceResume();
bool isSuspended();
bool ReadStoneMatgloss(std::vector<t_matgloss> & OUTPUT);
bool ReadWoodMatgloss (std::vector<t_matgloss> & OUTPUT);
bool ReadMetalMatgloss(std::vector<t_matgloss> & OUTPUT);
bool ReadPlantMatgloss(std::vector<t_matgloss> & OUTPUT);
/*
* SWIG reports that it can't wrap the second method here
*/
bool ReadPlantMatgloss (std::vector<t_matglossPlant> & OUTPUT);
bool ReadCreatureMatgloss(std::vector<t_matgloss> & OUTPUT);
bool ReadGeology( std::vector < std::vector <uint16_t> >& OUTPUT );
bool InitMap();
bool DestroyMap();
%apply uint32_t& OUTPUT { uint32_t& x };
%apply uint32_t& OUTPUT { uint32_t& y };
%apply uint32_t& OUTPUT { uint32_t& z };
void getSize(uint32_t& x, uint32_t& y, uint32_t& z);
bool isValidBlock(uint32_t blockx, uint32_t blocky, uint32_t blockz);
uint32_t getBlockPtr (uint32_t blockx, uint32_t blocky, uint32_t blockz);
bool ReadTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer);
bool WriteTileTypes(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint16_t *buffer);
bool ReadDesignations(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint32_t *buffer);
bool WriteDesignations (uint32_t blockx, uint32_t blocky, uint32_t blockz, uint32_t *buffer);
bool ReadOccupancy(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint32_t *buffer);
bool WriteOccupancy(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint32_t *buffer);
bool ReadDirtyBit(uint32_t blockx, uint32_t blocky, uint32_t blockz, bool &OUTPUT);
bool WriteDirtyBit(uint32_t blockx, uint32_t blocky, uint32_t blockz, bool dirtybit);
bool ReadRegionOffsets(uint32_t blockx, uint32_t blocky, uint32_t blockz, uint8_t *buffer);
bool ReadVeins(uint32_t blockx, uint32_t blocky, uint32_t blockz, std::vector <t_vein> & veins, std::vector <t_frozenliquidvein>& ices);
%apply uint32_t& OUTPUT { uint32_t & numObjs };
bool InitReadConstructions( uint32_t & numObjs );
bool ReadConstruction(const int32_t index, t_construction & OUTPUT);
void FinishReadConstructions();
bool InitReadBuildings ( uint32_t & numObjs );
bool ReadBuilding(const int32_t index, t_building & OUTPUT);
void FinishReadBuildings();
bool InitReadVegetation( uint32_t & numObjs );
bool ReadVegetation(const int32_t index, t_tree_desc & OUTPUT);
void FinishReadVegetation();
bool InitReadCreatures( uint32_t & numObjs );
int32_t ReadCreatureInBox(int32_t index, t_creature & OUTPUT,
const uint16_t x1, const uint16_t y1,const uint16_t z1,
const uint16_t x2, const uint16_t y2,const uint16_t z2);
bool ReadCreature(const int32_t index, t_creature & OUTPUT);
void FinishReadCreatures();
/*
A conundrum really.
*/
void ReadRaw (const uint32_t offset, const uint32_t size, uint8_t *target);
void WriteRaw (const uint32_t offset, const uint32_t size, uint8_t *source);
bool InitViewAndCursor();
bool InitReadNotes( uint32_t & numnotes );
bool ReadNote(const int32_t index, t_note & OUTPUT);
void FinishReadNotes();
bool InitReadSettlements( uint32_t & numObjs );
bool ReadSettlement(const int32_t index, t_settlement & OUTPUT);
bool ReadCurrentSettlement(t_settlement & settlement);
void FinishReadSettlements();
bool InitReadHotkeys( );
bool ReadHotkeys(t_hotkey hotkeys[]);
%apply int32_t& OUTPUT { int32_t& x };
%apply int32_t& OUTPUT { int32_t& y };
%apply int32_t& OUTPUT { int32_t& z };
bool getViewCoords (int32_t &x, int32_t &y, int32_t &z);
bool setViewCoords (const int32_t x, const int32_t y, const int32_t z);
bool getCursorCoords (int32_t &x, int32_t &y, int32_t &z);
bool setCursorCoords (const int32_t x, const int32_t y, const int32_t z);
bool getCurrentCursorCreatures(std::vector<uint32_t> &OUTPUT);
bool InitViewSize();
%apply int32_t& OUTPUT { int32_t& width };
%apply int32_t& OUTPUT { int32_t& height };
bool getWindowSize(int32_t & width, int32_t & height);
bool getItemIndexesInBox(std::vector<uint32_t> &indexes,
const uint16_t x1, const uint16_t y1, const uint16_t z1,
const uint16_t x2, const uint16_t y2, const uint16_t z2);
bool InitReadNameTables (std::map< std::string, std::vector<std::string> > & OUTPUT);
void FinishReadNameTables();
std::string TranslateName(const t_lastname & last, const std::map< std::string, std::vector< std::string > > &nameTable,const std::string & language="GENERIC");
std::string TranslateName(const t_squadname & squad, const std::map< std::string, std::vector< std::string > > &nameTable,const std::string & language="GENERIC");
std::string TranslateName (const int names[], int size, const std::map<std::string, std::vector<std::string> > &nameTable, const std::string & language="GENERIC");
void WriteLabors(const uint32_t index, uint8_t labors[NUM_CREATURE_LABORS]);
bool InitReadItems(uint32_t & numitems);
bool ReadItem(const uint32_t index, t_item & OUTPUT);
void FinishReadItems();
memory_info *getMemoryInfo();
Process * getProcess();
DFWindow * getWindow();
bool ReadItemTypes(std::vector< std::vector< t_itemType > > & itemTypes);
};
};
%enddef DFHACK_EXPORT

@ -3,7 +3,9 @@ x = dfhack.API("Memory.xml")
y = dfhack.MatglossVector() y = dfhack.MatglossVector()
if x.Attach(): if x.Attach():
if x.ReadStoneMatgloss(y): success,stones = x.ReadStoneMatgloss()
if success:
print "Dumping all stone" print "Dumping all stone"
for matgloss in y: for matgloss in stones:
print "ID %s, name %s" % (matgloss.id, matgloss.name) print "ID %s, name %s" % (matgloss.id, matgloss.name)
x.Detach()