From eeb63b8901a9c049f1bb594abb9ce9b4a9c47620 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 11 Jan 2021 16:39:43 +0000 Subject: zlib -> libdeflate (#5085) + Use libdeflate + Use std::byte * Fix passing temporary to string_view + Emulate make_unique_for_overwrite --- src/Bindings/LuaState.cpp | 29 ++++++++++++- src/Bindings/LuaState.h | 2 + src/Bindings/ManualBindings.cpp | 67 ++++++++++++------------------- src/Bindings/ManualBindings_BlockArea.cpp | 50 +++++++++++++++++++---- src/Bindings/Plugin.h | 2 +- src/Bindings/PluginLua.cpp | 2 +- src/Bindings/PluginLua.h | 2 +- src/Bindings/PluginManager.cpp | 2 +- src/Bindings/PluginManager.h | 2 +- 9 files changed, 102 insertions(+), 56 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp index c6042ac62..81dcb0e67 100644 --- a/src/Bindings/LuaState.cpp +++ b/src/Bindings/LuaState.cpp @@ -863,7 +863,7 @@ void cLuaState::Push(const AStringVector & a_Vector) int index = 1; for (AStringVector::const_iterator itr = a_Vector.begin(), end = a_Vector.end(); itr != end; ++itr, ++index) { - tolua_pushstring(m_LuaState, itr->c_str()); + Push(*itr); lua_rawseti(m_LuaState, newTable, index); } } @@ -916,6 +916,17 @@ void cLuaState::Push(const cLuaState::cRef & a_Ref) +void cLuaState::Push(const ContiguousByteBufferView a_Data) +{ + ASSERT(IsValid()); + + lua_pushlstring(m_LuaState, reinterpret_cast(a_Data.data()), a_Data.size()); +} + + + + + void cLuaState::Push(const Vector3d & a_Vector) { ASSERT(IsValid()); @@ -1355,6 +1366,22 @@ bool cLuaState::GetStackValue(int a_StackPos, cTrackedRefSharedPtr & a_Ref) +bool cLuaState::GetStackValue(int a_StackPos, ContiguousByteBuffer & a_Data) +{ + size_t Length = 0; + const char * const Data = lua_tolstring(m_LuaState, a_StackPos, &Length); + if (Data != nullptr) + { + a_Data.assign(reinterpret_cast(Data), Length); + return true; + } + return false; +} + + + + + bool cLuaState::GetStackValue(int a_StackPos, double & a_ReturnedVal) { if (lua_isnumber(m_LuaState, a_StackPos)) diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h index 0bdecdfc7..b3f567ecb 100644 --- a/src/Bindings/LuaState.h +++ b/src/Bindings/LuaState.h @@ -619,6 +619,7 @@ public: void Push(const cItem & a_Item); void Push(const cNil & a_Nil); void Push(const cRef & a_Ref); + void Push(ContiguousByteBufferView a_Data); void Push(const Vector3d & a_Vector); void Push(const Vector3i & a_Vector); @@ -658,6 +659,7 @@ public: bool GetStackValue(int a_StackPos, cTrackedRef & a_Ref); bool GetStackValue(int a_StackPos, cTrackedRefPtr & a_Ref); bool GetStackValue(int a_StackPos, cTrackedRefSharedPtr & a_Ref); + bool GetStackValue(int a_StackPos, ContiguousByteBuffer & a_Data); bool GetStackValue(int a_StackPos, double & a_Value); bool GetStackValue(int a_StackPos, eBlockFace & a_Value); bool GetStackValue(int a_StackPos, eWeather & a_Value); diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 92f7dd92b..20364100f 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -188,9 +188,7 @@ static int tolua_CompressStringZLIB(lua_State * tolua_S) S.GetStackValues(1, ToCompress, CompressionLevel); // Compress the string: - AString res; - CompressString(ToCompress.data(), ToCompress.size(), res, CompressionLevel); - S.Push(res); + S.Push(Compression::Compressor(CompressionLevel).CompressZLib(ToCompress.data(), ToCompress.size()).GetView()); return 1; } @@ -211,14 +209,21 @@ static int tolua_UncompressStringZLIB(lua_State * tolua_S) } // Get the params: - AString ToUncompress; + ContiguousByteBuffer ToUncompress; size_t UncompressedSize = 0; S.GetStackValues(1, ToUncompress, UncompressedSize); - // Compress the string: - AString res; - UncompressString(ToUncompress.data(), ToUncompress.size(), res, UncompressedSize); - S.Push(res); + try + { + // Decompress the string: + S.Push(Compression::Extractor().ExtractZLib(ToUncompress, UncompressedSize).GetView()); + } + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); + cLuaState::LogStackTrace(tolua_S); + return 0; + } return 1; } @@ -239,13 +244,11 @@ static int tolua_CompressStringGZIP(lua_State * tolua_S) } // Get the params: - AString ToCompress; + ContiguousByteBuffer ToCompress; S.GetStackValues(1, ToCompress); // Compress the string: - AString res; - CompressStringGZIP(ToCompress.data(), ToCompress.size(), res); - S.Push(res); + S.Push(Compression::Compressor().CompressGZip(ToCompress).GetView()); return 1; } @@ -253,7 +256,7 @@ static int tolua_CompressStringGZIP(lua_State * tolua_S) -static int tolua_UncompressStringGZIP(lua_State * tolua_S) +static int tolua_InflateString(lua_State * tolua_S) { cLuaState S(tolua_S); if ( @@ -266,40 +269,20 @@ static int tolua_UncompressStringGZIP(lua_State * tolua_S) } // Get the params: - AString ToUncompress; + ContiguousByteBuffer ToUncompress; S.GetStackValues(1, ToUncompress); - // Compress the string: - AString res; - UncompressStringGZIP(ToUncompress.data(), ToUncompress.size(), res); - S.Push(res); - return 1; -} - - - - - -static int tolua_InflateString(lua_State * tolua_S) -{ - cLuaState S(tolua_S); - if ( - !S.CheckParamString(1) || - !S.CheckParamEnd(2) - ) + try { + // Decompress the string: + S.Push(Compression::Extractor().ExtractZLib(ToUncompress).GetView()); + } + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); 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; } @@ -4552,7 +4535,7 @@ void cManualBindings::Bind(lua_State * tolua_S) 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, "UncompressStringGZIP", tolua_InflateString); tolua_function(tolua_S, "InflateString", tolua_InflateString); tolua_endmodule(tolua_S); diff --git a/src/Bindings/ManualBindings_BlockArea.cpp b/src/Bindings/ManualBindings_BlockArea.cpp index 5af150599..5f281fadc 100644 --- a/src/Bindings/ManualBindings_BlockArea.cpp +++ b/src/Bindings/ManualBindings_BlockArea.cpp @@ -435,7 +435,17 @@ static int tolua_cBlockArea_LoadFromSchematicFile(lua_State * a_LuaState) return L.ApiParamError("Invalid 'self', must not be nil"); } - L.Push(cSchematicFileSerializer::LoadFromSchematicFile(*self, fileName)); + try + { + cSchematicFileSerializer::LoadFromSchematicFile(*self, fileName); + L.Push(true); + } + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); + L.LogStackTrace(); + L.Push(false); + } return 1; } @@ -457,7 +467,7 @@ static int tolua_cBlockArea_LoadFromSchematicString(lua_State * a_LuaState) return 0; } cBlockArea * self; - AString data; + ContiguousByteBuffer data; if (!L.GetStackValues(1, self, data)) { return L.ApiParamError("Cannot read the parameters"); @@ -467,7 +477,17 @@ static int tolua_cBlockArea_LoadFromSchematicString(lua_State * a_LuaState) return L.ApiParamError("Invalid 'self', must not be nil"); } - L.Push(cSchematicFileSerializer::LoadFromSchematicString(*self, data)); + try + { + cSchematicFileSerializer::LoadFromSchematicString(*self, data); + L.Push(true); + } + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); + L.LogStackTrace(); + L.Push(false); + } return 1; } @@ -625,7 +645,17 @@ static int tolua_cBlockArea_SaveToSchematicFile(lua_State * a_LuaState) return L.ApiParamError("Invalid 'self', must not be nil"); } - L.Push(cSchematicFileSerializer::SaveToSchematicFile(*self, fileName)); + try + { + cSchematicFileSerializer::SaveToSchematicFile(*self, fileName); + L.Push(true); + } + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); + L.LogStackTrace(); + L.Push(false); + } return 1; } @@ -655,13 +685,17 @@ static int tolua_cBlockArea_SaveToSchematicString(lua_State * a_LuaState) return L.ApiParamError("Invalid 'self', must not be nil"); } - AString data; - if (cSchematicFileSerializer::SaveToSchematicString(*self, data)) + try { - L.Push(data); + L.Push(cSchematicFileSerializer::SaveToSchematicString(*self).GetView()); return 1; } - return 0; + catch (const std::exception & Oops) + { + LOGWARNING(Oops.what()); + L.LogStackTrace(); + return 0; + } } diff --git a/src/Bindings/Plugin.h b/src/Bindings/Plugin.h index e051ff61c..47f8820e8 100644 --- a/src/Bindings/Plugin.h +++ b/src/Bindings/Plugin.h @@ -95,7 +95,7 @@ public: virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) = 0; virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0; virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) = 0; - virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) = 0; + virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, ContiguousByteBufferView a_Message) = 0; virtual bool OnPluginsLoaded (void) = 0; virtual bool OnPostCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe) = 0; virtual bool OnPreCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe) = 0; diff --git a/src/Bindings/PluginLua.cpp b/src/Bindings/PluginLua.cpp index f4cfd4a86..9b6bccf7c 100644 --- a/src/Bindings/PluginLua.cpp +++ b/src/Bindings/PluginLua.cpp @@ -818,7 +818,7 @@ bool cPluginLua::OnPlayerUsingItem(cPlayer & a_Player, int a_BlockX, int a_Block -bool cPluginLua::OnPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) +bool cPluginLua::OnPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const ContiguousByteBufferView a_Message) { return CallSimpleHooks(cPluginManager::HOOK_PLUGIN_MESSAGE, &a_Client, a_Channel, a_Message); } diff --git a/src/Bindings/PluginLua.h b/src/Bindings/PluginLua.h index 2a48adb1e..c3064450f 100644 --- a/src/Bindings/PluginLua.h +++ b/src/Bindings/PluginLua.h @@ -117,7 +117,7 @@ public: virtual bool OnPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; virtual bool OnPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual bool OnPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ) override; - virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) override; + virtual bool OnPluginMessage (cClientHandle & a_Client, const AString & a_Channel, ContiguousByteBufferView a_Message) override; virtual bool OnPluginsLoaded (void) override; virtual bool OnPostCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe) override; virtual bool OnPreCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe) override; diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index 310a3968b..59761ead9 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -1009,7 +1009,7 @@ bool cPluginManager::CallHookPlayerUsingItem(cPlayer & a_Player, int a_BlockX, i -bool cPluginManager::CallHookPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message) +bool cPluginManager::CallHookPluginMessage(cClientHandle & a_Client, const AString & a_Channel, const ContiguousByteBufferView a_Message) { return GenericCallHook(HOOK_PLUGIN_MESSAGE, [&](cPlugin * a_Plugin) { diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h index 8d75509a1..c1f798291 100644 --- a/src/Bindings/PluginManager.h +++ b/src/Bindings/PluginManager.h @@ -288,7 +288,7 @@ public: bool CallHookPlayerUsedItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ); bool CallHookPlayerUsingBlock (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); bool CallHookPlayerUsingItem (cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ); - bool CallHookPluginMessage (cClientHandle & a_Client, const AString & a_Channel, const AString & a_Message); + bool CallHookPluginMessage (cClientHandle & a_Client, const AString & a_Channel, ContiguousByteBufferView a_Message); bool CallHookPluginsLoaded (void); bool CallHookPostCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe); bool CallHookPreCrafting (cPlayer & a_Player, cCraftingGrid & a_Grid, cCraftingRecipe & a_Recipe); -- cgit v1.2.3