2011-06-16 15:53:39 -06:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
#include "Internal.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2011-06-24 21:35:29 -06:00
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
2011-06-14 08:13:28 -06:00
|
|
|
using namespace std;
|
|
|
|
|
2011-06-22 10:04:22 -06:00
|
|
|
#include "dfhack/Error.h"
|
|
|
|
#include "dfhack/Process.h"
|
2011-06-14 08:13:28 -06:00
|
|
|
#include "dfhack/Core.h"
|
2011-06-22 00:14:21 -06:00
|
|
|
#include "dfhack/Console.h"
|
2011-07-09 03:33:58 -06:00
|
|
|
#include "dfhack/Module.h"
|
2011-06-14 08:13:28 -06:00
|
|
|
#include "dfhack/VersionInfoFactory.h"
|
2011-08-01 18:21:25 -06:00
|
|
|
#include "dfhack/VersionInfo.h"
|
2011-06-24 21:35:29 -06:00
|
|
|
#include "dfhack/PluginManager.h"
|
2011-06-17 07:02:43 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-06-14 08:13:28 -06:00
|
|
|
#include "dfhack/modules/Gui.h"
|
2011-07-18 08:22:49 -06:00
|
|
|
#include "dfhack/modules/World.h"
|
2011-07-26 21:59:09 -06:00
|
|
|
using namespace DFHack;
|
2011-07-09 03:33:58 -06:00
|
|
|
|
|
|
|
#include "dfhack/SDL_fakes/events.h"
|
|
|
|
|
2011-06-16 18:09:03 -06:00
|
|
|
#include <stdio.h>
|
2011-06-26 20:49:56 -06:00
|
|
|
#include <iomanip>
|
2011-07-12 04:13:14 -06:00
|
|
|
#include <stdlib.h>
|
2011-08-01 18:21:25 -06:00
|
|
|
#include <fstream>
|
2011-07-26 21:59:09 -06:00
|
|
|
#include "tinythread.h"
|
|
|
|
using namespace tthread;
|
2011-06-14 08:13:28 -06:00
|
|
|
|
2011-07-26 21:59:09 -06:00
|
|
|
|
|
|
|
struct Core::Cond
|
|
|
|
{
|
|
|
|
Cond()
|
2011-07-11 14:23:13 -06:00
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
predicate = false;
|
|
|
|
wakeup = new tthread::condition_variable();
|
|
|
|
}
|
|
|
|
~Cond()
|
|
|
|
{
|
|
|
|
delete wakeup;
|
|
|
|
}
|
|
|
|
bool Lock(tthread::mutex * m)
|
|
|
|
{
|
|
|
|
while(!predicate)
|
2011-07-11 14:23:13 -06:00
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
wakeup->wait(*m);
|
2011-07-11 14:23:13 -06:00
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
predicate = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Unlock()
|
|
|
|
{
|
|
|
|
predicate = true;
|
|
|
|
wakeup->notify_one();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
tthread::condition_variable * wakeup;
|
|
|
|
bool predicate;
|
|
|
|
};
|
2011-07-11 14:23:13 -06:00
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
void cheap_tokenise(string const& input, vector<string> &output)
|
2011-06-16 18:09:03 -06:00
|
|
|
{
|
2011-06-24 21:35:29 -06:00
|
|
|
istringstream str(input);
|
|
|
|
istream_iterator<string> cur(str), end;
|
|
|
|
output.assign(cur, end);
|
2011-06-19 20:29:38 -06:00
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2011-06-24 21:35:29 -06:00
|
|
|
struct IODATA
|
|
|
|
{
|
|
|
|
Core * core;
|
|
|
|
PluginManager * plug_mgr;
|
|
|
|
};
|
2011-06-16 15:53:39 -06:00
|
|
|
|
2011-07-09 03:33:58 -06:00
|
|
|
// A thread function... for handling hotkeys. This is needed because
|
|
|
|
// all the plugin commands are expected to be run from foreign threads.
|
|
|
|
// Running them from one of the main DF threads will result in deadlock!
|
2011-07-26 21:59:09 -06:00
|
|
|
void fHKthread(void * iodata)
|
2011-07-09 03:33:58 -06:00
|
|
|
{
|
|
|
|
Core * core = ((IODATA*) iodata)->core;
|
|
|
|
PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr;
|
|
|
|
if(plug_mgr == 0 || core == 0)
|
|
|
|
{
|
|
|
|
cerr << "Hotkey thread has croaked." << endl;
|
2011-07-26 21:59:09 -06:00
|
|
|
return;
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
std::string stuff = core->getHotkeyCmd(); // waits on mutex!
|
|
|
|
if(!stuff.empty())
|
|
|
|
{
|
|
|
|
vector <string> crap;
|
2011-08-05 20:37:29 -06:00
|
|
|
command_result cr = plug_mgr->InvokeCommand(stuff, crap, false);
|
|
|
|
if(cr == CR_WOULD_BREAK)
|
|
|
|
{
|
|
|
|
core->con.printerr("It isn't possible to run an interactive command outside the console.\n");
|
|
|
|
}
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A thread function... for the interactive console.
|
2011-07-26 21:59:09 -06:00
|
|
|
void fIOthread(void * iodata)
|
2011-06-19 20:29:38 -06:00
|
|
|
{
|
2011-07-13 20:05:27 -06:00
|
|
|
IODATA * iod = ((IODATA*) iodata);
|
|
|
|
Core * core = iod->core;
|
2011-06-24 21:35:29 -06:00
|
|
|
PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr;
|
2011-08-13 06:42:09 -06:00
|
|
|
CommandHistory main_history;
|
|
|
|
main_history.load("dfhack.history");
|
2011-07-13 20:05:27 -06:00
|
|
|
Console & con = core->con;
|
2011-07-09 03:33:58 -06:00
|
|
|
if(plug_mgr == 0 || core == 0)
|
2011-06-25 00:05:17 -06:00
|
|
|
{
|
2011-07-14 03:15:23 -06:00
|
|
|
con.printerr("Something horrible happened in Core's constructor...\n");
|
2011-07-26 21:59:09 -06:00
|
|
|
return;
|
2011-06-25 00:05:17 -06:00
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
con.print("DFHack is ready. Have a nice day!\n"
|
|
|
|
"Type in '?' or 'help' for general help, 'ls' to see all commands.\n");
|
2011-06-22 00:14:21 -06:00
|
|
|
int clueless_counter = 0;
|
2011-06-16 15:53:39 -06:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
string command = "";
|
2011-08-13 06:42:09 -06:00
|
|
|
int ret = con.lineedit("[DFHack]# ",command, main_history);
|
2011-07-18 08:22:49 -06:00
|
|
|
if(ret == -2)
|
2011-07-12 04:13:14 -06:00
|
|
|
{
|
2011-07-18 08:22:49 -06:00
|
|
|
cerr << "Console is shutting down properly." << endl;
|
2011-07-26 21:59:09 -06:00
|
|
|
return;
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
else if(ret == -1)
|
|
|
|
{
|
|
|
|
cerr << "Console caught an unspecified error." << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(ret)
|
|
|
|
{
|
|
|
|
// a proper, non-empty command was entered
|
2011-08-13 06:42:09 -06:00
|
|
|
main_history.add(command);
|
|
|
|
main_history.save("dfhack.history");
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
// cut the input into parts
|
|
|
|
vector <string> parts;
|
|
|
|
cheap_tokenise(command,parts);
|
|
|
|
if(parts.size() == 0)
|
|
|
|
{
|
|
|
|
clueless_counter ++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
string first = parts[0];
|
|
|
|
parts.erase(parts.begin());
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Invoking: " << command << endl;
|
|
|
|
|
2011-07-18 08:22:49 -06:00
|
|
|
// let's see what we actually got
|
|
|
|
if(first=="help" || first == "?")
|
|
|
|
{
|
|
|
|
if(!parts.size())
|
|
|
|
{
|
|
|
|
con.print("This is the DFHack console. You can type commands in and manage DFHack plugins from it.\n"
|
|
|
|
"Some basic editing capabilities are included (single-line text editing).\n"
|
|
|
|
"The console also has a command history - you can navigate it with Up and Down keys.\n"
|
|
|
|
"On Windows, you may have to resize your console window. The appropriate menu is accessible\n"
|
|
|
|
"by clicking on the program icon in the top bar of the window.\n\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
"Basic commands:\n"
|
|
|
|
" help|? - This text.\n"
|
2011-09-04 06:16:12 -06:00
|
|
|
" ls|dir [PLUGIN] - List available commands. Optionally for single plugin.\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
" cls - Clear the console.\n"
|
|
|
|
" fpause - Force DF to pause.\n"
|
2011-07-18 08:22:49 -06:00
|
|
|
" die - Force DF to close immediately\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
"Plugin management (useful for developers):\n"
|
|
|
|
//" belongs COMMAND - Tell which plugin a command belongs to.\n"
|
|
|
|
" plug [PLUGIN|v] - List plugin state and description.\n"
|
|
|
|
" load PLUGIN|all - Load a plugin by name or load all possible plugins.\n"
|
|
|
|
" unload PLUGIN|all - Unload a plugin or all loaded plugins.\n"
|
|
|
|
" reload PLUGIN|all - Reload a plugin or all loaded plugins.\n"
|
2011-07-18 08:22:49 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con.printerr("not implemented yet\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( first == "load" )
|
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
if(plugname == "all")
|
|
|
|
{
|
|
|
|
for(int i = 0; i < plug_mgr->size();i++)
|
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
|
|
|
plug->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
|
|
|
if(!plug) con.printerr("No such plugin\n");
|
|
|
|
plug->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( first == "reload" )
|
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
if(plugname == "all")
|
|
|
|
{
|
|
|
|
for(int i = 0; i < plug_mgr->size();i++)
|
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
|
|
|
plug->reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
|
|
|
if(!plug) con.printerr("No such plugin\n");
|
|
|
|
plug->reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( first == "unload" )
|
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
if(plugname == "all")
|
|
|
|
{
|
|
|
|
for(int i = 0; i < plug_mgr->size();i++)
|
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
|
|
|
plug->unload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
|
|
|
if(!plug) con.printerr("No such plugin\n");
|
|
|
|
plug->unload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
else if(first == "ls" || first == "dir")
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
const Plugin * plug = plug_mgr->getPluginByName(plugname);
|
|
|
|
if(!plug)
|
|
|
|
{
|
|
|
|
con.printerr("There's no plugin called %s!\n",plugname.c_str());
|
|
|
|
}
|
|
|
|
else for (int j = 0; j < plug->size();j++)
|
|
|
|
{
|
|
|
|
const PluginCommand & pcmd = (plug->operator[](j));
|
2011-07-20 12:58:19 -06:00
|
|
|
con.print(" %-22s - %s\n",pcmd.name.c_str(), pcmd.description.c_str());
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
else
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
2011-07-20 12:58:19 -06:00
|
|
|
con.print(
|
|
|
|
"builtin:\n"
|
|
|
|
" help|? - This text or help specific to a plugin.\n"
|
|
|
|
" ls [PLUGIN] - List available commands. Optionally for single plugin.\n"
|
|
|
|
" cls - Clear the console.\n"
|
|
|
|
" fpause - Force DF to pause.\n"
|
|
|
|
" die - Force DF to close immediately\n"
|
|
|
|
" belongs COMMAND - Tell which plugin a command belongs to.\n"
|
|
|
|
" plug [PLUGIN|v] - List plugin state and detailed description.\n"
|
|
|
|
" load PLUGIN|all - Load a plugin by name or load all possible plugins.\n"
|
|
|
|
" unload PLUGIN|all - Unload a plugin or all loaded plugins.\n"
|
|
|
|
" reload PLUGIN|all - Reload a plugin or all loaded plugins.\n"
|
|
|
|
"\n"
|
|
|
|
"plugins:\n"
|
|
|
|
);
|
|
|
|
for(int i = 0; i < plug_mgr->size();i++)
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
2011-07-20 12:58:19 -06:00
|
|
|
const Plugin * plug = (plug_mgr->operator[](i));
|
|
|
|
if(!plug->size())
|
|
|
|
continue;
|
|
|
|
for (int j = 0; j < plug->size();j++)
|
|
|
|
{
|
|
|
|
const PluginCommand & pcmd = (plug->operator[](j));
|
|
|
|
con.print(" %-22s- %s\n",pcmd.name.c_str(), pcmd.description.c_str());
|
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
else if(first == "plug")
|
2011-06-16 15:53:39 -06:00
|
|
|
{
|
2011-06-26 20:49:56 -06:00
|
|
|
for(int i = 0; i < plug_mgr->size();i++)
|
|
|
|
{
|
|
|
|
const Plugin * plug = (plug_mgr->operator[](i));
|
2011-07-12 04:13:14 -06:00
|
|
|
if(!plug->size())
|
|
|
|
continue;
|
2011-07-20 12:58:19 -06:00
|
|
|
con.print("%s\n", plug->getName().c_str());
|
2011-06-26 20:49:56 -06:00
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
else if(first == "fpause")
|
|
|
|
{
|
|
|
|
World * w = core->getWorld();
|
|
|
|
w->SetPauseState(true);
|
|
|
|
con.print("The game was forced to pause!");
|
|
|
|
}
|
|
|
|
else if(first == "cls")
|
2011-06-22 00:14:21 -06:00
|
|
|
{
|
2011-07-18 08:22:49 -06:00
|
|
|
con.clear();
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
else if(first == "die")
|
|
|
|
{
|
|
|
|
_exit(666);
|
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
else
|
|
|
|
{
|
2011-06-24 21:35:29 -06:00
|
|
|
vector <string> parts;
|
|
|
|
cheap_tokenise(command,parts);
|
|
|
|
if(parts.size() == 0)
|
2011-06-19 20:29:38 -06:00
|
|
|
{
|
2011-06-24 21:35:29 -06:00
|
|
|
clueless_counter++;
|
2011-06-19 20:29:38 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-24 21:35:29 -06:00
|
|
|
string first = parts[0];
|
|
|
|
parts.erase(parts.begin());
|
|
|
|
command_result res = plug_mgr->InvokeCommand(first, parts);
|
|
|
|
if(res == CR_NOT_IMPLEMENTED)
|
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
con.printerr("%s is not a recognized command.\n", first.c_str());
|
2011-06-24 21:35:29 -06:00
|
|
|
clueless_counter ++;
|
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
/*
|
2011-06-24 21:35:29 -06:00
|
|
|
else if(res == CR_FAILURE)
|
|
|
|
{
|
2011-07-14 03:15:23 -06:00
|
|
|
con.printerr("ERROR!\n");
|
2011-06-24 21:35:29 -06:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
*/
|
2011-06-19 20:29:38 -06:00
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
2011-06-22 00:14:21 -06:00
|
|
|
if(clueless_counter == 3)
|
|
|
|
{
|
2011-07-14 00:02:29 -06:00
|
|
|
con.print("Do 'help' or '?' for the list of available commands.\n");
|
2011-06-22 00:14:21 -06:00
|
|
|
clueless_counter = 0;
|
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
|
|
|
}
|
2011-06-14 08:13:28 -06:00
|
|
|
|
|
|
|
Core::Core()
|
|
|
|
{
|
2011-06-22 00:14:21 -06:00
|
|
|
// init the console. This must be always the first step!
|
2011-06-24 21:35:29 -06:00
|
|
|
plug_mgr = 0;
|
2011-07-09 03:33:58 -06:00
|
|
|
vif = 0;
|
|
|
|
p = 0;
|
|
|
|
errorstate = false;
|
|
|
|
vinfo = 0;
|
|
|
|
started = false;
|
|
|
|
memset(&(s_mods), 0, sizeof(s_mods));
|
|
|
|
|
|
|
|
// create mutex for syncing with interactive tasks
|
|
|
|
AccessMutex = 0;
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex = 0;
|
2011-07-11 14:23:13 -06:00
|
|
|
core_cond = 0;
|
2011-07-09 03:33:58 -06:00
|
|
|
// set up hotkey capture
|
|
|
|
memset(hotkey_states,0,sizeof(hotkey_states));
|
|
|
|
hotkey_set = false;
|
|
|
|
HotkeyMutex = 0;
|
|
|
|
HotkeyCond = 0;
|
2011-08-13 06:42:09 -06:00
|
|
|
misc_data_mutex=0;
|
2011-07-09 03:33:58 -06:00
|
|
|
};
|
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
void Core::fatal (std::string output, bool deactivate)
|
|
|
|
{
|
|
|
|
stringstream out;
|
|
|
|
out << output ;
|
|
|
|
if(deactivate)
|
|
|
|
out << "DFHack will now deactivate.\n";
|
|
|
|
if(con.isInited())
|
|
|
|
{
|
|
|
|
con.printerr("%s", out.str().c_str());
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s\n", out.str().c_str());
|
|
|
|
#ifndef LINUX_BUILD
|
|
|
|
out << "Check file stderr.log for details\n";
|
|
|
|
MessageBox(0,out.str().c_str(),"DFHack error!", MB_OK | MB_ICONERROR);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-09 03:33:58 -06:00
|
|
|
bool Core::Init()
|
|
|
|
{
|
2011-07-27 06:22:37 -06:00
|
|
|
if(started)
|
|
|
|
return true;
|
2011-08-17 05:26:03 -06:00
|
|
|
if(errorstate)
|
|
|
|
return false;
|
2011-07-31 19:31:52 -06:00
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
// find out what we are...
|
2011-08-14 00:42:21 -06:00
|
|
|
#ifdef LINUX_BUILD
|
|
|
|
const char * path = "hack/Memory.xml";
|
|
|
|
#else
|
|
|
|
const char * path = "hack\\Memory.xml";
|
|
|
|
#endif
|
2011-11-04 02:08:29 -06:00
|
|
|
vif = new DFHack::VersionInfoFactory();
|
|
|
|
cerr << "Identifying DF version.\n";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
vif->loadFile(path);
|
|
|
|
}
|
|
|
|
catch(Error::All & err)
|
|
|
|
{
|
|
|
|
std::stringstream out;
|
|
|
|
out << "Error while reading Memory.xml:\n";
|
|
|
|
out << err.what() << std::endl;
|
|
|
|
delete vif;
|
|
|
|
vif = nullptr;
|
|
|
|
errorstate = true;
|
|
|
|
fatal(out.str(), true);
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-14 08:13:28 -06:00
|
|
|
p = new DFHack::Process(vif);
|
2011-07-31 19:31:52 -06:00
|
|
|
vinfo = p->getDescriptor();
|
2011-08-16 15:39:18 -06:00
|
|
|
|
2011-11-01 06:06:27 -06:00
|
|
|
if(!vinfo || !p->isIdentified())
|
2011-08-01 18:21:25 -06:00
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
fatal ("Not a known DF version.\n", true);
|
2011-08-16 15:39:18 -06:00
|
|
|
errorstate = true;
|
|
|
|
delete p;
|
|
|
|
p = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Version: " << vinfo->getVersion() << endl;
|
2011-08-16 15:39:18 -06:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Initializing Console.\n";
|
2011-07-31 19:31:52 -06:00
|
|
|
// init the console.
|
|
|
|
Gui * g = getGui();
|
2011-11-04 02:08:29 -06:00
|
|
|
bool is_text_mode = false;
|
|
|
|
if(g->init && g->init->graphics.flags.is_set(GRAPHICS_TEXT))
|
2011-07-31 19:31:52 -06:00
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
is_text_mode = true;
|
2011-07-31 19:31:52 -06:00
|
|
|
}
|
2011-11-04 02:08:29 -06:00
|
|
|
if(con.init(is_text_mode))
|
|
|
|
cerr << "Console is running.\n";
|
|
|
|
else
|
|
|
|
fatal ("Console has failed to initialize!\n", false);
|
2011-07-31 19:31:52 -06:00
|
|
|
|
2011-08-16 15:39:18 -06:00
|
|
|
// dump offsets to a file
|
|
|
|
std::ofstream dump("offsets.log");
|
|
|
|
if(!dump.fail())
|
2011-06-14 08:13:28 -06:00
|
|
|
{
|
2011-08-16 15:39:18 -06:00
|
|
|
dump << vinfo->PrintOffsets();
|
|
|
|
dump.close();
|
2011-06-14 08:13:28 -06:00
|
|
|
}
|
2011-07-31 19:31:52 -06:00
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
// create mutex for syncing with interactive tasks
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex = new mutex();
|
|
|
|
AccessMutex = new mutex();
|
2011-08-07 00:41:46 -06:00
|
|
|
misc_data_mutex=new mutex();
|
2011-07-11 14:23:13 -06:00
|
|
|
core_cond = new Core::Cond();
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Initializing Plugins.\n";
|
2011-07-09 03:33:58 -06:00
|
|
|
// create plugin manager
|
2011-06-24 21:35:29 -06:00
|
|
|
plug_mgr = new PluginManager(this);
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Starting IO thread.\n";
|
2011-06-16 15:53:39 -06:00
|
|
|
// create IO thread
|
2011-06-25 00:05:17 -06:00
|
|
|
IODATA *temp = new IODATA;
|
|
|
|
temp->core = this;
|
|
|
|
temp->plug_mgr = plug_mgr;
|
2011-07-26 21:59:09 -06:00
|
|
|
thread * IO = new thread(fIOthread, (void *) temp);
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Starting DF input capture thread.\n";
|
2011-07-09 03:33:58 -06:00
|
|
|
// set up hotkey capture
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyMutex = new mutex();
|
|
|
|
HotkeyCond = new condition_variable();
|
|
|
|
thread * HK = new thread(fHKthread, (void *) temp);
|
2011-07-09 03:33:58 -06:00
|
|
|
started = true;
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "DFHack is running.\n";
|
2011-07-09 03:33:58 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/// sets the current hotkey command
|
|
|
|
bool Core::setHotkeyCmd( std::string cmd )
|
|
|
|
{
|
|
|
|
// access command
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyMutex->lock();
|
2011-07-09 03:33:58 -06:00
|
|
|
{
|
|
|
|
hotkey_set = true;
|
|
|
|
hotkey_cmd = cmd;
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyCond->notify_all();
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyMutex->unlock();
|
2011-07-09 03:33:58 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/// removes the hotkey command and gives it to the caller thread
|
|
|
|
std::string Core::getHotkeyCmd( void )
|
|
|
|
{
|
|
|
|
string returner;
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyMutex->lock();
|
2011-07-09 03:33:58 -06:00
|
|
|
while ( ! hotkey_set )
|
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyCond->wait(*HotkeyMutex);
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
|
|
|
hotkey_set = false;
|
|
|
|
returner = hotkey_cmd;
|
|
|
|
hotkey_cmd.clear();
|
2011-07-26 21:59:09 -06:00
|
|
|
HotkeyMutex->unlock();
|
2011-07-09 03:33:58 -06:00
|
|
|
return returner;
|
|
|
|
}
|
|
|
|
|
2011-08-07 00:41:46 -06:00
|
|
|
void Core::RegisterData( void *p, std::string key )
|
2011-08-04 13:00:21 -06:00
|
|
|
{
|
2011-08-07 00:41:46 -06:00
|
|
|
misc_data_mutex->lock();
|
|
|
|
misc_data_map[key] = p;
|
|
|
|
misc_data_mutex->unlock();
|
2011-08-04 13:00:21 -06:00
|
|
|
}
|
2011-08-07 00:41:46 -06:00
|
|
|
|
|
|
|
void *Core::GetData( std::string key )
|
2011-08-04 13:00:21 -06:00
|
|
|
{
|
2011-08-07 00:41:46 -06:00
|
|
|
misc_data_mutex->lock();
|
|
|
|
std::map<std::string,void*>::iterator it=misc_data_map.find(key);
|
|
|
|
|
|
|
|
if ( it != misc_data_map.end() )
|
|
|
|
{
|
|
|
|
void *p=it->second;
|
|
|
|
misc_data_mutex->unlock();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
misc_data_mutex->unlock();
|
|
|
|
return 0;// or throw an error.
|
|
|
|
}
|
2011-08-04 13:00:21 -06:00
|
|
|
}
|
2011-06-14 08:13:28 -06:00
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
void Core::Suspend()
|
2011-06-14 08:13:28 -06:00
|
|
|
{
|
2011-07-11 14:23:13 -06:00
|
|
|
Core::Cond * nc = new Core::Cond();
|
|
|
|
// put the condition on a stack
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex->lock();
|
2011-07-11 14:23:13 -06:00
|
|
|
suspended_tools.push(nc);
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex->unlock();
|
2011-07-11 14:23:13 -06:00
|
|
|
// wait until Core::Update() wakes up the tool
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->lock();
|
2011-07-11 14:23:13 -06:00
|
|
|
nc->Lock(AccessMutex);
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->unlock();
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Resume()
|
|
|
|
{
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->lock();
|
2011-07-11 14:23:13 -06:00
|
|
|
core_cond->Unlock();
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->unlock();
|
2011-06-14 08:13:28 -06:00
|
|
|
}
|
|
|
|
|
2011-07-09 03:33:58 -06:00
|
|
|
// should always be from simulation thread!
|
2011-06-14 08:13:28 -06:00
|
|
|
int Core::Update()
|
|
|
|
{
|
2011-07-31 19:31:52 -06:00
|
|
|
if(!started)
|
|
|
|
Init();
|
2011-06-14 08:13:28 -06:00
|
|
|
if(errorstate)
|
|
|
|
return -1;
|
2011-07-11 14:23:13 -06:00
|
|
|
|
2011-06-26 18:13:01 -06:00
|
|
|
// notify all the plugins that a game tick is finished
|
|
|
|
plug_mgr->OnUpdate();
|
2011-07-11 14:23:13 -06:00
|
|
|
// wake waiting tools
|
|
|
|
// do not allow more tools to join in while we process stuff here
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex->lock();
|
2011-07-11 14:23:13 -06:00
|
|
|
while (!suspended_tools.empty())
|
|
|
|
{
|
|
|
|
Core::Cond * nc = suspended_tools.top();
|
|
|
|
suspended_tools.pop();
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->lock();
|
2011-07-11 14:23:13 -06:00
|
|
|
// wake tool
|
|
|
|
nc->Unlock();
|
|
|
|
// wait for tool to wake us
|
|
|
|
core_cond->Lock(AccessMutex);
|
2011-07-26 21:59:09 -06:00
|
|
|
AccessMutex->unlock();
|
2011-07-11 14:23:13 -06:00
|
|
|
// destroy condition
|
|
|
|
delete nc;
|
|
|
|
}
|
2011-07-26 21:59:09 -06:00
|
|
|
StackMutex->unlock();
|
2011-06-14 08:13:28 -06:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2011-07-12 04:13:14 -06:00
|
|
|
// FIXME: needs to terminate the IO threads and properly dismantle all the machinery involved.
|
2011-06-14 08:13:28 -06:00
|
|
|
int Core::Shutdown ( void )
|
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
if(errorstate)
|
|
|
|
return true;
|
2011-06-17 07:02:43 -06:00
|
|
|
errorstate = 1;
|
2011-06-24 21:35:29 -06:00
|
|
|
if(plug_mgr)
|
|
|
|
{
|
|
|
|
delete plug_mgr;
|
|
|
|
plug_mgr = 0;
|
|
|
|
}
|
2011-06-17 07:02:43 -06:00
|
|
|
// 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-07-13 03:45:30 -06:00
|
|
|
con.shutdown();
|
2011-06-17 07:02:43 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:40:23 -06:00
|
|
|
// from ncurses
|
|
|
|
#define KEY_F0 0410 /* Function keys. Space for 64 */
|
|
|
|
#define KEY_F(n) (KEY_F0+(n)) /* Value of function key n */
|
|
|
|
|
|
|
|
bool Core::ncurses_wgetch(int in, int & out)
|
|
|
|
{
|
|
|
|
if(!started)
|
|
|
|
{
|
|
|
|
out = in;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if(in >= KEY_F(1) && in <= KEY_F(8))
|
|
|
|
{
|
|
|
|
int idx = in - KEY_F(1);
|
|
|
|
// FIXME: copypasta, push into a method!
|
|
|
|
Gui * g = getGui();
|
|
|
|
if(g->hotkeys && g->df_interface && g->df_menu_state)
|
|
|
|
{
|
|
|
|
t_viewscreen * ws = g->GetCurrentScreen();
|
|
|
|
// FIXME: put hardcoded values into memory.xml
|
|
|
|
if(ws->getClassName() == "viewscreen_dwarfmodest" && *g->df_menu_state == 0x23)
|
|
|
|
{
|
|
|
|
out = in;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t_hotkey & hotkey = (*g->hotkeys)[idx];
|
|
|
|
setHotkeyCmd(hotkey.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = in;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-09 03:33:58 -06:00
|
|
|
int Core::SDL_Event(SDL::Event* ev, int orig_return)
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
2011-07-09 03:33:58 -06:00
|
|
|
// do NOT process events before we are ready.
|
|
|
|
if(!started) return orig_return;
|
|
|
|
if(!ev)
|
|
|
|
return orig_return;
|
|
|
|
if(ev && ev->type == SDL::ET_KEYDOWN || ev->type == SDL::ET_KEYUP)
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
2011-07-09 03:33:58 -06:00
|
|
|
SDL::KeyboardEvent * ke = (SDL::KeyboardEvent *)ev;
|
|
|
|
bool shift = ke->ksym.mod & SDL::KMOD_SHIFT;
|
|
|
|
// consuming F1 .. F8
|
|
|
|
int idx = ke->ksym.sym - SDL::K_F1;
|
|
|
|
if(idx < 0 || idx > 7)
|
|
|
|
return orig_return;
|
|
|
|
idx += 8*shift;
|
|
|
|
// now we have the real index...
|
|
|
|
if(ke->state == SDL::BTN_PRESSED && !hotkey_states[idx])
|
|
|
|
{
|
|
|
|
hotkey_states[idx] = 1;
|
|
|
|
Gui * g = getGui();
|
2011-07-16 10:10:08 -06:00
|
|
|
if(g->hotkeys && g->df_interface && g->df_menu_state)
|
2011-07-09 03:33:58 -06:00
|
|
|
{
|
|
|
|
t_viewscreen * ws = g->GetCurrentScreen();
|
2011-07-18 08:22:49 -06:00
|
|
|
// FIXME: put hardcoded values into memory.xml
|
2011-07-16 10:10:08 -06:00
|
|
|
if(ws->getClassName() == "viewscreen_dwarfmodest" && *g->df_menu_state == 0x23)
|
2011-07-09 03:33:58 -06:00
|
|
|
return orig_return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t_hotkey & hotkey = (*g->hotkeys)[idx];
|
|
|
|
setHotkeyCmd(hotkey.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(ke->state == SDL::BTN_RELEASED)
|
|
|
|
{
|
|
|
|
hotkey_states[idx] = 0;
|
|
|
|
}
|
2011-07-07 19:55:37 -06:00
|
|
|
}
|
2011-07-09 03:33:58 -06:00
|
|
|
return orig_return;
|
|
|
|
// do stuff with the events...
|
2011-07-07 19:55:37 -06:00
|
|
|
}
|
|
|
|
|
2011-07-24 22:24:34 -06:00
|
|
|
////////////////
|
|
|
|
// ClassNamCheck
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
// Since there is no Process.cpp, put ClassNamCheck stuff in Core.cpp
|
|
|
|
|
|
|
|
static std::set<std::string> known_class_names;
|
|
|
|
static std::map<std::string, void*> known_vptrs;
|
|
|
|
|
|
|
|
ClassNameCheck::ClassNameCheck(std::string _name) : name(_name), vptr(0)
|
|
|
|
{
|
|
|
|
known_class_names.insert(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassNameCheck &ClassNameCheck::operator= (const ClassNameCheck &b)
|
|
|
|
{
|
|
|
|
name = b.name; vptr = b.vptr; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ClassNameCheck::operator() (Process *p, void * ptr) const {
|
|
|
|
if (vptr == 0 && p->readClassName(ptr) == name)
|
|
|
|
{
|
|
|
|
vptr = ptr;
|
|
|
|
known_vptrs[name] = ptr;
|
|
|
|
}
|
|
|
|
return (vptr && vptr == ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassNameCheck::getKnownClassNames(std::vector<std::string> &names)
|
|
|
|
{
|
|
|
|
std::set<std::string>::iterator it = known_class_names.begin();
|
|
|
|
|
|
|
|
for (; it != known_class_names.end(); it++)
|
|
|
|
names.push_back(*it);
|
|
|
|
}
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
M O D U L E S
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#define MODULE_GETTER(TYPE) \
|
|
|
|
TYPE * Core::get##TYPE() \
|
|
|
|
{ \
|
2011-06-19 17:12:07 -06:00
|
|
|
if(errorstate) return NULL;\
|
2011-06-17 07:02:43 -06:00
|
|
|
if(!s_mods.p##TYPE)\
|
|
|
|
{\
|
|
|
|
Module * mod = create##TYPE();\
|
|
|
|
s_mods.p##TYPE = (TYPE *) mod;\
|
|
|
|
allModules.push_back(mod);\
|
|
|
|
}\
|
|
|
|
return s_mods.p##TYPE;\
|
2011-06-14 08:13:28 -06:00
|
|
|
}
|
2011-06-17 07:02:43 -06:00
|
|
|
|
|
|
|
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);
|
2011-07-13 00:17:51 -06:00
|
|
|
MODULE_GETTER(Constructions);
|
|
|
|
MODULE_GETTER(Vermin);
|
2011-07-20 19:26:52 -06:00
|
|
|
MODULE_GETTER(Notes);
|