dfhack/library/Core.cpp

276 lines
7.2 KiB
C++

/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
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"
#include <string>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
using namespace std;
#include "dfhack/Error.h"
#include "dfhack/Process.h"
#include "dfhack/Core.h"
2011-06-22 00:14:21 -06:00
#include "dfhack/Console.h"
#include "dfhack/VersionInfoFactory.h"
#include "ModuleFactory.h"
#include "dfhack/modules/Gui.h"
#include "dfhack/modules/Vegetation.h"
#include "dfhack/modules/Maps.h"
#include "dfhack/modules/World.h"
#include "dfhack/extra/rlutil.h"
#include <stdio.h>
2011-06-19 20:29:38 -06:00
#ifdef LINUX_BUILD
#include <dirent.h>
#include <errno.h>
#else
#include "wdirent.h"
#endif
using namespace DFHack;
2011-06-19 20:29:38 -06:00
static int getdir (string dir, vector<string> &files)
{
2011-06-19 20:29:38 -06:00
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
2011-06-22 00:14:21 -06:00
dfout << "Error(" << errno << ") opening " << dir << endl;
2011-06-19 20:29:38 -06:00
return errno;
}
2011-06-19 20:29:38 -06:00
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
2011-06-19 20:29:38 -06:00
closedir(dp);
return 0;
}
2011-06-19 20:29:38 -06:00
int fIOthread(void * _core)
{
Core * core = (Core *) _core;
#ifdef LINUX_BUILD
string path = core->p->getPath() + "/plugins/";
const char * searchstr = ".plug.so";
#else
string path = core->p->getPath() + "\\plugins\\";
const char * searchstr = ".plug.dll";
#endif
2011-06-19 20:29:38 -06:00
vector <string> filez;
map <string, int (*)(Core *)> plugins;
getdir(path, filez);
2011-06-19 20:29:38 -06:00
const char * (*_PlugName)(void) = 0;
int (*_PlugRun)(Core *) = 0;
for(int i = 0; i < filez.size();i++)
{
if(strstr(filez[i].c_str(),searchstr))
{
string fullpath = path + filez[i];
DFLibrary * plug = OpenPlugin(fullpath.c_str());
2011-06-19 20:29:38 -06:00
if(!plug)
{
2011-06-22 00:14:21 -06:00
dfout << "Can't load plugin " << filez[i] << endl;
2011-06-19 20:29:38 -06:00
continue;
}
2011-06-19 20:29:38 -06:00
_PlugName = (const char * (*)()) LookupPlugin(plug, "plugin_name");
if(!_PlugName)
{
2011-06-22 00:14:21 -06:00
dfout << "Plugin " << filez[i] << " has no name." << endl;
2011-06-19 20:29:38 -06:00
ClosePlugin(plug);
continue;
}
_PlugRun = (int (*)(Core * c)) LookupPlugin(plug, "plugin_run");
if(!_PlugRun)
{
2011-06-22 00:14:21 -06:00
dfout << "Plugin " << filez[i] << " has no run function." << endl;
2011-06-19 20:29:38 -06:00
ClosePlugin(plug);
continue;
}
dfout << "Loaded plugin " << filez[i] << endl;
2011-06-19 20:29:38 -06:00
plugins[string(_PlugName())] = _PlugRun;
}
}
2011-06-22 00:14:21 -06:00
fprintf(dfout_C,"DFHack is ready. Have a nice day! Type in '?' or 'help' for help.\n");
//dfterm << << endl;
int clueless_counter = 0;
while (true)
{
string command = "";
2011-06-22 00:14:21 -06:00
dfout <<"[DFHack]# ";
getline(cin, command);
2011-06-22 00:14:21 -06:00
if (cin.eof())
{
command = "q";
2011-06-22 00:14:21 -06:00
dfout << std::endl; // No newline from the user here!
}
if(command=="help" || command == "?")
{
2011-06-22 00:14:21 -06:00
dfout << "Available commands:" << endl;
2011-06-19 20:29:38 -06:00
for (map <string, int (*)(Core *)>::iterator iter = plugins.begin(); iter != plugins.end(); iter++)
{
2011-06-22 00:14:21 -06:00
dfout << iter->first << endl;
2011-06-19 20:29:38 -06:00
}
}
2011-06-22 00:14:21 -06:00
else if( command == "" )
{
clueless_counter++;
}
else
{
2011-06-19 20:29:38 -06:00
map <string, int (*)(Core *)>::iterator iter = plugins.find(command);
if(iter != plugins.end())
{
iter->second(core);
}
else
{
2011-06-22 00:14:21 -06:00
dfout << "Invalid command." << endl;
clueless_counter ++;
2011-06-19 20:29:38 -06:00
}
}
2011-06-22 00:14:21 -06:00
if(clueless_counter == 3)
{
dfout << "Do 'help' or '?' for the list of available commands." << endl;
clueless_counter = 0;
}
}
}
Core::Core()
{
2011-06-22 00:14:21 -06:00
// init the console. This must be always the first step!
con = new Console();
// find out what we are...
vif = new DFHack::VersionInfoFactory("Memory.xml");
p = new DFHack::Process(vif);
if (!p->isIdentified())
{
2011-06-22 00:14:21 -06:00
dfout << "Couldn't identify this version of DF." << std::endl;
errorstate = true;
delete p;
p = NULL;
return;
}
vinfo = p->getDescriptor();
// init module storage
allModules.clear();
memset(&(s_mods), 0, sizeof(s_mods));
// create mutex for syncing with interactive tasks
AccessMutex = SDL_CreateMutex();
if(!AccessMutex)
{
2011-06-22 00:14:21 -06:00
dfout << "Mutex creation failed." << std::endl;
errorstate = true;
return;
}
// all OK
errorstate = false;
// lock mutex
SDL_mutexP(AccessMutex);
2011-06-19 20:29:38 -06:00
// look for all plugins,
// create IO thread
2011-06-19 20:29:38 -06:00
DFThread * IO = SDL_CreateThread(fIOthread, this);
// and let DF do its thing.
};
void Core::Suspend()
{
SDL_mutexP(AccessMutex);
}
void Core::Resume()
{
for(unsigned int i = 0 ; i < allModules.size(); i++)
{
allModules[i]->OnResume();
}
SDL_mutexV(AccessMutex);
}
int Core::Update()
{
if(errorstate)
return -1;
// do persistent stuff here
SDL_mutexV(AccessMutex);
// other threads can claim the mutex here and use DFHack.
// NO CODE SHOULD EVER BE PLACED HERE
SDL_mutexP(AccessMutex);
return 0;
};
int Core::Shutdown ( void )
{
errorstate = 1;
2011-06-22 00:14:21 -06:00
// TODO:shutdown all plugins
// invalidate all modules
for(unsigned int i = 0 ; i < allModules.size(); i++)
{
delete allModules[i];
}
allModules.clear();
memset(&(s_mods), 0, sizeof(s_mods));
2011-06-22 00:14:21 -06:00
dfout << std::endl;
// kill the console object
2011-06-22 06:25:39 -06:00
delete con;
con = 0;
return -1;
}
/*******************************************************************************
M O D U L E S
*******************************************************************************/
#define MODULE_GETTER(TYPE) \
TYPE * Core::get##TYPE() \
{ \
if(errorstate) return NULL;\
if(!s_mods.p##TYPE)\
{\
Module * mod = create##TYPE();\
s_mods.p##TYPE = (TYPE *) mod;\
allModules.push_back(mod);\
}\
return s_mods.p##TYPE;\
}
MODULE_GETTER(Creatures);
MODULE_GETTER(Engravings);
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);