Sorta hacky work around for doing nukeitem in item quests. Added lua bitop library. Made excluding io/os optional

This commit is contained in:
KimLS 2013-06-25 14:51:06 -07:00
parent ffe6494147
commit 1480b8911f
12 changed files with 233 additions and 15 deletions

View File

@ -161,6 +161,11 @@ IF(EQEMU_BUILD_LUA)
FIND_PACKAGE(Boost REQUIRED) FIND_PACKAGE(Boost REQUIRED)
INCLUDE_DIRECTORIES("${LUA_INCLUDE_DIR}" "${Boost_INCLUDE_DIRS}" "luabind") 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) ENDIF(EQEMU_BUILD_LUA)
INCLUDE_DIRECTORIES("${ZLIB_INCLUDE_DIRS}" "${MySQL_INCLUDE_DIR}") INCLUDE_DIRECTORIES("${ZLIB_INCLUDE_DIRS}" "${MySQL_INCLUDE_DIR}")

View File

@ -30,6 +30,7 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
std::list<ItemInst*> dirty_inst;
int32 NextItemInstSerialNumber = 1; int32 NextItemInstSerialNumber = 1;
static inline int32 GetNextItemInstSerialNumber() { static inline int32 GetNextItemInstSerialNumber() {
@ -110,7 +111,6 @@ ItemInstQueue::~ItemInstQueue() {
Inventory::~Inventory() { Inventory::~Inventory() {
std::map<int16, ItemInst*>::iterator cur,end; std::map<int16, ItemInst*>::iterator cur,end;
cur = m_worn.begin(); cur = m_worn.begin();
end = m_worn.end(); end = m_worn.end();
for(; cur != end; cur++) { for(; cur != end; cur++) {
@ -1116,7 +1116,7 @@ bool Inventory::DeleteItem(int16 slot_id, uint8 quantity)
(!item_to_delete->IsStackable() && (!item_to_delete->IsStackable() &&
((item_to_delete->GetItem()->MaxCharges == 0) || item_to_delete->IsExpendable()))) { ((item_to_delete->GetItem()->MaxCharges == 0) || item_to_delete->IsExpendable()))) {
// Item can now be destroyed // Item can now be destroyed
safe_delete(item_to_delete); Inventory::MarkDirty(item_to_delete);
return true; return true;
} }
} }
@ -1126,8 +1126,7 @@ bool Inventory::DeleteItem(int16 slot_id, uint8 quantity)
return false; return false;
} }
safe_delete(item_to_delete); Inventory::MarkDirty(item_to_delete);
return true; return true;
} }
@ -1418,7 +1417,7 @@ int16 Inventory::_PutItem(int16 slot_id, ItemInst* inst)
if (result == SLOT_INVALID) { if (result == SLOT_INVALID) {
LogFile->write(EQEMuLog::Error, "Inventory::_PutItem: Invalid slot_id specified (%i)", slot_id); 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; return result;
@ -1933,3 +1932,18 @@ bool Item_Struct::IsEquipable(uint16 Race, uint16 Class_) const
} }
return (IsRace && IsClass); 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);
}
}

View File

@ -136,6 +136,9 @@ public:
~Inventory(); ~Inventory();
static void CleanDirty();
static void MarkDirty(ItemInst *inst);
// Retrieve a writeable item at specified slot // Retrieve a writeable item at specified slot
ItemInst* GetItem(int16 slot_id) const; ItemInst* GetItem(int16 slot_id) const;
ItemInst* GetItem(int16 slot_id, uint8 bagidx) const; ItemInst* GetItem(int16 slot_id, uint8 bagidx) const;

View File

