Merge remote-tracking branch 'origin/master' into hunger

master
madmaxoft 2013-07-29 09:53:47 +02:00
commit 752e57108d
15 changed files with 1665 additions and 482 deletions

View File

@ -1,6 +1,7 @@
COMPILING
=========
To compile MCServer on *nix, you need a GNUmake-compatible make that reads GNUmakefile.
Run "make" to build a debug version (slow, but gives more info on crash)
Run "make release=1" to build a release version (fast, less info on crash)
COMPILING
=========
To compile MCServer on *nix, you need a GNUmake-compatible make that reads GNUmakefile.
Run "make" to build a debug version (slow, but gives more info on crash)
Run "make release=1" to build a release version (fast, less info on crash)
Add addm32=1 to compile in 32-bit mode on 64-bit systems.

File diff suppressed because one or more lines are too long

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
/*
** Lua binding: AllToLua
** Generated automatically by tolua++-1.0.92 on 07/28/13 22:51:47.
** Generated automatically by tolua++-1.0.92 on 07/29/13 02:06:11.
*/
/* Exported function */

262
source/LuaScript.cpp Normal file
View File

@ -0,0 +1,262 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include "LuaScript.h"
extern "C"
{
#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)
{
}
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);
// 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);
}
bool cLuaScript::LoadFile( const char* a_FilePath )
{
// Make sure the plugin is initialized
ASSERT(m_LuaState);
// Load the file into the Lua state
int s = luaL_loadfile(m_LuaState, a_FilePath );
if (ReportErrors(s))
{
return false;
}
return true;
}
bool cLuaScript::Execute()
{
// 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 ) )
{
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, "");
}
lua_pop(m_LuaState, 1);
return true;
}
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;
}

42
source/LuaScript.h Normal file
View File

@ -0,0 +1,42 @@
#pragma once
struct lua_State;
struct sLuaUsertype
{
sLuaUsertype(void* a_pObject, const char* a_pClassName) : Object(a_pObject), ClassName(a_pClassName) {}
//
void* Object;
const char* ClassName;
};
class cLuaScript
{
public:
cLuaScript();
~cLuaScript();
/// Prepares a Lua state
void Initialize();
/// Load a Lua script on the given path
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);
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;
};

View File

@ -1272,6 +1272,59 @@ static int tolua_get_HTTPRequest_FormData(lua_State* tolua_S)
static int tolua_cWebAdmin_GetPlugins(lua_State * tolua_S)
{
cWebAdmin* self = (cWebAdmin*) tolua_tousertype(tolua_S,1,0);
const cWebAdmin::PluginList & AllPlugins = self->GetPlugins();
lua_createtable(tolua_S, AllPlugins.size(), 0);
int newTable = lua_gettop(tolua_S);
int index = 1;
cWebAdmin::PluginList::const_iterator iter = AllPlugins.begin();
while(iter != AllPlugins.end())
{
const cWebPlugin* Plugin = *iter;
tolua_pushusertype( tolua_S, (void*)Plugin, "const cWebPlugin" );
lua_rawseti(tolua_S, newTable, index);
++iter;
++index;
}
return 1;
}
static int tolua_cWebPlugin_GetTabNames(lua_State * tolua_S)
{
cWebPlugin* self = (cWebPlugin*) tolua_tousertype(tolua_S,1,0);
const cWebPlugin::TabNameList & TabNames = self->GetTabNames();
lua_newtable(tolua_S);
int newTable = lua_gettop(tolua_S);
int index = 1;
cWebPlugin::TabNameList::const_iterator iter = TabNames.begin();
while(iter != TabNames.end())
{
const AString & FancyName = iter->first;
const AString & WebName = iter->second;
tolua_pushstring( tolua_S, WebName.c_str() ); // Because the WebName is supposed to be unique, use it as key
tolua_pushstring( tolua_S, FancyName.c_str() );
//
lua_rawset(tolua_S, -3);
++iter;
++index;
}
return 1;
}
static int Lua_ItemGrid_GetSlotCoords(lua_State * L)
{
tolua_Error tolua_err;
@ -1377,6 +1430,14 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_variable(tolua_S,"PostParams",tolua_get_HTTPRequest_PostParams,0);
tolua_variable(tolua_S,"FormData",tolua_get_HTTPRequest_FormData,0);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cWebAdmin");
tolua_function(tolua_S, "GetPlugins", tolua_cWebAdmin_GetPlugins);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cWebPlugin");
tolua_function(tolua_S, "GetTabNames", tolua_cWebPlugin_GetTabNames);
tolua_endmodule(tolua_S);
tolua_beginmodule(tolua_S, "cClientHandle");
tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE);

