summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2013-08-08 16:30:02 +0200
committermadmaxoft <github@xoft.cz>2013-08-08 16:30:02 +0200
commiteb323166d9ae600fe422725ea29575ba4613b6d2 (patch)
treebad726267d13fec9994a2c20e8d88885fca29bd0
parentcPlugin_NewLua is now completely rewritten to use templated LuaState calls. (diff)
downloadcuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar.gz
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar.bz2
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar.lz
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar.xz
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.tar.zst
cuberite-eb323166d9ae600fe422725ea29575ba4613b6d2.zip
-rw-r--r--VC2008/MCServer.vcproj8
-rw-r--r--source/LuaScript.cpp71
-rw-r--r--source/LuaScript.h45
-rw-r--r--source/LuaState.cpp26
-rw-r--r--source/LuaState.h4
-rw-r--r--source/WebAdmin.cpp23
-rw-r--r--source/WebAdmin.h132
7 files changed, 128 insertions, 181 deletions
diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj
index 8c22210d3..ad2580312 100644
--- a/VC2008/MCServer.vcproj
+++ b/VC2008/MCServer.vcproj
@@ -1490,14 +1490,6 @@
>
</File>
<File
- RelativePath="..\source\LuaScript.cpp"
- >
- </File>
- <File
- RelativePath="..\source\LuaScript.h"
- >
- </File>
- <File
RelativePath="..\source\LuaState.cpp"
>
</File>
diff --git a/source/LuaScript.cpp b/source/LuaScript.cpp
deleted file mode 100644
index d406c2c02..000000000
--- a/source/LuaScript.cpp
+++ /dev/null
@@ -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;
-}
-
-
-
-
diff --git a/source/LuaScript.h b/source/LuaScript.h
deleted file mode 100644
index 99ccb3d49..000000000
--- a/source/LuaScript.h
+++ /dev/null
@@ -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;
-} ;
-
-
-
-
diff --git a/source/LuaState.cpp b/source/LuaState.cpp
index 83581d19a..367a04266 100644
--- a/source/LuaState.cpp
+++ b/source/LuaState.cpp
@@ -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);
diff --git a/source/LuaState.h b/source/LuaState.h
index 6ae4bc0d5..6472dc626 100644
--- a/source/LuaState.h
+++ b/source/LuaState.h
@@ -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>
diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp
index be7efa18d..788bf664e 100644
--- a/source/WebAdmin.cpp
+++ b/source/WebAdmin.cpp
@@ -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();
}
diff --git a/source/WebAdmin.h b/source/WebAdmin.h
index 4b12b955e..a62b532f1 100644
--- a/source/WebAdmin.h
+++ b/source/WebAdmin.h
@@ -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;
+
+
+
+
+
+// tolua_begin
+struct HTTPFormData
+{
+ std::string Name;
+ std::string Value;
+ std::string Type;
+} ;
+// tolua_end
+
-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 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;
+
+ bool m_bConnected;
+ cSocket m_ListenSocket;
+
+ cIniFile * m_IniFile;
+ PluginList m_Plugins;
+
+ cEvent * m_Event;
+
+ webserver * m_WebServer;
-#ifdef _WIN32
- static DWORD WINAPI ListenThread(LPVOID lpParam);
-#else
- static void * ListenThread( void *lpParam );
-#endif
+ /// The Lua template script to provide templates:
+ cLuaState m_TemplateScript;
- AString GetTemplate();
- cLuaScript* m_pTemplate;
+ #ifdef _WIN32
+ static DWORD WINAPI ListenThread(LPVOID lpParam);
+ #else
+ static void * ListenThread(void * lpParam);
+ #endif
- int m_Port;
+ AString GetTemplate();
+} ; // tolua_export
- bool m_bConnected;
- cSocket m_ListenSocket;
- cIniFile* m_IniFile;
- PluginList m_Plugins;
- cEvent* m_Event;
- webserver* m_WebServer;
-}; // tolua_export \ No newline at end of file