1
0
Fork 0

Console logging supports cCompositeChat as its parameters.

This commit is contained in:
madmaxoft 2014-03-31 22:51:14 +02:00
parent 55d0db1606
commit 8126d9e66e
5 changed files with 78 additions and 34 deletions

View File

@ -75,6 +75,15 @@ function Initialize(Plugin)
-- TestPluginCalls();
TestBlockAreasString()
--[[
-- Test cCompositeChat usage in console-logging:
LOGINFO(cCompositeChat("This is a simple message with some @2 color formatting @4 and http://links.to .")
:AddSuggestCommandPart("(Suggested command)", "cmd")
:AddRunCommandPart("(Run command)", "cmd")
:SetMessageType(mtInfo)
)
--]]
return true
end;

View File

@ -115,10 +115,35 @@ static int tolua_StringSplitAndTrim(lua_State * tolua_S)
static int tolua_LOG(lua_State* tolua_S)
/** Retrieves the log message from the first param on the Lua stack.
Can take either a string or a cCompositeChat.
*/
static AString GetLogMessage(lua_State * tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
cMCLogger::GetInstance()->LogSimple( str, 0 );
tolua_Error err;
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
{
return ((cCompositeChat *)tolua_tousertype(tolua_S, 1, NULL))->ExtractText();
}
else
{
size_t len = 0;
const char * str = lua_tolstring(tolua_S, 1, &len);
if (str != NULL)
{
return AString(str, len);
}
}
return "";
}
static int tolua_LOG(lua_State * tolua_S)
{
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), 0);
return 0;
}
@ -126,10 +151,9 @@ static int tolua_LOG(lua_State* tolua_S)
static int tolua_LOGINFO(lua_State* tolua_S)
static int tolua_LOGINFO(lua_State * tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
cMCLogger::GetInstance()->LogSimple( str, 1 );
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), 1);
return 0;
}
@ -137,10 +161,9 @@ static int tolua_LOGINFO(lua_State* tolua_S)
static int tolua_LOGWARN(lua_State* tolua_S)
static int tolua_LOGWARN(lua_State * tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
cMCLogger::GetInstance()->LogSimple( str, 2 );
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), 2);
return 0;
}
@ -148,10 +171,9 @@ static int tolua_LOGWARN(lua_State* tolua_S)
static int tolua_LOGERROR(lua_State* tolua_S)
static int tolua_LOGERROR(lua_State * tolua_S)
{
const char* str = tolua_tocppstring(tolua_S,1,0);
cMCLogger::GetInstance()->LogSimple( str, 3 );
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), 3);
return 0;
}

View File

@ -314,6 +314,35 @@ void cCompositeChat::UnderlineUrls(void)
AString cCompositeChat::ExtractText(void) const
{
AString Msg;
for (cParts::const_iterator itr = m_Parts.begin(), end = m_Parts.end(); itr != end; ++itr)
{
switch ((*itr)->m_PartType)
{
case ptText:
case ptClientTranslated:
case ptRunCommand:
case ptSuggestCommand:
{
Msg.append((*itr)->m_Text);
break;
}
case ptUrl:
{
Msg.append(((cUrlPart *)(*itr))->m_Url);
break;
}
} // switch (PartType)
} // for itr - m_Parts[]
return Msg;
}
void cCompositeChat::AddStyle(AString & a_Style, const AString & a_AddStyle)
{
if (a_AddStyle.empty())

View File

@ -164,6 +164,11 @@ public:
/** Returns the message type set previously by SetMessageType(). */
eMessageType GetMessageType(void) const { return m_MessageType; }
/** Returns the text from the parts that comprises the human-readable data.
Used for older protocols that don't support composite chat
and for console-logging. */
AString ExtractText(void) const;
// tolua_end
const cParts & GetParts(void) const { return m_Parts; }

View File

@ -239,32 +239,11 @@ void cProtocol125::SendChat(const AString & a_Message)
void cProtocol125::SendChat(const cCompositeChat & a_Message)
{
// This version doesn't support composite messages, just extract each part's text and use it:
AString Msg;
const cCompositeChat::cParts & Parts = a_Message.GetParts();
for (cCompositeChat::cParts::const_iterator itr = Parts.begin(), end = Parts.end(); itr != end; ++itr)
{
switch ((*itr)->m_PartType)
{
case cCompositeChat::ptText:
case cCompositeChat::ptClientTranslated:
case cCompositeChat::ptRunCommand:
case cCompositeChat::ptSuggestCommand:
{
Msg.append((*itr)->m_Text);
break;
}
case cCompositeChat::ptUrl:
{
Msg.append(((cCompositeChat::cUrlPart *)(*itr))->m_Url);
break;
}
} // switch (PartType)
} // for itr - Parts[]
// Send the message:
cCSLock Lock(m_CSPacket);
WriteByte (PACKET_CHAT);
WriteString(Msg);
WriteString(a_Message.ExtractText());
Flush();
}