View File

@ -1565,7 +1565,7 @@ const char * cPlugin_NewLua::GetHookFnName(cPluginManager::PluginHook a_Hook)
AString cPlugin_NewLua::HandleWebRequest( HTTPRequest * a_Request )
AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request )
{
cCSLock Lock(m_CriticalSection);
std::string RetVal = "";
@ -1592,7 +1592,7 @@ AString cPlugin_NewLua::HandleWebRequest( HTTPRequest * a_Request )
//LOGINFO("2. Stack size: %i", lua_gettop(m_LuaState) );
// Push HTTPRequest
tolua_pushusertype( m_LuaState, a_Request, "HTTPRequest" );
tolua_pushusertype( m_LuaState, (void*)a_Request, "const HTTPRequest" );
//LOGINFO("Calling bound function! :D");
int s = lua_pcall( m_LuaState, 1, 1, 0);

View File

@ -86,10 +86,10 @@ public:
virtual bool CanAddHook(cPluginManager::PluginHook a_Hook) override;
// cWebPlugin override
virtual const AString & GetWebTitle(void) const {return GetName(); }
virtual const AString GetWebTitle(void) const {return GetName(); }
// cWebPlugin and WebAdmin stuff
virtual AString HandleWebRequest( HTTPRequest * a_Request ) override;
virtual AString HandleWebRequest(const HTTPRequest * a_Request ) override;
bool AddWebTab(const AString & a_Title, lua_State * a_LuaState, int a_FunctionReference); // >> EXPORTED IN MANUALBINDINGS <<
/// Binds the command to call the function specified by a Lua function reference. Simply adds to CommandMap.

View File

@ -60,7 +60,7 @@ public: // tolua_export
void KickUser(int a_ClientID, const AString & a_Reason);
void AuthenticateUser(int a_ClientID); // Called by cAuthenticator to auth the specified user
const AString & GetServerID(void) const;
const AString & GetServerID(void) const; // tolua_export
void ClientDestroying(const cClientHandle * a_Client); // Called by cClientHandle::Destroy(); stop m_SocketThreads from calling back into a_Client

View File

@ -13,6 +13,7 @@
#include "Player.h"
#include "Server.h"
#include "Root.h"
#include "LuaScript.h"
#include "../iniFile/iniFile.h"
@ -59,6 +60,7 @@ cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
{
WebAdmin = this;
m_Event = new cEvent();
m_pTemplate = new cLuaScript();
Init( m_Port );
}
@ -68,10 +70,12 @@ cWebAdmin::cWebAdmin( int a_Port /* = 8080 */ )
cWebAdmin::~cWebAdmin()
{
WebAdmin = 0;
m_WebServer->Stop();
delete m_WebServer;
delete m_pTemplate;
delete m_IniFile;
m_Event->Wait();
@ -146,40 +150,17 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
bDontShowTemplate = true;
}
std::string UserPassword = WebAdmin->m_IniFile->GetValue( "User:"+r->username_, "Password", "");
AString UserPassword = WebAdmin->m_IniFile->GetValue( "User:"+r->username_, "Password", "");
if ((UserPassword != "") && (r->password_ == UserPassword))
{
std::string BaseURL = "./";
if (Split.size() > 1)
{
for (unsigned int i = 0; i < Split.size(); i++)
{
BaseURL += "../";
}
BaseURL += "webadmin/";
}
AString Template;
std::string Menu;
std::string Content;
std::string Template = bDontShowTemplate ? "{CONTENT}" : WebAdmin->GetTemplate();
std::string FoundPlugin;
for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
{
cWebPlugin* WebPlugin = *itr;
std::list< std::pair<std::string, std::string> > NameList = WebPlugin->GetTabNames();
for( std::list< std::pair<std::string, std::string> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names )
{
Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
}
}
HTTPRequest Request;
Request.Username = r->username_;
Request.Method = r->method_;
Request.Params = r->params_;
Request.PostParams = r->params_post_;
Request.Path = r->path_.substr(1);
HTTPTemplateRequest TemplateRequest;
TemplateRequest.Request.Username = r->username_;
TemplateRequest.Request.Method = r->method_;
TemplateRequest.Request.Params = r->params_;
TemplateRequest.Request.PostParams = r->params_post_;
TemplateRequest.Request.Path = r->path_.substr(1);
for( unsigned int i = 0; i < r->multipart_formdata_.size(); ++i )
{
@ -190,101 +171,113 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
HTTPfd.Type = fd.content_type_;
HTTPfd.Name = fd.name_;
LOGINFO("Form data name: %s", fd.name_.c_str() );
Request.FormData[ fd.name_ ] = HTTPfd;
TemplateRequest.Request.FormData[ fd.name_ ] = HTTPfd;
}
if (Split.size() > 1)
bool bLuaTemplateSuccessful = false;
if (!bDontShowTemplate)
{
// New Lua web template
bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallFunction("ShowPage", sLuaUsertype(WebAdmin, "cWebAdmin"), sLuaUsertype(&TemplateRequest, "HTTPTemplateRequest"), Template);
}
if (!bLuaTemplateSuccessful)
{
AString BaseURL = WebAdmin->GetBaseURL(Split);
AString Menu;
Template = bDontShowTemplate ? "{CONTENT}" : WebAdmin->GetTemplate();
AString FoundPlugin;
for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
{
if ((*itr)->GetWebTitle() == Split[1])
cWebPlugin* WebPlugin = *itr;
std::list< std::pair<AString, AString> > NameList = WebPlugin->GetTabNames();
for( std::list< std::pair<AString, AString> >::iterator Names = NameList.begin(); Names != NameList.end(); ++Names )
{
Content = (*itr)->HandleWebRequest(&Request);
cWebPlugin * WebPlugin = *itr;
FoundPlugin = WebPlugin->GetWebTitle();
AString TabName = WebPlugin->GetTabNameForRequest(&Request).first;
if (!TabName.empty())
Menu += "<li><a href='" + BaseURL + WebPlugin->GetWebTitle().c_str() + "/" + (*Names).second + "'>" + (*Names).first + "</a></li>";
}
}
sWebAdminPage Page = WebAdmin->GetPage(TemplateRequest.Request);
AString Content = Page.Content;
FoundPlugin = Page.PluginName;
if (!Page.TabName.empty())
FoundPlugin += " - " + Page.TabName;
if( FoundPlugin.empty() ) // Default page
{
Content.clear();
FoundPlugin = "Current Game";
Content += "<h4>Server Name:</h4>";
Content += "<p>" + AString( cRoot::Get()->GetServer()->GetServerID() ) + "</p>";
Content += "<h4>Plugins:</h4><ul>";
cPluginManager* PM = cRoot::Get()->GetPluginManager();
if( PM )
{
const cPluginManager::PluginMap & List = PM->GetAllPlugins();
for( cPluginManager::PluginMap::const_iterator itr = List.begin(); itr != List.end(); ++itr )
{
FoundPlugin += " - " + TabName;
if( itr->second == NULL ) continue;
AString VersionNum;
AppendPrintf(Content, "<li>%s V.%i</li>", itr->second->GetName().c_str(), itr->second->GetVersion());
}
break;
}
}
}
Content += "</ul>";
Content += "<h4>Players:</h4><ul>";
if( FoundPlugin.empty() ) // Default page
{
Content.clear();
FoundPlugin = "Current Game";
Content += "<h4>Server Name:</h4>";
Content += "<p>" + std::string( cRoot::Get()->GetServer()->GetServerID() ) + "</p>";
Content += "<h4>Plugins:</h4><ul>";
cPluginManager* PM = cRoot::Get()->GetPluginManager();
if( PM )
{
const cPluginManager::PluginMap & List = PM->GetAllPlugins();
for( cPluginManager::PluginMap::const_iterator itr = List.begin(); itr != List.end(); ++itr )
cPlayerAccum PlayerAccum;
cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
if( World != NULL )
{
if( itr->second == NULL ) continue;
AString VersionNum;
AppendPrintf(Content, "<li>%s V.%i</li>", itr->second->GetName().c_str(), itr->second->GetVersion());
World->ForEachPlayer(PlayerAccum);
Content.append(PlayerAccum.m_Contents);
}
Content += "</ul><br>";
}
Content += "</ul>";
Content += "<h4>Players:</h4><ul>";
cPlayerAccum PlayerAccum;
cWorld * World = cRoot::Get()->GetDefaultWorld(); // TODO - Create a list of worlds and players
if( World != NULL )
if (!bDontShowTemplate && (Split.size() > 1))
{
World->ForEachPlayer(PlayerAccum);
Content.append(PlayerAccum.m_Contents);
Content += "\n<p><a href='" + BaseURL + "'>Go back</a></p>";
}
Content += "</ul><br>";
}
if (!bDontShowTemplate && (Split.size() > 1))
{
Content += "\n<p><a href='" + BaseURL + "'>Go back</a></p>";
}
// mem usage
// mem usage
#ifndef _WIN32
rusage resource_usage;
if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
{
ReplaceString( Template, std::string("{MEM}"), "Error :(" );
}
else
{
AString MemUsage;
Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
ReplaceString(Template, std::string("{MEM}"), MemUsage);
}
rusage resource_usage;
if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
{
ReplaceString( Template, AString("{MEM}"), "Error :(" );
}
else
{
AString MemUsage;
Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
ReplaceString(Template, AString("{MEM}"), MemUsage);
}
#else
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
{
AString MemUsage;
Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
ReplaceString( Template, "{MEM}", MemUsage );
}
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
{
AString MemUsage;
Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
ReplaceString( Template, "{MEM}", MemUsage );
}
#endif
// end mem usage
// end mem usage
ReplaceString( Template, "{USERNAME}", r->username_ );
ReplaceString( Template, "{MENU}", Menu );
ReplaceString( Template, "{PLUGIN_NAME}", FoundPlugin );
ReplaceString( Template, "{CONTENT}", Content );
ReplaceString( Template, "{TITLE}", "MCServer" );
ReplaceString( Template, "{USERNAME}", r->username_ );
ReplaceString( Template, "{MENU}", Menu );
ReplaceString( Template, "{PLUGIN_NAME}", FoundPlugin );
ReplaceString( Template, "{CONTENT}", Content );
ReplaceString( Template, "{TITLE}", "MCServer" );
AString NumChunks;
Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
ReplaceString(Template, "{NUMCHUNKS}", NumChunks);
AString NumChunks;
Printf(NumChunks, "%d", cRoot::Get()->GetTotalChunkCount());
ReplaceString(Template, "{NUMCHUNKS}", NumChunks);
}
r->answer_ = Template;
}
@ -309,6 +302,14 @@ bool cWebAdmin::Init( int a_Port )
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())
{
LOGWARN("Could not load WebAdmin template.");
}
LOG("Starting WebAdmin on port %i", m_Port);
#ifdef _WIN32
@ -354,9 +355,9 @@ void *cWebAdmin::ListenThread( void *lpParam )
std::string cWebAdmin::GetTemplate()
AString cWebAdmin::GetTemplate()
{
std::string retVal = "";
AString retVal = "";
char SourceFile[] = "webadmin/template.html";
@ -370,4 +371,91 @@ std::string cWebAdmin::GetTemplate()
f.ReadRestOfFile(retVal);
return retVal;
}
}
sWebAdminPage cWebAdmin::GetPage(const HTTPRequest& a_Request)
{
sWebAdminPage Page;
AStringVector Split = StringSplit(a_Request.Path, "/");
// Find the plugin that corresponds to the requested path
AString FoundPlugin;
if (Split.size() > 1)
{
for (PluginList::iterator itr = WebAdmin->m_Plugins.begin(); itr != WebAdmin->m_Plugins.end(); ++itr)
{
if ((*itr)->GetWebTitle() == Split[1])
{
Page.Content = (*itr)->HandleWebRequest(&a_Request);
cWebPlugin * WebPlugin = *itr;
FoundPlugin = WebPlugin->GetWebTitle();
AString TabName = WebPlugin->GetTabNameForRequest(&a_Request).first;
Page.PluginName = FoundPlugin;
Page.TabName = TabName;
break;
}
}
}
// Return the page contents
return Page;
}
AString cWebAdmin::GetBaseURL( const AString& a_URL )
{
return GetBaseURL(StringSplit(a_URL, "/"));
}
AString cWebAdmin::GetBaseURL( const AStringVector& a_URLSplit )
{
AString BaseURL = "./";
if (a_URLSplit.size() > 1)
{
for (unsigned int i = 0; i < a_URLSplit.size(); i++)
{
BaseURL += "../";
}
BaseURL += "webadmin/";
}
return BaseURL;
}
AString cWebAdmin::GetMemoryUsage() const
{
AString MemUsage;
#ifndef _WIN32
rusage resource_usage;
if (getrusage(RUSAGE_SELF, &resource_usage) != 0)
{
MemUsage = "Error :(";
}
else
{
Printf(MemUsage, "%0.2f", ((double)resource_usage.ru_maxrss / 1024 / 1024) );
}
#else
HANDLE hProcess = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
if( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc) ) )
{
Printf(MemUsage, "%0.2f", (pmc.WorkingSetSize / 1024.f / 1024.f) );
}
#endif
return MemUsage;
}

