From 13f81a051d948fd5acb278c5d8679efa5fb3297c Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Sun, 22 Feb 2015 17:34:20 +0100 Subject: Exported CompressString and UncompressString to Lua --- src/Bindings/ManualBindings.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 69d16ac2b..ee3d81014 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -31,6 +31,7 @@ #include "../LineBlockTracer.h" #include "../WorldStorage/SchematicFileSerializer.h" #include "../CompositeChat.h" +#include "../StringCompression.h" @@ -110,6 +111,40 @@ static int tolua_Clamp(lua_State * tolua_S) +static int tolua_CompressString(lua_State * tolua_S) +{ + cLuaState LuaState(tolua_S); + const char * ToCompress = tolua_tocppstring(LuaState, 1, 0); + int Length = (int)tolua_tonumber(LuaState, 2, 0); + int Factor = (int)tolua_tonumber(LuaState, 3, 0); + AString res; + + CompressString(ToCompress, Length, res, Factor); + LuaState.Push(res); + return 1; +} + + + + + +static int tolua_UncompressString(lua_State * tolua_S) +{ + cLuaState LuaState(tolua_S); + const char * ToUncompress = tolua_tocppstring(LuaState, 1, 0); + int Length = (int)tolua_tonumber(LuaState, 2, 0); + int UncompressedSize = (int)tolua_tonumber(LuaState, 3, 0); + AString res; + + UncompressString(ToUncompress, Length, res, UncompressedSize); + LuaState.Push(res); + return 1; +} + + + + + static int tolua_StringSplit(lua_State * tolua_S) { cLuaState LuaState(tolua_S); @@ -3519,6 +3554,8 @@ void ManualBindings::Bind(lua_State * tolua_S) // Globals: tolua_function(tolua_S, "Clamp", tolua_Clamp); + tolua_function(tolua_S, "CompressString", tolua_CompressString); + tolua_function(tolua_S, "UncompressString", tolua_UncompressString); tolua_function(tolua_S, "StringSplit", tolua_StringSplit); tolua_function(tolua_S, "StringSplitAndTrim", tolua_StringSplitAndTrim); tolua_function(tolua_S, "LOG", tolua_LOG); -- cgit v1.2.3 From f073636805682ff5018f876ff3ae4a0fa34cdff8 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Sun, 22 Feb 2015 17:40:44 +0100 Subject: Documented CompressString and UncompressString --- MCServer/Plugins/APIDump/APIDesc.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index 61fc69d32..02ac537ee 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -2900,6 +2900,7 @@ end BlockStringToType = {Params = "BlockTypeString", Return = "BLOCKTYPE", Notes = "Returns the block type parsed from the given string"}, Clamp = {Params = "Number, Min, Max", Return = "number", Notes = "Clamp the number to the specified range."}, ClickActionToString = {Params = "{{Globals#ClickAction|ClickAction}}", Return = "string", Notes = "Returns a string description of the ClickAction enumerated value"}, + CompressString = {Params = "string, length, factor", Return = "string", Notes = "Compresses a string using ZLIB"}, DamageTypeToString = {Params = "{{Globals#DamageType|DamageType}}", Return = "string", Notes = "Converts the {{Globals#DamageType|DamageType}} enumerated value to a string representation "}, EscapeString = {Params = "string", Return = "string", Notes = "Returns a copy of the string with all quotes and backslashes escaped by a backslash"}, GetChar = {Params = "String, Pos", Return = "string", Notes = "Returns one character from the string, specified by position "}, @@ -2951,6 +2952,7 @@ end StringToMobType = {Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "DEPRECATED! Please use cMonster:StringToMobType(). Converts a string representation to a {{Globals#MobType|MobType}} enumerated value"}, StripColorCodes = {Params = "string", Return = "string", Notes = "Removes all control codes used by MC for colors and styles"}, TrimString = {Params = "string", Return = "string", Notes = "Trims whitespace at both ends of the string"}, + UncompressString = {Params = "string, length, uncompressed length", Return = "string", Notes = "Uncompresses a string using ZLIB"}, md5 = {Params = "string", Return = "string", Notes = "OBSOLETE, use the {{cCryptoHash}} functions instead.
Converts a string to a raw binary md5 hash."}, }, ConstantGroups = -- cgit v1.2.3 From 54410bfe4dac8793d6b46df725c400f7ce8d365e Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 12:53:02 +0100 Subject: Exported all compression functions in a new class. --- src/Bindings/ManualBindings.cpp | 145 +++++++++++++++++++++++++++++++++++----- 1 file changed, 129 insertions(+), 16 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index ee3d81014..451161d87 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -111,16 +111,58 @@ static int tolua_Clamp(lua_State * tolua_S) -static int tolua_CompressString(lua_State * tolua_S) +static int tolua_CompressStringZLIB(lua_State * tolua_S) { - cLuaState LuaState(tolua_S); - const char * ToCompress = tolua_tocppstring(LuaState, 1, 0); - int Length = (int)tolua_tonumber(LuaState, 2, 0); - int Factor = (int)tolua_tonumber(LuaState, 3, 0); + cLuaState S(tolua_S); + if ( + !lua_isstring(tolua_S, 1) || + ( + !lua_isnumber(tolua_S, 2) && + !lua_isnil(tolua_S, 2) + ) + ) + { + cLuaState::LogStackTrace(tolua_S); + return 0; + } + + // Get the params: + AString ToCompress; + int CompressionLevel = 5; + S.GetStackValues(1, ToCompress, CompressionLevel); + + // Compress the string: AString res; + CompressString(ToCompress.data(), ToCompress.size(), res, CompressionLevel); + S.Push(res); + return 1; +} + + - CompressString(ToCompress, Length, res, Factor); - LuaState.Push(res); + + +static int tolua_UncompressStringZLIB(lua_State * tolua_S) +{ + cLuaState S(tolua_S); + if ( + !lua_isstring(tolua_S, 1) || + !lua_isnumber(tolua_S, 2) + ) + { + cLuaState::LogStackTrace(tolua_S); + return 0; + } + + // Get the params: + AString ToUncompress; + int UncompressedSize; + S.GetStackValues(1, ToUncompress, UncompressedSize); + + // Compress the string: + AString res; + UncompressString(ToUncompress.data(), ToUncompress.size(), res, UncompressedSize); + S.Push(res); return 1; } @@ -128,16 +170,79 @@ static int tolua_CompressString(lua_State * tolua_S) -static int tolua_UncompressString(lua_State * tolua_S) +static int tolua_CompressStringGZIP(lua_State * tolua_S) { - cLuaState LuaState(tolua_S); - const char * ToUncompress = tolua_tocppstring(LuaState, 1, 0); - int Length = (int)tolua_tonumber(LuaState, 2, 0); - int UncompressedSize = (int)tolua_tonumber(LuaState, 3, 0); + cLuaState S(tolua_S); + if (!lua_isstring(tolua_S, 1)) + { + cLuaState::LogStackTrace(tolua_S); + return 0; + } + + // Get the params: + AString ToCompress; + S.GetStackValues(1, ToCompress); + + // Compress the string: + AString res; + CompressStringGZIP(ToCompress.data(), ToCompress.size(), res); + S.Push(res); + return 1; +} + + + + + +static int tolua_UncompressStringGZIP(lua_State * tolua_S) +{ + cLuaState S(tolua_S); + if ( + !lua_isstring(tolua_S, 1) + ) + { + cLuaState::LogStackTrace(tolua_S); + return 0; + } + + + + // Get the params: + AString ToUncompress; + S.GetStackValues(1, ToUncompress); + + // Compress the string: AString res; + UncompressStringGZIP(ToUncompress.data(), ToUncompress.size(), res); + S.Push(res); + return 1; +} - UncompressString(ToUncompress, Length, res, UncompressedSize); - LuaState.Push(res); + + + + +static int tolua_InflateString(lua_State * tolua_S) +{ + cLuaState S(tolua_S); + if ( + !lua_isstring(tolua_S, 1) + ) + { + cLuaState::LogStackTrace(tolua_S); + return 0; + } + + + + // Get the params: + AString ToUncompress; + S.GetStackValues(1, ToUncompress); + + // Compress the string: + AString res; + InflateString(ToUncompress.data(), ToUncompress.size(), res); + S.Push(res); return 1; } @@ -3551,11 +3656,11 @@ void ManualBindings::Bind(lua_State * tolua_S) // Create the new classes: tolua_usertype(tolua_S, "cCryptoHash"); tolua_cclass(tolua_S, "cCryptoHash", "cCryptoHash", "", nullptr); + tolua_usertype(tolua_S, "cStringCompression"); + tolua_cclass(tolua_S, "cStringCompression", "cStringCompression", "", nullptr); // Globals: tolua_function(tolua_S, "Clamp", tolua_Clamp); - tolua_function(tolua_S, "CompressString", tolua_CompressString); - tolua_function(tolua_S, "UncompressString", tolua_UncompressString); tolua_function(tolua_S, "StringSplit", tolua_StringSplit); tolua_function(tolua_S, "StringSplitAndTrim", tolua_StringSplitAndTrim); tolua_function(tolua_S, "LOG", tolua_LOG); @@ -3729,6 +3834,14 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_function(tolua_S, "sha1HexString", tolua_sha1HexString); tolua_endmodule(tolua_S); + tolua_beginmodule(tolua_S, "cStringCompression"); + tolua_function(tolua_S, "CompressStringZLIB", tolua_CompressStringZLIB); + tolua_function(tolua_S, "UncompressStringZLIB", tolua_UncompressStringZLIB); + tolua_function(tolua_S, "CompressStringGZIP", tolua_CompressStringGZIP); + tolua_function(tolua_S, "UncompressStringGZIP", tolua_UncompressStringGZIP); + tolua_function(tolua_S, "InflateString", tolua_InflateString); + tolua_endmodule(tolua_S); + BindRankManager(tolua_S); BindNetwork(tolua_S); -- cgit v1.2.3 From 174f5080210aafb5856813c5e178aa7347b8ef8d Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 12:53:34 +0100 Subject: Documented cStringCompression --- MCServer/Plugins/APIDump/APIDesc.lua | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index 02ac537ee..c214434b5 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -2254,6 +2254,27 @@ end ShouldAuthenticate = { Params = "", Return = "bool", Notes = "Returns true iff the server is set to authenticate players (\"online mode\")." }, }, }, -- cServer + + cStringCompression = + { + Desc = [[ + Provides functions to compress or decompress string +

+ All functions in this class are static, so they should be called in the dot convention: +

+local CompressedString = cStringCompression.CompressStringGZIP("DataToCompress")
+
+ ]], + + Functions = + { + CompressStringGZIP = {Params = "string", Return = "string", Notes = "Compress a string using GZIP"}, + CompressStringZLIB = {Params = "string, factor", Return = "string", Notes = "Compresses a string using ZLIB"}, + InflateString = {Params = "string", Return = "string", Notes = "Uncompresses a string using Inflate"}, + UncompressStringGZIP = {Params = "string", Return = "string", Notes = "Uncompress a string using GZIP"}, + UncompressStringZLIB = {Params = "string, uncompressed length", Return = "string", Notes = "Uncompresses a string using ZLIB"}, + }, + }, cTeam = { @@ -2900,7 +2921,6 @@ end BlockStringToType = {Params = "BlockTypeString", Return = "BLOCKTYPE", Notes = "Returns the block type parsed from the given string"}, Clamp = {Params = "Number, Min, Max", Return = "number", Notes = "Clamp the number to the specified range."}, ClickActionToString = {Params = "{{Globals#ClickAction|ClickAction}}", Return = "string", Notes = "Returns a string description of the ClickAction enumerated value"}, - CompressString = {Params = "string, length, factor", Return = "string", Notes = "Compresses a string using ZLIB"}, DamageTypeToString = {Params = "{{Globals#DamageType|DamageType}}", Return = "string", Notes = "Converts the {{Globals#DamageType|DamageType}} enumerated value to a string representation "}, EscapeString = {Params = "string", Return = "string", Notes = "Returns a copy of the string with all quotes and backslashes escaped by a backslash"}, GetChar = {Params = "String, Pos", Return = "string", Notes = "Returns one character from the string, specified by position "}, @@ -2952,7 +2972,6 @@ end StringToMobType = {Params = "string", Return = "{{Globals#MobType|MobType}}", Notes = "DEPRECATED! Please use cMonster:StringToMobType(). Converts a string representation to a {{Globals#MobType|MobType}} enumerated value"}, StripColorCodes = {Params = "string", Return = "string", Notes = "Removes all control codes used by MC for colors and styles"}, TrimString = {Params = "string", Return = "string", Notes = "Trims whitespace at both ends of the string"}, - UncompressString = {Params = "string, length, uncompressed length", Return = "string", Notes = "Uncompresses a string using ZLIB"}, md5 = {Params = "string", Return = "string", Notes = "OBSOLETE, use the {{cCryptoHash}} functions instead.
Converts a string to a raw binary md5 hash."}, }, ConstantGroups = -- cgit v1.2.3 From 8c8ec1094d8ba6df410f1fb0d0fba216b98a8f9a Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 15:29:07 +0100 Subject: Replaced lua_isXYZ with cLuaState::CheckParamXYZ --- src/Bindings/ManualBindings.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 451161d87..7255f73a4 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -115,10 +115,10 @@ static int tolua_CompressStringZLIB(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !lua_isstring(tolua_S, 1) || + !S.CheckParamString(1) && ( - !lua_isnumber(tolua_S, 2) && - !lua_isnil(tolua_S, 2) + !S.CheckParamNumber(2) || + !S.CheckParamEnd(2) ) ) { @@ -146,8 +146,8 @@ static int tolua_UncompressStringZLIB(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !lua_isstring(tolua_S, 1) || - !lua_isnumber(tolua_S, 2) + !S.CheckParamString(1) && + !S.CheckParamNumber(2) ) { cLuaState::LogStackTrace(tolua_S); @@ -173,7 +173,10 @@ static int tolua_UncompressStringZLIB(lua_State * tolua_S) static int tolua_CompressStringGZIP(lua_State * tolua_S) { cLuaState S(tolua_S); - if (!lua_isstring(tolua_S, 1)) + if ( + !S.CheckParamString(1) && + !S.CheckParamEnd(2) + ) { cLuaState::LogStackTrace(tolua_S); return 0; @@ -198,15 +201,14 @@ static int tolua_UncompressStringGZIP(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !lua_isstring(tolua_S, 1) + !S.CheckParamString(1) && + !S.CheckParamEnd(2) ) { cLuaState::LogStackTrace(tolua_S); return 0; } - - // Get the params: AString ToUncompress; S.GetStackValues(1, ToUncompress); @@ -226,15 +228,14 @@ static int tolua_InflateString(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !lua_isstring(tolua_S, 1) + !S.CheckParamString(1) && + !S.CheckParamEnd(2) ) { cLuaState::LogStackTrace(tolua_S); return 0; } - - // Get the params: AString ToUncompress; S.GetStackValues(1, ToUncompress); -- cgit v1.2.3 From d39d2ca5e9592760246b11bdb0336e067ed8ee8a Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 15:40:31 +0100 Subject: Added forgotten indent --- src/Bindings/ManualBindings.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 7255f73a4..2e9d49ef0 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -117,8 +117,8 @@ static int tolua_CompressStringZLIB(lua_State * tolua_S) if ( !S.CheckParamString(1) && ( - !S.CheckParamNumber(2) || - !S.CheckParamEnd(2) + !S.CheckParamNumber(2) || + !S.CheckParamEnd(2) ) ) { -- cgit v1.2.3 From 9e1db16ba497fd527b95fc00c69229de3957876d Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 16:09:35 +0100 Subject: Fixed operators --- src/Bindings/ManualBindings.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 2e9d49ef0..a6ae4869b 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -115,9 +115,9 @@ static int tolua_CompressStringZLIB(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !S.CheckParamString(1) && + !S.CheckParamString(1) || ( - !S.CheckParamNumber(2) || + !S.CheckParamNumber(2) && !S.CheckParamEnd(2) ) ) @@ -146,7 +146,7 @@ static int tolua_UncompressStringZLIB(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !S.CheckParamString(1) && + !S.CheckParamString(1) || !S.CheckParamNumber(2) ) { @@ -174,7 +174,7 @@ static int tolua_CompressStringGZIP(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !S.CheckParamString(1) && + !S.CheckParamString(1) || !S.CheckParamEnd(2) ) { @@ -201,7 +201,7 @@ static int tolua_UncompressStringGZIP(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !S.CheckParamString(1) && + !S.CheckParamString(1) || !S.CheckParamEnd(2) ) { @@ -228,7 +228,7 @@ static int tolua_InflateString(lua_State * tolua_S) { cLuaState S(tolua_S); if ( - !S.CheckParamString(1) && + !S.CheckParamString(1) || !S.CheckParamEnd(2) ) { -- cgit v1.2.3 From 056b42cb94acd50029baf1526aca4d246df25814 Mon Sep 17 00:00:00 2001 From: STRWarrior Date: Mon, 23 Feb 2015 18:43:01 +0100 Subject: Added documentation for CompressStringZLIB There was no info about the factor. --- MCServer/Plugins/APIDump/APIDesc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MCServer/Plugins/APIDump/APIDesc.lua b/MCServer/Plugins/APIDump/APIDesc.lua index c214434b5..f2ec2546a 100644 --- a/MCServer/Plugins/APIDump/APIDesc.lua +++ b/MCServer/Plugins/APIDump/APIDesc.lua @@ -2269,7 +2269,7 @@ local CompressedString = cStringCompression.CompressStringGZIP("DataToCompress") Functions = { CompressStringGZIP = {Params = "string", Return = "string", Notes = "Compress a string using GZIP"}, - CompressStringZLIB = {Params = "string, factor", Return = "string", Notes = "Compresses a string using ZLIB"}, + CompressStringZLIB = {Params = "string, factor", Return = "string", Notes = "Compresses a string using ZLIB. Factor 0 is no compression and factor 9 is maximum compression"}, InflateString = {Params = "string", Return = "string", Notes = "Uncompresses a string using Inflate"}, UncompressStringGZIP = {Params = "string", Return = "string", Notes = "Uncompress a string using GZIP"}, UncompressStringZLIB = {Params = "string, uncompressed length", Return = "string", Notes = "Uncompresses a string using ZLIB"}, -- cgit v1.2.3