sprinkled some exceptions over the memory reading

develop
mizipzor 2010-02-28 03:34:54 +01:00
parent 2b1940430f
commit 6be18fbf20
4 changed files with 149 additions and 212 deletions

@ -85,6 +85,7 @@ namespace DFHack
#include "DFMemInfoManager.h"
#include "DFVector.h"
#include "DFMemInfo.h"
#include "DFError.h"
#include <stdlib.h>
#include "tinyxml/tinyxml.h"

@ -26,6 +26,7 @@ distribution.
#define ERROR_H_INCLUDED
#include <string>
#include <sstream>
#include <exception>
namespace DFHack
@ -72,16 +73,39 @@ namespace DFHack
}
};
// a call to DFHack::mem_info::getAdress() failed
class MissingAddress : public exception
// a call to DFHack::mem_info::get* failed
class MissingMemoryDefinition : public exception
{
public:
MissingAddress(const char* _address = "UNKNOWN") : address(_address) {}
const std::string address;
MissingMemoryDefinition(const char* _type, const char* _key) : type(_type), key(_key) {}
// Used by functios using integer keys, such as getTrait
MissingMemoryDefinition(const char* _type, uint32_t _key) : type(_type)
{
// FIXME fancy hex printer goes here
std::stringstream s;
s << _key;
key = s.str();
}
// (perhaps it should be an enum, but this is intended for easy printing/logging)
// type can be any of the following:
//
// address
// offset
// hexvalue
// string
// profession
// job
// skill
// trait
// traitname
// labor
const std::string type;
std::string key;
virtual const char* what() const throw()
{
return "memory address missing";
return "memory definition missing";
}
};
}

