From 5951bc76ec8fbb188777de66cb965f05446c7f38 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 7 Jul 2013 14:42:07 +0000 Subject: Added StringUtils functions to Lua API; added StripColorCodes(); ChatLog now strips color codes from logged messages. Fixes FS #398 git-svn-id: http://mc-server.googlecode.com/svn/trunk@1661 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- MCServer/Plugins/ChatLog/plugin.lua | 2 +- source/AllToLua.pkg | 1 + source/Bindings.cpp | 166 +++++++++++++++++++++++++++++++++++- source/Bindings.h | 2 +- source/StringUtils.cpp | 23 ++++- source/StringUtils.h | 11 ++- 6 files changed, 196 insertions(+), 9 deletions(-) diff --git a/MCServer/Plugins/ChatLog/plugin.lua b/MCServer/Plugins/ChatLog/plugin.lua index 9ef321b48..c8358a6d3 100644 --- a/MCServer/Plugins/ChatLog/plugin.lua +++ b/MCServer/Plugins/ChatLog/plugin.lua @@ -26,7 +26,7 @@ end function OnChat(Player, Message) -- Lets get loggin' - LOGINFO("[" .. Player:GetName() .. "]: " .. Message); + LOGINFO("[" .. Player:GetName() .. "]: " .. StripColorCodes(Message)); return false end \ No newline at end of file diff --git a/source/AllToLua.pkg b/source/AllToLua.pkg index 9e6f8cf8b..7041211b5 100644 --- a/source/AllToLua.pkg +++ b/source/AllToLua.pkg @@ -18,6 +18,7 @@ $cfile "ChunkDef.h" $cfile "../iniFile/iniFile.h" $cfile "BlockID.h" +$cfile "StringUtils.h" $cfile "Defines.h" $cfile "LuaFunctions.h" $cfile "StringMap.h" diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 9f201b19d..72e82c25f 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 07/07/13 15:41:32. +** Generated automatically by tolua++-1.0.92 on 07/07/13 16:40:05. */ #ifndef __cplusplus @@ -18,6 +18,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S); #include "ChunkDef.h" #include "../iniFile/iniFile.h" #include "BlockID.h" +#include "StringUtils.h" #include "Defines.h" #include "LuaFunctions.h" #include "StringMap.h" @@ -2649,6 +2650,164 @@ static int tolua_AllToLua_StringToDamageType00(lua_State* tolua_S) } #endif //#ifndef TOLUA_DISABLE +/* function: TrimString */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_TrimString00 +static int tolua_AllToLua_TrimString00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_iscppstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const AString str = ((const AString) tolua_tocppstring(tolua_S,1,0)); + { + AString tolua_ret = (AString) TrimString(str); + tolua_pushcppstring(tolua_S,(const char*)tolua_ret); + tolua_pushcppstring(tolua_S,(const char*)str); + } + } + return 2; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'TrimString'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* function: NoCaseCompare */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_NoCaseCompare00 +static int tolua_AllToLua_NoCaseCompare00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_iscppstring(tolua_S,1,0,&tolua_err) || + !tolua_iscppstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const AString s1 = ((const AString) tolua_tocppstring(tolua_S,1,0)); + const AString s2 = ((const AString) tolua_tocppstring(tolua_S,2,0)); + { + int tolua_ret = (int) NoCaseCompare(s1,s2); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + tolua_pushcppstring(tolua_S,(const char*)s1); + tolua_pushcppstring(tolua_S,(const char*)s2); + } + } + return 3; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'NoCaseCompare'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* function: ReplaceString */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_ReplaceString00 +static int tolua_AllToLua_ReplaceString00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_iscppstring(tolua_S,1,0,&tolua_err) || + !tolua_iscppstring(tolua_S,2,0,&tolua_err) || + !tolua_iscppstring(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + AString iHayStack = ((AString) tolua_tocppstring(tolua_S,1,0)); + const AString iNeedle = ((const AString) tolua_tocppstring(tolua_S,2,0)); + const AString iReplaceWith = ((const AString) tolua_tocppstring(tolua_S,3,0)); + { + ReplaceString(iHayStack,iNeedle,iReplaceWith); + tolua_pushcppstring(tolua_S,(const char*)iHayStack); + tolua_pushcppstring(tolua_S,(const char*)iNeedle); + tolua_pushcppstring(tolua_S,(const char*)iReplaceWith); + } + } + return 3; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'ReplaceString'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* function: EscapeString */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_EscapeString00 +static int tolua_AllToLua_EscapeString00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_iscppstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const AString a_Message = ((const AString) tolua_tocppstring(tolua_S,1,0)); + { + AString tolua_ret = (AString) EscapeString(a_Message); + tolua_pushcppstring(tolua_S,(const char*)tolua_ret); + tolua_pushcppstring(tolua_S,(const char*)a_Message); + } + } + return 2; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'EscapeString'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* function: StripColorCodes */ +#ifndef TOLUA_DISABLE_tolua_AllToLua_StripColorCodes00 +static int tolua_AllToLua_StripColorCodes00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_iscppstring(tolua_S,1,0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const AString a_Message = ((const AString) tolua_tocppstring(tolua_S,1,0)); + { + AString tolua_ret = (AString) StripColorCodes(a_Message); + tolua_pushcppstring(tolua_S,(const char*)tolua_ret); + tolua_pushcppstring(tolua_S,(const char*)a_Message); + } + } + return 2; +#ifndef TOLUA_RELEASE + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'StripColorCodes'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + /* get function: g_BlockLightValue */ #ifndef TOLUA_DISABLE_tolua_get_AllToLua_g_BlockLightValue static int tolua_get_AllToLua_g_BlockLightValue(lua_State* tolua_S) @@ -28119,6 +28278,11 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_function(tolua_S,"StringToDimension",tolua_AllToLua_StringToDimension00); tolua_function(tolua_S,"DamageTypeToString",tolua_AllToLua_DamageTypeToString00); tolua_function(tolua_S,"StringToDamageType",tolua_AllToLua_StringToDamageType00); + tolua_function(tolua_S,"TrimString",tolua_AllToLua_TrimString00); + tolua_function(tolua_S,"NoCaseCompare",tolua_AllToLua_NoCaseCompare00); + tolua_function(tolua_S,"ReplaceString",tolua_AllToLua_ReplaceString00); + tolua_function(tolua_S,"EscapeString",tolua_AllToLua_EscapeString00); + tolua_function(tolua_S,"StripColorCodes",tolua_AllToLua_StripColorCodes00); tolua_array(tolua_S,"g_BlockLightValue",tolua_get_AllToLua_g_BlockLightValue,tolua_set_AllToLua_g_BlockLightValue); tolua_array(tolua_S,"g_BlockSpreadLightFalloff",tolua_get_AllToLua_g_BlockSpreadLightFalloff,tolua_set_AllToLua_g_BlockSpreadLightFalloff); tolua_array(tolua_S,"g_BlockTransparent",tolua_get_AllToLua_g_BlockTransparent,tolua_set_AllToLua_g_BlockTransparent); diff --git a/source/Bindings.h b/source/Bindings.h index 4b278f618..ec9ded740 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 07/07/13 15:41:32. +** Generated automatically by tolua++-1.0.92 on 07/07/13 16:40:06. */ /* Exported function */ diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index 2bb6ae76e..403b10a7c 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -594,8 +594,8 @@ AString EscapeString(const AString & a_Message) last = i + 1; break; } - } - } + } // switch (ch) + } // for i - a_Message[] if (len > last) { EscapedMsg.append(a_Message, last, len - last); @@ -606,3 +606,22 @@ AString EscapeString(const AString & a_Message) + +AString StripColorCodes(const AString & a_Message) +{ + AString res(a_Message); + size_t idx = 0; + while (true) + { + idx = res.find("\xc2\xa7", idx); + if (idx == AString::npos) + { + return res; + } + res.erase(idx, 3); + } +} + + + + diff --git a/source/StringUtils.h b/source/StringUtils.h index be2fe2359..211799e91 100644 --- a/source/StringUtils.h +++ b/source/StringUtils.h @@ -40,19 +40,19 @@ extern AStringVector StringSplit(const AString & str, const AString & delim); extern AStringVector StringSplitAndTrim(const AString & str, const AString & delim); /// Trime whitespace at both ends of the string -extern AString TrimString(const AString & str); +extern AString TrimString(const AString & str); // tolua_export /// In-place string conversion to uppercase; returns the same string extern AString & StrToUpper(AString & s); /// Case-insensitive string comparison; returns 0 if the strings are the same -extern int NoCaseCompare(const AString & s1, const AString & s2); +extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export /// Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()] extern unsigned int RateCompareString(const AString & s1, const AString & s2 ); /// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith -extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); +extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export /// Returns the list of all items in the specified directory (files, folders, nix pipes, whatever's there) extern AStringList GetDirectoryContents(const char * a_Directory); @@ -67,7 +67,10 @@ extern AString & UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length, AStr extern AString & CreateHexDump(AString & a_Out, const void * a_Data, int a_Size, int a_BytesPerLine); /// Returns a copy of a_Message with all quotes and backslashes escaped by a backslash -extern AString EscapeString(const AString & a_Message); +extern AString EscapeString(const AString & a_Message); // tolua_export + +/// Removes all control codes used by MC for colors and styles +extern AString StripColorCodes(const AString & a_Message); // tolua_export // If you have any other string helper functions, declare them here -- cgit v1.2.3