2012-01-27 08:47:14 -07:00
|
|
|
/*
|
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-12-31 04:48:42 -07:00
|
|
|
#include "Error.h"
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "Core.h"
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Module.h"
|
|
|
|
#include "VersionInfoFactory.h"
|
|
|
|
#include "VersionInfo.h"
|
|
|
|
#include "PluginManager.h"
|
2011-06-17 07:02:43 -06:00
|
|
|
#include "ModuleFactory.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/World.h"
|
|
|
|
#include "modules/Graphic.h"
|
2012-03-04 17:34:04 -07:00
|
|
|
#include "modules/Windows.h"
|
2012-03-14 09:57:29 -06:00
|
|
|
#include "RemoteServer.h"
|
2012-04-15 09:09:25 -06:00
|
|
|
#include "LuaTools.h"
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
2011-07-26 21:59:09 -06:00
|
|
|
using namespace DFHack;
|
2011-07-09 03:33:58 -06:00
|
|
|
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/world_data.h"
|
2012-03-15 12:00:47 -06:00
|
|
|
#include "df/interfacest.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2012-02-28 04:59:02 -07:00
|
|
|
#include <df/graphic.h>
|
2011-12-30 07:12:15 -07:00
|
|
|
|
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"
|
2011-06-14 08:13:28 -06:00
|
|
|
|
2012-06-13 18:15:43 -06:00
|
|
|
#include "SDL_events.h"
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
using namespace tthread;
|
|
|
|
using namespace df::enums;
|
2012-01-31 09:17:27 -07:00
|
|
|
using df::global::init;
|
2012-05-17 10:38:27 -06:00
|
|
|
using df::global::world;
|
2011-07-26 21:59:09 -06:00
|
|
|
|
2012-03-13 18:52:58 -06:00
|
|
|
// FIXME: A lot of code in one file, all doing different things... there's something fishy about it.
|
|
|
|
|
2012-05-19 11:31:42 -06:00
|
|
|
static bool parseKeySpec(std::string keyspec, int *psym, int *pmod, std::string *pfocus = NULL);
|
2012-03-12 00:31:29 -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
|
|
|
|
2012-03-15 05:33:19 -06:00
|
|
|
struct Core::Private
|
|
|
|
{
|
|
|
|
tthread::mutex AccessMutex;
|
|
|
|
tthread::mutex StackMutex;
|
|
|
|
std::stack<Core::Cond*> suspended_tools;
|
|
|
|
Core::Cond core_cond;
|
|
|
|
thread::id df_suspend_thread;
|
|
|
|
int df_suspend_depth;
|
|
|
|
|
|
|
|
Private() {
|
|
|
|
df_suspend_depth = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-04-15 08:40:19 -06:00
|
|
|
void Core::cheap_tokenise(string const& input, vector<string> &output)
|
2011-06-16 18:09:03 -06:00
|
|
|
{
|
2011-12-29 06:37:07 -07:00
|
|
|
string *cur = NULL;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < input.size(); i++) {
|
2012-02-29 17:29:55 -07:00
|
|
|
unsigned char c = input[i];
|
2011-12-29 06:37:07 -07:00
|
|
|
if (isspace(c)) {
|
|
|
|
cur = NULL;
|
|
|
|
} else {
|
|
|
|
if (!cur) {
|
|
|
|
output.push_back("");
|
|
|
|
cur = &output.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '"') {
|
|
|
|
for (i++; i < input.size(); i++) {
|
|
|
|
c = input[i];
|
|
|
|
if (c == '"')
|
|
|
|
break;
|
|
|
|
else if (c == '\\') {
|
|
|
|
if (++i < input.size())
|
|
|
|
cur->push_back(input[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cur->push_back(c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cur->push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
color_ostream_proxy out(core->getConsole());
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
auto rv = core->runCommand(out, stuff);
|
2011-12-30 07:27:55 -07:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
if (rv == CR_NOT_IMPLEMENTED)
|
|
|
|
out.printerr("Invalid hotkey command: '%s'\n", stuff.c_str());
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 15:22:41 -07:00
|
|
|
struct sortable
|
|
|
|
{
|
|
|
|
bool recolor;
|
|
|
|
string name;
|
|
|
|
string description;
|
|
|
|
//FIXME: Nuke when MSVC stops failing at being C++11 compliant
|
|
|
|
sortable(bool recolor_,const string& name_,const string & description_): recolor(recolor_), name(name_), description(description_){};
|
|
|
|
bool operator <(const sortable & rhs) const
|
|
|
|
{
|
|
|
|
if( name < rhs.name )
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2012-06-24 11:52:40 -06:00
|
|
|
static std::string getScriptHelp(std::string path, std::string helpprefix)
|
2012-05-04 09:47:18 -06:00
|
|
|
{
|
2012-05-24 09:31:20 -06:00
|
|
|
ifstream script(path.c_str());
|
2012-05-04 09:47:18 -06:00
|
|
|
|
|
|
|
if (script.good())
|
|
|
|
{
|
|
|
|
std::string help;
|
|
|
|
if (getline(script, help) &&
|
2012-06-24 11:52:40 -06:00
|
|
|
help.substr(0,helpprefix.length()) == helpprefix)
|
|
|
|
return help.substr(helpprefix.length());
|
2012-05-04 09:47:18 -06:00
|
|
|
}
|
|
|
|
|
2012-06-24 11:52:40 -06:00
|
|
|
return "No help available.";
|
2012-05-04 09:47:18 -06:00
|
|
|
}
|
|
|
|
|
2012-06-24 11:52:40 -06:00
|
|
|
static std::map<string,string> listScripts(PluginManager *plug_mgr, std::string path)
|
2012-05-04 09:47:18 -06:00
|
|
|
{
|
|
|
|
std::vector<string> files;
|
|
|
|
getdir(path, files);
|
|
|
|
|
|
|
|
std::map<string,string> pset;
|
|
|
|
for (size_t i = 0; i < files.size(); i++)
|
|
|
|
{
|
|
|
|
if (hasEnding(files[i], ".lua"))
|
|
|
|
{
|
2012-06-24 11:52:40 -06:00
|
|
|
std::string help = getScriptHelp(path + files[i], "-- ");
|
2012-05-04 09:47:18 -06:00
|
|
|
|
|
|
|
pset[files[i].substr(0, files[i].size()-4)] = help;
|
|
|
|
}
|
2012-06-24 11:52:40 -06:00
|
|
|
else if (plug_mgr->eval_ruby && hasEnding(files[i], ".rb"))
|
|
|
|
{
|
|
|
|
std::string help = getScriptHelp(path + files[i], "# ");
|
|
|
|
|
|
|
|
pset[files[i].substr(0, files[i].size()-3)] = help;
|
|
|
|
}
|
2012-05-04 09:47:18 -06:00
|
|
|
}
|
|
|
|
return pset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool fileExists(std::string path)
|
|
|
|
{
|
2012-05-24 09:31:20 -06:00
|
|
|
ifstream script(path.c_str());
|
2012-05-04 09:47:18 -06:00
|
|
|
return script.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct ScriptArgs {
|
|
|
|
const string *pcmd;
|
|
|
|
vector<string> *pargs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool init_run_script(color_ostream &out, lua_State *state, void *info)
|
|
|
|
{
|
|
|
|
auto args = (ScriptArgs*)info;
|
|
|
|
if (!lua_checkstack(state, args->pargs->size()+10))
|
|
|
|
return false;
|
|
|
|
Lua::PushDFHack(state);
|
|
|
|
lua_getfield(state, -1, "run_script");
|
|
|
|
lua_remove(state, -2);
|
|
|
|
lua_pushstring(state, args->pcmd->c_str());
|
|
|
|
for (size_t i = 0; i < args->pargs->size(); i++)
|
|
|
|
lua_pushstring(state, (*args->pargs)[i].c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-14 02:46:12 -06:00
|
|
|
static command_result runLuaScript(color_ostream &out, std::string name, vector<string> &args)
|
2012-05-04 09:47:18 -06:00
|
|
|
{
|
|
|
|
ScriptArgs data;
|
2012-06-14 02:46:12 -06:00
|
|
|
data.pcmd = &name;
|
2012-05-04 09:47:18 -06:00
|
|
|
data.pargs = &args;
|
|
|
|
|
|
|
|
bool ok = Lua::RunCoreQueryLoop(out, Lua::Core::State, init_run_script, &data);
|
|
|
|
|
|
|
|
return ok ? CR_OK : CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
static command_result runRubyScript(color_ostream &out, PluginManager *plug_mgr, std::string name, vector<string> &args)
|
2012-06-24 11:52:40 -06:00
|
|
|
{
|
|
|
|
std::string rbcmd = "$script_args = [";
|
|
|
|
for (size_t i = 0; i < args.size(); i++)
|
|
|
|
rbcmd += "'" + args[i] + "', ";
|
|
|
|
rbcmd += "]\n";
|
|
|
|
|
|
|
|
rbcmd += "load './hack/scripts/" + name + ".rb'";
|
|
|
|
|
2012-07-03 08:10:10 -06:00
|
|
|
return plug_mgr->eval_ruby(out, rbcmd.c_str());
|
2012-06-24 11:52:40 -06:00
|
|
|
}
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
command_result Core::runCommand(color_ostream &out, const std::string &command)
|
2011-06-19 20:29:38 -06:00
|
|
|
{
|
2011-12-30 12:11:34 -07:00
|
|
|
if (!command.empty())
|
2011-06-25 00:05:17 -06:00
|
|
|
{
|
2011-07-18 08:22:49 -06:00
|
|
|
vector <string> parts;
|
2012-04-15 08:40:19 -06:00
|
|
|
Core::cheap_tokenise(command,parts);
|
2011-07-18 08:22:49 -06:00
|
|
|
if(parts.size() == 0)
|
2012-05-04 09:47:18 -06:00
|
|
|
return CR_NOT_IMPLEMENTED;
|
|
|
|
|
2011-07-18 08:22:49 -06:00
|
|
|
string first = parts[0];
|
|
|
|
parts.erase(parts.begin());
|
2011-12-30 12:11:34 -07:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
if (first[0] == '#')
|
|
|
|
return CR_OK;
|
2011-12-30 12:11:34 -07:00
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Invoking: " << command << endl;
|
2012-05-04 09:47:18 -06:00
|
|
|
return runCommand(out, first, parts);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return CR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_result Core::runCommand(color_ostream &con, const std::string &first, vector<string> &parts)
|
|
|
|
{
|
|
|
|
if (!first.empty())
|
|
|
|
{
|
2011-07-18 08:22:49 -06:00
|
|
|
// let's see what we actually got
|
2012-02-22 11:54:07 -07:00
|
|
|
if(first=="help" || first == "?" || first == "man")
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
if(!parts.size())
|
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
if (con.is_console())
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
con.print("Basic commands:\n"
|
2012-02-22 11:54:07 -07:00
|
|
|
" help|?|man - This text.\n"
|
2012-01-28 05:03:56 -07:00
|
|
|
" help COMMAND - Usage help for the given command.\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-12-30 12:25:50 -07:00
|
|
|
" keybinding - Modify bindings of commands to keys\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
"Plugin management (useful for developers):\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
|
|
|
);
|
|
|
|
}
|
2011-12-31 02:25:46 -07:00
|
|
|
else if (parts.size() == 1)
|
|
|
|
{
|
|
|
|
Plugin *plug = plug_mgr->getPluginByCommand(parts[0]);
|
|
|
|
if (plug) {
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t j = 0; j < plug->size();j++)
|
2011-12-31 02:25:46 -07:00
|
|
|
{
|
|
|
|
const PluginCommand & pcmd = (plug->operator[](j));
|
|
|
|
if (pcmd.name != parts[0])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pcmd.isHotkeyCommand())
|
2012-08-18 23:21:25 -06:00
|
|
|
con.color(COLOR_CYAN);
|
2011-12-31 02:25:46 -07:00
|
|
|
con.print("%s: %s\n",pcmd.name.c_str(), pcmd.description.c_str());
|
|
|
|
con.reset_color();
|
|
|
|
if (!pcmd.usage.empty())
|
|
|
|
con << "Usage:\n" << pcmd.usage << flush;
|
2012-05-04 09:47:18 -06:00
|
|
|
return CR_OK;
|
2011-12-31 02:25:46 -07:00
|
|
|
}
|
|
|
|
}
|
2012-06-24 11:52:40 -06:00
|
|
|
auto filename = getHackPath() + "scripts/" + parts[0];
|
|
|
|
if (fileExists(filename + ".lua"))
|
|
|
|
{
|
|
|
|
string help = getScriptHelp(filename + ".lua", "-- ");
|
|
|
|
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
|
2012-05-04 09:47:18 -06:00
|
|
|
{
|
2012-06-24 11:52:40 -06:00
|
|
|
string help = getScriptHelp(filename + ".rb", "# ");
|
2012-05-04 09:47:18 -06:00
|
|
|
con.print("%s: %s\n", parts[0].c_str(), help.c_str());
|
2012-05-04 10:59:06 -06:00
|
|
|
return CR_OK;
|
2012-05-04 09:47:18 -06:00
|
|
|
}
|
2011-12-31 02:25:46 -07:00
|
|
|
con.printerr("Unknown command: %s\n", parts[0].c_str());
|
|
|
|
}
|
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")
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0; i < plug_mgr->size();i++)
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->load(con);
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
2011-11-07 02:55:18 -07:00
|
|
|
if(!plug)
|
|
|
|
{
|
|
|
|
con.printerr("No such plugin\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->load(con);
|
2011-11-07 02:55:18 -07:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( first == "reload" )
|
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
if(plugname == "all")
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0; i < plug_mgr->size();i++)
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->reload(con);
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
2011-11-07 02:55:18 -07:00
|
|
|
if(!plug)
|
|
|
|
{
|
|
|
|
con.printerr("No such plugin\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->reload(con);
|
2011-11-07 02:55:18 -07:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( first == "unload" )
|
|
|
|
{
|
|
|
|
if(parts.size())
|
|
|
|
{
|
|
|
|
string & plugname = parts[0];
|
|
|
|
if(plugname == "all")
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0; i < plug_mgr->size();i++)
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
Plugin * plug = (plug_mgr->operator[](i));
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->unload(con);
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Plugin * plug = plug_mgr->getPluginByName(plugname);
|
2011-11-07 02:55:18 -07:00
|
|
|
if(!plug)
|
|
|
|
{
|
|
|
|
con.printerr("No such plugin\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-15 03:01:23 -06:00
|
|
|
plug->unload(con);
|
2011-11-07 02:55:18 -07:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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());
|
|
|
|
}
|
2012-01-31 09:55:38 -07:00
|
|
|
else for (size_t j = 0; j < plug->size();j++)
|
2011-07-18 08:22:49 -06:00
|
|
|
{
|
|
|
|
const PluginCommand & pcmd = (plug->operator[](j));
|
2011-12-31 02:25:46 -07:00
|
|
|
if (pcmd.isHotkeyCommand())
|
2012-08-18 23:21:25 -06:00
|
|
|
con.color(COLOR_CYAN);
|
2011-07-20 12:58:19 -06:00
|
|
|
con.print(" %-22s - %s\n",pcmd.name.c_str(), pcmd.description.c_str());
|
2011-12-31 02:25:46 -07:00
|
|
|
con.reset_color();
|
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"
|
2012-02-22 11:54:07 -07:00
|
|
|
" help|?|man - This text or help specific to a plugin.\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
" 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"
|
2012-02-05 20:47:13 -07:00
|
|
|
" keybinding - Modify bindings of commands to keys\n"
|
2012-03-12 00:31:29 -06:00
|
|
|
" script FILENAME - Run the commands specified in a file.\n"
|
2011-07-20 12:58:19 -06:00
|
|
|
" 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"
|
|
|
|
);
|
2012-02-22 15:22:41 -07:00
|
|
|
std::set <sortable> out;
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t 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;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t j = 0; j < plug->size();j++)
|
2011-07-20 12:58:19 -06:00
|
|
|
{
|
|
|
|
const PluginCommand & pcmd = (plug->operator[](j));
|
2012-02-21 10:30:31 -07:00
|
|
|
out.insert(sortable(pcmd.isHotkeyCommand(),pcmd.name,pcmd.description));
|
2011-07-20 12:58:19 -06:00
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
2012-02-21 07:21:50 -07:00
|
|
|
for(auto iter = out.begin();iter != out.end();iter++)
|
|
|
|
{
|
|
|
|
if ((*iter).recolor)
|
2012-08-18 23:21:25 -06:00
|
|
|
con.color(COLOR_CYAN);
|
2012-02-21 07:21:50 -07:00
|
|
|
con.print(" %-22s- %s\n",(*iter).name.c_str(), (*iter).description.c_str());
|
|
|
|
con.reset_color();
|
|
|
|
}
|
2012-06-24 11:52:40 -06:00
|
|
|
auto scripts = listScripts(plug_mgr, getHackPath() + "scripts/");
|
2012-05-04 09:47:18 -06:00
|
|
|
if (!scripts.empty())
|
|
|
|
{
|
|
|
|
con.print("\nscripts:\n");
|
|
|
|
for (auto iter = scripts.begin(); iter != scripts.end(); ++iter)
|
|
|
|
con.print(" %-22s- %s\n", iter->first.c_str(), iter->second.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
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0; i < plug_mgr->size();i++)
|
2011-06-26 20:49:56 -06:00
|
|
|
{
|
|
|
|
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-12-30 12:25:50 -07:00
|
|
|
else if(first == "keybinding")
|
|
|
|
{
|
|
|
|
if (parts.size() >= 3 && (parts[0] == "set" || parts[0] == "add"))
|
|
|
|
{
|
|
|
|
std::string keystr = parts[1];
|
|
|
|
if (parts[0] == "set")
|
2012-05-04 09:47:18 -06:00
|
|
|
ClearKeyBindings(keystr);
|
2011-12-30 12:25:50 -07:00
|
|
|
for (int i = parts.size()-1; i >= 2; i--)
|
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
if (!AddKeyBinding(keystr, parts[i])) {
|
2011-12-30 12:25:50 -07:00
|
|
|
con.printerr("Invalid key spec: %s\n", keystr.c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (parts.size() >= 2 && parts[0] == "clear")
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 1; i < parts.size(); i++)
|
2011-12-30 12:25:50 -07:00
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
if (!ClearKeyBindings(parts[i])) {
|
2011-12-30 12:25:50 -07:00
|
|
|
con.printerr("Invalid key spec: %s\n", parts[i].c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-31 02:25:46 -07:00
|
|
|
else if (parts.size() == 2 && parts[0] == "list")
|
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
std::vector<std::string> list = ListKeyBindings(parts[1]);
|
2011-12-31 02:25:46 -07:00
|
|
|
if (list.empty())
|
|
|
|
con << "No bindings." << endl;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < list.size(); i++)
|
2011-12-31 02:25:46 -07:00
|
|
|
con << " " << list[i] << endl;
|
|
|
|
}
|
2011-12-30 12:25:50 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
con << "Usage:" << endl
|
2011-12-31 02:25:46 -07:00
|
|
|
<< " keybinding list <key>" << endl
|
2012-05-19 11:31:42 -06:00
|
|
|
<< " keybinding clear <key>[@context]..." << endl
|
|
|
|
<< " keybinding set <key>[@context] \"cmdline\" \"cmdline\"..." << endl
|
|
|
|
<< " keybinding add <key>[@context] \"cmdline\" \"cmdline\"..." << endl
|
|
|
|
<< "Later adds, and earlier items within one command have priority." << endl
|
|
|
|
<< "Supported keys: [Ctrl-][Alt-][Shift-](A-Z, or F1-F9, or Enter)." << endl
|
|
|
|
<< "Context may be used to limit the scope of the binding, by" << endl
|
|
|
|
<< "requiring the current context to have a certain prefix." << endl
|
|
|
|
<< "Current UI context is: "
|
|
|
|
<< Gui::getFocusString(Core::getTopViewscreen()) << endl;
|
2011-12-30 12:25:50 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-18 08:22:49 -06:00
|
|
|
else if(first == "fpause")
|
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
World * w = getWorld();
|
2011-07-18 08:22:49 -06:00
|
|
|
w->SetPauseState(true);
|
2012-05-04 09:47:18 -06:00
|
|
|
con.print("The game was forced to pause!\n");
|
2011-07-18 08:22:49 -06:00
|
|
|
}
|
|
|
|
else if(first == "cls")
|
2011-06-22 00:14:21 -06:00
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
if (con.is_console())
|
|
|
|
((Console&)con).clear();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con.printerr("No console to clear.\n");
|
|
|
|
return CR_NEEDS_CONSOLE;
|
|
|
|
}
|
2011-06-22 00:14:21 -06:00
|
|
|
}
|
2011-07-20 12:58:19 -06:00
|
|
|
else if(first == "die")
|
|
|
|
{
|
|
|
|
_exit(666);
|
|
|
|
}
|
2012-03-12 00:31:29 -06:00
|
|
|
else if(first == "script")
|
|
|
|
{
|
|
|
|
if(parts.size() == 1)
|
|
|
|
{
|
2012-05-04 09:47:18 -06:00
|
|
|
loadScriptFile(con, parts[0], false);
|
2012-03-12 00:31:29 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con << "Usage:" << endl
|
|
|
|
<< " script <filename>" << endl;
|
2012-05-04 09:47:18 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2012-03-12 00:31:29 -06:00
|
|
|
}
|
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
else
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
command_result res = plug_mgr->InvokeCommand(con, first, parts);
|
2012-02-29 17:29:55 -07:00
|
|
|
if(res == CR_NOT_IMPLEMENTED)
|
2011-06-19 20:29:38 -06:00
|
|
|
{
|
2012-06-24 11:52:40 -06:00
|
|
|
auto filename = getHackPath() + "scripts/" + first;
|
|
|
|
if (fileExists(filename + ".lua"))
|
2012-06-14 02:46:12 -06:00
|
|
|
res = runLuaScript(con, first, parts);
|
2012-06-24 11:52:40 -06:00
|
|
|
else if (plug_mgr->eval_ruby && fileExists(filename + ".rb"))
|
2012-07-03 08:10:10 -06:00
|
|
|
res = runRubyScript(con, plug_mgr, first, parts);
|
2012-05-04 09:47:18 -06:00
|
|
|
else
|
|
|
|
con.printerr("%s is not a recognized command.\n", first.c_str());
|
2011-06-19 20:29:38 -06:00
|
|
|
}
|
2012-05-04 09:47:18 -06:00
|
|
|
else if (res == CR_NEEDS_CONSOLE)
|
|
|
|
con.printerr("%s needs interactive console to work.\n", first.c_str());
|
|
|
|
return res;
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
2012-05-04 09:47:18 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
2011-12-30 12:11:34 -07:00
|
|
|
}
|
2012-05-04 09:47:18 -06:00
|
|
|
|
|
|
|
return CR_NOT_IMPLEMENTED;
|
2011-12-30 12:11:34 -07:00
|
|
|
}
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
bool Core::loadScriptFile(color_ostream &out, string fname, bool silent)
|
2011-12-30 12:11:34 -07:00
|
|
|
{
|
2012-03-28 18:49:44 -06:00
|
|
|
if(!silent)
|
2012-05-04 09:47:18 -06:00
|
|
|
out << "Loading script at " << fname << std::endl;
|
2012-05-24 09:31:20 -06:00
|
|
|
ifstream script(fname.c_str());
|
2012-03-12 01:33:59 -06:00
|
|
|
if (script.good())
|
2012-03-12 00:31:29 -06:00
|
|
|
{
|
2012-03-12 01:33:59 -06:00
|
|
|
string command;
|
|
|
|
while (getline(script, command))
|
|
|
|
{
|
|
|
|
if (!command.empty())
|
2012-05-04 09:47:18 -06:00
|
|
|
runCommand(out, command);
|
2012-03-12 01:33:59 -06:00
|
|
|
}
|
2012-05-04 09:47:18 -06:00
|
|
|
return true;
|
2012-03-12 00:31:29 -06:00
|
|
|
}
|
2012-03-12 01:33:59 -06:00
|
|
|
else
|
2011-12-30 12:11:34 -07:00
|
|
|
{
|
2012-03-28 18:49:44 -06:00
|
|
|
if(!silent)
|
2012-05-04 09:47:18 -06:00
|
|
|
out.printerr("Error loading script\n");
|
|
|
|
return false;
|
2011-12-30 12:11:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A thread function... for the interactive console.
|
|
|
|
void fIOthread(void * iodata)
|
|
|
|
{
|
|
|
|
IODATA * iod = ((IODATA*) iodata);
|
|
|
|
Core * core = iod->core;
|
|
|
|
PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr;
|
|
|
|
|
|
|
|
CommandHistory main_history;
|
|
|
|
main_history.load("dfhack.history");
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
Console & con = core->getConsole();
|
2011-12-30 12:11:34 -07:00
|
|
|
if(plug_mgr == 0 || core == 0)
|
|
|
|
{
|
|
|
|
con.printerr("Something horrible happened in Core's constructor...\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
core->loadScriptFile(con, "dfhack.init", true);
|
2011-12-30 12:11:34 -07:00
|
|
|
|
|
|
|
con.print("DFHack is ready. Have a nice day!\n"
|
|
|
|
"Type in '?' or 'help' for general help, 'ls' to see all commands.\n");
|
|
|
|
|
|
|
|
int clueless_counter = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
string command = "";
|
|
|
|
int ret = con.lineedit("[DFHack]# ",command, main_history);
|
2012-05-26 14:08:15 -06:00
|
|
|
fprintf(stderr,"Command: [%s]\n",command.c_str());
|
2011-12-30 12:11:34 -07:00
|
|
|
if(ret == -2)
|
|
|
|
{
|
|
|
|
cerr << "Console is shutting down properly." << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ret == -1)
|
|
|
|
{
|
|
|
|
cerr << "Console caught an unspecified error." << endl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(ret)
|
|
|
|
{
|
|
|
|
// a proper, non-empty command was entered
|
2012-05-26 14:08:15 -06:00
|
|
|
fprintf(stderr,"Adding command to history\n");
|
2011-12-30 12:11:34 -07:00
|
|
|
main_history.add(command);
|
2012-05-26 14:08:15 -06:00
|
|
|
fprintf(stderr,"Saving history\n");
|
2011-12-30 12:11:34 -07:00
|
|
|
main_history.save("dfhack.history");
|
|
|
|
}
|
2012-05-26 14:08:15 -06:00
|
|
|
|
|
|
|
fprintf(stderr,"Running command\n");
|
2011-12-30 12:11:34 -07:00
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
auto rv = core->runCommand(con, command);
|
|
|
|
|
|
|
|
if (rv == CR_NOT_IMPLEMENTED)
|
|
|
|
clueless_counter++;
|
2011-12-30 12:11:34 -07: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()
|
|
|
|
{
|
2012-03-15 05:33:19 -06:00
|
|
|
d = new Private();
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
// set up hotkey capture
|
|
|
|
hotkey_set = false;
|
|
|
|
HotkeyMutex = 0;
|
|
|
|
HotkeyCond = 0;
|
2011-08-13 06:42:09 -06:00
|
|
|
misc_data_mutex=0;
|
2011-12-30 07:12:15 -07:00
|
|
|
last_world_data_ptr = NULL;
|
2012-03-31 18:56:54 -06:00
|
|
|
last_local_map_ptr = NULL;
|
2012-07-05 10:03:02 -06:00
|
|
|
last_pause_state = false;
|
2011-12-30 12:25:50 -07:00
|
|
|
top_viewscreen = NULL;
|
2012-03-04 17:34:04 -07:00
|
|
|
screen_window = NULL;
|
2012-03-14 09:57:29 -06:00
|
|
|
server = NULL;
|
2012-03-15 01:07:43 -06:00
|
|
|
|
|
|
|
color_ostream::log_errors_to_stderr = true;
|
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
|
|
|
|
}
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
std::string Core::getHackPath()
|
|
|
|
{
|
|
|
|
#ifdef LINUX_BUILD
|
|
|
|
return p->getPath() + "/hack/";
|
|
|
|
#else
|
|
|
|
return p->getPath() + "\\hack\\";
|
|
|
|
#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
|
2012-02-11 11:24:44 -07:00
|
|
|
const char * path = "hack/symbols.xml";
|
2011-08-14 00:42:21 -06:00
|
|
|
#else
|
2012-02-11 11:24:44 -07:00
|
|
|
const char * path = "hack\\symbols.xml";
|
2011-08-14 00:42:21 -06:00
|
|
|
#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;
|
2012-02-11 11:24:44 -07:00
|
|
|
out << "Error while reading symbols.xml:\n";
|
2011-11-04 02:08:29 -06:00
|
|
|
out << err.what() << std::endl;
|
|
|
|
delete vif;
|
2011-11-14 01:24:36 -07:00
|
|
|
vif = NULL;
|
2011-11-04 02:08:29 -06:00
|
|
|
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
|
|
|
|
2012-06-14 03:08:39 -06:00
|
|
|
// Init global object pointers
|
|
|
|
df::global::InitGlobals();
|
|
|
|
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Initializing Console.\n";
|
2011-07-31 19:31:52 -06:00
|
|
|
// init the console.
|
2011-11-04 02:08:29 -06:00
|
|
|
bool is_text_mode = false;
|
2012-01-31 09:17:27 -07:00
|
|
|
if(init && init->display.flag.is_set(init_display_flags::TEXT))
|
2011-07-31 19:31:52 -06:00
|
|
|
{
|
2011-11-04 02:08:29 -06:00
|
|
|
is_text_mode = true;
|
2012-06-14 03:08:39 -06:00
|
|
|
con.init(true);
|
|
|
|
cerr << "Console is not available. Use dfhack-run to send commands.\n";
|
2011-07-31 19:31:52 -06:00
|
|
|
}
|
2012-06-14 03:08:39 -06:00
|
|
|
else if(con.init(false))
|
2011-11-04 02:08:29 -06:00
|
|
|
cerr << "Console is running.\n";
|
|
|
|
else
|
|
|
|
fatal ("Console has failed to initialize!\n", false);
|
2012-02-08 19:07:26 -07: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
|
|
|
{
|
2012-02-08 19:07:26 -07:00
|
|
|
//dump << vinfo->PrintOffsets();
|
2011-08-16 15:39:18 -06:00
|
|
|
dump.close();
|
2011-06-14 08:13:28 -06:00
|
|
|
}
|
2012-02-08 19:07:26 -07:00
|
|
|
*/
|
2011-12-24 03:51:58 -07:00
|
|
|
// initialize data defs
|
2011-12-29 05:30:55 -07:00
|
|
|
virtual_identity::Init(this);
|
2011-07-31 19:31:52 -06:00
|
|
|
|
2012-04-15 09:09:25 -06:00
|
|
|
// initialize common lua context
|
|
|
|
Lua::Core::Init(con);
|
|
|
|
|
2011-06-17 07:02:43 -06:00
|
|
|
// create mutex for syncing with interactive tasks
|
2011-08-07 00:41:46 -06:00
|
|
|
misc_data_mutex=new mutex();
|
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-06-25 00:05:17 -06:00
|
|
|
IODATA *temp = new IODATA;
|
|
|
|
temp->core = this;
|
|
|
|
temp->plug_mgr = plug_mgr;
|
2012-06-14 03:08:39 -06:00
|
|
|
if (!is_text_mode)
|
|
|
|
{
|
|
|
|
cerr << "Starting IO thread.\n";
|
|
|
|
// create IO thread
|
|
|
|
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);
|
2012-03-04 17:34:04 -07:00
|
|
|
screen_window = new Windows::top_level_window();
|
2012-03-04 18:24:02 -07:00
|
|
|
screen_window->addChild(new Windows::dfhack_dummy(5,10));
|
2011-07-09 03:33:58 -06:00
|
|
|
started = true;
|
2012-03-14 09:57:29 -06:00
|
|
|
|
|
|
|
cerr << "Starting the TCP listener.\n";
|
|
|
|
server = new ServerMain();
|
|
|
|
if (!server->listen(RemoteClient::GetDefaultPort()))
|
|
|
|
cerr << "TCP listen failed.\n";
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-31 05:40:54 -06:00
|
|
|
void Core::print(const char *format, ...)
|
|
|
|
{
|
|
|
|
color_ostream_proxy proxy(getInstance().con);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args,format);
|
|
|
|
proxy.vprint(format,args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
void Core::printerr(const char *format, ...)
|
|
|
|
{
|
|
|
|
color_ostream_proxy proxy(getInstance().con);
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args,format);
|
|
|
|
proxy.vprinterr(format,args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-04-15 09:09:25 -06:00
|
|
|
bool Core::isSuspended(void)
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
|
|
|
|
return (d->df_suspend_depth > 0 && d->df_suspend_thread == this_thread::get_id());
|
|
|
|
}
|
|
|
|
|
2011-06-16 15:53:39 -06:00
|
|
|
void Core::Suspend()
|
2011-06-14 08:13:28 -06:00
|
|
|
{
|
2012-03-15 05:33:19 -06:00
|
|
|
auto tid = this_thread::get_id();
|
|
|
|
|
|
|
|
// If recursive, just increment the count
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
|
|
|
|
if (d->df_suspend_depth > 0 && d->df_suspend_thread == tid)
|
|
|
|
{
|
|
|
|
d->df_suspend_depth++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:23:13 -06:00
|
|
|
// put the condition on a stack
|
2012-03-15 05:33:19 -06:00
|
|
|
Core::Cond *nc = new Core::Cond();
|
|
|
|
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lock2(d->StackMutex);
|
|
|
|
|
|
|
|
d->suspended_tools.push(nc);
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:23:13 -06:00
|
|
|
// wait until Core::Update() wakes up the tool
|
2012-03-15 05:33:19 -06:00
|
|
|
{
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
|
|
|
|
nc->Lock(&d->AccessMutex);
|
|
|
|
|
|
|
|
assert(d->df_suspend_depth == 0);
|
|
|
|
d->df_suspend_thread = tid;
|
|
|
|
d->df_suspend_depth = 1;
|
|
|
|
}
|
2011-06-16 15:53:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void Core::Resume()
|
|
|
|
{
|
2012-03-15 05:33:19 -06:00
|
|
|
auto tid = this_thread::get_id();
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
|
|
|
|
assert(d->df_suspend_depth > 0 && d->df_suspend_thread == tid);
|
|
|
|
|
|
|
|
if (--d->df_suspend_depth == 0)
|
|
|
|
d->core_cond.Unlock();
|
2011-06-14 08:13:28 -06:00
|
|
|
}
|
|
|
|
|
2012-02-28 04:59:02 -07:00
|
|
|
int Core::TileUpdate()
|
|
|
|
{
|
2012-03-04 17:34:04 -07:00
|
|
|
if(!started)
|
|
|
|
return false;
|
|
|
|
screen_window->paint();
|
2012-02-28 04:59:02 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
int Core::ClaimSuspend(bool force_base)
|
2011-06-14 08:13:28 -06:00
|
|
|
{
|
2012-08-18 01:52:38 -06:00
|
|
|
auto tid = this_thread::get_id();
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
2011-07-11 14:23:13 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
if (force_base || d->df_suspend_depth <= 0)
|
2012-04-03 03:29:59 -06:00
|
|
|
{
|
|
|
|
assert(d->df_suspend_depth == 0);
|
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
d->df_suspend_thread = tid;
|
|
|
|
d->df_suspend_depth = 1000000;
|
|
|
|
return 1000000;
|
|
|
|
}
|
|
|
|
else
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
{
|
2012-08-18 01:52:38 -06:00
|
|
|
assert(d->df_suspend_thread == tid);
|
|
|
|
return ++d->df_suspend_depth;
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
}
|
2012-08-18 01:52:38 -06:00
|
|
|
}
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
void Core::DisclaimSuspend(int level)
|
|
|
|
{
|
|
|
|
auto tid = this_thread::get_id();
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
|
|
|
|
assert(d->df_suspend_depth == level && d->df_suspend_thread == tid);
|
|
|
|
|
|
|
|
if (level == 1000000)
|
|
|
|
d->df_suspend_depth = 0;
|
|
|
|
else
|
|
|
|
--d->df_suspend_depth;
|
|
|
|
}
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
void Core::doUpdate(color_ostream &out, bool first_update)
|
|
|
|
{
|
2012-04-15 09:09:25 -06:00
|
|
|
Lua::Core::Reset(out, "DF code execution");
|
|
|
|
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
if (first_update)
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, SC_CORE_INITIALIZED);
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
|
2011-12-30 07:12:15 -07:00
|
|
|
// detect if the game was loaded or unloaded in the meantime
|
|
|
|
void *new_wdata = NULL;
|
2012-03-31 18:56:54 -06:00
|
|
|
void *new_mapdata = NULL;
|
|
|
|
if (df::global::world)
|
|
|
|
{
|
2011-12-30 07:12:15 -07:00
|
|
|
df::world_data *wdata = df::global::world->world_data;
|
|
|
|
// when the game is unloaded, world_data isn't deleted, but its contents are
|
|
|
|
if (wdata && !wdata->sites.empty())
|
|
|
|
new_wdata = wdata;
|
2012-03-31 18:56:54 -06:00
|
|
|
new_mapdata = df::global::world->map.block_index;
|
2011-12-30 07:12:15 -07:00
|
|
|
}
|
2012-04-01 06:43:40 -06:00
|
|
|
|
2012-03-31 18:56:54 -06:00
|
|
|
// if the world changes
|
|
|
|
if (new_wdata != last_world_data_ptr)
|
|
|
|
{
|
|
|
|
// we check for map change too
|
2012-04-10 02:11:00 -06:00
|
|
|
bool had_map = isMapLoaded();
|
2012-04-01 06:43:40 -06:00
|
|
|
last_world_data_ptr = new_wdata;
|
|
|
|
last_local_map_ptr = new_mapdata;
|
|
|
|
|
|
|
|
getWorld()->ClearPersistentCache();
|
|
|
|
|
2012-03-31 18:56:54 -06:00
|
|
|
// and if the world is going away, we report the map change first
|
2012-04-10 02:11:00 -06:00
|
|
|
if(had_map)
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, SC_MAP_UNLOADED);
|
2012-03-31 18:56:54 -06:00
|
|
|
// and if the world is appearing, we report map change after that
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, new_wdata ? SC_WORLD_LOADED : SC_WORLD_UNLOADED);
|
2012-04-10 02:11:00 -06:00
|
|
|
if(isMapLoaded())
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, SC_MAP_LOADED);
|
2012-03-31 18:56:54 -06:00
|
|
|
}
|
|
|
|
// otherwise just check for map change...
|
|
|
|
else if (new_mapdata != last_local_map_ptr)
|
|
|
|
{
|
2012-04-10 02:11:00 -06:00
|
|
|
bool had_map = isMapLoaded();
|
2012-03-31 18:56:54 -06:00
|
|
|
last_local_map_ptr = new_mapdata;
|
2012-04-10 02:11:00 -06:00
|
|
|
|
|
|
|
if (isMapLoaded() != had_map)
|
|
|
|
{
|
|
|
|
getWorld()->ClearPersistentCache();
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED);
|
2012-04-10 02:11:00 -06:00
|
|
|
}
|
2011-12-30 07:12:15 -07:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
// detect if the viewscreen changed
|
|
|
|
if (df::global::gview)
|
|
|
|
{
|
|
|
|
df::viewscreen *screen = &df::global::gview->view;
|
|
|
|
while (screen->child)
|
|
|
|
screen = screen->child;
|
|
|
|
if (screen != top_viewscreen)
|
|
|
|
{
|
|
|
|
top_viewscreen = screen;
|
2012-05-17 10:04:09 -06:00
|
|
|
onStateChange(out, SC_VIEWSCREEN_CHANGED);
|
2011-12-30 12:25:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-05 10:03:02 -06:00
|
|
|
if (df::global::pause_state)
|
|
|
|
{
|
|
|
|
if (*df::global::pause_state != last_pause_state)
|
|
|
|
{
|
|
|
|
onStateChange(out, last_pause_state ? SC_UNPAUSED : SC_PAUSED);
|
|
|
|
last_pause_state = *df::global::pause_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-17 10:04:09 -06:00
|
|
|
// Execute per-frame handlers
|
|
|
|
onUpdate(out);
|
2012-05-04 10:59:06 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
out << std::flush;
|
|
|
|
}
|
|
|
|
|
|
|
|
// should always be from simulation thread!
|
|
|
|
int Core::Update()
|
|
|
|
{
|
|
|
|
if(errorstate)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
color_ostream_proxy out(con);
|
|
|
|
|
|
|
|
// Pretend this thread has suspended the core in the usual way,
|
|
|
|
// and run various processing hooks.
|
2012-04-03 03:29:59 -06:00
|
|
|
{
|
2012-08-18 01:52:38 -06:00
|
|
|
CoreSuspendClaimer suspend(true);
|
2012-04-03 03:29:59 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
// Initialize the core
|
|
|
|
bool first_update = false;
|
2012-04-03 03:29:59 -06:00
|
|
|
|
2012-08-18 01:52:38 -06:00
|
|
|
if(!started)
|
|
|
|
{
|
|
|
|
first_update = true;
|
|
|
|
Init();
|
|
|
|
if(errorstate)
|
|
|
|
return -1;
|
|
|
|
Lua::Core::Reset(con, "core init");
|
|
|
|
}
|
|
|
|
|
|
|
|
doUpdate(out, first_update);
|
|
|
|
}
|
2011-12-30 07:12:15 -07:00
|
|
|
|
2011-07-11 14:23:13 -06:00
|
|
|
// wake waiting tools
|
|
|
|
// do not allow more tools to join in while we process stuff here
|
2012-03-15 05:33:19 -06:00
|
|
|
lock_guard<mutex> lock_stack(d->StackMutex);
|
|
|
|
|
|
|
|
while (!d->suspended_tools.empty())
|
2011-07-11 14:23:13 -06:00
|
|
|
{
|
2012-03-15 05:33:19 -06:00
|
|
|
Core::Cond * nc = d->suspended_tools.top();
|
|
|
|
d->suspended_tools.pop();
|
|
|
|
|
|
|
|
lock_guard<mutex> lock(d->AccessMutex);
|
|
|
|
// wake tool
|
|
|
|
nc->Unlock();
|
|
|
|
// wait for tool to wake us
|
|
|
|
d->core_cond.Lock(&d->AccessMutex);
|
|
|
|
// verify
|
|
|
|
assert(d->df_suspend_depth == 0);
|
2011-07-11 14:23:13 -06:00
|
|
|
// destroy condition
|
|
|
|
delete nc;
|
2012-04-15 09:09:25 -06:00
|
|
|
// check lua stack depth
|
2012-08-18 01:52:38 -06:00
|
|
|
Lua::Core::Reset(out, "suspend");
|
2011-07-11 14:23:13 -06:00
|
|
|
}
|
2012-03-15 05:33:19 -06:00
|
|
|
|
2011-06-14 08:13:28 -06:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2012-05-17 10:38:27 -06:00
|
|
|
extern bool buildings_do_onupdate;
|
|
|
|
void buildings_onStateChange(color_ostream &out, state_change_event event);
|
|
|
|
void buildings_onUpdate(color_ostream &out);
|
|
|
|
|
|
|
|
static int buildings_timer = 0;
|
|
|
|
|
2012-05-17 10:04:09 -06:00
|
|
|
void Core::onUpdate(color_ostream &out)
|
|
|
|
{
|
2012-05-17 10:38:27 -06:00
|
|
|
// convert building reagents
|
|
|
|
if (buildings_do_onupdate && (++buildings_timer & 1))
|
|
|
|
buildings_onUpdate(out);
|
|
|
|
|
2012-05-17 10:04:09 -06:00
|
|
|
// notify all the plugins that a game tick is finished
|
|
|
|
plug_mgr->OnUpdate(out);
|
|
|
|
|
|
|
|
// process timers in lua
|
|
|
|
Lua::Core::onUpdate(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::onStateChange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
2012-05-17 10:38:27 -06:00
|
|
|
buildings_onStateChange(out, event);
|
|
|
|
|
2012-05-17 10:04:09 -06:00
|
|
|
plug_mgr->OnStateChange(out, event);
|
|
|
|
|
|
|
|
Lua::Core::onStateChange(out, event);
|
|
|
|
}
|
|
|
|
|
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;
|
2012-08-23 09:51:55 -06:00
|
|
|
CoreSuspendClaimer suspend;
|
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
|
2012-01-31 09:55:38 -07:00
|
|
|
for(size_t i = 0 ; i < allModules.size(); i++)
|
2011-06-17 07:02:43 -06:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-27 19:37:56 -07:00
|
|
|
// FIXME: this is HORRIBLY broken
|
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!
|
2012-03-03 06:38:24 -07:00
|
|
|
if(df::global::ui && df::global::gview)
|
2011-07-31 20:40:23 -06:00
|
|
|
{
|
2012-08-24 03:20:08 -06:00
|
|
|
df::viewscreen * ws = Gui::getCurViewscreen();
|
2012-03-03 06:38:24 -07:00
|
|
|
if (strict_virtual_cast<df::viewscreen_dwarfmodest>(ws) &&
|
2012-06-28 07:02:48 -06:00
|
|
|
df::global::ui->main.mode != ui_sidebar_mode::Hotkeys &&
|
|
|
|
df::global::ui->main.hotkeys[idx].cmd == df::ui_hotkey::T_cmd::None)
|
2011-07-31 20:40:23 -06:00
|
|
|
{
|
2012-03-03 06:38:24 -07:00
|
|
|
setHotkeyCmd(df::global::ui->main.hotkeys[idx].name);
|
|
|
|
return false;
|
2011-07-31 20:40:23 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-03 06:38:24 -07:00
|
|
|
out = in;
|
|
|
|
return true;
|
2011-07-31 20:40:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out = in;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-13 18:25:15 -06:00
|
|
|
int UnicodeAwareSym(const SDL::KeyboardEvent& ke)
|
2012-03-09 01:26:31 -07:00
|
|
|
{
|
2012-03-09 02:07:47 -07:00
|
|
|
// Assume keyboard layouts don't change the order of numbers:
|
2012-06-13 18:15:43 -06:00
|
|
|
if( '0' <= ke.ksym.sym && ke.ksym.sym <= '9') return ke.ksym.sym;
|
|
|
|
if(SDL::K_F1 <= ke.ksym.sym && ke.ksym.sym <= SDL::K_F12) return ke.ksym.sym;
|
2012-03-09 01:26:31 -07:00
|
|
|
|
2012-05-16 08:10:07 -06:00
|
|
|
// These keys are mapped to the same control codes as Ctrl-?
|
2012-06-13 18:15:43 -06:00
|
|
|
switch (ke.ksym.sym)
|
|
|
|
{
|
|
|
|
case SDL::K_RETURN:
|
|
|
|
case SDL::K_KP_ENTER:
|
|
|
|
case SDL::K_TAB:
|
|
|
|
case SDL::K_ESCAPE:
|
|
|
|
case SDL::K_DELETE:
|
|
|
|
return ke.ksym.sym;
|
|
|
|
default:
|
|
|
|
break;
|
2012-05-16 08:10:07 -06:00
|
|
|
}
|
|
|
|
|
2012-06-13 18:15:43 -06:00
|
|
|
int unicode = ke.ksym.unicode;
|
2012-03-09 01:26:31 -07:00
|
|
|
|
|
|
|
// convert Ctrl characters to their 0x40-0x5F counterparts:
|
|
|
|
if (unicode < ' ')
|
2012-06-13 18:15:43 -06:00
|
|
|
{
|
2012-03-09 01:26:31 -07:00
|
|
|
unicode += 'A' - 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:07:47 -07:00
|
|
|
// convert A-Z to their a-z counterparts:
|
|
|
|
if('A' < unicode && unicode < 'Z')
|
|
|
|
{
|
2012-03-09 01:26:31 -07:00
|
|
|
unicode += 'a' - 'A';
|
2012-06-13 18:15:43 -06:00
|
|
|
}
|
2012-03-09 02:07:47 -07:00
|
|
|
|
|
|
|
// convert various other punctuation marks:
|
|
|
|
if('\"' == unicode) unicode = '\'';
|
|
|
|
if('+' == unicode) unicode = '=';
|
|
|
|
if(':' == unicode) unicode = ';';
|
|
|
|
if('<' == unicode) unicode = ',';
|
|
|
|
if('>' == unicode) unicode = '.';
|
|
|
|
if('?' == unicode) unicode = '/';
|
|
|
|
if('{' == unicode) unicode = '[';
|
|
|
|
if('|' == unicode) unicode = '\\';
|
|
|
|
if('}' == unicode) unicode = ']';
|
|
|
|
if('~' == unicode) unicode = '`';
|
|
|
|
|
|
|
|
return unicode;
|
2012-03-09 01:26:31 -07:00
|
|
|
}
|
|
|
|
|
2012-06-13 18:15:43 -06:00
|
|
|
|
2012-02-28 04:59:02 -07:00
|
|
|
//MEMO: return false if event is consumed
|
2012-06-13 18:15:43 -06:00
|
|
|
int Core::DFH_SDL_Event(SDL::Event* ev)
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
2011-07-09 03:33:58 -06:00
|
|
|
// do NOT process events before we are ready.
|
2012-02-27 19:37:56 -07:00
|
|
|
if(!started) return true;
|
2011-07-09 03:33:58 -06:00
|
|
|
if(!ev)
|
2012-02-27 19:37:56 -07:00
|
|
|
return true;
|
2012-06-13 18:15:43 -06:00
|
|
|
if(ev && (ev->type == SDL::ET_KEYDOWN || ev->type == SDL::ET_KEYUP))
|
2011-07-07 19:55:37 -06:00
|
|
|
{
|
2012-06-13 18:15:43 -06:00
|
|
|
auto ke = (SDL::KeyboardEvent *)ev;
|
2012-02-11 11:24:44 -07:00
|
|
|
|
2012-06-13 18:15:43 -06:00
|
|
|
if(ke->state == SDL::BTN_PRESSED && !hotkey_states[ke->ksym.sym])
|
2011-07-09 03:33:58 -06:00
|
|
|
{
|
2012-06-13 18:15:43 -06:00
|
|
|
hotkey_states[ke->ksym.sym] = true;
|
2011-12-30 12:25:50 -07:00
|
|
|
|
|
|
|
int mod = 0;
|
2012-06-13 18:15:43 -06:00
|
|
|
if (ke->ksym.mod & SDL::KMOD_SHIFT) mod |= 1;
|
|
|
|
if (ke->ksym.mod & SDL::KMOD_CTRL) mod |= 2;
|
|
|
|
if (ke->ksym.mod & SDL::KMOD_ALT) mod |= 4;
|
2011-12-30 12:25:50 -07:00
|
|
|
|
2012-03-09 01:26:31 -07:00
|
|
|
// Use unicode so Windows gives the correct value for the
|
|
|
|
// user's Input Language
|
2012-06-13 18:15:43 -06:00
|
|
|
if((ke->ksym.unicode & 0xff80) == 0)
|
2012-03-09 02:07:47 -07:00
|
|
|
{
|
|
|
|
int key = UnicodeAwareSym(*ke);
|
2012-03-09 01:26:31 -07:00
|
|
|
SelectHotkey(key, mod);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Pretend non-ascii characters don't happen:
|
2012-06-13 18:15:43 -06:00
|
|
|
SelectHotkey(ke->ksym.sym, mod);
|
2012-03-09 01:26:31 -07:00
|
|
|
}
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
2012-06-13 18:15:43 -06:00
|
|
|
else if(ke->state == SDL::BTN_RELEASED)
|
2011-07-09 03:33:58 -06:00
|
|
|
{
|
2012-06-13 18:15:43 -06:00
|
|
|
hotkey_states[ke->ksym.sym] = false;
|
2011-07-09 03:33:58 -06:00
|
|
|
}
|
2011-07-07 19:55:37 -06:00
|
|
|
}
|
2012-02-27 19:37:56 -07:00
|
|
|
return true;
|
2011-07-09 03:33:58 -06:00
|
|
|
// do stuff with the events...
|
2011-07-07 19:55:37 -06:00
|
|
|
}
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
bool Core::SelectHotkey(int sym, int modifiers)
|
|
|
|
{
|
|
|
|
// Find the topmost viewscreen
|
|
|
|
if (!df::global::gview || !df::global::ui)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
df::viewscreen *screen = &df::global::gview->view;
|
|
|
|
while (screen->child)
|
|
|
|
screen = screen->child;
|
|
|
|
|
2012-06-13 18:15:43 -06:00
|
|
|
if (sym == SDL::K_KP_ENTER)
|
|
|
|
sym = SDL::K_RETURN;
|
2012-05-19 11:31:42 -06:00
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
std::string cmd;
|
2012-05-19 11:31:42 -06:00
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
{
|
|
|
|
tthread::lock_guard<tthread::mutex> lock(*HotkeyMutex);
|
2012-05-19 11:31:42 -06:00
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
// Check the internal keybindings
|
|
|
|
std::vector<KeyBinding> &bindings = key_bindings[sym];
|
|
|
|
for (int i = bindings.size()-1; i >= 0; --i) {
|
|
|
|
if (bindings[i].modifiers != modifiers)
|
|
|
|
continue;
|
2012-05-19 11:31:42 -06:00
|
|
|
if (!bindings[i].focus.empty() &&
|
|
|
|
!prefix_matches(bindings[i].focus, Gui::getFocusString(screen)))
|
|
|
|
continue;
|
2011-12-30 12:25:50 -07:00
|
|
|
if (!plug_mgr->CanInvokeHotkey(bindings[i].command[0], screen))
|
|
|
|
continue;
|
|
|
|
cmd = bindings[i].cmdline;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd.empty()) {
|
|
|
|
// Check the hotkey keybindings
|
2012-06-13 18:15:43 -06:00
|
|
|
int idx = sym - SDL::K_F1;
|
2011-12-30 12:25:50 -07:00
|
|
|
if(idx >= 0 && idx < 8)
|
|
|
|
{
|
|
|
|
if (modifiers & 1)
|
|
|
|
idx += 8;
|
|
|
|
|
2012-01-01 12:05:45 -07:00
|
|
|
if (strict_virtual_cast<df::viewscreen_dwarfmodest>(screen) &&
|
2012-06-28 07:02:48 -06:00
|
|
|
df::global::ui->main.mode != ui_sidebar_mode::Hotkeys &&
|
|
|
|
df::global::ui->main.hotkeys[idx].cmd == df::ui_hotkey::T_cmd::None)
|
2011-12-30 12:25:50 -07:00
|
|
|
{
|
|
|
|
cmd = df::global::ui->main.hotkeys[idx].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmd.empty()) {
|
|
|
|
setHotkeyCmd(cmd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-19 11:31:42 -06:00
|
|
|
static bool parseKeySpec(std::string keyspec, int *psym, int *pmod, std::string *pfocus)
|
2011-12-30 12:25:50 -07:00
|
|
|
{
|
|
|
|
*pmod = 0;
|
|
|
|
|
2012-05-19 11:31:42 -06:00
|
|
|
if (pfocus)
|
|
|
|
{
|
|
|
|
*pfocus = "";
|
|
|
|
|
|
|
|
size_t idx = keyspec.find('@');
|
|
|
|
if (idx != std::string::npos)
|
|
|
|
{
|
|
|
|
*pfocus = keyspec.substr(idx+1);
|
|
|
|
keyspec = keyspec.substr(0, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
// ugh, ugly
|
|
|
|
for (;;) {
|
|
|
|
if (keyspec.size() > 6 && keyspec.substr(0, 6) == "Shift-") {
|
|
|
|
*pmod |= 1;
|
|
|
|
keyspec = keyspec.substr(6);
|
|
|
|
} else if (keyspec.size() > 5 && keyspec.substr(0, 5) == "Ctrl-") {
|
|
|
|
*pmod |= 2;
|
|
|
|
keyspec = keyspec.substr(5);
|
|
|
|
} else if (keyspec.size() > 4 && keyspec.substr(0, 4) == "Alt-") {
|
|
|
|
*pmod |= 4;
|
|
|
|
keyspec = keyspec.substr(4);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyspec.size() == 1 && keyspec[0] >= 'A' && keyspec[0] <= 'Z') {
|
2012-06-13 18:15:43 -06:00
|
|
|
*psym = SDL::K_a + (keyspec[0]-'A');
|
2011-12-30 12:25:50 -07:00
|
|
|
return true;
|
|
|
|
} else if (keyspec.size() == 2 && keyspec[0] == 'F' && keyspec[1] >= '1' && keyspec[1] <= '9') {
|
2012-06-13 18:15:43 -06:00
|
|
|
*psym = SDL::K_F1 + (keyspec[1]-'1');
|
2011-12-30 12:25:50 -07:00
|
|
|
return true;
|
2012-05-19 11:31:42 -06:00
|
|
|
} else if (keyspec == "Enter") {
|
2012-06-13 18:15:43 -06:00
|
|
|
*psym = SDL::K_RETURN;
|
2012-05-19 11:31:42 -06:00
|
|
|
return true;
|
2011-12-30 12:25:50 -07:00
|
|
|
} else
|
2012-03-04 17:34:04 -07:00
|
|
|
return false;
|
2011-12-30 12:25:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::ClearKeyBindings(std::string keyspec)
|
|
|
|
{
|
|
|
|
int sym, mod;
|
2012-05-19 11:31:42 -06:00
|
|
|
std::string focus;
|
|
|
|
if (!parseKeySpec(keyspec, &sym, &mod, &focus))
|
2011-12-30 12:25:50 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
tthread::lock_guard<tthread::mutex> lock(*HotkeyMutex);
|
|
|
|
|
|
|
|
std::vector<KeyBinding> &bindings = key_bindings[sym];
|
|
|
|
for (int i = bindings.size()-1; i >= 0; --i) {
|
2012-05-19 11:31:42 -06:00
|
|
|
if (bindings[i].modifiers == mod && prefix_matches(focus, bindings[i].focus))
|
2011-12-30 12:25:50 -07:00
|
|
|
bindings.erase(bindings.begin()+i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Core::AddKeyBinding(std::string keyspec, std::string cmdline)
|
|
|
|
{
|
|
|
|
int sym;
|
|
|
|
KeyBinding binding;
|
2012-05-19 11:31:42 -06:00
|
|
|
if (!parseKeySpec(keyspec, &sym, &binding.modifiers, &binding.focus))
|
2011-12-30 12:25:50 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
cheap_tokenise(cmdline, binding.command);
|
|
|
|
if (binding.command.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tthread::lock_guard<tthread::mutex> lock(*HotkeyMutex);
|
|
|
|
|
2011-12-31 02:25:46 -07:00
|
|
|
// Don't add duplicates
|
|
|
|
std::vector<KeyBinding> &bindings = key_bindings[sym];
|
|
|
|
for (int i = bindings.size()-1; i >= 0; --i) {
|
|
|
|
if (bindings[i].modifiers == binding.modifiers &&
|
2012-05-19 11:31:42 -06:00
|
|
|
bindings[i].cmdline == cmdline &&
|
|
|
|
bindings[i].focus == binding.focus)
|
2011-12-31 02:25:46 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-30 12:25:50 -07:00
|
|
|
binding.cmdline = cmdline;
|
2011-12-31 02:25:46 -07:00
|
|
|
bindings.push_back(binding);
|
2011-12-30 12:25:50 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-31 02:25:46 -07:00
|
|
|
std::vector<std::string> Core::ListKeyBindings(std::string keyspec)
|
2011-12-30 12:25:50 -07:00
|
|
|
{
|
2011-12-31 02:25:46 -07:00
|
|
|
int sym, mod;
|
|
|
|
std::vector<std::string> rv;
|
|
|
|
if (!parseKeySpec(keyspec, &sym, &mod))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
tthread::lock_guard<tthread::mutex> lock(*HotkeyMutex);
|
|
|
|
|
|
|
|
std::vector<KeyBinding> &bindings = key_bindings[sym];
|
|
|
|
for (int i = bindings.size()-1; i >= 0; --i) {
|
|
|
|
if (bindings[i].modifiers == mod)
|
2012-05-19 11:31:42 -06:00
|
|
|
{
|
|
|
|
std::string cmd = bindings[i].cmdline;
|
|
|
|
if (!bindings[i].focus.empty())
|
|
|
|
cmd = "@" + bindings[i].focus + ": " + cmd;
|
|
|
|
rv.push_back(cmd);
|
|
|
|
}
|
2011-12-31 02:25:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2011-12-30 12:25:50 -07: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);
|
|
|
|
}
|
|
|
|
|
2012-08-17 04:32:04 -06:00
|
|
|
bool Process::patchMemory(void *target, const void* src, size_t count)
|
|
|
|
{
|
|
|
|
uint8_t *sptr = (uint8_t*)target;
|
|
|
|
uint8_t *eptr = sptr + count;
|
|
|
|
|
|
|
|
// Find the valid memory ranges
|
|
|
|
std::vector<t_memrange> ranges;
|
|
|
|
getMemRanges(ranges);
|
|
|
|
|
|
|
|
unsigned start = 0;
|
|
|
|
while (start < ranges.size() && ranges[start].end <= sptr)
|
|
|
|
start++;
|
|
|
|
if (start >= ranges.size() || ranges[start].start > sptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned end = start+1;
|
|
|
|
while (end < ranges.size() && ranges[end].start < eptr)
|
|
|
|
{
|
|
|
|
if (ranges[end].start != ranges[end-1].end)
|
|
|
|
return false;
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
if (ranges[end-1].end < eptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Verify current permissions
|
|
|
|
for (unsigned i = start; i < end; i++)
|
|
|
|
if (!ranges[i].valid || !(ranges[i].read || ranges[i].execute) || ranges[i].shared)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Apply writable permissions & update
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
for (unsigned i = start; i < end && ok; i++)
|
|
|
|
{
|
|
|
|
t_memrange perms = ranges[i];
|
|
|
|
perms.write = perms.read = true;
|
|
|
|
if (!setPermisions(perms, perms))
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
memmove(target, src, count);
|
|
|
|
|
|
|
|
for (unsigned i = start; i < end && ok; i++)
|
|
|
|
setPermisions(ranges[i], ranges[i]);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
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(World);
|
|
|
|
MODULE_GETTER(Materials);
|
2011-07-20 19:26:52 -06:00
|
|
|
MODULE_GETTER(Notes);
|
2011-12-07 12:37:09 -07:00
|
|
|
MODULE_GETTER(Graphic);
|