Moved lua out of dfusion.

Now lua is a script. supports --file (or -f) flag, usage: lua <command> or lua --file <filename> or just "lua" for interactive interpreter.
develop
Warmist 2012-09-25 00:24:37 +03:00
parent 28354715ff
commit bd2f3a9998
2 changed files with 10 additions and 62 deletions

@ -33,8 +33,6 @@ DFHACK_PLUGIN("dfusion")
command_result dfusion (color_ostream &out, std::vector <std::string> &parameters); command_result dfusion (color_ostream &out, std::vector <std::string> &parameters);
command_result dfuse (color_ostream &out, std::vector <std::string> &parameters); command_result dfuse (color_ostream &out, std::vector <std::string> &parameters);
command_result lua_run (color_ostream &out, std::vector <std::string> &parameters);
command_result lua_run_file (color_ostream &out, std::vector <std::string> &parameters);
DFhackCExport const char * plugin_name ( void ) DFhackCExport const char * plugin_name ( void )
{ {
@ -65,8 +63,6 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector <Plugi
commands.push_back(PluginCommand("dfusion","Run dfusion system (interactive i.e. can input further commands).",dfusion,true)); 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("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));
mymutex=new tthread::mutex; mymutex=new tthread::mutex;
return CR_OK; return CR_OK;
} }
@ -107,64 +103,6 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
mymutex->unlock(); mymutex->unlock();
return CR_OK; return CR_OK;
} }
command_result lua_run_file (color_ostream &out, std::vector <std::string> &parameters)
{
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> &parameters)
{
if (!parameters.empty())
{
if (parameters[0] == "--core-context")
{
Lua::InterpreterLoop(out, Lua::Core::State, "core lua");
return CR_OK;
}
else if (parameters[0] == "--core-reload")
{
CoreSuspender suspend;
for (size_t i = 1; i < parameters.size(); i++)
{
lua_getglobal(Lua::Core::State, "reload");
lua_pushstring(Lua::Core::State, parameters[i].c_str());
Lua::SafeCall(out, Lua::Core::State, 1, 0);
}
return CR_OK;
}
}
mymutex->lock();
lua::state s=lua::glua::Get();
if(parameters.size()>0)
{
try{
s.loadfile(parameters[0]); //load file
for(size_t i=1;i<parameters.size();i++)
s.push(parameters[i]);
Lua::SafeCall(out, s, parameters.size()-1,0);// run it
}
catch(lua::exception &e)
{
out.printerr("Error:%s\n",e.what());
out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
}
}
else
{
Lua::InterpreterLoop(out, s);
}
s.settop(0);// clean up
mymutex->unlock();
return CR_OK;
}
void RunDfusion(color_ostream &out, std::vector <std::string> &parameters) void RunDfusion(color_ostream &out, std::vector <std::string> &parameters)
{ {
mymutex->lock(); mymutex->lock();

@ -0,0 +1,10 @@
local args={...}
if args[1]=="--file" or args[1]=="-f" then
local f=loadfile (args[2])
dfhack.pcall(f,table.unpack(args,3))
elseif args[1]~=nil then
local f=load(args[1],'=(lua command)', 't',)
dfhack.pcall(f,table.unpack(args,2))
else
dfhack.interpreter("lua","lua.history")
end