2011-07-16 08:22:45 -06:00
|
|
|
#include <dfhack/Core.h>
|
|
|
|
#include <dfhack/Console.h>
|
|
|
|
#include <dfhack/Export.h>
|
|
|
|
#include <dfhack/PluginManager.h>
|
2011-07-17 03:00:29 -06:00
|
|
|
#include <dfhack/Process.h>
|
2011-07-27 19:08:57 -06:00
|
|
|
#include "dfhack/extra/stopwatch.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-04 15:22:25 -06:00
|
|
|
#include "lua_Misc.h"
|
2011-07-17 03:00:29 -06:00
|
|
|
#include "functioncall.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-07-27 19:08:57 -06:00
|
|
|
uint64_t timeLast=0;
|
2011-07-16 10:29:46 -06:00
|
|
|
|
2011-07-16 08:22:45 -06:00
|
|
|
DFhackCExport command_result dfusion (Core * c, vector <string> & parameters);
|
2011-07-16 15:11:21 -06:00
|
|
|
DFhackCExport command_result lua_run (Core * c, vector <string> & parameters);
|
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)
|
|
|
|
{
|
|
|
|
commands.clear();
|
2011-07-16 13:08:58 -06:00
|
|
|
//maybe remake it to run automaticaly
|
|
|
|
lua::RegisterConsole(lua::glua::Get(),&c->con);
|
2011-07-27 18:11:33 -06:00
|
|
|
lua::RegisterProcess(lua::glua::Get(),c->p);
|
2011-08-03 07:07:57 -06:00
|
|
|
lua::RegisterHexsearch(lua::glua::Get());
|
2011-08-04 15:22:25 -06:00
|
|
|
lua::RegisterMisc(lua::glua::Get());
|
2011-07-16 13:08:58 -06:00
|
|
|
commands.push_back(PluginCommand("dfusion","Init dfusion system.",dfusion));
|
2011-07-16 15:11:21 -06:00
|
|
|
commands.push_back(PluginCommand("lua", "Run interactive interpreter.\
|
|
|
|
\n Options: <filename> = run <filename> instead",lua_run));
|
|
|
|
|
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-07-16 10:29:46 -06:00
|
|
|
|
2011-07-16 08:22:45 -06:00
|
|
|
// shutdown stuff
|
2011-07-27 14:10:20 -06:00
|
|
|
delete mymutex;
|
2011-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onupdate ( Core * c )
|
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)
|
|
|
|
{
|
|
|
|
c->con.printerr("Error OnTick:%s\n",e.what());
|
2011-08-08 15:30:30 -06:00
|
|
|
c->con.printerr("%s",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-16 13:23:44 -06:00
|
|
|
c->con.msleep(1000);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2011-07-27 14:41:56 -06:00
|
|
|
void InterpreterLoop(Core* c)
|
|
|
|
{
|
|
|
|
Console &con=c->con;
|
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
string curline;
|
|
|
|
con.print("Type quit to exit interactive mode\n");
|
|
|
|
con.lineedit(">>",curline);
|
|
|
|
|
|
|
|
while (curline!="quit") {
|
2011-07-27 19:08:57 -06:00
|
|
|
con.history_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());
|
2011-08-08 15:30:30 -06:00
|
|
|
c->con.printerr("%s",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-27 14:41:56 -06:00
|
|
|
s.settop(0);
|
|
|
|
}
|
|
|
|
con.lineedit(">>",curline);
|
|
|
|
}
|
|
|
|
s.settop(0);
|
|
|
|
}
|
2011-07-16 15:11:21 -06:00
|
|
|
DFhackCExport command_result lua_run (Core * c, vector <string> & parameters)
|
|
|
|
{
|
|
|
|
Console &con=c->con;
|
2011-07-27 14:10:20 -06:00
|
|
|
mymutex->lock();
|
2011-07-16 15:11:21 -06:00
|
|
|
lua::state s=lua::glua::Get();
|
|
|
|
if(parameters.size()>0)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
s.loadfile(parameters[0]); //load file
|
|
|
|
s.pcall(0,0);// run it
|
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
|
|
|
con.printerr("Error:%s\n",e.what());
|
2011-08-08 15:30:30 -06:00
|
|
|
c->con.printerr("%s",lua::DebugDump(lua::glua::Get()).c_str());
|
2011-07-16 15:11:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-27 14:41:56 -06:00
|
|
|
InterpreterLoop(c);
|
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;
|
|
|
|
}
|
2011-07-16 08:22:45 -06:00
|
|
|
DFhackCExport command_result dfusion (Core * c, vector <string> & parameters)
|
|
|
|
{
|
2011-07-16 15:11:21 -06:00
|
|
|
|
2011-07-16 09:34:24 -06:00
|
|
|
Console &con=c->con;
|
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-07-16 13:08:58 -06:00
|
|
|
s.loadfile("dfusion/init.lua"); //load script
|
|
|
|
s.pcall(0,0);// run it
|
2011-07-16 10:29:46 -06:00
|
|
|
}
|
|
|
|
catch(lua::exception &e)
|
|
|
|
{
|
2011-07-16 11:43:57 -06:00
|
|
|
con.printerr("Error:%s\n",e.what());
|
2011-08-08 15:30:30 -06:00
|
|
|
c->con.printerr("%s",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-07-16 08:22:45 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|