Removed LuaScript.
The WebAdmin now uses LuaState directly to call the one function it needs.
This commit is contained in:
parent
cc920ea929
commit
eb323166d9
@ -1489,14 +1489,6 @@
|
|||||||
RelativePath="..\source\LuaFunctions.h"
|
RelativePath="..\source\LuaFunctions.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
|
||||||
RelativePath="..\source\LuaScript.cpp"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
|
||||||
RelativePath="..\source\LuaScript.h"
|
|
||||||
>
|
|
||||||
</File>
|
|
||||||
<File
|
<File
|
||||||
RelativePath="..\source\LuaState.cpp"
|
RelativePath="..\source\LuaState.cpp"
|
||||||
>
|
>
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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;
|
|
||||||
} ;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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)
|
void cLuaState::GetReturn(int a_StackPos, bool & a_ReturnedVal)
|
||||||
{
|
{
|
||||||
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);
|
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);
|
||||||
|
@ -48,6 +48,8 @@ struct TakeDamageInfo;
|
|||||||
class cWindow;
|
class cWindow;
|
||||||
class cPlugin_NewLua;
|
class cPlugin_NewLua;
|
||||||
struct HTTPRequest;
|
struct HTTPRequest;
|
||||||
|
class cWebAdmin;
|
||||||
|
struct HTTPTemplateRequest;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -167,6 +169,8 @@ public:
|
|||||||
void Push(cWindow * a_Window);
|
void Push(cWindow * a_Window);
|
||||||
void Push(cPlugin_NewLua * a_Plugin);
|
void Push(cPlugin_NewLua * a_Plugin);
|
||||||
void Push(const HTTPRequest * a_Request);
|
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:
|
/// Call any 0-param 0-return Lua function in a single line:
|
||||||
template <typename FnT>
|
template <typename FnT>
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "Server.h"
|
#include "Server.h"
|
||||||
#include "Root.h"
|
#include "Root.h"
|
||||||
#include "LuaScript.h"
|
|
||||||
|
|
||||||
#include "../iniFile/iniFile.h"
|
#include "../iniFile/iniFile.h"
|
||||||
|
|
||||||
@ -54,13 +53,13 @@ cWebAdmin * WebAdmin = NULL;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
|
cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ ) :
|
||||||
: m_Port( a_Port )
|
m_Port(a_Port),
|
||||||
, m_bConnected( false )
|
m_bConnected(false),
|
||||||
|
m_TemplateScript("<webadmin_template>")
|
||||||
{
|
{
|
||||||
WebAdmin = this;
|
WebAdmin = this;
|
||||||
m_Event = new cEvent();
|
m_Event = new cEvent();
|
||||||
m_pTemplate = new cLuaScript();
|
|
||||||
Init( m_Port );
|
Init( m_Port );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +74,6 @@ cWebAdmin::~cWebAdmin()
|
|||||||
m_WebServer->Stop();
|
m_WebServer->Stop();
|
||||||
|
|
||||||
delete m_WebServer;
|
delete m_WebServer;
|
||||||
delete m_pTemplate;
|
|
||||||
delete m_IniFile;
|
delete m_IniFile;
|
||||||
|
|
||||||
m_Event->Wait();
|
m_Event->Wait();
|
||||||
@ -174,11 +172,11 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
|
|||||||
TemplateRequest.Request.FormData[ fd.name_ ] = HTTPfd;
|
TemplateRequest.Request.FormData[ fd.name_ ] = HTTPfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Try to get the template from the Lua template script
|
||||||
bool bLuaTemplateSuccessful = false;
|
bool bLuaTemplateSuccessful = false;
|
||||||
if (!bDontShowTemplate)
|
if (!bDontShowTemplate)
|
||||||
{
|
{
|
||||||
// New Lua web template
|
bLuaTemplateSuccessful = WebAdmin->m_TemplateScript.Call("ShowPage", WebAdmin, &TemplateRequest, cLuaState::Return, Template);
|
||||||
bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallShowPage(*WebAdmin, TemplateRequest, Template);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bLuaTemplateSuccessful)
|
if (!bLuaTemplateSuccessful)
|
||||||
@ -280,10 +278,11 @@ bool cWebAdmin::Init( int a_Port )
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the WebAdmin template script and load the file
|
// Initialize the WebAdmin template script and load the file
|
||||||
m_pTemplate->Initialize();
|
m_TemplateScript.Create();
|
||||||
if (!m_pTemplate->LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,34 +2,62 @@
|
|||||||
|
|
||||||
#include "../WebServer/WebServer.h"
|
#include "../WebServer/WebServer.h"
|
||||||
#include "OSSupport/Socket.h"
|
#include "OSSupport/Socket.h"
|
||||||
|
#include "LuaState.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// fwd:
|
||||||
class cStringMap;
|
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, std::string > StringStringMap;
|
||||||
typedef std::map< std::string, HTTPFormData > FormDataMap;
|
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 Params; // >> EXPORTED IN MANUALBINDINGS <<
|
||||||
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
|
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
|
||||||
AString Username; // tolua_export
|
|
||||||
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
|
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
|
||||||
} ; // tolua_export
|
} ; // tolua_export
|
||||||
|
|
||||||
struct HTTPTemplateRequest // tolua_export
|
|
||||||
{ // tolua_export
|
|
||||||
HTTPRequest Request; // tolua_export
|
|
||||||
|
|
||||||
}; // tolua_export
|
|
||||||
|
|
||||||
|
|
||||||
|
// tolua_begin
|
||||||
|
struct HTTPTemplateRequest
|
||||||
|
{
|
||||||
|
HTTPRequest Request;
|
||||||
|
} ;
|
||||||
|
// tolua_end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// tolua_begin
|
// tolua_begin
|
||||||
struct sWebAdminPage
|
struct sWebAdminPage
|
||||||
@ -40,14 +68,19 @@ struct sWebAdminPage
|
|||||||
};
|
};
|
||||||
// tolua_end
|
// 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( int a_Port = 8080 );
|
||||||
~cWebAdmin();
|
~cWebAdmin();
|
||||||
|
|
||||||
@ -56,32 +89,25 @@ public: // tolua_export
|
|||||||
void AddPlugin( cWebPlugin* a_Plugin );
|
void AddPlugin( cWebPlugin* a_Plugin );
|
||||||
void RemovePlugin( 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
|
// 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 <<
|
PluginList GetPlugins() const { return m_Plugins; } // >> EXPORTED IN MANUALBINDINGS <<
|
||||||
|
|
||||||
static void Request_Handler(webserver::http_request* r);
|
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);
|
AString GetBaseURL(const AStringVector& a_URLSplit);
|
||||||
|
|
||||||
static AString GetMemoryUsage(void); // tolua_export
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
static DWORD WINAPI ListenThread(LPVOID lpParam);
|
|
||||||
#else
|
|
||||||
static void * ListenThread( void *lpParam );
|
|
||||||
#endif
|
|
||||||
|
|
||||||
AString GetTemplate();
|
|
||||||
|
|
||||||
cLuaScript* m_pTemplate;
|
|
||||||
|
|
||||||
int m_Port;
|
int m_Port;
|
||||||
|
|
||||||
bool m_bConnected;
|
bool m_bConnected;
|
||||||
@ -93,4 +119,20 @@ private:
|
|||||||
cEvent * m_Event;
|
cEvent * m_Event;
|
||||||
|
|
||||||
webserver * m_WebServer;
|
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
|
} ; // tolua_export
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user