Added EQEmu::OutBuffer() - stringstream-derived class with a few additional methods

This commit is contained in:
Uleat
2016-05-18 22:38:41 -04:00
parent 67c92bf171
commit ffb88e0a8f
10 changed files with 631 additions and 638 deletions
+61 -76
View File
@@ -22,7 +22,7 @@ namespace SoF
static OpcodeManager *opcodes = nullptr;
static Strategy struct_strategy;
void SerializeItem(std::stringstream& ss, const ItemInst *inst, int16 slot_id, uint8 depth);
void SerializeItem(EQEmu::OutBuffer& ob, const ItemInst *inst, int16 slot_id, uint8 depth);
// server to client inventory location converters
static inline uint32 ServerToSoFSlot(uint32 serverSlot);
@@ -343,25 +343,22 @@ namespace SoF
InternalSerializedItem_Struct* eq = (InternalSerializedItem_Struct*)in->pBuffer;
std::stringstream ss(std::stringstream::in | std::stringstream::out);
std::stringstream::pos_type last_pos = ss.tellp();
EQEmu::OutBuffer ob;
EQEmu::OutBuffer::pos_type last_pos = ob.tellp();
ss.write((const char*)&item_count, sizeof(uint32));
ob.write((const char*)&item_count, sizeof(uint32));
for (int index = 0; index < item_count; ++index, ++eq) {
SerializeItem(ss, (const ItemInst*)eq->inst, eq->slot_id, 0);
if (ss.tellp() == last_pos)
SerializeItem(ob, (const ItemInst*)eq->inst, eq->slot_id, 0);
if (ob.tellp() == last_pos)
Log.Out(Logs::General, Logs::Netcode, "[STRUCTS] Serialization failed on item slot %d during OP_CharInventory. Item skipped.", eq->slot_id);
last_pos = ss.tellp();
last_pos = ob.tellp();
}
std::string serialized = ss.str();
in->size = serialized.size();
in->pBuffer = new uchar[in->size];
memcpy(in->pBuffer, serialized.c_str(), serialized.size());
in->size = ob.size();
in->pBuffer = ob.detach();
delete[] __emu_buffer;
dest->FastQueuePacket(&in, ack_req);
@@ -823,30 +820,24 @@ namespace SoF
//store away the emu struct
uchar* __emu_buffer = in->pBuffer;
//ItemPacket_Struct* old_item_pkt = (ItemPacket_Struct*)__emu_buffer;
//InternalSerializedItem_Struct* int_struct = (InternalSerializedItem_Struct*)(old_item_pkt->SerializedItem);
InternalSerializedItem_Struct* int_struct = (InternalSerializedItem_Struct*)(&__emu_buffer[4]);
std::stringstream ss(std::stringstream::in | std::stringstream::out);
std::stringstream::pos_type last_pos = ss.tellp();
EQEmu::OutBuffer ob;
EQEmu::OutBuffer::pos_type last_pos = ob.tellp();
ss.write((const char*)__emu_buffer, 4);
ob.write((const char*)__emu_buffer, 4);
SerializeItem(ss, (const ItemInst*)int_struct->inst, int_struct->slot_id, 0);
if (ss.tellp() == last_pos) {
SerializeItem(ob, (const ItemInst*)int_struct->inst, int_struct->slot_id, 0);
if (ob.tellp() == last_pos) {
Log.Out(Logs::General, Logs::Netcode, "[STRUCTS] Serialization failed on item slot %d.", int_struct->slot_id);
delete in;
return;
}
std::string serialized = ss.str();
in->size = serialized.size();
in->pBuffer = new uchar[in->size];
//ItemPacket_Struct* new_item_pkt = (ItemPacket_Struct*)in->pBuffer;
//new_item_pkt->PacketType = old_item_pkt->PacketType;
memcpy(in->pBuffer, serialized.c_str(), serialized.size());
in->size = ob.size();
in->pBuffer = ob.detach();
delete[] __emu_buffer;
dest->FastQueuePacket(&in, ack_req);
@@ -2865,7 +2856,7 @@ namespace SoF
return NextItemInstSerialNumber;
}
void SerializeItem(std::stringstream& ss, const ItemInst *inst, int16 slot_id_in, uint8 depth)
void SerializeItem(EQEmu::OutBuffer& ob, const ItemInst *inst, int16 slot_id_in, uint8 depth)
{
const Item_Struct *item = inst->GetUnscaledItem();
@@ -2893,19 +2884,19 @@ namespace SoF
hdr.unknown061 = 0;
hdr.ItemClass = item->ItemClass;
ss.write((const char*)&hdr, sizeof(SoF::structs::ItemSerializationHeader));
ob.write((const char*)&hdr, sizeof(SoF::structs::ItemSerializationHeader));
if (strlen(item->Name) > 0)
ss.write(item->Name, strlen(item->Name));
ss.write("\0", 1);
ob.write(item->Name, strlen(item->Name));
ob.write("\0", 1);
if (strlen(item->Lore) > 0)
ss.write(item->Lore, strlen(item->Lore));
ss.write("\0", 1);
ob.write(item->Lore, strlen(item->Lore));
ob.write("\0", 1);
if (strlen(item->IDFile) > 0)
ss.write(item->IDFile, strlen(item->IDFile));
ss.write("\0", 1);
ob.write(item->IDFile, strlen(item->IDFile));
ob.write("\0", 1);
SoF::structs::ItemBodyStruct ibs;
memset(&ibs, 0, sizeof(SoF::structs::ItemBodyStruct));
@@ -2994,12 +2985,12 @@ namespace SoF
ibs.FactionAmt4 = item->FactionAmt4;
ibs.FactionMod4 = item->FactionMod4;
ss.write((const char*)&ibs, sizeof(SoF::structs::ItemBodyStruct));
ob.write((const char*)&ibs, sizeof(SoF::structs::ItemBodyStruct));
//charm text
if (strlen(item->CharmFile) > 0)
ss.write((const char*)item->CharmFile, strlen(item->CharmFile));
ss.write("\0", 1);
ob.write((const char*)item->CharmFile, strlen(item->CharmFile));
ob.write("\0", 1);
SoF::structs::ItemSecondaryBodyStruct isbs;
memset(&isbs, 0, sizeof(SoF::structs::ItemSecondaryBodyStruct));
@@ -3027,11 +3018,11 @@ namespace SoF
isbs.book = item->Book;
isbs.booktype = item->BookType;
ss.write((const char*)&isbs, sizeof(SoF::structs::ItemSecondaryBodyStruct));
ob.write((const char*)&isbs, sizeof(SoF::structs::ItemSecondaryBodyStruct));
if (strlen(item->Filename) > 0)
ss.write((const char*)item->Filename, strlen(item->Filename));
ss.write("\0", 1);
ob.write((const char*)item->Filename, strlen(item->Filename));
ob.write("\0", 1);
SoF::structs::ItemTertiaryBodyStruct itbs;
memset(&itbs, 0, sizeof(SoF::structs::ItemTertiaryBodyStruct));
@@ -3055,7 +3046,7 @@ namespace SoF
itbs.no_transfer = item->NoTransfer;
itbs.expendablearrow = item->ExpendableArrow;
ss.write((const char*)&itbs, sizeof(SoF::structs::ItemTertiaryBodyStruct));
ob.write((const char*)&itbs, sizeof(SoF::structs::ItemTertiaryBodyStruct));
// Effect Structures Broken down to allow variable length strings for effect names
int32 effect_unknown = 0;
@@ -3072,13 +3063,13 @@ namespace SoF
ices.recast = item->RecastDelay;
ices.recast_type = item->RecastType;
ss.write((const char*)&ices, sizeof(SoF::structs::ClickEffectStruct));
ob.write((const char*)&ices, sizeof(SoF::structs::ClickEffectStruct));
if (strlen(item->ClickName) > 0)
ss.write((const char*)item->ClickName, strlen(item->ClickName));
ss.write("\0", 1);
ob.write((const char*)item->ClickName, strlen(item->ClickName));
ob.write("\0", 1);
ss.write((const char*)&effect_unknown, sizeof(int32)); // clickunk7
ob.write((const char*)&effect_unknown, sizeof(int32)); // clickunk7
SoF::structs::ProcEffectStruct ipes;
memset(&ipes, 0, sizeof(SoF::structs::ProcEffectStruct));
@@ -3089,13 +3080,13 @@ namespace SoF
ipes.level = item->Proc.Level;
ipes.procrate = item->ProcRate;
ss.write((const char*)&ipes, sizeof(SoF::structs::ProcEffectStruct));
ob.write((const char*)&ipes, sizeof(SoF::structs::ProcEffectStruct));
if (strlen(item->ProcName) > 0)
ss.write((const char*)item->ProcName, strlen(item->ProcName));
ss.write("\0", 1);
ob.write((const char*)item->ProcName, strlen(item->ProcName));
ob.write("\0", 1);
ss.write((const char*)&effect_unknown, sizeof(int32)); // unknown5
ob.write((const char*)&effect_unknown, sizeof(int32)); // unknown5
SoF::structs::WornEffectStruct iwes;
memset(&iwes, 0, sizeof(SoF::structs::WornEffectStruct));
@@ -3105,13 +3096,13 @@ namespace SoF
iwes.type = item->Worn.Type;
iwes.level = item->Worn.Level;
ss.write((const char*)&iwes, sizeof(SoF::structs::WornEffectStruct));
ob.write((const char*)&iwes, sizeof(SoF::structs::WornEffectStruct));
if (strlen(item->WornName) > 0)
ss.write((const char*)item->WornName, strlen(item->WornName));
ss.write("\0", 1);
ob.write((const char*)item->WornName, strlen(item->WornName));
ob.write("\0", 1);
ss.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
ob.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
SoF::structs::WornEffectStruct ifes;
memset(&ifes, 0, sizeof(SoF::structs::WornEffectStruct));
@@ -3121,13 +3112,13 @@ namespace SoF
ifes.type = item->Focus.Type;
ifes.level = item->Focus.Level;
ss.write((const char*)&ifes, sizeof(SoF::structs::WornEffectStruct));
ob.write((const char*)&ifes, sizeof(SoF::structs::WornEffectStruct));
if (strlen(item->FocusName) > 0)
ss.write((const char*)item->FocusName, strlen(item->FocusName));
ss.write("\0", 1);
ob.write((const char*)item->FocusName, strlen(item->FocusName));
ob.write("\0", 1);
ss.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
ob.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
SoF::structs::WornEffectStruct ises;
memset(&ises, 0, sizeof(SoF::structs::WornEffectStruct));
@@ -3137,13 +3128,13 @@ namespace SoF
ises.type = item->Scroll.Type;
ises.level = item->Scroll.Level;
ss.write((const char*)&ises, sizeof(SoF::structs::WornEffectStruct));
ob.write((const char*)&ises, sizeof(SoF::structs::WornEffectStruct));
if (strlen(item->ScrollName) > 0)
ss.write((const char*)item->ScrollName, strlen(item->ScrollName));
ss.write("\0", 1);
ob.write((const char*)item->ScrollName, strlen(item->ScrollName));
ob.write("\0", 1);
ss.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
ob.write((const char*)&effect_unknown, sizeof(int32)); // unknown6
// End of Effects
SoF::structs::ItemQuaternaryBodyStruct iqbs;
@@ -3172,12 +3163,12 @@ namespace SoF
iqbs.HealAmt = item->HealAmt;
iqbs.SpellDmg = item->SpellDmg;
ss.write((const char*)&iqbs, sizeof(SoF::structs::ItemQuaternaryBodyStruct));
ob.write((const char*)&iqbs, sizeof(SoF::structs::ItemQuaternaryBodyStruct));
std::stringstream::pos_type count_pos = ss.tellp();
std::stringstream::pos_type count_pos = ob.tellp();
uint32 subitem_count = 0;
ss.write((const char*)&subitem_count, sizeof(uint32));
ob.write((const char*)&subitem_count, sizeof(uint32));
for (uint32 index = SUB_INDEX_BEGIN; index < EQEmu::legacy::ITEM_CONTAINER_SIZE; ++index) {
ItemInst* sub = inst->GetItem(index);
@@ -3194,20 +3185,14 @@ namespace SoF
else
SubSlotNumber = slot_id_in;
ss.write((const char*)&index, sizeof(uint32));
ob.write((const char*)&index, sizeof(uint32));
SerializeItem(ss, sub, SubSlotNumber, (depth + 1));
SerializeItem(ob, sub, SubSlotNumber, (depth + 1));
++subitem_count;
}
if (subitem_count) {
std::stringstream::pos_type cur_pos = ss.tellp();
ss.seekp(count_pos);
ss.write((const char*)&subitem_count, sizeof(uint32));
ss.seekp(cur_pos);
}
if (subitem_count)
ob.overwrite(count_pos, (const char*)&subitem_count, sizeof(uint32));
}
static inline uint32 ServerToSoFSlot(uint32 serverSlot)