Added a common exception for DFHack, made World and Position use it for feature detection.

develop
Petr Mrázek 2010-09-01 18:03:52 +02:00
parent 988b1d8692
commit f1307d9f13
4 changed files with 52 additions and 33 deletions

@ -1596,9 +1596,9 @@
<Group name="Position"> <Group name="Position">
<Address name="screen_tiles_pointer" value="0x18313D0" /> <Address name="screen_tiles_pointer" value="0x18313D0" />
</Group> </Group>
<Group name="World"> <Group name="World">
<Address name="current_weather" value="0x14BCDEE" /> <Address name="current_weather" value="0x14BCDEE" />
</Group> </Group>
</Offsets> </Offsets>
</Version> </Version>

@ -34,7 +34,12 @@ namespace DFHack
{ {
namespace Error namespace Error
{ {
class DFHACK_EXPORT NoProcess : public std::exception /*
* our wrapper for the C++ exception. used to differentiate
* the whole array of DFHack exceptions from the rest
*/
class DFHACK_EXPORT All : public std::exception{};
class DFHACK_EXPORT NoProcess : public All
{ {
public: public:
virtual const char* what() const throw() virtual const char* what() const throw()
@ -43,7 +48,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT CantAttach : public std::exception class DFHACK_EXPORT CantAttach : public All
{ {
public: public:
virtual const char* what() const throw() virtual const char* what() const throw()
@ -52,7 +57,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT NoMapLoaded : public std::exception class DFHACK_EXPORT NoMapLoaded : public All
{ {
public: public:
virtual const char* what() const throw() virtual const char* what() const throw()
@ -61,7 +66,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT BadMapDimensions : public std::exception class DFHACK_EXPORT BadMapDimensions : public All
{ {
public: public:
BadMapDimensions(uint32_t& _x, uint32_t& _y) : x(_x), y(_y) {} BadMapDimensions(uint32_t& _x, uint32_t& _y) : x(_x), y(_y) {}
@ -75,7 +80,7 @@ namespace DFHack
}; };
// a call to DFHack::mem_info::get* failed // a call to DFHack::mem_info::get* failed
class DFHACK_EXPORT MissingMemoryDefinition : public std::exception class DFHACK_EXPORT MissingMemoryDefinition : public All
{ {
public: public:
MissingMemoryDefinition(const char* _type, const std::string _key) : type(_type), key(_key) MissingMemoryDefinition(const char* _type, const std::string _key) : type(_type), key(_key)
@ -108,7 +113,7 @@ namespace DFHack
}; };
// a call to DFHack::mem_info::get* failed // a call to DFHack::mem_info::get* failed
class DFHACK_EXPORT UnsetMemoryDefinition : public std::exception class DFHACK_EXPORT UnsetMemoryDefinition : public All
{ {
public: public:
UnsetMemoryDefinition(const char* _type, const std::string _key) : type(_type), key(_key) UnsetMemoryDefinition(const char* _type, const std::string _key) : type(_type), key(_key)
@ -141,7 +146,7 @@ namespace DFHack
}; };
// Syntax errors and whatnot, the xml cant be read // Syntax errors and whatnot, the xml cant be read
class DFHACK_EXPORT MemoryXmlParse : public std::exception class DFHACK_EXPORT MemoryXmlParse : public All
{ {
public: public:
MemoryXmlParse(const char* _desc, int _id, int _row, int _col) MemoryXmlParse(const char* _desc, int _id, int _row, int _col)
@ -167,7 +172,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT MemoryXmlBadAttribute : public std::exception class DFHACK_EXPORT MemoryXmlBadAttribute : public All
{ {
public: public:
MemoryXmlBadAttribute(const char* _attr) : attr(_attr) MemoryXmlBadAttribute(const char* _attr) : attr(_attr)
@ -187,7 +192,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT MemoryXmlNoRoot : public std::exception class DFHACK_EXPORT MemoryXmlNoRoot : public All
{ {
public: public:
MemoryXmlNoRoot() {} MemoryXmlNoRoot() {}
@ -200,7 +205,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT MemoryXmlNoDFExtractor : public std::exception class DFHACK_EXPORT MemoryXmlNoDFExtractor : public All
{ {
public: public:
MemoryXmlNoDFExtractor(const char* _name) : name(_name) MemoryXmlNoDFExtractor(const char* _name) : name(_name)
@ -220,7 +225,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT MemoryXmlUnderspecifiedEntry : public std::exception class DFHACK_EXPORT MemoryXmlUnderspecifiedEntry : public All
{ {
public: public:
MemoryXmlUnderspecifiedEntry(const char * _where) : where(_where) MemoryXmlUnderspecifiedEntry(const char * _where) : where(_where)
@ -238,7 +243,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT MemoryXmlUnknownType : public std::exception class DFHACK_EXPORT MemoryXmlUnknownType : public All
{ {
public: public:
MemoryXmlUnknownType(const char* _type) : type(_type) MemoryXmlUnknownType(const char* _type) : type(_type)
@ -258,7 +263,7 @@ namespace DFHack
} }
}; };
class DFHACK_EXPORT SHMServerDisappeared : public std::exception class DFHACK_EXPORT SHMServerDisappeared : public All
{ {
public: public:
SHMServerDisappeared(){} SHMServerDisappeared(){}
@ -268,7 +273,7 @@ namespace DFHack
return "The server process has disappeared"; return "The server process has disappeared";
} }
}; };
class DFHACK_EXPORT SHMLockingError : public std::exception class DFHACK_EXPORT SHMLockingError : public All
{ {
public: public:
SHMLockingError(const char* _type) : type(_type) SHMLockingError(const char* _type) : type(_type)
@ -287,7 +292,7 @@ namespace DFHack
return full.c_str(); return full.c_str();
} }
}; };
class DFHACK_EXPORT MemoryAccessDenied : public std::exception class DFHACK_EXPORT MemoryAccessDenied : public All
{ {
public: public:
MemoryAccessDenied() {} MemoryAccessDenied() {}
@ -297,7 +302,7 @@ namespace DFHack
return "SHM ACCESS DENIED"; return "SHM ACCESS DENIED";
} }
}; };
class DFHACK_EXPORT SHMVersionMismatch : public std::exception class DFHACK_EXPORT SHMVersionMismatch : public All
{ {
public: public:
SHMVersionMismatch() {} SHMVersionMismatch() {}
@ -307,7 +312,7 @@ namespace DFHack
return "SHM VERSION MISMATCH"; return "SHM VERSION MISMATCH";
} }
}; };
class DFHACK_EXPORT SHMAttachFailure : public std::exception class DFHACK_EXPORT SHMAttachFailure : public All
{ {
public: public:
SHMAttachFailure() {} SHMAttachFailure() {}
@ -317,7 +322,7 @@ namespace DFHack
return "SHM ATTACH FAILURE"; return "SHM ATTACH FAILURE";
} }
}; };
class DFHACK_EXPORT ModuleNotInitialized : public std::exception class DFHACK_EXPORT ModuleNotInitialized : public All
{ {
public: public:
ModuleNotInitialized() {} ModuleNotInitialized() {}

@ -27,6 +27,7 @@ distribution.
#include "dfhack/modules/Position.h" #include "dfhack/modules/Position.h"
#include "dfhack/VersionInfo.h" #include "dfhack/VersionInfo.h"
#include "dfhack/DFProcess.h" #include "dfhack/DFProcess.h"
#include "dfhack/DFError.h"
using namespace DFHack; using namespace DFHack;
struct Position::Private struct Position::Private
@ -72,7 +73,7 @@ Position::Position(DFContextShared * d_)
d->window_dims_offset = OG_Position->getAddress ("window_dims"); d->window_dims_offset = OG_Position->getAddress ("window_dims");
d->Started = true; d->Started = true;
} }
catch(exception &){}; catch(Error::All &){};
try try
{ {
OffsetGroup * OG_Hotkeys = mem->getGroup("Hotkeys"); OffsetGroup * OG_Hotkeys = mem->getGroup("Hotkeys");
@ -82,13 +83,13 @@ Position::Position(DFContextShared * d_)
d->hotkey_size = OG_Hotkeys->getHexValue("size"); d->hotkey_size = OG_Hotkeys->getHexValue("size");
d->StartedHotkeys = true; d->StartedHotkeys = true;
} }
catch(exception &){}; catch(Error::All &){};
try try
{ {
d->screen_tiles_ptr_offset = OG_Position->getAddress ("screen_tiles_pointer"); d->screen_tiles_ptr_offset = OG_Position->getAddress ("screen_tiles_pointer");
d->StartedScreen = true; d->StartedScreen = true;
} }
catch(exception &){}; catch(Error::All &){};
} }
Position::~Position() Position::~Position()

@ -40,13 +40,15 @@ FIXME: Japa said that he had to do this with the time stuff he got from here
#include "dfhack/DFProcess.h" #include "dfhack/DFProcess.h"
#include "dfhack/VersionInfo.h" #include "dfhack/VersionInfo.h"
#include "dfhack/DFTypes.h" #include "dfhack/DFTypes.h"
#include "dfhack/DFError.h"
using namespace DFHack; using namespace DFHack;
struct World::Private struct World::Private
{ {
bool Inited; bool Inited;
bool Started; bool StartedTime;
bool StartedWeather;
uint32_t year_offset; uint32_t year_offset;
uint32_t tick_offset; uint32_t tick_offset;
uint32_t weather_offset; uint32_t weather_offset;
@ -60,12 +62,23 @@ World::World(DFContextShared * _d)
d = new Private; d = new Private;
d->d = _d; d->d = _d;
d->owner = _d->p; d->owner = _d->p;
d->Inited = d->StartedTime = d->StartedWeather = false;
OffsetGroup * OG_World = d->d->offset_descriptor->getGroup("World"); OffsetGroup * OG_World = d->d->offset_descriptor->getGroup("World");
d->year_offset = OG_World->getAddress( "current_year" ); try
d->tick_offset = OG_World->getAddress( "current_tick" ); {
d->weather_offset = OG_World->getAddress( "current_weather" ); d->year_offset = OG_World->getAddress( "current_year" );
d->Inited = d->Started = true; d->tick_offset = OG_World->getAddress( "current_tick" );
d->StartedTime = true;
}
catch(Error::All &){};
try
{
d->weather_offset = OG_World->getAddress( "current_weather" );
d->StartedWeather = true;
}
catch(Error::All &){};
d->Inited = true;
} }
World::~World() World::~World()
@ -85,14 +98,14 @@ bool World::Finish()
uint32_t World::ReadCurrentYear() uint32_t World::ReadCurrentYear()
{ {
if(d->Inited) if(d->Inited && d->StartedTime)
return(d->owner->readDWord(d->year_offset)); return(d->owner->readDWord(d->year_offset));
return 0; return 0;
} }
uint32_t World::ReadCurrentTick() uint32_t World::ReadCurrentTick()
{ {
if(d->Inited) if(d->Inited && d->StartedTime)
return(d->owner->readDWord(d->tick_offset)); return(d->owner->readDWord(d->tick_offset));
return 0; return 0;
} }
@ -119,13 +132,13 @@ uint32_t World::ReadCurrentDay()
uint8_t World::ReadCurrentWeather() uint8_t World::ReadCurrentWeather()
{ {
if (d->Inited) if (d->Inited && d->StartedWeather)
return(d->owner->readByte(d->weather_offset)); return(d->owner->readByte(d->weather_offset));
return 0; return 0;
} }
void World::SetCurrentWeather(uint8_t weather) void World::SetCurrentWeather(uint8_t weather)
{ {
if (d->Inited) if (d->Inited && d->StartedWeather)
d->owner->writeByte(d->weather_offset,weather); d->owner->writeByte(d->weather_offset,weather);
} }