View File

@ -4,66 +4,93 @@
#include "OSSupport/Socket.h"
class cStringMap;
class cLuaScript;
struct HTTPFormData // tolua_export
{ // tolua_export
std::string Name; // tolua_export
std::string Value; // tolua_export
std::string Type; // tolua_export
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
struct HTTPRequest // tolua_export
{ // tolua_export
typedef std::map< std::string, std::string > StringStringMap;
typedef std::map< std::string, HTTPFormData > FormDataMap;
std::string Method; // tolua_export
std::string Path; // tolua_export
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
std::string Username; // tolua_export
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
AString Method; // tolua_export
AString Path; // tolua_export
StringStringMap Params; // >> EXPORTED IN MANUALBINDINGS <<
StringStringMap PostParams; // >> EXPORTED IN MANUALBINDINGS <<
AString Username; // tolua_export
FormDataMap FormData; // >> EXPORTED IN MANUALBINDINGS <<
}; // tolua_export
struct HTTPTemplateRequest // tolua_export
{ // tolua_export
HTTPRequest Request; // tolua_export
}; // tolua_export
// tolua_begin
struct sWebAdminPage
{
AString Content;
AString PluginName;
AString TabName;
};
// tolua_end
struct lua_State;
class cEvent;
class cIniFile;
class cWebPlugin;
class cWebAdmin
{
public:
class cWebAdmin // tolua_export
{ // tolua_export
public: // tolua_export
cWebAdmin( int a_Port = 8080 );
~cWebAdmin();
bool Init( int a_Port );
bool Init( int a_Port );
void AddPlugin( cWebPlugin* a_Plugin );
void RemovePlugin( cWebPlugin* a_Plugin );
void AddPlugin( cWebPlugin* a_Plugin );
void RemovePlugin( cWebPlugin* a_Plugin );
typedef std::list< cWebPlugin* > PluginList;
PluginList GetPlugins() { return m_Plugins; }
static void Request_Handler(webserver::http_request* r);
// 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 <<
int GetPort() { return m_Port; }
static void Request_Handler(webserver::http_request* r);
int GetPort() { return m_Port; } // tolua_export
sWebAdminPage GetPage(const HTTPRequest& a_Request); // tolua_export
AString GetBaseURL(const AString& a_URL); // tolua_export
AString GetBaseURL(const AStringVector& a_URLSplit);
AString GetMemoryUsage() const; // tolua_export
private:
#ifdef _WIN32
static DWORD WINAPI ListenThread(LPVOID lpParam);
#else
static void *ListenThread( void *lpParam );
static void * ListenThread( void *lpParam );
#endif
std::string GetTemplate();
AString GetTemplate();
int m_Port;
cLuaScript* m_pTemplate;
bool m_bConnected;
cSocket m_ListenSocket;
int m_Port;
cIniFile* m_IniFile;
PluginList m_Plugins;
bool m_bConnected;
cSocket m_ListenSocket;
cEvent* m_Event;
cIniFile* m_IniFile;
PluginList m_Plugins;
webserver* m_WebServer;
};
cEvent* m_Event;
webserver* m_WebServer;
}; // tolua_export

View File

@ -59,7 +59,7 @@ std::list<std::pair<AString, AString> > cWebPlugin::GetTabNames(void)
std::pair< AString, AString > cWebPlugin::GetTabNameForRequest(HTTPRequest * a_Request)
std::pair< AString, AString > cWebPlugin::GetTabNameForRequest(const HTTPRequest * a_Request)
{
std::pair< AString, AString > Names;
AStringVector Split = StringSplit(a_Request->Path, "/");

View File

@ -16,10 +16,10 @@ public:
cWebPlugin();
virtual ~cWebPlugin();
virtual const AString & GetWebTitle(void) const = 0;
// tolua_begin
virtual const AString GetWebTitle(void) const = 0;
virtual AString HandleWebRequest( HTTPRequest * a_Request ) = 0;
virtual AString HandleWebRequest(const HTTPRequest * a_Request ) = 0;
static AString SafeString( const AString & a_String );
// tolua_end
@ -35,8 +35,9 @@ public:
typedef std::list< sWebPluginTab* > TabList;
TabList & GetTabs() { return m_Tabs; }
std::list< std::pair<AString, AString> > GetTabNames();
std::pair< AString, AString > GetTabNameForRequest( HTTPRequest* a_Request );
typedef std::list< std::pair<AString, AString> > TabNameList;
TabNameList GetTabNames(); // >> EXPORTED IN MANUALBINDINGS <<
std::pair< AString, AString > GetTabNameForRequest(const HTTPRequest* a_Request );
private:
TabList m_Tabs;