Lua packet handler dispatch

This commit is contained in:
KimLS 2013-07-13 13:53:41 -07:00
parent 485fc2b2b7
commit d472c05f5d
10 changed files with 70 additions and 32 deletions

View File

@ -29,6 +29,7 @@
#include <assert.h> #include <assert.h>
extern Zone* zone; extern Zone* zone;
extern void MapOpcodes();
QuestParserCollection::QuestParserCollection() { QuestParserCollection::QuestParserCollection() {
_player_quest_status = QuestUnloaded; _player_quest_status = QuestUnloaded;
@ -66,6 +67,7 @@ void QuestParserCollection::ReloadQuests(bool reset_timers) {
quest_manager.ClearAllTimers(); quest_manager.ClearAllTimers();
} }
MapOpcodes();
_npc_quest_status.clear(); _npc_quest_status.clear();
_player_quest_status = QuestUnloaded; _player_quest_status = QuestUnloaded;
_global_player_quest_status = QuestUnloaded; _global_player_quest_status = QuestUnloaded;

View File

@ -388,6 +388,17 @@ void MapOpcodes() {
ConnectedOpcodes[OP_OpenContainer] = &Client::Handle_OP_OpenContainer; ConnectedOpcodes[OP_OpenContainer] = &Client::Handle_OP_OpenContainer;
} }
void ClearMappedOpcode(EmuOpcode op) {
if(op >= _maxEmuOpcode)
return;
ConnectedOpcodes[op] = nullptr;
auto iter = ConnectingOpcodes.find(op);
if(iter != ConnectingOpcodes.end()) {
ConnectingOpcodes.erase(iter);
}
}
int Client::HandlePacket(const EQApplicationPacket *app) int Client::HandlePacket(const EQApplicationPacket *app)
{ {
_ZP(Client_HandlePacket); _ZP(Client_HandlePacket);
@ -420,10 +431,14 @@ int Client::HandlePacket(const EQApplicationPacket *app)
switch(client_state) { switch(client_state) {
case CLIENT_CONNECTING: { case CLIENT_CONNECTING: {
if(ConnectingOpcodes.count(opcode) != 1) { if(ConnectingOpcodes.count(opcode) != 1) {
//TODO: replace this 0 with the EQ opcode //Hate const cast but everything in lua needs to be non-const even if i make it non-mutable
LogFile->write(EQEMuLog::Error, "HandlePacket() Opcode error: Unexpected packet during CLIENT_CONNECTING: opcode: %s (#%d eq=0x%04x), size: %i", OpcodeNames[opcode], opcode, 0, app->size); std::vector<void*> args;
#if EQDEBUG >= 9 args.push_back(const_cast<EQApplicationPacket*>(app));
std::cout << "Unexpected packet during CLIENT_CONNECTING: OpCode: 0x" << hex << setw(4) << setfill('0') << opcode << dec << ", size: " << app->size << std::endl; parse->EventPlayer(EVENT_UNHANDLED_OPCODE, this, "", 1, &args);
#if EQDEBUG >= 10
LogFile->write(EQEMuLog::Error, "HandlePacket() Opcode error: Unexpected packet during CLIENT_CONNECTING: opcode:"
" %s (#%d eq=0x%04x), size: %i", OpcodeNames[opcode], opcode, 0, app->size);
DumpPacket(app); DumpPacket(app);
#endif #endif
break; break;
@ -446,10 +461,15 @@ int Client::HandlePacket(const EQApplicationPacket *app)
ClientPacketProc p; ClientPacketProc p;
p = ConnectedOpcodes[opcode]; p = ConnectedOpcodes[opcode];
if(p == nullptr) { if(p == nullptr) {
#if (EQDEBUG>=5) std::vector<void*> args;
args.push_back(const_cast<EQApplicationPacket*>(app));
parse->EventPlayer(EVENT_UNHANDLED_OPCODE, this, "", 0, &args);
#if (EQDEBUG >= 10)
char buffer[64]; char buffer[64];
app->build_header_dump(buffer); app->build_header_dump(buffer);
mlog(CLIENT__NET_ERR, "Unhandled incoming opcode: %s", buffer); mlog(CLIENT__NET_ERR, "Unhandled incoming opcode: %s", buffer);
if(app->size < 1000) if(app->size < 1000)
DumpPacket(app->pBuffer, app->size); DumpPacket(app->pBuffer, app->size);
else{ else{
@ -476,28 +496,6 @@ int Client::HandlePacket(const EQApplicationPacket *app)
return(true); return(true);
} }
/*void Client::Handle_Connect_OP_SetDataRate(const EQApplicationPacket *app)
{
// Set client datarate
//if (app->size != sizeof(float)) {
//LogFile->write(EQEMuLog::Error,"Wrong size on OP_SetDatarate. Got: %i, Expected: %i", app->size, sizeof(float));
//return;
//}
//LogFile->write(EQEMuLog::Debug, "HandlePacket() OP_SetDataRate request : %f", *(float*) app->pBuffer);
//float tmpDR = *(float*) app->pBuffer;
//if (tmpDR <= 0.0f) {
//LogFile->write(EQEMuLog::Error,"HandlePacket() OP_SetDataRate INVALID request : %f <= 0", tmpDR);
//LogFile->write(EQEMuLog::Normal,"WARNING: Setting datarate for client to 5.0 expect a client lock up =(");
//tmpDR = 5.0f;
//}
//if (tmpDR > 25.0f)
//tmpDR = 25.0f;
//eqs->SetDataRate(tmpDR);
return;
}*/
void Client::Handle_Connect_OP_ZoneEntry(const EQApplicationPacket *app) void Client::Handle_Connect_OP_ZoneEntry(const EQApplicationPacket *app)
{ {
if(app->size != sizeof(ClientZoneEntry_Struct)) if(app->size != sizeof(ClientZoneEntry_Struct))

View File

@ -111,7 +111,8 @@ const char *QuestEventSubroutines[_LargestEventID] = {
"EVENT_ENTER_AREA", "EVENT_ENTER_AREA",
"EVENT_LEAVE_AREA", "EVENT_LEAVE_AREA",
"EVENT_RESPAWN", "EVENT_RESPAWN",
"EVENT_DEATH_COMPLETE" "EVENT_DEATH_COMPLETE",
"EVENT_UNHANDLED_OPCODE"
}; };
PerlembParser::PerlembParser() : perl(nullptr) { PerlembParser::PerlembParser() : perl(nullptr) {

View File

@ -80,6 +80,7 @@ typedef enum {
EVENT_LEAVE_AREA, EVENT_LEAVE_AREA,
EVENT_RESPAWN, EVENT_RESPAWN,
EVENT_DEATH_COMPLETE, EVENT_DEATH_COMPLETE,
EVENT_UNHANDLED_OPCODE,
_LargestEventID _LargestEventID
} QuestEventID; } QuestEventID;

View File

@ -31,6 +31,8 @@ struct lua_registered_event {
}; };
extern std::map<std::string, std::list<lua_registered_event>> lua_encounter_events_registered; extern std::map<std::string, std::list<lua_registered_event>> lua_encounter_events_registered;
extern void MapOpcodes();
extern void ClearMappedOpcode(EmuOpcode op);
void load_encounter(std::string name) { void load_encounter(std::string name) {
parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, 0); parse->EventEncounter(EVENT_ENCOUNTER_LOAD, name, 0);
@ -1009,6 +1011,14 @@ Lua_ItemInst lua_get_quest_item() {
return quest_manager.GetQuestItem(); return quest_manager.GetQuestItem();
} }
void lua_map_opcodes() {
MapOpcodes();
}
void lua_clear_opcode(int op) {
ClearMappedOpcode(static_cast<EmuOpcode>(op));
}
luabind::scope lua_register_general() { luabind::scope lua_register_general() {
return luabind::namespace_("eq") return luabind::namespace_("eq")
[ [
@ -1168,7 +1178,9 @@ luabind::scope lua_register_general() {
luabind::def("stop_follow", &lua_stop_follow), luabind::def("stop_follow", &lua_stop_follow),
luabind::def("get_initiator", &lua_get_initiator), luabind::def("get_initiator", &lua_get_initiator),
luabind::def("get_owner", &lua_get_owner), luabind::def("get_owner", &lua_get_owner),
luabind::def("get_quest_item", &lua_get_quest_item) luabind::def("get_quest_item", &lua_get_quest_item),
luabind::def("map_opcodes", &lua_map_opcodes),
luabind::def("clear_opcode", &lua_clear_opcode)
]; ];
} }
@ -1247,7 +1259,8 @@ luabind::scope lua_register_events() {
luabind::value("augment_remove", static_cast<int>(EVENT_AUGMENT_REMOVE)), luabind::value("augment_remove", static_cast<int>(EVENT_AUGMENT_REMOVE)),
luabind::value("enter_area", static_cast<int>(EVENT_ENTER_AREA)), luabind::value("enter_area", static_cast<int>(EVENT_ENTER_AREA)),
luabind::value("leave_area", static_cast<int>(EVENT_LEAVE_AREA)), luabind::value("leave_area", static_cast<int>(EVENT_LEAVE_AREA)),
luabind::value("death_complete", static_cast<int>(EVENT_DEATH_COMPLETE)) luabind::value("death_complete", static_cast<int>(EVENT_DEATH_COMPLETE)),
luabind::value("unhandled_opcode", static_cast<int>(EVENT_UNHANDLED_OPCODE))
]; ];
} }

View File

@ -43,6 +43,11 @@ Lua_Packet::Lua_Packet(const Lua_Packet& o) {
} }
} }
int Lua_Packet::GetSize() {
Lua_Safe_Call_Int();
return static_cast<int>(self->size);
}
int Lua_Packet::GetOpcode() { int Lua_Packet::GetOpcode() {
Lua_Safe_Call_Int(); Lua_Safe_Call_Int();
return static_cast<int>(self->GetOpcode()); return static_cast<int>(self->GetOpcode());
@ -241,6 +246,7 @@ luabind::scope lua_register_packet() {
.def(luabind::constructor<int,int>()) .def(luabind::constructor<int,int>())
.property("null", &Lua_Packet::Null) .property("null", &Lua_Packet::Null)
.property("valid", &Lua_Packet::Valid) .property("valid", &Lua_Packet::Valid)
.def("GetSize", &Lua_Packet::GetSize)
.def("GetOpcode", &Lua_Packet::GetOpcode) .def("GetOpcode", &Lua_Packet::GetOpcode)
.def("SetOpcode", &Lua_Packet::SetOpcode) .def("SetOpcode", &Lua_Packet::SetOpcode)
.def("WriteInt8", &Lua_Packet::WriteInt8) .def("WriteInt8", &Lua_Packet::WriteInt8)

View File

@ -25,6 +25,7 @@ public:
Lua_Packet(const Lua_Packet& o); Lua_Packet(const Lua_Packet& o);
virtual ~Lua_Packet() { if(owned_) { EQApplicationPacket *ptr = GetLuaPtrData(); if(ptr) { delete ptr; } } } virtual ~Lua_Packet() { if(owned_) { EQApplicationPacket *ptr = GetLuaPtrData(); if(ptr) { delete ptr; } } }
int GetSize();
int GetOpcode(); int GetOpcode();
void SetOpcode(int op); void SetOpcode(int op);
void WriteInt8(int offset, int value); void WriteInt8(int offset, int value);

View File

@ -113,7 +113,8 @@ const char *LuaEvents[_LargestEventID] = {
"event_enter_area", "event_enter_area",
"event_leave_area", "event_leave_area",
"event_respawn", "event_respawn",
"event_death_complete" "event_death_complete",
"event_unhandled_opcode"
}; };
extern Zone *zone; extern Zone *zone;
@ -190,6 +191,7 @@ LuaParser::LuaParser() {
PlayerArgumentDispatch[EVENT_ENTER_AREA] = handle_player_area; PlayerArgumentDispatch[EVENT_ENTER_AREA] = handle_player_area;
PlayerArgumentDispatch[EVENT_LEAVE_AREA] = handle_player_area; PlayerArgumentDispatch[EVENT_LEAVE_AREA] = handle_player_area;
PlayerArgumentDispatch[EVENT_RESPAWN] = handle_player_respawn; PlayerArgumentDispatch[EVENT_RESPAWN] = handle_player_respawn;
PlayerArgumentDispatch[EVENT_UNHANDLED_OPCODE] = handle_player_packet;
ItemArgumentDispatch[EVENT_ITEM_CLICK] = handle_item_click; ItemArgumentDispatch[EVENT_ITEM_CLICK] = handle_item_click;
ItemArgumentDispatch[EVENT_ITEM_CLICK_CAST] = handle_item_click; ItemArgumentDispatch[EVENT_ITEM_CLICK_CAST] = handle_item_click;

View File

@ -21,6 +21,7 @@
#include "lua_corpse.h" #include "lua_corpse.h"
#include "lua_door.h" #include "lua_door.h"
#include "lua_object.h" #include "lua_object.h"
#include "lua_packet.h"
#include "zone.h" #include "zone.h"
#include "lua_parser_events.h" #include "lua_parser_events.h"
@ -472,6 +473,17 @@ void handle_player_respawn(QuestInterface *parse, lua_State* L, Client* client,
lua_setfield(L, -2, "resurrect"); lua_setfield(L, -2, "resurrect");
} }
void handle_player_packet(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data,
std::vector<void*> *extra_pointers) {
Lua_Packet l_packet(reinterpret_cast<EQApplicationPacket*>(extra_pointers->at(0)));
luabind::object l_packet_o = luabind::object(L, l_packet);
l_packet_o.push(L);
lua_setfield(L, -2, "packet");
lua_pushboolean(L, extra_data == 1 ? true : false);
lua_setfield(L, -2, "connecting");
}
void handle_player_null(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data, void handle_player_null(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data,
std::vector<void*> *extra_pointers) { std::vector<void*> *extra_pointers) {
} }

View File

@ -88,6 +88,8 @@ void handle_player_area(QuestInterface *parse, lua_State* L, Client* client, std
std::vector<void*> *extra_pointers); std::vector<void*> *extra_pointers);
void handle_player_respawn(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data, void handle_player_respawn(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data,
std::vector<void*> *extra_pointers); std::vector<void*> *extra_pointers);
void handle_player_packet(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data,
std::vector<void*> *extra_pointers);
void handle_player_null(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data, void handle_player_null(QuestInterface *parse, lua_State* L, Client* client, std::string data, uint32 extra_data,
std::vector<void*> *extra_pointers); std::vector<void*> *extra_pointers);