2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "MemAccess.h"
|
|
|
|
#include "MiscUtils.h"
|
2011-07-16 08:22:45 -06:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2011-07-16 11:43:57 -06:00
|
|
|
|
2011-07-27 14:10:20 -06:00
|
|
|
|
|
|
|
#include "tinythread.h"
|
|
|
|
|
|
|
|
|
2011-07-16 13:08:58 -06:00
|
|
|
#include "luamain.h"
|
|
|
|
#include "lua_Console.h"
|
2011-07-27 18:11:33 -06:00
|
|
|
#include "lua_Process.h"
|
2011-08-03 07:07:57 -06:00
|
|
|
#include "lua_Hexsearch.h"
|
2011-08-13 06:42:09 -06:00
|
|
|
#include "lua_Misc.h"
|
2011-08-08 17:46:32 -06:00
|
|
|
#include "lua_VersionInfo.h"
|
2011-07-17 03:00:29 -06:00
|
|
|
#include "functioncall.h"
|
2011-12-04 09:45:34 -07:00
|
|
|
#include "lua_FunctionCall.h"
|
2012-03-04 05:45:15 -07:00
|
|
|
#include "lua_Offsets.h"
|
2011-07-16 10:29:46 -06:00
|
|
|
|
2011-07-16 08:22:45 -06:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using namespace DFHack;
|
|
|
|
|
2011-07-27 14:10:20 -06:00
|
|
|
static tthread::mutex* mymutex=0;
|
2011-08-19 09:09:00 -06:00
|
|
|
static tthread::thread* thread_dfusion=0;
|
2011-07-27 19:08:57 -06:00
|
|
|
uint64_t timeLast=0;
|
2011-07-16 10:29:46 -06:00
|
|
|
|
2012-02-22 15:41:27 -07:00
|
|
|
DFHACK_PLUGIN("dfusion")
|
|
|
|
|
2012-03-11 06:33:08 -06:00
|
|
|
command_result dfusion (color_ostream &out, std::vector <std::string> ¶meters);
|
|
|
|
command_result dfuse (color_ostream &out, std::vector <std::string> ¶meters);
|
|
|
|
command_result lua_run (color_ostream &out, std::vector <std::string> ¶meters);
|
|
|
|
command_result lua_run_file (color_ostream &out, std::vector <std::string> ¶meters);
|
2011-07-16 08:22:45 -06:00
|
|
|
|
|
|
|
DFhackCExport const char * plugin_name ( void )
|
|
|
|
{
|
|
|
|
return "dfusion";
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
2011-08-20 15:15:40 -06:00
|
|
|
lua::state st=lua::glua::Get();
|
2011-07-16 13:08:58 -06:00
|
|
|
//maybe remake it to run automaticaly
|
2012-03-11 06:33:08 -06:00
|
|
|
lua::RegisterConsole(st);
|
2011-08-20 15:15:40 -06:00
|
|
|
lua::RegisterProcess(st,c->p);
|
|
|
|
lua::RegisterHexsearch(st);
|
|
|
|
lua::RegisterMisc(st);
|
|
|
|
lua::RegisterVersionInfo(st);
|
2011-12-04 09:45:34 -07:00
|
|
|
lua::RegisterFunctionCall(st);
|
2012-03-04 05:45:15 -07:00
|
|
|
lua::RegisterEngine(st);
|
2011-12-04 09:45:34 -07:00
|
|
|
|
2011-08-20 15:15:40 -06:00
|
|
|
#ifdef LINUX_BUILD
|
|
|
|
st.push(1);
|
|
|
|
st.setglobal("LINUX");
|
|
|
|
#else
|
|
|
|
st.push(1);
|
|
|
|
st.setglobal("WINDOWS");
|
|
|
|
#endif
|
|
|
|
|
2012-03-11 06:33:08 -06:00
|
|
|
commands.push_back(PluginCommand("dfusion","Run dfusion system (interactive i.e. can input further commands).",dfusion,true));
|
|
|
|
commands.push_back(PluginCommand("dfuse","Init dfusion system (not interactive).",dfuse,false));
|
|
|
|
commands.push_back(PluginCommand("lua", "Run interactive interpreter. Use 'lua <filename>' to run <filename> instead.",lua_run,true));
|
|
|
|
commands.push_back(PluginCommand("runlua", "Run non-interactive interpreter. Use 'runlua <filename>' to run <filename>.",lua_run_file,false));
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex=new tthread::mutex;
|
2011-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( Core * c )
|
|
|
|
{
|
2011-08-08 17:46:32 -06:00
|
|
|
|
2011-07-16 08:22:45 -06:00
|
|
|
// shutdown stuff
|
2011-08-19 09:09:00 -06:00
|
|
|
if(thread_dfusion)
|
|
|
|
delete thread_dfusion;
|
2011-07-27 14:10:20 -06:00
|
|
|
delete mymutex;
|
2011-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-19 09:09:00 -06:00
|
|
|
DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
|
2011-08-08 17:46:32 -06:00
|
|
|
{
|
2011-07-27 19:08:57 -06:00
|
|
|
uint64_t time2 = GetTimeMs64();
|
|
|
|
uint64_t delta = time2-timeLast;
|
|
|
|
if(delta<100)
|
|
|
|
return CR_OK;
|
|
|
|
timeLast = time2;
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->lock();
|
2011-07-16 11:43:57 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
s.getglobal("OnTick");
|
|
|
|
if(s.is<lua::function>())
|
|
|
|
{
|
2011-07-16 13:23:44 -06:00
|
|
|
try{
|
|
|
|
s.pcall();
|
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
c->getConsole().printerr("Error OnTick:%s\n",e.what());
|
|
|
|
c->getConsole().printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
|
|
|
|
c->getConsole().msleep(1000);
|
2011-07-16 13:23:44 -06:00
|
|
|
}
|
2011-07-16 11:43:57 -06:00
|
|
|
}
|
|
|
|
s.settop(0);
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->unlock();
|
2011-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-11 06:33:08 -06:00
|
|
|
void InterpreterLoop(color_ostream &out)
|
2011-07-27 14:41:56 -06:00
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
|
2011-08-13 06:42:09 -06:00
|
|
|
DFHack::CommandHistory hist;
|
2011-07-27 14:41:56 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
string curline;
|
2012-03-11 06:33:08 -06:00
|
|
|
out.print("Type quit to exit interactive mode\n");
|
|
|
|
assert(out.is_console());
|
|
|
|
Console &con = static_cast<Console&>(out);
|
2011-08-13 06:42:09 -06:00
|
|
|
con.lineedit(">>",curline,hist);
|
2011-08-08 17:46:32 -06:00
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
while (curline!="quit") {
|
2011-08-13 06:42:09 -06:00
|
|
|
hist.add(curline);
|
2011-07-27 14:41:56 -06:00
|
|
|
try
|
|
|
|
{
|
|
|
|
s.loadstring(curline);
|
|
|
|
s.pcall();
|
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
|
|
|
con.printerr("Error:%s\n",e.what());
|
2012-03-11 06:33:08 -06:00
|
|
|
con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-27 14:41:56 -06:00
|
|
|
s.settop(0);
|
|
|
|
}
|
2011-08-13 06:42:09 -06:00
|
|
|
con.lineedit(">>",curline,hist);
|
2011-07-27 14:41:56 -06:00
|
|
|
}
|
|
|
|
s.settop(0);
|
|
|
|
}
|
2012-03-11 06:33:08 -06:00
|
|
|
command_result lua_run_file (color_ostream &out, std::vector <std::string> ¶meters)
|
|
|
|
{
|
|
|
|
if(parameters.size()==0)
|
|
|
|
{
|
|
|
|
out.printerr("runlua without file to run!");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
return lua_run(out,parameters);
|
|
|
|
}
|
|
|
|
command_result lua_run (color_ostream &out, std::vector <std::string> ¶meters)
|
2011-07-16 15:11:21 -06:00
|
|
|
{
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->lock();
|
2011-07-16 15:11:21 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
2012-03-11 06:33:08 -06:00
|
|
|
lua::SetConsole(s,out);
|
2011-07-16 15:11:21 -06:00
|
|
|
if(parameters.size()>0)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
s.loadfile(parameters[0]); //load file
|
|
|
|
s.pcall(0,0);// run it
|
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
out.printerr("Error:%s\n",e.what());
|
|
|
|
out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-16 15:11:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
InterpreterLoop(out);
|
2011-07-16 15:11:21 -06:00
|
|
|
}
|
|
|
|
s.settop(0);// clean up
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->unlock();
|
2011-07-16 15:11:21 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-03-11 06:33:08 -06:00
|
|
|
void RunDfusion(color_ostream &out)
|
2011-07-16 08:22:45 -06:00
|
|
|
{
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->lock();
|
2011-07-16 13:08:58 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
2011-07-16 10:29:46 -06:00
|
|
|
try{
|
2011-09-17 05:59:10 -06:00
|
|
|
s.getglobal("err");
|
|
|
|
int errpos=s.gettop();
|
2011-07-16 13:08:58 -06:00
|
|
|
s.loadfile("dfusion/init.lua"); //load script
|
2011-09-17 05:59:10 -06:00
|
|
|
|
|
|
|
s.pcall(0,0,errpos);// run it
|
2011-07-16 10:29:46 -06:00
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
out.printerr("Error:%s\n",e.what());
|
|
|
|
out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-16 10:29:46 -06:00
|
|
|
}
|
2011-07-16 13:08:58 -06:00
|
|
|
s.settop(0);// clean up
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->unlock();
|
2011-08-19 09:09:00 -06:00
|
|
|
}
|
2012-03-11 06:33:08 -06:00
|
|
|
command_result dfuse(color_ostream &out, std::vector <std::string> ¶meters)
|
2011-08-19 09:09:00 -06:00
|
|
|
{
|
2012-03-11 06:33:08 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
lua::SetConsole(s,out);
|
|
|
|
s.push(1);
|
|
|
|
s.setglobal("INIT");
|
|
|
|
RunDfusion(out);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
command_result dfusion (color_ostream &out, std::vector <std::string> ¶meters)
|
|
|
|
{
|
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
lua::SetConsole(s,out);
|
|
|
|
s.push();
|
|
|
|
s.setglobal("INIT");
|
|
|
|
RunDfusion(out);
|
2011-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|