dfhack/library/DFContext.cpp

169 lines
3.7 KiB
C++

2010-05-23 15:07:20 -06:00
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
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.
*/
#include "Internal.h"
2010-05-23 15:07:20 -06:00
2010-05-25 22:48:23 -06:00
#include "dfhack/DFProcess.h"
#include "dfhack/DFProcessEnumerator.h"
#include "dfhack/DFContext.h"
#include "dfhack/DFError.h"
#include "dfhack/DFModule.h"
2010-05-23 15:07:20 -06:00
#include "private/ContextShared.h"
#include "private/ModuleFactory.h"
2010-05-23 15:07:20 -06:00
using namespace DFHack;
Context::Context (Process* p) : d (new DFContextShared())
2010-05-23 15:07:20 -06:00
{
d->p = p;
d->offset_descriptor = p->getDescriptor();
d->shm_start = 0;
}
Context::~Context()
{
Detach();
delete d;
}
bool Context::isValid()
{
//FIXME: check for error states here
if(d->p->isIdentified())
return true;
return false;
2010-05-23 15:07:20 -06:00
}
bool Context::Attach()
{
if (!d->p->attach())
{
//throw Error::CantAttach();
return false;
}
d->shm_start = d->p->getSHMStart();
// process is attached, everything went just fine... hopefully
return true;
}
bool Context::Detach()
{
if (!d->p->detach())
{
cerr << "Context::Detach failed!" << endl;
2010-05-23 15:07:20 -06:00
return false;
}
d->shm_start = 0;
// invalidate all modules
2010-11-17 12:50:50 -07:00
for(unsigned int i = 0 ; i < d->allModules.size(); i++)
2010-06-24 23:11:26 -06:00
{
delete d->allModules[i];
}
d->allModules.clear();
memset(&(d->s_mods), 0, sizeof(d->s_mods));
2010-05-23 15:07:20 -06:00
return true;
}
bool Context::isAttached()
{
return d->p->isAttached();
}
bool Context::Suspend()
{
return d->p->suspend();
}
bool Context::AsyncSuspend()
{
return d->p->asyncSuspend();
}
bool Context::Resume()
{
2010-11-17 12:50:50 -07:00
for(unsigned int i = 0 ; i < d->allModules.size(); i++)
{
d->allModules[i]->OnResume();
}
2010-05-23 15:07:20 -06:00
return d->p->resume();
}
bool Context::ForceResume()
{
return d->p->forceresume();
}
bool Context::isSuspended()
{
return d->p->isSuspended();
}
void Context::ReadRaw (const uint32_t offset, const uint32_t size, uint8_t *target)
{
d->p->read (offset, size, target);
}
void Context::WriteRaw (const uint32_t offset, const uint32_t size, uint8_t *source)
{
d->p->write (offset, size, source);
}
VersionInfo *Context::getMemoryInfo()
2010-05-23 15:07:20 -06:00
{
return d->offset_descriptor;
}
2010-05-23 15:07:20 -06:00
Process * Context::getProcess()
{
return d->p;
}
/*******************************************************************************
M O D U L E S
*******************************************************************************/
2010-06-24 23:11:26 -06:00
#define MODULE_GETTER(TYPE) \
TYPE * Context::get##TYPE() \
{ \
if(!d->s_mods.p##TYPE)\
{\
Module * mod = create##TYPE(d);\
d->s_mods.p##TYPE = (TYPE *) mod;\
d->allModules.push_back(mod);\
2010-06-24 23:11:26 -06:00
}\
return d->s_mods.p##TYPE;\
}
MODULE_GETTER(Creatures);
2011-04-02 16:15:47 -06:00
MODULE_GETTER(Engravings);
2010-06-24 23:11:26 -06:00
MODULE_GETTER(Maps);
MODULE_GETTER(Gui);
MODULE_GETTER(WindowIO);
MODULE_GETTER(World);
MODULE_GETTER(Materials);
MODULE_GETTER(Items);
MODULE_GETTER(Translation);
MODULE_GETTER(Vegetation);
MODULE_GETTER(Buildings);
MODULE_GETTER(Constructions);