Removed LuaScript.

The WebAdmin now uses LuaState directly to call the one function it needs.
master
madmaxoft 2013-08-08 16:30:02 +02:00
parent cc920ea929
commit eb323166d9
7 changed files with 128 additions and 181 deletions

View File

@ -1489,14 +1489,6 @@
RelativePath="..\source\LuaFunctions.h"
>
</File>
<File
RelativePath="..\source\LuaScript.cpp"
>
</File>
<File
RelativePath="..\source\LuaScript.h"
>
</File>
<File
RelativePath="..\source\LuaState.cpp"
>

View File

@ -1,71 +0,0 @@
// LuaScript.cpp
// Implements the cLuaScript class that loads a Lua script file to produce a web template out of it
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "LuaScript.h"
#include "tolua++.h"
cLuaScript::cLuaScript()
: m_LuaState("cLuaScript")
{
}
void cLuaScript::Initialize()
{
// Check to see if this script has not been initialized before
ASSERT(!m_LuaState.IsValid());
// Create a Lua state and bind all libraries to it
m_LuaState.Create();
}
bool cLuaScript::LoadFile(const char * a_FilePath)
{
// Make sure the plugin is initialized
ASSERT(m_LuaState.IsValid());
return m_LuaState.LoadFile(a_FilePath);
}
bool cLuaScript::CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString)
{
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;
}
if (lua_isstring(m_LuaState, -1))
{
a_ReturnedString.assign(tolua_tostring(m_LuaState, -1, ""));
}
lua_pop(m_LuaState, 1);
return true;
}

View File

@ -1,45 +0,0 @@
// LuaScript.h
// Declares the cLuaScript class that loads a Lua script file to produce a web template out of it
#pragma once
#include "LuaState.h"
// fwd:
class cWebAdmin;
struct HTTPTemplateRequest;
class cLuaScript
{
public:
cLuaScript(void);
/// Prepares a Lua state
void Initialize();
/// Load a Lua script on the given path
bool LoadFile(const char * a_FilePath);
bool CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString);
protected:
cLuaState m_LuaState;
} ;

View File

@ -599,6 +599,32 @@ void cLuaState::Push(const HTTPRequest * a_Request)
void cLuaState::Push(cWebAdmin * a_WebAdmin)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
tolua_pushusertype(m_LuaState, a_WebAdmin, "cWebAdmin");
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::Push(const HTTPTemplateRequest * a_Request)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
tolua_pushusertype(m_LuaState, (void *)a_Request, "HTTPTemplateRequest");
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::GetReturn(int a_StackPos, bool & a_ReturnedVal)
{
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);

View File

@ -48,6 +48,8 @@ struct TakeDamageInfo;
class cWindow;
class cPlugin_NewLua;
struct HTTPRequest;
class cWebAdmin;
struct HTTPTemplateRequest;
@ -167,6 +169,8 @@ public:
void Push(cWindow * a_Window);
void Push(cPlugin_NewLua * a_Plugin);
void Push(const HTTPRequest * a_Request);
void Push(cWebAdmin * a_WebAdmin);
void Push(const HTTPTemplateRequest * a_Request);
/// Call any 0-param 0-return Lua function in a single line:
template <typename FnT>

View File

