From 92133f7de4bf75c203bace5d5269360eb6ab78f9 Mon Sep 17 00:00:00 2001 From: Lukas Pioch Date: Mon, 31 Jul 2017 11:54:31 +0200 Subject: Added end check for stack, use ForEachArrayElement to read table --- src/Bindings/ManualBindings.cpp | 34 +++++++++++++++------------------- src/BookContent.h | 3 --- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index b6ef2b275..45fc11ccb 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -3972,11 +3972,13 @@ static int tolua_cBookContent_GetPages(lua_State * tolua_S) // cBookContent::GetPages() -> table of strings cLuaState L(tolua_S); - if (!L.CheckParamSelf("cBookContent")) + if ( + !L.CheckParamSelf("cBookContent") || + !L.CheckParamEnd(2) + ) { return 0; } - cBookContent * BookContent = reinterpret_cast(tolua_tousertype(tolua_S, 1, nullptr)); L.Push(BookContent->GetPages()); return 1; @@ -3993,29 +3995,23 @@ static int tolua_cBookContent_SetPages(lua_State * tolua_S) cLuaState L(tolua_S); if ( !L.CheckParamSelf("cBookContent") || - !L.CheckParamTable(2)) + !L.CheckParamTable(2) || + !L.CheckParamEnd(3) + ) { return 0; } - cBookContent * BookContent = reinterpret_cast(tolua_tousertype(tolua_S, 1, nullptr)); - - // Convert the input table into AStringVector: - AStringVector Pages; - int NumPages = luaL_getn(L, 2); - Pages.reserve(static_cast(NumPages)); - for (int i = 1; i <= NumPages; i++) - { - lua_rawgeti(L, 2, i); - AString Page; - L.GetStackValue(-1, Page); - if (!Page.empty()) + cLuaState::cStackTablePtr Pages; + L.GetStackValue(2, Pages); + Pages->ForEachArrayElement([=](cLuaState & a_LuaState, int a_Index) -> bool { - Pages.push_back(Page); + AString Page; + a_LuaState.GetStackValue(-1, Page); + BookContent->AddPage(Page); + return false; } - lua_pop(L, 1); - } - BookContent->SetPages(Pages); + ); return 0; } diff --git a/src/BookContent.h b/src/BookContent.h index 51e261ecc..001da824c 100644 --- a/src/BookContent.h +++ b/src/BookContent.h @@ -42,9 +42,6 @@ public: /** Returns a AStringVector ref to the pages. Used in ManualBindings and for saving the book */ const AStringVector & GetPages(void) const { return m_Pages; } - /** Set the pages. Used in ManualBindings */ - void SetPages(const AStringVector & a_Pages) { m_Pages = a_Pages; } - /** Read the book content from nbt. The boolean a_SaveAsJson is optional. If a player creates a book, the text should be in a json string */ static void ParseFromNBT(int TagTag, cBookContent & a_BookContent, const cParsedNBT & a_NBT, bool a_SaveAsJson = false); -- cgit v1.2.3