1
0

cLuaScript now uses cLuaState

This commit is contained in:
madmaxoft 2013-08-06 19:28:09 +02:00
parent 0281b1db6e
commit c55fabb5ad
5 changed files with 51 additions and 243 deletions

View File

@ -4,6 +4,10 @@ function Output(String)
table.insert(SiteContent, String)
end
function GetTableSize(Table)
local Size = 0
for key,value in pairs(Table) do
@ -12,6 +16,10 @@ function GetTableSize(Table)
return Size
end
function GetDefaultPage()
local PM = cRoot:Get():GetPluginManager()
@ -42,11 +50,15 @@ function GetDefaultPage()
return Content, SubTitle
end
function ShowPage(WebAdmin, TemplateRequest)
SiteContent = {}
local BaseURL = WebAdmin:GetBaseURL(TemplateRequest.Request.Path)
local Title = "MCServer"
local MemoryUsage = WebAdmin:GetMemoryUsage()
local MemoryUsage = cWebAdmin:GetMemoryUsage()
local NumChunks = cRoot:Get():GetTotalChunkCount()
local PluginPage = WebAdmin:GetPage(TemplateRequest.Request)
local PageContent = PluginPage.Content
@ -408,6 +420,7 @@ function ShowPage(WebAdmin, TemplateRequest)
<div id="sidebar">
<ul class="sideNav">
]])
local AllPlugins = WebAdmin:GetPlugins()
for key,value in pairs(AllPlugins) do
@ -421,6 +434,7 @@ function ShowPage(WebAdmin, TemplateRequest)
end
end
end
Output([[
</ul>

View File

@ -9,31 +9,16 @@
extern "C"
{
#include "lualib.h"
#include "lualib.h"
}
#include "tolua++.h"
#include "Bindings.h"
#include "ManualBindings.h"
// fwd: SQLite/lsqlite3.c
extern "C"
{
LUALIB_API int luaopen_lsqlite3(lua_State * L);
}
// fwd: LuaExpat/lxplib.c:
extern "C"
{
int luaopen_lxp(lua_State * L);
}
cLuaScript::cLuaScript()
: m_LuaState(NULL)
: m_LuaState("cLuaScript")
{
}
@ -42,166 +27,45 @@ cLuaScript::cLuaScript()
cLuaScript::~cLuaScript()
{
if( m_LuaState )
{
lua_close( m_LuaState );
m_LuaState = 0;
}
}
void cLuaScript::Initialize()
{
// Check to see if this script has not been initialized before
ASSERT(!m_LuaState);
ASSERT(!m_LuaState.IsValid());
// Create a Lua state and bind all libraries to it
m_LuaState = lua_open();
luaL_openlibs(m_LuaState);
tolua_AllToLua_open(m_LuaState);
ManualBindings::Bind(m_LuaState);
luaopen_lsqlite3(m_LuaState);
luaopen_lxp(m_LuaState);
m_LuaState.Create();
}
bool cLuaScript::LoadFile( const char* a_FilePath )
bool cLuaScript::LoadFile(const char * a_FilePath)
{
// Make sure the plugin is initialized
ASSERT(m_LuaState);
ASSERT(m_LuaState.IsValid());
// Load the file into the Lua state
int s = luaL_loadfile(m_LuaState, a_FilePath );
if (ReportErrors(s))
{
return false;
}
return true;
return m_LuaState.LoadFile(a_FilePath);
}
bool cLuaScript::Execute()
bool cLuaScript::CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString)
{
// Make sure we got a Lua state
ASSERT(m_LuaState);
// Execute the script as it is right now
int s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
if( ReportErrors( s ) )
ASSERT(m_LuaState.IsValid());
m_LuaState.PushFunction("ShowPage");
m_LuaState.PushUserType(&a_WebAdmin, "cWebAdmin");
m_LuaState.PushUserType(&a_Request, "HTTPTemplateRequest");
if (!m_LuaState.CallFunction(1))
{
return false;
}
return true;
}
bool cLuaScript::ReportErrors( int a_Status )
{
if (a_Status == 0)
{
// No error to report
return false;
}
// Status was set to error so get the error from the Lua state and log it
LOGERROR("LUA: %s", lua_tostring(m_LuaState, -1));
lua_pop(m_LuaState, 1);
// Return true to indicate that an error was returned
return true;
}
bool cLuaScript::LuaPushFunction( const char * a_FunctionName, bool a_bLogError /*= true*/ )
{
ASSERT(m_LuaState);
// Find and push the function on the Lua stack
lua_getglobal(m_LuaState, a_FunctionName);
// Make sure we found a function
if (!lua_isfunction(m_LuaState, -1))
{
if (a_bLogError)
{
LOGWARN("LUA: Could not find function %s()", a_FunctionName);
}
// Pop the pushed 'object' back
lua_pop(m_LuaState, 1);
return false;
}
// Successfully pushed a function to the Lua stack
return true;
}
bool cLuaScript::LuaCallFunction( int a_NumArgs, int a_NumResults, const char * a_FunctionName )
{
ASSERT(m_LuaState);
// Make sure there's a lua function on the stack
ASSERT(lua_isfunction(m_LuaState, -a_NumArgs - 1));
// Call the desired function
int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);
// Check for errors
if (ReportErrors(s))
{
LOGWARN("LUA: Error calling function %s()", a_FunctionName);
return false;
}
// Successfully executed function
return true;
}
bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString )
{
// Make sure we have the required things to call a function
ASSERT(m_LuaState);
ASSERT(a_Function);
// Push the desired function on the stack
if (!LuaPushFunction(a_Function))
{
return false;
}
if (!LuaCallFunction(0, 1, a_Function))
{
return false;
}
if (lua_isstring(m_LuaState, -1))
{
ReturnedString = tolua_tostring(m_LuaState, -1, "");
a_ReturnedString.assign(tolua_tostring(m_LuaState, -1, ""));
}
lua_pop(m_LuaState, 1);
return true;
@ -210,69 +74,3 @@ bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString )
bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString )
{
// Make sure we have the required things to call a function
ASSERT(m_LuaState);
ASSERT(a_Function);
// Push the desired function on the stack
if (!LuaPushFunction(a_Function))
{
return false;
}
tolua_pushusertype(m_LuaState, a_UserType.Object, a_UserType.ClassName);
if (!LuaCallFunction(1, 1, a_Function))
{
return false;
}
if (lua_isstring(m_LuaState, -1))
{
ReturnedString = tolua_tostring(m_LuaState, -1, "");
}
lua_pop(m_LuaState, 1);
return true;
}
bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString )
{
// Make sure we have the required things to call a function
ASSERT(m_LuaState);
ASSERT(a_Function);
// Push the desired function on the stack
if (!LuaPushFunction(a_Function))
{
return false;
}
tolua_pushusertype(m_LuaState, a_UserType1.Object, a_UserType1.ClassName);
tolua_pushusertype(m_LuaState, a_UserType2.Object, a_UserType2.ClassName);
if (!LuaCallFunction(2, 1, a_Function))
{
return false;
}
if (lua_isstring(m_LuaState, -1))
{
ReturnedString = tolua_tostring(m_LuaState, -1, "");
}
lua_pop(m_LuaState, 1);
return true;
}