@ -32,6 +32,7 @@ SET(zone_sources
horse.cpp horse.cpp
inventory.cpp inventory.cpp
loottables.cpp loottables.cpp
lua_bit.cpp
lua_corpse.cpp lua_corpse.cpp
lua_client.cpp lua_client.cpp
lua_door.cpp lua_door.cpp
@ -141,6 +142,7 @@ SET(zone_headers
guild_mgr.h guild_mgr.h
hate_list.h hate_list.h
horse.h horse.h
lua_bit.h
lua_client.h lua_client.h
lua_corpse.h lua_corpse.h
lua_entity.h lua_entity.h

View File

@ -2683,9 +2683,6 @@ bool Client::BindWound(Mob* bindmob, bool start, bool fail){
} }
// Send client bind done // 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 bind_out->type = 1; // Done
QueuePacket(outapp); QueuePacket(outapp);
bind_out->type = 0; bind_out->type = 0;

View File

@ -692,14 +692,9 @@ bool Client::Process() {
/************ Get all packets from packet manager out queue and process them ************/ /************ Get all packets from packet manager out queue and process them ************/
adverrorinfo = 5; adverrorinfo = 5;
EQApplicationPacket *app = 0; EQApplicationPacket *app = nullptr;
// if(eqs->GetState()==CLOSING && eqs->CheckActive()) if(!eqs->CheckState(CLOSING))
if(eqs->CheckState(CLOSING))
{ {
//eqs->Close();
//return false;
//handled below
} else {
while(ret && (app = (EQApplicationPacket *)eqs->PopPacket())) { while(ret && (app = (EQApplicationPacket *)eqs->PopPacket())) {
if(app) if(app)
ret = HandlePacket(app); ret = HandlePacket(app);

179
zone/lua_bit.cpp Normal file
View File

@ -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 <stdint.h>
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;
}

6
zone/lua_bit.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef _LUABIT_H
#define _LUABIT_H
int luaopen_bit(lua_State *L);
#endif

View File

@ -609,6 +609,11 @@ int Lua_Client::GetHorseId() {
return self->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) { void Lua_Client::NukeItem(uint32 item_num, int where_to_check) {
Lua_Safe_Call_Void(); Lua_Safe_Call_Void();
self->NukeItem(item_num, where_to_check); 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("AutoSplitEnabled", (bool(Lua_Client::*)(void))&Lua_Client::AutoSplitEnabled)
.def("SetHorseId", (void(Lua_Client::*)(int))&Lua_Client::SetHorseId) .def("SetHorseId", (void(Lua_Client::*)(int))&Lua_Client::SetHorseId)
.def("GetHorseId", (int(Lua_Client::*)(void))&Lua_Client::GetHorseId) .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("NukeItem", (void(Lua_Client::*)(uint32,int))&Lua_Client::NukeItem)
.def("SetTint", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetTint) .def("SetTint", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetTint)
.def("SetMaterial", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetMaterial) .def("SetMaterial", (void(Lua_Client::*)(int,uint32))&Lua_Client::SetMaterial)

View File

@ -147,6 +147,7 @@ public:
bool AutoSplitEnabled(); bool AutoSplitEnabled();
void SetHorseId(int id); void SetHorseId(int id);
int GetHorseId(); int GetHorseId();
void NukeItem(uint32 item_num);
void NukeItem(uint32 item_num, int where_to_check); void NukeItem(uint32 item_num, int where_to_check);
void SetTint(int slot_id, uint32 color); void SetTint(int slot_id, uint32 color);
void SetMaterial(int slot_id, uint32 item_id); void SetMaterial(int slot_id, uint32 item_id);

View File

@ -12,6 +12,7 @@
#include "masterentity.h" #include "masterentity.h"
#include "../common/spdat.h" #include "../common/spdat.h"
#include "lua_bit.h"
#include "lua_entity.h" #include "lua_entity.h"
#include "lua_item.h" #include "lua_item.h"
#include "lua_iteminst.h" #include "lua_iteminst.h"
@ -769,11 +770,18 @@ void LuaParser::ReloadQuests() {
L = luaL_newstate(); L = luaL_newstate();
luaL_openlibs(L); 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_pushnil(L);
lua_setglobal(L, "os"); lua_setglobal(L, "os");
lua_pushnil(L); lua_pushnil(L);
lua_setglobal(L, "io"); lua_setglobal(L, "io");
#endif
lua_getglobal(L, "package"); lua_getglobal(L, "package");
lua_getfield(L, -1, "path"); lua_getfield(L, -1, "path");

View File

@ -1286,6 +1286,8 @@ bool Zone::Process() {
if(spawn2_timer.Check()) { if(spawn2_timer.Check()) {
LinkedListIterator<Spawn2*> iterator(spawn2_list); LinkedListIterator<Spawn2*> iterator(spawn2_list);
Inventory::CleanDirty();
iterator.Reset(); iterator.Reset();
while (iterator.MoreElements()) { while (iterator.MoreElements()) {
if (iterator.GetData()->Process()) { if (iterator.GetData()->Process()) {