@ -13,7 +13,6 @@
#include "Player.h"
#include "Server.h"
#include "Root.h"
#include "LuaScript.h"
#include "../iniFile/iniFile.h"
@ -54,13 +53,13 @@ cWebAdmin * WebAdmin = NULL;
cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
: m_Port( a_Port )
, m_bConnected( false )
cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ ) :
m_Port(a_Port),
m_bConnected(false),
m_TemplateScript("<webadmin_template>")
{
WebAdmin = this;
m_Event = new cEvent();
m_pTemplate = new cLuaScript();
Init( m_Port );
}
@ -75,7 +74,6 @@ cWebAdmin::~cWebAdmin()
m_WebServer->Stop();
delete m_WebServer;
delete m_pTemplate;
delete m_IniFile;
m_Event->Wait();
@ -174,11 +172,11 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
TemplateRequest.Request.FormData[ fd.name_ ] = HTTPfd;
}
// Try to get the template from the Lua template script
bool bLuaTemplateSuccessful = false;
if (!bDontShowTemplate)
{
// New Lua web template
bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallShowPage(*WebAdmin, TemplateRequest, Template);
bLuaTemplateSuccessful = WebAdmin->m_TemplateScript.Call("ShowPage", WebAdmin, &TemplateRequest, cLuaState::Return, Template);
}
if (!bLuaTemplateSuccessful)
@ -269,7 +267,7 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
bool cWebAdmin::Init( int a_Port )
bool cWebAdmin::Init(int a_Port)
{
m_Port = a_Port;
@ -280,10 +278,11 @@ bool cWebAdmin::Init( int a_Port )
}
// Initialize the WebAdmin template script and load the file
m_pTemplate->Initialize();
if (!m_pTemplate->LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
m_TemplateScript.Create();
if (!m_TemplateScript.LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
{
LOGWARN("Could not load WebAdmin template.");
LOGWARN("Could not load WebAdmin template \"%s\", using default template.", FILE_IO_PREFIX "webadmin/template.lua");
m_TemplateScript.Close();
}

View File

@ -2,34 +2,62 @@
#include "../WebServer/WebServer.h"
#include "OSSupport/Socket.h"
#include "LuaState.h"
// fwd:
class cStringMap;
class cLuaScript;
class cEvent;
class cIniFile;
class cWebPlugin;
struct HTTPFormData // tolua_export
{ // tolua_export
std::string Name; // tolua_export
std::string Value; // tolua_export
std::string Type; // tolua_export
};// tolua_export
struct HTTPRequest // tolua_export
{ // tolua_export
// tolua_begin
struct HTTPFormData
{
std::string Name;
std::string Value;
std::string Type;
} ;
// tolua_end
// tolua_begin
struct HTTPRequest
{
typedef std::map< std::string, std::string > StringStringMap;
typedef std::map< std::string, HTTPFormData > FormDataMap;
AString Method; // tolua_export
AString Path; // tolua_export
AString Method;
AString Path;
AString Username;
// tolua_end
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
AString Username; // tolua_export
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
}; // tolua_export
} ; // tolua_export
// tolua_begin
struct HTTPTemplateRequest
{
HTTPRequest Request;
} ;
// tolua_end
struct HTTPTemplateRequest // tolua_export
{ // tolua_export
HTTPRequest Request; // tolua_export
}; // tolua_export
// tolua_begin
struct sWebAdminPage
@ -40,14 +68,19 @@ struct sWebAdminPage
};
// tolua_end
struct lua_State;
class cEvent;
class cIniFile;
class cWebPlugin;
class cWebAdmin // tolua_export
{ // tolua_export
public: // tolua_export
// tolua_begin
class cWebAdmin
{
public:
// tolua_end
typedef std::list< cWebPlugin* > PluginList;
cWebAdmin( int a_Port = 8080 );
~cWebAdmin();
@ -56,41 +89,50 @@ public: // tolua_export
void AddPlugin( cWebPlugin* a_Plugin );
void RemovePlugin( cWebPlugin* a_Plugin );
typedef std::list< cWebPlugin* > PluginList;
// TODO: Convert this to the auto-locking callback mechanism used for looping players in worlds and such
PluginList GetPlugins() const { return m_Plugins; } // >> EXPORTED IN MANUALBINDINGS <<
static void Request_Handler(webserver::http_request* r);
int GetPort() { return m_Port; } // tolua_export
// tolua_begin
static AString GetMemoryUsage(void);
int GetPort() { return m_Port; }
sWebAdminPage GetPage(const HTTPRequest& a_Request);
AString GetBaseURL(const AString& a_URL);
// tolua_end
sWebAdminPage GetPage(const HTTPRequest& a_Request); // tolua_export
AString GetBaseURL(const AString& a_URL); // tolua_export
AString GetBaseURL(const AStringVector& a_URLSplit);
static AString GetMemoryUsage(void); // tolua_export
private:
int m_Port;
#ifdef _WIN32
static DWORD WINAPI ListenThread(LPVOID lpParam);
#else
static void * ListenThread( void *lpParam );
#endif
bool m_bConnected;
cSocket m_ListenSocket;
AString GetTemplate();
cIniFile * m_IniFile;
PluginList m_Plugins;
cLuaScript* m_pTemplate;
cEvent * m_Event;
int m_Port;
webserver * m_WebServer;
/// The Lua template script to provide templates:
cLuaState m_TemplateScript;
#ifdef _WIN32
static DWORD WINAPI ListenThread(LPVOID lpParam);
#else
static void * ListenThread(void * lpParam);
#endif
AString GetTemplate();
} ; // tolua_export
bool m_bConnected;
cSocket m_ListenSocket;
cIniFile* m_IniFile;
PluginList m_Plugins;
cEvent* m_Event;
webserver* m_WebServer;
}; // tolua_export