2010-05-23 15:07:20 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
|
|
|
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
|
2010-05-23 15:07:20 -06:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
2011-04-10 02:19:15 -06:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstring>
|
|
|
|
using namespace std;
|
|
|
|
|
2011-06-12 15:17:40 -06:00
|
|
|
#include "dfhack/Process.h"
|
|
|
|
#include "dfhack/Context.h"
|
|
|
|
#include "dfhack/Error.h"
|
|
|
|
#include "dfhack/Module.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "private/ContextShared.h"
|
2011-03-18 01:53:59 -06:00
|
|
|
#include "private/ModuleFactory.h"
|
2010-05-23 15:07:20 -06:00
|
|
|
|
|
|
|
using namespace DFHack;
|
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
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()
|
|
|
|
{
|
2011-06-14 08:13:28 -06:00
|
|
|
//Detach();
|
2010-05-23 15:07:20 -06:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Context::isValid()
|
|
|
|
{
|
|
|
|
//FIXME: check for error states here
|
2010-05-24 20:16:24 -06:00
|
|
|
if(d->p->isIdentified())
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
/*
|
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())
|
|
|
|
{
|
2010-08-13 17:25:18 -06:00
|
|
|
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();
|
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
*/
|
2010-05-23 15:07:20 -06:00
|
|
|
bool Context::Suspend()
|
|
|
|
{
|
2011-06-12 15:17:40 -06:00
|
|
|
// return d->p->suspend();
|
2011-06-14 08:13:28 -06:00
|
|
|
return true;
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
|
|
|
bool Context::AsyncSuspend()
|
|
|
|
{
|
2011-06-12 15:17:40 -06:00
|
|
|
// return d->p->asyncSuspend();
|
2011-06-14 08:13:28 -06:00
|
|
|
return true;
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Context::Resume()
|
|
|
|
{
|
2010-11-17 12:50:50 -07:00
|
|
|
for(unsigned int i = 0 ; i < d->allModules.size(); i++)
|
2010-07-06 17:35:38 -06:00
|
|
|
{
|
|
|
|
d->allModules[i]->OnResume();
|
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
//return d->p->resume();
|
2011-06-14 08:13:28 -06:00
|
|
|
return true;
|
2010-05-23 15:07:20 -06:00
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
/*
|
2010-05-23 15:07:20 -06:00
|
|
|
bool Context::ForceResume()
|
|
|
|
{
|
2011-05-06 19:44:10 -06: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->forceresume();
|
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
*/
|
2010-05-23 15:07:20 -06:00
|
|
|
bool Context::isSuspended()
|
|
|
|
{
|
|
|
|
return d->p->isSuspended();
|
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
/*
|
2010-05-23 15:07:20 -06:00
|
|
|
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);
|
|
|
|
}
|
2011-06-12 15:17:40 -06:00
|
|
|
*/
|
2010-08-20 06:10:05 -06:00
|
|
|
VersionInfo *Context::getMemoryInfo()
|
2010-05-23 15:07:20 -06:00
|
|
|
{
|
|
|
|
return d->offset_descriptor;
|
|
|
|
}
|
2010-05-26 04:24:45 -06:00
|
|
|
|
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)\
|
|
|
|
{\
|
2011-03-18 01:53:59 -06:00
|
|
|
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(World);
|
|
|
|
MODULE_GETTER(Materials);
|
|
|
|
MODULE_GETTER(Items);
|
|
|
|
MODULE_GETTER(Translation);
|
|
|
|
MODULE_GETTER(Vegetation);
|
|
|
|
MODULE_GETTER(Buildings);
|
|
|
|
MODULE_GETTER(Constructions);
|