/*************************************************************************** * Copyright (C) 2004 by Janos Horvath * * bourne@freemail.hu * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include "cconsole.h" #include "cpilua.h" #include "cluainterpreter.h" #include using namespace nDirectConnect; using namespace nStringUtils; namespace nScripts { cConsole::cConsole(cpiLua *lua) : mLua(lua), mCmdLuaScriptAdd(1,"!luaload ", "(.*)", &mcfLuaScriptAdd), mCmdLuaScriptGet(0,"!lualist", "", &mcfLuaScriptGet), mCmdLuaScriptDel(2,"!luaunload ", "(.*)", &mcfLuaScriptDel), mCmdLuaScriptRe(2,"!luareload ", "(.*)", &mcfLuaScriptRe), mCmdr(this) { mCmdr.Add(&mCmdLuaScriptAdd); mCmdr.Add(&mCmdLuaScriptDel); mCmdr.Add(&mCmdLuaScriptGet); mCmdr.Add(&mCmdLuaScriptRe); } cConsole::~cConsole() { } int cConsole::DoCommand(const string &str, cConnDC * conn) { ostringstream os; if(mCmdr.ParseAll(str, os, conn) >= 0) { mLua->mServer->DCPublicHS(os.str().c_str(),conn); return 1; } return 0; } bool cConsole::cfGetLuaScript::operator()() { (*mOS) << "Loaded LUA scripts:" << "\r\n"; for(int i = 0; i < GetPI()->Size(); i++) { (*mOS) << i << ", " << GetPI()->mLua[i]->mScriptName << "\r\n"; } return true; } bool cConsole::cfDelLuaScript::operator()() { string scriptfile; GetParStr(1,scriptfile); bool found = false; //tvLuaInterpreter::iterator it; vector::iterator it; cLuaInterpreter *li; for(it = GetPI()->mLua.begin(); it != GetPI()->mLua.end(); ++it) { li = *it; if (StrCompare(li->mScriptName,0,li->mScriptName.size(),scriptfile)==0) { found = true; delete li; GetPI()->mLua.erase(it); (*mOS) << "Script: " << scriptfile << " unloaded." << "\r\n"; break; } } if(!found) (*mOS) << "Script: " << scriptfile << " not unloaded, because not found." << "\r\n"; return true; } bool cConsole::cfAddLuaScript::operator()() { string scriptfile; GetParStr(1, scriptfile); cLuaInterpreter *ip = new cLuaInterpreter(scriptfile); if(ip) if(ip->Init()) { (*mOS) << "Script: " << scriptfile << " successfully loaded & initialized." << "\r\n"; GetPI()->AddData(ip); } else { (*mOS) << "Script: " << scriptfile << " not found or could not be parsed!" << "\r\n"; delete ip; } return true; } bool cConsole::cfReloadLuaScript::operator()() { string scriptfile; GetParStr(1,scriptfile); bool found = false; //tvLuaInterpreter::iterator it; vector::iterator it; cLuaInterpreter *li; for(it = GetPI()->mLua.begin(); it != GetPI()->mLua.end(); ++it) { li = *it; if (StrCompare(li->mScriptName,0,li->mScriptName.size(),scriptfile)==0) { found = true; delete li; GetPI()->mLua.erase(it); (*mOS) << "Script: " << scriptfile << " unloaded." << "\r\n"; break; } } if(!found) { (*mOS) << "Script: " << scriptfile << " not unloaded, because not found or not loaded." << "\r\n"; } else { GetParStr(1, scriptfile); cLuaInterpreter *ip = new cLuaInterpreter(scriptfile); if(ip) if(ip->Init()) { (*mOS) << "Script: " << scriptfile << " successfully loaded & initialized." << "\r\n"; GetPI()->AddData(ip); } else { (*mOS) << "Script: " << scriptfile << " not found or could not be parsed!" << "\r\n"; delete ip; } return true; } } };