@ -23,7 +23,6 @@ distribution.
*/
#include "DFCommonInternal.h"
#include "DFError.h"
using namespace DFHack;
/*
@ -659,23 +658,6 @@ bool API::ReadGeology (vector < vector <uint16_t> >& assign)
uint32_t regionX, regionY, regionZ;
uint16_t worldSizeX, worldSizeY;
// check if we have 'em all
if (
! (
region_x_offset && region_y_offset && region_z_offset && world_size_x && world_size_y
&& world_offset && world_regions_offset && world_geoblocks_offset && region_size
&& region_geo_index_offset && geolayer_geoblock_offset
)
)
{
// FIXME should probably be moved into getAdress or getOffset for easy storing of
// the missing definition
throw Error::MissingAddress();
// fail if we don't have them
//return false;
}
// read position of the region inside DF world
g_pProcess->readDWord (region_x_offset, regionX);
g_pProcess->readDWord (region_y_offset, regionY);
@ -744,21 +726,10 @@ bool API::ReadGeology (vector < vector <uint16_t> >& assign)
bool API::InitReadBuildings ( uint32_t& numbuildings )
{
int buildings = d->offset_descriptor->getAddress ("buildings");
if(buildings)
{
d->buildingsInited = true;
d->p_bld = new DfVector (d->p->readVector (buildings, 4));
numbuildings = d->p_bld->getSize();
return true;
}
else
{
d->buildingsInited = false;
numbuildings = 0;
throw Error::MissingAddress("buildings");
//return false;
}
}
@ -809,21 +780,10 @@ void API::FinishReadBuildings()
bool API::InitReadConstructions(uint32_t & numconstructions)
{
int constructions = d->offset_descriptor->getAddress ("constructions");
if(constructions)
{
d->p_cons = new DfVector (d->p->readVector (constructions, 4));
d->constructionsInited = true;
numconstructions = d->p_cons->getSize();
return true;
}
else
{
d->constructionsInited = false;
numconstructions = 0;
throw Error::MissingAddress("constructions");
//return false;
}
}
@ -861,22 +821,21 @@ void API::FinishReadConstructions()
bool API::InitReadVegetation(uint32_t & numplants)
{
try
{
int vegetation = d->offset_descriptor->getAddress ("vegetation");
d->tree_offset = d->offset_descriptor->getOffset ("tree_desc_offset");
if(vegetation && d->tree_offset)
{
d->vegetationInited = true;
d->p_veg = new DfVector (d->p->readVector (vegetation, 4));
numplants = d->p_veg->getSize();
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->vegetationInited = false;
numplants = 0;
throw Error::MissingAddress();
//return false;
throw;
}
}
@ -908,6 +867,8 @@ void API::FinishReadVegetation()
bool API::InitReadCreatures( uint32_t &numcreatures )
{
try
{
memory_info * minfo = d->offset_descriptor;
int creatures = d->offset_descriptor->getAddress ("creatures");
d->creature_pos_offset = minfo->getOffset ("creature_position");
@ -934,47 +895,24 @@ bool API::InitReadCreatures( uint32_t &numcreatures )
d->creature_happiness_offset = minfo->getOffset ("creature_happiness");
d->creature_traits_offset = minfo->getOffset ("creature_traits");
d->creature_likes_offset = minfo->getOffset("creature_likes");
if (creatures
&& d->creature_pos_offset
&& d->creature_type_offset
&& d->creature_flags1_offset
&& d->creature_flags2_offset
&& d->creature_nick_name_offset
&& d->creature_custom_profession_offset
&& d->creature_profession_offset
&& d->creature_sex_offset
&& d->creature_id_offset
&& d->creature_squad_name_offset
&& d->creature_squad_leader_id_offset
&& d->creature_money_offset
&& d->creature_current_job_offset
&& d->creature_strength_offset
&& d->creature_agility_offset
&& d->creature_toughness_offset
&& d->creature_skills_offset
&& d->creature_labors_offset
&& d->creature_happiness_offset
&& d->creature_traits_offset
// && d->creature_likes_offset
)
{
d->p_cre = new DfVector (d->p->readVector (creatures, 4));
//InitReadNameTables();
d->creaturesInited = true;
numcreatures = d->p_cre->getSize();
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->creaturesInited = false;
numcreatures = 0;
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::InitReadNotes( uint32_t &numnotes )
{
try
{
memory_info * minfo = d->offset_descriptor;
int notes = minfo->getAddress ("notes");
d->note_foreground_offset = minfo->getOffset ("note_foreground");
@ -982,25 +920,16 @@ bool API::InitReadNotes( uint32_t &numnotes )
d->note_name_offset = minfo->getOffset ("note_name");
d->note_xyz_offset = minfo->getOffset ("note_xyz");
if (notes
&& d->note_foreground_offset
&& d->note_background_offset
&& d->note_name_offset
&& d->note_xyz_offset
)
{
d->p_notes = new DfVector (d->p->readVector (notes, 4));
d->notesInited = true;
numnotes = d->p_notes->getSize();
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->notesInited = false;
numnotes = 0;
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::ReadNote (const int32_t index, t_note & note)
@ -1017,6 +946,8 @@ bool API::ReadNote (const int32_t index, t_note & note)
}
bool API::InitReadSettlements( uint32_t & numsettlements )
{
try
{
memory_info * minfo = d->offset_descriptor;
int allSettlements = minfo->getAddress ("settlements");
int currentSettlement = minfo->getAddress("settlement_current");
@ -1024,25 +955,17 @@ bool API::InitReadSettlements( uint32_t & numsettlements )
d->settlement_world_xy_offset = minfo->getOffset ("settlement_world_xy");
d->settlement_local_xy_offset = minfo->getOffset ("settlement_local_xy");
if (allSettlements && currentSettlement
&& d->settlement_name_offset
&& d->settlement_world_xy_offset
&& d->settlement_local_xy_offset
)
{
d->p_settlements = new DfVector (d->p->readVector (allSettlements, 4));
d->p_current_settlement = new DfVector(d->p->readVector(currentSettlement,4));
d->settlementsInited = true;
numsettlements = d->p_settlements->getSize();
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->settlementsInited = false;
numsettlements = 0;
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::ReadSettlement(const int32_t index, t_settlement & settlement)
@ -1058,6 +981,7 @@ bool API::ReadSettlement(const int32_t index, t_settlement & settlement)
g_pProcess->read(temp + d->settlement_local_xy_offset, 4 * sizeof(int16_t), (uint8_t *) &settlement.local_x1);
return true;
}
bool API::ReadCurrentSettlement(t_settlement & settlement)
{
if(!d->settlementsInited) return false;
@ -1089,23 +1013,21 @@ void API::FinishReadSettlements()
bool API::InitReadHotkeys( )
{
try
{
memory_info * minfo = d->offset_descriptor;
d->hotkey_start = minfo->getAddress("hotkey_start");
d->hotkey_mode_offset = minfo->getOffset ("hotkey_mode");
d->hotkey_xyz_offset = minfo->getOffset("hotkey_xyz");
d->hotkey_size = minfo->getHexValue("hotkey_size");
if (d->hotkey_start && d->hotkey_mode_offset && d->hotkey_size)
{
d->hotkeyInited = true;
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->hotkeyInited = false;
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::ReadHotkeys(t_hotkey hotkeys[])
@ -1263,12 +1185,12 @@ void API::WriteLabors(const uint32_t index, uint8_t labors[NUM_CREATURE_LABORS])
bool API::InitReadNameTables (map< string, vector<string> > & nameTable)
{
try
{
int genericAddress = d->offset_descriptor->getAddress ("language_vector");
int transAddress = d->offset_descriptor->getAddress ("translation_vector");
int word_table_offset = d->offset_descriptor->getOffset ("word_table");
if(genericAddress && transAddress && word_table_offset)
{
DfVector genericVec (d->p->readVector (genericAddress, 4));
DfVector transVec (d->p->readVector (transAddress, 4));
@ -1294,12 +1216,10 @@ bool API::InitReadNameTables (map< string, vector<string> > & nameTable)
d->nameTablesInited = true;
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->nameTablesInited = false;
throw Error::MissingAddress();
//return false;
throw;
}
}
@ -1482,6 +1402,8 @@ void API::WriteRaw (const uint32_t offset, const uint32_t size, uint8_t *source)
bool API::InitViewAndCursor()
{
try
{
d->window_x_offset = d->offset_descriptor->getAddress ("window_x");
d->window_y_offset = d->offset_descriptor->getAddress ("window_y");
d->window_z_offset = d->offset_descriptor->getAddress ("window_z");
@ -1492,32 +1414,27 @@ bool API::InitViewAndCursor()
d->pause_state_offset = d->offset_descriptor->getAddress ("pause_state");
d->view_screen_offset = d->offset_descriptor->getAddress ("view_screen");
if (d->window_x_offset && d->window_y_offset && d->window_z_offset &&
d->current_cursor_creature_offset && d->current_menu_state_offset &&
d->pause_state_offset && d->view_screen_offset)
{
d->cursorWindowInited = true;
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::InitViewSize()
{
d->window_dims_offset = d->offset_descriptor->getAddress ("window_dims");
if (d->window_dims_offset)
try
{
d->window_dims_offset = d->offset_descriptor->getAddress ("window_dims");
d->viewSizeInited = true;
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
throw Error::MissingAddress();
//return false;
throw;
}
}
@ -1595,23 +1512,21 @@ DFWindow * API::getWindow()
bool API::InitReadItems(uint32_t & numitems)
{
try
{
int items = d->offset_descriptor->getAddress ("items");
d->item_material_offset = d->offset_descriptor->getOffset ("item_materials");
if(items && d->item_material_offset)
{
d->p_itm = new DfVector (d->p->readVector (items, 4));
d->itemsInited = true;
numitems = d->p_itm->getSize();
return true;
}
else
catch (Error::MissingMemoryDefinition&)
{
d->itemsInited = false;
numitems = 0;
throw Error::MissingAddress();
//return false;
throw;
}
}
bool API::ReadItem (const uint32_t index, t_item & item)

@ -450,7 +450,7 @@ uint32_t memory_info::getAddress (const char *key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("address", key);
}
@ -462,7 +462,7 @@ uint32_t memory_info::getOffset (const char *key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("offset", key);
}
// Get named numerical value
@ -473,7 +473,7 @@ uint32_t memory_info::getHexValue (const char *key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("hexvalue", key);
}
@ -486,7 +486,7 @@ uint32_t memory_info::getAddress (const string &key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("address", key.c_str());
}
@ -498,7 +498,7 @@ uint32_t memory_info::getOffset (const string &key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("offset", key.c_str());
}
// Get named numerical value
@ -509,7 +509,7 @@ uint32_t memory_info::getHexValue (const string &key)
{
return (*iter).second;
}
return 0;
throw Error::MissingMemoryDefinition("hexvalue", key.c_str());
}
// Get named string
@ -520,7 +520,7 @@ std::string memory_info::getString (const string &key)
{
return (*iter).second;
}
return string("");
throw Error::MissingMemoryDefinition("string", key.c_str());
}
// Get Profession
@ -530,10 +530,7 @@ string memory_info::getProfession (const uint32_t key) const
{
return d->professions[key];
}
else
{
return string("");
}
throw Error::MissingMemoryDefinition("profession", key);
}
// Get Job
@ -543,7 +540,7 @@ string memory_info::getJob (const uint32_t key) const
{
return d->jobs[key];
}
return string("Job Does Not Exist");
throw Error::MissingMemoryDefinition("job", key);
}
string memory_info::getSkill (const uint32_t key) const
@ -552,7 +549,7 @@ string memory_info::getSkill (const uint32_t key) const
{
return d->skills[key];
}
return string("Skill is not Defined");
throw Error::MissingMemoryDefinition("skill", key);
}
// FIXME: ugly hack that needs to die
@ -585,7 +582,7 @@ string memory_info::getTrait (const uint32_t traitIdx, const uint32_t traitValue
else
return d->traits[traitIdx][0];
}
return string("Trait is not Defined");
throw Error::MissingMemoryDefinition("trait", traitIdx);
}
string memory_info::getTraitName(const uint32_t traitIdx) const
@ -594,7 +591,7 @@ string memory_info::getTraitName(const uint32_t traitIdx) const
{
return d->traits[traitIdx][d->traits[traitIdx].size()-1];
}
return string("Trait is not Defined");
throw Error::MissingMemoryDefinition("traitname", traitIdx);
}
string memory_info::getLabor (const uint32_t laborIdx)
@ -603,7 +600,7 @@ string memory_info::getLabor (const uint32_t laborIdx)
{
return d->labors[laborIdx];
}
return string("");
throw Error::MissingMemoryDefinition("labor", laborIdx);
}
// Reset everything