diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e20b48ad..ca8fb61b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,6 +161,11 @@ IF(EQEMU_BUILD_LUA) FIND_PACKAGE(Boost REQUIRED) INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}" "${Boost_INCLUDE_DIRS}" "luabind") + + OPTION(EQEMU_SANITIZE_LUA_LIBS "Sanitize Lua Libraries (Remove OS and IO standard libraries from being able to run)." ON) + IF(EQEMU_SANITIZE_LUA_LIBS) + ADD_DEFINITIONS(-DSANITIZE_LUA_LIBS) + ENDIF(EQEMU_SANITIZE_LUA_LIBS) ENDIF(EQEMU_BUILD_LUA) INCLUDE_DIRECTORIES("${ZLIB_INCLUDE_DIRS}" "${MySQL_INCLUDE_DIR}") diff --git a/common/Item.cpp b/common/Item.cpp index 0d03bc5c7..0c750c238 100644 --- a/common/Item.cpp +++ b/common/Item.cpp @@ -30,6 +30,7 @@ #include #include +std::list dirty_inst; int32 NextItemInstSerialNumber = 1; static inline int32 GetNextItemInstSerialNumber() { @@ -110,7 +111,6 @@ ItemInstQueue::~ItemInstQueue() { Inventory::~Inventory() { std::map::iterator cur,end; - cur = m_worn.begin(); end = m_worn.end(); for(; cur != end; cur++) { @@ -1116,7 +1116,7 @@ bool Inventory::DeleteItem(int16 slot_id, uint8 quantity) (!item_to_delete->IsStackable() && ((item_to_delete->GetItem()->MaxCharges == 0) || item_to_delete->IsExpendable()))) { // Item can now be destroyed - safe_delete(item_to_delete); + Inventory::MarkDirty(item_to_delete); return true; } } @@ -1126,8 +1126,7 @@ bool Inventory::DeleteItem(int16 slot_id, uint8 quantity) return false; } - safe_delete(item_to_delete); - + Inventory::MarkDirty(item_to_delete); return true; } @@ -1418,7 +1417,7 @@ int16 Inventory::_PutItem(int16 slot_id, ItemInst* inst) if (result == SLOT_INVALID) { LogFile->write(EQEMuLog::Error, "Inventory::_PutItem: Invalid slot_id specified (%i)", slot_id); - safe_delete(inst); // Slot not found, clean up + Inventory::MarkDirty(inst); // Slot not found, clean up } return result; @@ -1933,3 +1932,18 @@ bool Item_Struct::IsEquipable(uint16 Race, uint16 Class_) const } return (IsRace && IsClass); } + +void Inventory::CleanDirty() { + auto iter = dirty_inst.begin(); + while(iter != dirty_inst.end()) { + delete (*iter); + ++iter; + } + dirty_inst.clear(); +} + +void Inventory::MarkDirty(ItemInst *inst) { + if(inst) { + dirty_inst.push_back(inst); + } +} diff --git a/common/Item.h b/common/Item.h index 08a0aaa3f..47eb7556a 100644 --- a/common/Item.h +++ b/common/Item.h @@ -136,6 +136,9 @@ public: ~Inventory(); + static void CleanDirty(); + static void MarkDirty(ItemInst *inst); + // Retrieve a writeable item at specified slot ItemInst* GetItem(int16 slot_id) const; ItemInst* GetItem(int16 slot_id, uint8 bagidx) const; diff --git a/zone/CMakeLists.txt b/zone/CMakeLists.txt index 0e63f4fd5..2e46a3d14 100644 --- a/zone/CMakeLists.txt +++ b/zone/CMakeLists.txt @@ -32,6 +32,7 @@ SET(zone_sources horse.cpp inventory.cpp loottables.cpp + lua_bit.cpp lua_corpse.cpp lua_client.cpp lua_door.cpp @@ -141,6 +142,7 @@ SET(zone_headers guild_mgr.h hate_list.h horse.h + lua_bit.h lua_client.h lua_corpse.h lua_entity.h diff --git a/zone/client.cpp b/zone/client.cpp index 6ecc3031b..64cae2eaa 100644 --- a/zone/client.cpp +++ b/zone/client.cpp @@ -2683,9 +2683,6 @@ bool Client::BindWound(Mob* bindmob, bool start, bool fail){ } // Send client bind done - //this is taken care of on start of bind, not finish now, and is improved - //DeleteItemInInventory(m_inv.HasItem(13009, 1), 1, true); - bind_out->type = 1; // Done QueuePacket(outapp); bind_out->type = 0; diff --git a/zone/client_process.cpp b/zone/client_process.cpp index acc8baf88..9ca1ac707 100644 --- a/zone/client_process.cpp +++ b/zone/client_process.cpp @@ -692,14 +692,9 @@ bool Client::Process() { /************ Get all packets from packet manager out queue and process them ************/ adverrorinfo = 5; - EQApplicationPacket *app = 0; -// if(eqs->GetState()==CLOSING && eqs->CheckActive()) - if(eqs->CheckState(CLOSING)) + EQApplicationPacket *app = nullptr; + if(!eqs->CheckState(CLOSING)) { - //eqs->Close(); - //return false; - //handled below - } else { while(ret && (app = (EQApplicationPacket *)eqs->PopPacket())) { if(app) ret = HandlePacket(app); diff --git a/zone/lua_bit.cpp b/zone/lua_bit.cpp new file mode 100644 index 000000000..730d152b7 --- /dev/null +++ b/zone/lua_bit.cpp @@ -0,0 +1,179 @@ +/* +** Lua BitOp -- a bit operations library for Lua 5.1/5.2. +** http://bitop.luajit.org/ +** +** Copyright (C) 2008-2012 Mike Pall. All rights reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +*/ + +#define LUA_BITOP_VERSION "1.0.2" + +#include "lua.hpp" +#include + +typedef int32_t SBits; +typedef uint32_t UBits; + +typedef union { + lua_Number n; +#ifdef LUA_NUMBER_DOUBLE + uint64_t b; +#else + UBits b; +#endif +} BitNum; + +/* Convert argument to bit type. */ +static UBits barg(lua_State *L, int idx) +{ + BitNum bn; + UBits b; +#if LUA_VERSION_NUM < 502 + bn.n = lua_tonumber(L, idx); +#else + bn.n = luaL_checknumber(L, idx); +#endif +#if defined(LUA_NUMBER_DOUBLE) + bn.n += 6755399441055744.0; /* 2^52+2^51 */ +#ifdef SWAPPED_DOUBLE + b = (UBits)(bn.b >> 32); +#else + b = (UBits)bn.b; +#endif +#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \ + defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \ + defined(LUA_NUMBER_LLONG) + if (sizeof(UBits) == sizeof(lua_Number)) + b = bn.b; + else + b = (UBits)(SBits)bn.n; +#elif defined(LUA_NUMBER_FLOAT) +#error "A 'float' lua_Number type is incompatible with this library" +#else +#error "Unknown number type, check LUA_NUMBER_* in luaconf.h" +#endif +#if LUA_VERSION_NUM < 502 + if (b == 0 && !lua_isnumber(L, idx)) { + luaL_typerror(L, idx, "number"); + } +#endif + return b; +} + +/* Return bit type. */ +#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1; + +static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) } +static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) } + +#define BIT_OP(func, opr) \ + static int func(lua_State *L) { int i; UBits b = barg(L, 1); \ + for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) } +BIT_OP(bit_band, &=) +BIT_OP(bit_bor, |=) +BIT_OP(bit_bxor, ^=) + +#define bshl(b, n) (b << n) +#define bshr(b, n) (b >> n) +#define bsar(b, n) ((SBits)b >> n) +#define brol(b, n) ((b << n) | (b >> (32-n))) +#define bror(b, n) ((b << (32-n)) | (b >> n)) +#define BIT_SH(func, fn) \ + static int func(lua_State *L) { \ + UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) } +BIT_SH(bit_lshift, bshl) +BIT_SH(bit_rshift, bshr) +BIT_SH(bit_arshift, bsar) +BIT_SH(bit_rol, brol) +BIT_SH(bit_ror, bror) + +static int bit_bswap(lua_State *L) +{ + UBits b = barg(L, 1); + b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24); + BRET(b) +} + +static int bit_tohex(lua_State *L) +{ + UBits b = barg(L, 1); + SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2); + const char *hexdigits = "0123456789abcdef"; + char buf[8]; + int i; + if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } + if (n > 8) n = 8; + for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } + lua_pushlstring(L, buf, (size_t)n); + return 1; +} + +static const struct luaL_Reg bit_funcs[] = { + { "tobit", bit_tobit }, + { "bnot", bit_bnot }, + { "band", bit_band }, + { "bor", bit_bor }, + { "bxor", bit_bxor }, + { "lshift", bit_lshift }, + { "rshift", bit_rshift }, + { "arshift", bit_arshift }, + { "rol", bit_rol }, + { "ror", bit_ror }, + { "bswap", bit_bswap }, + { "tohex", bit_tohex }, + { NULL, NULL } +}; + +/* Signed right-shifts are implementation-defined per C89/C99. +** But the de facto standard are arithmetic right-shifts on two's +** complement CPUs. This behaviour is required here, so test for it. +*/ +#define BAD_SAR (bsar(-8, 2) != (SBits)-2) + +LUALIB_API int luaopen_bit(lua_State *L) +{ + UBits b; + lua_pushnumber(L, (lua_Number)1437217655L); + b = barg(L, -1); + if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */ + const char *msg = "compiled with incompatible luaconf.h"; +#ifdef LUA_NUMBER_DOUBLE +#ifdef _WIN32 + if (b == (UBits)1610612736L) + msg = "use D3DCREATE_FPU_PRESERVE with DirectX"; +#endif + if (b == (UBits)1127743488L) + msg = "not compiled with SWAPPED_DOUBLE"; +#endif + if (BAD_SAR) + msg = "arithmetic right-shift broken"; + luaL_error(L, "bit library self-test failed (%s)", msg); + } +#if LUA_VERSION_NUM < 502 + luaL_register(L, "bit", bit_funcs); +#else + luaL_newlib(L, bit_funcs); +#endif + return 1; +} + diff --git a/zone/lua_bit.h b/zone/lua_bit.h new file mode 100644 index 000000000..67cd659ba --- /dev/null +++ b/zone/lua_bit.h @@ -0,0 +1,6 @@ +#ifndef _LUABIT_H +#define _LUABIT_H + +int luaopen_bit(lua_State *L); + +#endif diff --git a/zone/lua_client.cpp b/zone/lua_client.cpp index fae5c3045..b2361e129 100644 --- a/zone/lua_client.cpp +++ b/zone/lua_client.cpp @@ -609,6 +609,11 @@ int Lua_Client::GetHorseId() { return self->GetHorseId(); } +void Lua_Client::NukeItem(uint32 item_num) { + Lua_Safe_Call_Void(); + self->NukeItem(item_num, 0xFF); +} + void Lua_Client::NukeItem(uint32 item_num, int where_to_check) { Lua_Safe_Call_Void(); self->NukeItem(item_num, where_to_check); @@ -1300,6 +1305,7 @@ luabind::scope lua_register_client() { .def("AutoSplitEnabled", (bool(Lua_Client::*)(void))&Lua_Client::AutoSplitEnabled) .def("SetHorseId", (void(Lua_Client::*)(int))&Lua_Client::SetHorseId) .def("GetHorseId", (int(Lua_Client::*)(void))&Lua_Client::GetHorseId) + .def("NukeItem", (void(Lua_Client::*)(uint32))&Lua_Client::NukeItem) .def("NukeItem", (void(Lua_Client::*)(uint32,int))&Lua_Client::NukeItem) .def("SetTint", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetTint) .def("SetMaterial", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetMaterial) diff --git a/zone/lua_client.h b/zone/lua_client.h index 54b4009b4..9c4705cd1 100644 --- a/zone/lua_client.h +++ b/zone/lua_client.h @@ -147,6 +147,7 @@ public: bool AutoSplitEnabled(); void SetHorseId(int id); int GetHorseId(); + void NukeItem(uint32 item_num); void NukeItem(uint32 item_num, int where_to_check); void SetTint(int slot_id, uint32 color); void SetMaterial(int slot_id, uint32 item_id); diff --git a/zone/lua_parser.cpp b/zone/lua_parser.cpp index 27cf68d90..5313e6528 100644 --- a/zone/lua_parser.cpp +++ b/zone/lua_parser.cpp @@ -12,6 +12,7 @@ #include "masterentity.h" #include "../common/spdat.h" +#include "lua_bit.h" #include "lua_entity.h" #include "lua_item.h" #include "lua_iteminst.h" @@ -769,11 +770,18 @@ void LuaParser::ReloadQuests() { L = luaL_newstate(); luaL_openlibs(L); + if(luaopen_bit(L) != 1) { + std::string error = lua_tostring(L, -1); + AddError(error); + } + +#ifdef SANITIZE_LUA_LIBS lua_pushnil(L); lua_setglobal(L, "os"); lua_pushnil(L); lua_setglobal(L, "io"); +#endif lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); diff --git a/zone/zone.cpp b/zone/zone.cpp index 1c61ee357..a4d068146 100644 --- a/zone/zone.cpp +++ b/zone/zone.cpp @@ -1286,6 +1286,8 @@ bool Zone::Process() { if(spawn2_timer.Check()) { LinkedListIterator iterator(spawn2_list); + Inventory::CleanDirty(); + iterator.Reset(); while (iterator.MoreElements()) { if (iterator.GetData()->Process()) {