View File

@ -9,12 +9,13 @@
#pragma once
struct lua_State;
#include "LuaState.h"
/*
struct sLuaUsertype
{
sLuaUsertype(void* a_pObject, const char* a_pClassName) : Object(a_pObject), ClassName(a_pClassName) {}
@ -22,6 +23,15 @@ struct sLuaUsertype
void* Object;
const char* ClassName;
} ;
*/
// fwd:
class cWebAdmin;
struct HTTPTemplateRequest;
@ -30,32 +40,18 @@ struct sLuaUsertype
class cLuaScript
{
public:
cLuaScript();
~cLuaScript();
cLuaScript(void);
/// Prepares a Lua state
void Initialize();
void Initialize();
/// Load a Lua script on the given path
bool LoadFile(const char* a_FilePath);
bool LoadFile(const char * a_FilePath);
/// Execute the loaded Lua script
bool Execute();
/// Call a function on the Lua script. Put all overloads here
bool CallFunction(const char* a_Function, AString& ReturnedString);
bool CallFunction(const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString);
bool CallFunction(const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString);
bool CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString);
protected:
/// Reports an error in the log if a_Status is flagged as an error. Returns true when a_Status is flagged as error, returns false when no error occured.
bool ReportErrors(int a_Status);
/// Helper functions for calling functions in Lua
bool LuaPushFunction(const char * a_FunctionName, bool a_bLogError = true);
bool LuaCallFunction(int a_NumArgs, int a_NumResults, const char * a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
private:
lua_State* m_LuaState;
cLuaState m_LuaState;
} ;

View File

@ -420,7 +420,7 @@ bool cLuaState::CallFunction(int a_NumResults)
int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, 0);
if (ReportErrors(s))
{
LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName);
LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName.c_str());
m_NumCurrentFunctionArgs = -1;
m_CurrentFunctionName.clear();
return false;

View File

@ -178,7 +178,7 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
if (!bDontShowTemplate)
{
// New Lua web template
bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallFunction("ShowPage", sLuaUsertype(WebAdmin, "cWebAdmin"), sLuaUsertype(&TemplateRequest, "HTTPTemplateRequest"), Template);
bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallShowPage(*WebAdmin, TemplateRequest, Template);
}
if (!bLuaTemplateSuccessful)
@ -274,14 +274,14 @@ bool cWebAdmin::Init( int a_Port )
m_Port = a_Port;
m_IniFile = new cIniFile("webadmin.ini");
if( m_IniFile->ReadFile() )
if (m_IniFile->ReadFile())
{
m_Port = m_IniFile->GetValueI("WebAdmin", "Port", 8080 );
m_Port = m_IniFile->GetValueI("WebAdmin", "Port", 8080);
}
// Initialize the WebAdmin template script and load the file
m_pTemplate->Initialize();
if (!m_pTemplate->LoadFile( FILE_IO_PREFIX "webadmin/template.lua") || !m_pTemplate->Execute())
if (!m_pTemplate->LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
{
LOGWARN("Could not load WebAdmin template.");
}