PacketStruct.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <iostream>
  17. #include <exception>
  18. #include "PacketStruct.h"
  19. #include "ConfigReader.h"
  20. #include "../common/debug.h"
  21. #include "MiscFunctions.h"
  22. #include "Log.h"
  23. #ifdef WORLD
  24. #include "../WorldServer/Items/Items.h"
  25. #include "../WorldServer/Player.h"
  26. #endif
  27. extern ConfigReader configReader;
  28. using namespace std;
  29. DataStruct::DataStruct(){
  30. item_size = 0;
  31. type = 0;
  32. type2 = 0;
  33. length = 1;
  34. if_set = false;
  35. if_not_set = false;
  36. if_not_equals = false;
  37. if_equals = false;
  38. is_set = false;
  39. oversized = 0;
  40. oversized_byte = 0;
  41. add = false;
  42. addType = 0;
  43. maxArraySize = 0;
  44. default_value = 0;
  45. }
  46. DataStruct::DataStruct(DataStruct* data_struct){
  47. type = data_struct->GetType();
  48. type2 = data_struct->GetType2();
  49. length = data_struct->GetLength();
  50. name = data_struct->GetName();
  51. array_size_variable = data_struct->array_size_variable;
  52. default_value = data_struct->default_value;
  53. add = true;
  54. addType = type;
  55. oversized = data_struct->GetOversized();
  56. oversized_byte = data_struct->GetOversizedByte();
  57. maxArraySize = data_struct->GetMaxArraySize();
  58. if_set = false;
  59. if_not_set = false;
  60. if_not_equals = false;
  61. if_equals = false;
  62. if(data_struct->GetIfSet())
  63. SetIfSetVariable(data_struct->GetIfSetVariable());
  64. if(data_struct->GetIfNotSet())
  65. SetIfNotSetVariable(data_struct->GetIfNotSetVariable());
  66. if (data_struct->GetIfNotEquals())
  67. SetIfNotEqualsVariable(data_struct->GetIfNotEqualsVariable());
  68. if (data_struct->GetIfEquals())
  69. SetIfEqualsVariable(data_struct->GetIfEqualsVariable());
  70. is_set = false;
  71. item_size = 0;
  72. }
  73. DataStruct::DataStruct(const char* new_name, const char* new_type, int32 new_length, const char* new_type2){
  74. name = string(new_name);
  75. type = 0;
  76. type2 = 0;
  77. SetType(new_type, &type);
  78. if(new_type2)
  79. SetType(new_type2, &type2);
  80. length = new_length;
  81. add = true;
  82. addType = type;
  83. if_set = false;
  84. if_not_set = false;
  85. is_set = false;
  86. if_not_equals = false;
  87. item_size = 0;
  88. }
  89. const char* DataStruct::GetArraySizeVariable(){
  90. return array_size_variable.c_str();
  91. }
  92. void DataStruct::SetArraySizeVariable(const char* new_name){
  93. array_size_variable = string(new_name);
  94. }
  95. void DataStruct::SetType(const char* new_type, int8* output_type){
  96. if(strlen(new_type) > 3 && strncasecmp("int", new_type, 3) == 0){
  97. if(strncasecmp("int8", new_type, 4) == 0)
  98. *output_type = DATA_STRUCT_INT8;
  99. else if(strncasecmp("int16", new_type, 5) == 0)
  100. *output_type = DATA_STRUCT_INT16;
  101. else if(strncasecmp("int32", new_type, 5) == 0)
  102. *output_type = DATA_STRUCT_INT32;
  103. else if(strncasecmp("int64", new_type, 5) == 0)
  104. *output_type = DATA_STRUCT_INT64;
  105. }
  106. else if(strlen(new_type) > 4 && strncasecmp("sint", new_type, 4) == 0){
  107. if(strncasecmp("sint8", new_type, 5) == 0)
  108. *output_type = DATA_STRUCT_SINT8;
  109. else if(strncasecmp("sint16", new_type, 6) == 0)
  110. *output_type = DATA_STRUCT_SINT16;
  111. else if(strncasecmp("sint32", new_type, 6) == 0)
  112. *output_type = DATA_STRUCT_SINT32;
  113. else if(strncasecmp("sint64", new_type, 6) == 0)
  114. *output_type = DATA_STRUCT_SINT64;
  115. }
  116. else if(strlen(new_type) == 4 && strncasecmp("char", new_type, 4) == 0)
  117. *output_type = DATA_STRUCT_CHAR;
  118. else if(strlen(new_type) == 5 && strncasecmp("float", new_type, 5) == 0)
  119. *output_type = DATA_STRUCT_FLOAT;
  120. else if(strlen(new_type) == 6 && strncasecmp("double", new_type, 6) == 0)
  121. *output_type = DATA_STRUCT_DOUBLE;
  122. else if(strlen(new_type) >= 5 && strncasecmp("EQ2_", new_type, 4) == 0){
  123. if(strncasecmp("EQ2_8", new_type, 5) == 0)
  124. *output_type = DATA_STRUCT_EQ2_8BIT_STRING;
  125. else if(strncasecmp("EQ2_16", new_type, 6) == 0)
  126. *output_type = DATA_STRUCT_EQ2_16BIT_STRING;
  127. else if(strncasecmp("EQ2_32", new_type, 6) == 0)
  128. *output_type = DATA_STRUCT_EQ2_32BIT_STRING;
  129. else if(strncasecmp("EQ2_E", new_type, 5) == 0)
  130. *output_type = DATA_STRUCT_EQUIPMENT;
  131. else if(strncasecmp("EQ2_C", new_type, 5) == 0)
  132. *output_type = DATA_STRUCT_COLOR;
  133. else if(strncasecmp("EQ2_I", new_type, 5) == 0)
  134. *output_type = DATA_STRUCT_ITEM;
  135. }
  136. else if(strlen(new_type) >= 5){
  137. if(strncasecmp("Array", new_type, 5) == 0)
  138. *output_type = DATA_STRUCT_ARRAY;
  139. }
  140. else
  141. LogWrite(PACKET__ERROR, 0, "Packet", "Invalid Type: %s", new_type);
  142. }
  143. DataStruct::DataStruct(const char* new_name, int32 new_length){
  144. name = string(new_name);
  145. length = new_length;
  146. if_set = false;
  147. if_not_set = false;
  148. is_set = false;
  149. item_size = 0;
  150. }
  151. DataStruct::DataStruct(const char* new_name, int8 new_type, int32 new_length, int8 new_type2){
  152. name = string(new_name);
  153. type = new_type;
  154. length = new_length;
  155. type2 = new_type2;
  156. addType = type;
  157. if_set = false;
  158. if_not_set = false;
  159. is_set = false;
  160. item_size = 0;
  161. }
  162. void DataStruct::SetType(int8 new_type){
  163. type = new_type;
  164. addType = type;
  165. }
  166. void DataStruct::SetMaxArraySize(int8 size){
  167. maxArraySize = size;
  168. }
  169. void DataStruct::SetOversized(int8 val){
  170. oversized = val;
  171. }
  172. void DataStruct::SetDefaultValue(int8 new_val){
  173. default_value = new_val;
  174. }
  175. void DataStruct::SetName(const char* new_name){
  176. name = string(new_name);
  177. }
  178. void DataStruct::SetLength(int32 new_length){
  179. length = new_length;
  180. }
  181. void DataStruct::SetOversizedByte(int8 val){
  182. oversized_byte = val;
  183. }
  184. void DataStruct::SetItemSize(int32 val){
  185. item_size = val;
  186. }
  187. void DataStruct::SetIfEqualsVariable(const char* variable){
  188. if(variable){
  189. if_equals = true;
  190. if_equals_variable = string(variable);
  191. }
  192. else
  193. if_equals = false;
  194. }
  195. void DataStruct::SetIfNotEqualsVariable(const char* variable){
  196. if(variable){
  197. if_not_equals = true;
  198. if_not_equals_variable = string(variable);
  199. }
  200. else
  201. if_not_equals = false;
  202. }
  203. void DataStruct::SetIfSetVariable(const char* variable){
  204. if(variable){
  205. if_set = true;
  206. if_set_variable = string(variable);
  207. }
  208. else
  209. if_set = false;
  210. }
  211. void DataStruct::SetIfNotSetVariable(const char* variable){
  212. if(variable){
  213. if_not_set = true;
  214. if_not_set_variable = string(variable);
  215. }
  216. else
  217. if_not_set = false;
  218. }
  219. void DataStruct::SetIsSet(bool val){
  220. is_set = val;
  221. }
  222. bool DataStruct::IsSet(){
  223. return is_set;
  224. }
  225. int32 DataStruct::GetItemSize(){
  226. return item_size;
  227. }
  228. bool DataStruct::GetIfSet(){
  229. return if_set;
  230. }
  231. const char* DataStruct::GetIfSetVariable(){
  232. if(if_set_variable.length() > 0)
  233. return if_set_variable.c_str();
  234. return 0;
  235. }
  236. bool DataStruct::GetIfNotSet(){
  237. return if_not_set;
  238. }
  239. const char* DataStruct::GetIfNotSetVariable(){
  240. if(if_not_set_variable.length() > 0)
  241. return if_not_set_variable.c_str();
  242. return 0;
  243. }
  244. bool DataStruct::GetIfEquals(){
  245. return if_equals;
  246. }
  247. const char* DataStruct::GetIfEqualsVariable(){
  248. if(if_equals_variable.length() > 0)
  249. return if_equals_variable.c_str();
  250. return 0;
  251. }
  252. bool DataStruct::GetIfNotEquals(){
  253. return if_not_equals;
  254. }
  255. const char* DataStruct::GetIfNotEqualsVariable(){
  256. if(if_not_equals_variable.length() > 0)
  257. return if_not_equals_variable.c_str();
  258. return 0;
  259. }
  260. int8 DataStruct::GetDefaultValue(){
  261. return default_value;
  262. }
  263. int8 DataStruct::GetType(){
  264. return type;
  265. }
  266. int8 DataStruct::GetType2(){
  267. return type2;
  268. }
  269. const char* DataStruct::GetName(){
  270. return name.c_str();
  271. }
  272. int8 DataStruct::GetOversized(){
  273. return oversized;
  274. }
  275. int8 DataStruct::GetOversizedByte(){
  276. return oversized_byte;
  277. }
  278. int8 DataStruct::GetMaxArraySize(){
  279. return maxArraySize;
  280. }
  281. int32 DataStruct::GetLength(){
  282. return length;
  283. }
  284. string DataStruct::GetStringName(){
  285. return name;
  286. }
  287. bool DataStruct::AddToStruct(){
  288. return add;
  289. }
  290. void DataStruct::SetAddToStruct(bool val){
  291. add = val;
  292. }
  293. int8 DataStruct::GetAddType(){
  294. return addType;
  295. }
  296. void DataStruct::SetAddType(int8 new_type){
  297. addType = new_type;
  298. }
  299. int32 DataStruct::GetDataSizeInBytes(){
  300. int32 ret = 0;
  301. switch (type){
  302. case DATA_STRUCT_INT8:
  303. ret = length * sizeof(int8);
  304. break;
  305. case DATA_STRUCT_INT16:
  306. ret = length * sizeof(int16);
  307. break;
  308. case DATA_STRUCT_INT32:
  309. ret = length * sizeof(int32);
  310. break;
  311. case DATA_STRUCT_INT64:
  312. ret = length * sizeof(int64);
  313. break;
  314. case DATA_STRUCT_SINT8:
  315. ret = length * sizeof(sint8);
  316. break;
  317. case DATA_STRUCT_SINT16:
  318. ret = length * sizeof(sint16);
  319. break;
  320. case DATA_STRUCT_SINT32:
  321. ret = length * sizeof(sint32);
  322. break;
  323. case DATA_STRUCT_SINT64:
  324. ret = length * sizeof(sint64);
  325. break;
  326. case DATA_STRUCT_FLOAT:
  327. ret = length * sizeof(float);
  328. break;
  329. case DATA_STRUCT_DOUBLE:
  330. ret = length * sizeof(double);
  331. break;
  332. case DATA_STRUCT_ARRAY:
  333. // Array elements won't have a size so get out now to avoid the warning.
  334. break;
  335. default:
  336. LogWrite(PACKET__WARNING, 0, "DataStruct", "Tried retrieving a data size from an unsupported data struct type in GetDataSizeInBytes()");
  337. break;
  338. }
  339. return ret;
  340. }
  341. PacketStruct::PacketStruct(PacketStruct* packet, int16 in_client_version){
  342. parent = packet->parent;
  343. client_version = in_client_version;
  344. vector<DataStruct*>::iterator itr2;
  345. name = packet->name;
  346. for(itr2=packet->structs.begin();itr2!=packet->structs.end();itr2++) {
  347. add(new DataStruct(*itr2));
  348. }
  349. sub_packet = false;
  350. opcode = packet->opcode;
  351. version = packet->version;
  352. opcode_type = packet->opcode_type;
  353. sub_packet_size = 1;
  354. addPacketArrays(packet);
  355. }
  356. PacketStruct::PacketStruct(){
  357. parent = 0;
  358. opcode = OP_Unknown;
  359. }
  360. PacketStruct::PacketStruct(PacketStruct* packet, bool sub){
  361. vector<DataStruct*>::iterator itr2;
  362. for(itr2=packet->structs.begin();itr2!=packet->structs.end();itr2++) {
  363. add(new DataStruct(*itr2));
  364. }
  365. sub_packet = sub;
  366. opcode = packet->opcode;
  367. version = packet->version;
  368. opcode_type = packet->opcode_type;
  369. name = packet->name;
  370. sub_packet_size = 0;
  371. parent = 0;
  372. }
  373. PacketStruct::~PacketStruct(){
  374. deleteDataStructs(&structs);
  375. deleteDataStructs(&orig_structs);
  376. deletePacketArrays(this);
  377. struct_map.clear();
  378. struct_data.clear();
  379. }
  380. void PacketStruct::deleteDataStructs(vector<DataStruct*>* data_structs){
  381. DataStruct* data = 0;
  382. vector<DataStruct*>::iterator itr;
  383. for(itr=data_structs->begin();itr!=data_structs->end();itr++) {
  384. data = *itr;
  385. void* ptr = GetStructPointer(data);
  386. switch(data->GetType()){
  387. case DATA_STRUCT_EQ2_8BIT_STRING:{
  388. EQ2_8BitString* real_ptr = (EQ2_8BitString*)ptr;
  389. safe_delete(real_ptr);
  390. break;
  391. }
  392. case DATA_STRUCT_EQ2_16BIT_STRING:{
  393. EQ2_16BitString* real_ptr = (EQ2_16BitString*)ptr;
  394. safe_delete(real_ptr);
  395. break;
  396. }
  397. case DATA_STRUCT_EQ2_32BIT_STRING:{
  398. EQ2_32BitString* real_ptr = (EQ2_32BitString*)ptr;
  399. safe_delete(real_ptr);
  400. break;
  401. }
  402. case DATA_STRUCT_EQUIPMENT:{
  403. EQ2_EquipmentItem* real_ptr = (EQ2_EquipmentItem*)ptr;
  404. safe_delete_array(real_ptr);
  405. break;
  406. }
  407. case DATA_STRUCT_DOUBLE:{
  408. double* real_ptr = (double*)ptr;
  409. safe_delete_array(real_ptr);
  410. break;
  411. }
  412. case DATA_STRUCT_FLOAT:{
  413. float* real_ptr = (float*)ptr;
  414. safe_delete_array(real_ptr);
  415. break;
  416. }
  417. case DATA_STRUCT_INT8:{
  418. int8* real_ptr = (int8*)ptr;
  419. safe_delete_array(real_ptr);
  420. break;
  421. }
  422. case DATA_STRUCT_INT16:{
  423. int16* real_ptr = (int16*)ptr;
  424. safe_delete_array(real_ptr);
  425. break;
  426. }
  427. case DATA_STRUCT_INT32:{
  428. int32* real_ptr = (int32*)ptr;
  429. safe_delete_array(real_ptr);
  430. break;
  431. }
  432. case DATA_STRUCT_INT64:{
  433. int64* real_ptr = (int64*)ptr;
  434. safe_delete_array(real_ptr);
  435. break;
  436. }
  437. case DATA_STRUCT_SINT8:{
  438. sint8* real_ptr = (sint8*)ptr;
  439. safe_delete_array(real_ptr);
  440. break;
  441. }
  442. case DATA_STRUCT_SINT16:{
  443. sint16* real_ptr = (sint16*)ptr;
  444. safe_delete_array(real_ptr);
  445. break;
  446. }
  447. case DATA_STRUCT_SINT32:{
  448. sint32* real_ptr = (sint32*)ptr;
  449. safe_delete_array(real_ptr);
  450. break;
  451. }
  452. case DATA_STRUCT_SINT64:{
  453. sint64* real_ptr = (sint64*)ptr;
  454. safe_delete_array(real_ptr);
  455. break;
  456. }
  457. case DATA_STRUCT_ITEM:{
  458. uchar* real_ptr = (uchar*)ptr;
  459. safe_delete_array(real_ptr);
  460. break;
  461. }
  462. case DATA_STRUCT_CHAR:{
  463. char* real_ptr = (char*)ptr;
  464. safe_delete_array(real_ptr);
  465. break;
  466. }
  467. case DATA_STRUCT_COLOR:{
  468. EQ2_Color* real_ptr = (EQ2_Color*)ptr;
  469. safe_delete_array(real_ptr);
  470. break;
  471. }
  472. }
  473. ptr = 0;
  474. safe_delete(data);
  475. }
  476. }
  477. void PacketStruct::deletePacketArrays(PacketStruct* packet){
  478. if(!packet)
  479. return;
  480. vector<PacketStruct*>::iterator itr;
  481. for(itr=packet->arrays.begin();itr!=packet->arrays.end();itr++)
  482. safe_delete(*itr);
  483. packet->arrays.clear();
  484. for (itr = packet->orig_packets.begin(); itr != packet->orig_packets.end(); itr++)
  485. safe_delete(*itr);
  486. packet->orig_packets.clear();
  487. }
  488. void PacketStruct::renameSubstructArray(const char* substruct, int32 index){
  489. vector<PacketStruct*>::iterator itr;
  490. char tmp[10] = {0};
  491. sprintf(tmp, "%i", index);
  492. for(itr=arrays.begin();itr!=arrays.end();itr++) {
  493. (*itr)->SetName(string(substruct).append("_").append((*itr)->GetName()).append("_").append(tmp).c_str());
  494. }
  495. }
  496. void PacketStruct::addPacketArrays(PacketStruct* packet){
  497. if(!packet)
  498. return;
  499. vector<PacketStruct*>::iterator itr;
  500. for(itr=packet->arrays.begin();itr!=packet->arrays.end();itr++) {
  501. PacketStruct* tmp = new PacketStruct(*itr, true);
  502. tmp->addPacketArrays(*itr);
  503. add(tmp);
  504. }
  505. }
  506. bool PacketStruct::IsStringValueType(string in_name, int32 index){
  507. DataStruct* data = findStruct(in_name.c_str(), index);
  508. switch(data->GetType()){
  509. case DATA_STRUCT_CHAR:
  510. case DATA_STRUCT_EQ2_8BIT_STRING:
  511. case DATA_STRUCT_EQ2_16BIT_STRING:
  512. case DATA_STRUCT_EQ2_32BIT_STRING:
  513. return true;
  514. }
  515. return false;
  516. }
  517. bool PacketStruct::IsColorValueType(string in_name, int32 index){
  518. DataStruct* data = findStruct(in_name.c_str(), index);
  519. if(data->GetType() == DATA_STRUCT_COLOR)
  520. return true;
  521. else
  522. return false;
  523. }
  524. void PacketStruct::setColor(DataStruct* data, int8 red, int8 green, int8 blue, int32 index = 0){
  525. if(data && data->GetType() == DATA_STRUCT_COLOR){
  526. EQ2_Color* color = (EQ2_Color*)GetStructPointer(data);
  527. color[index].blue = blue;
  528. color[index].red = red;
  529. color[index].green = green;
  530. if(blue >0 || green > 0 || red > 0)
  531. data->SetIsSet(true);
  532. }
  533. }
  534. void PacketStruct::setEquipment(DataStruct* data, int16 type, int8 c_red, int8 c_blue, int8 c_green, int8 h_red, int8 h_blue, int8 h_green, int32 index){
  535. if(data){
  536. EQ2_EquipmentItem* equipment = (EQ2_EquipmentItem*)GetStructPointer(data);
  537. EQ2_Color* color = (EQ2_Color*)&equipment[index].color;
  538. EQ2_Color* highlight = (EQ2_Color*)&equipment[index].highlight;
  539. equipment[index].type = type;
  540. color->blue = c_blue;
  541. color->red = c_red;
  542. color->green = c_green;
  543. highlight->blue = h_blue;
  544. highlight->red = h_red;
  545. highlight->green = h_green;
  546. if(c_red > 0 || c_blue > 0 || c_green > 0 || h_red > 0 || h_blue > 0 || h_green > 0)
  547. data->SetIsSet(true);
  548. }
  549. }
  550. void PacketStruct::add(PacketStruct* packet_struct){
  551. packet_struct->parent = this;
  552. arrays.push_back(packet_struct);
  553. }
  554. const char* PacketStruct::GetOpcodeType(){
  555. return opcode_type.c_str();
  556. }
  557. void PacketStruct::SetOpcodeType(const char* in_type){
  558. if(in_type)
  559. opcode_type = string(in_type);
  560. else
  561. opcode_type = string("");
  562. }
  563. void PacketStruct::setDataType(DataStruct* data_struct, sint8 data, int32 index){
  564. if(data_struct){
  565. sint8* ptr = (sint8*)GetStructPointer(data_struct);
  566. ptr[index] = data;
  567. if(data != 0 && data != -1)
  568. data_struct->SetIsSet(true);
  569. }
  570. }
  571. void PacketStruct::setDataType(DataStruct* data_struct, sint16 data, int32 index){
  572. if(data_struct){
  573. sint16* ptr = (sint16*)GetStructPointer(data_struct);
  574. ptr[index] = data;
  575. if(data != 0 && data != -1)
  576. data_struct->SetIsSet(true);
  577. }
  578. }
  579. void PacketStruct::setDataType(DataStruct* data_struct, sint32 data, int32 index){
  580. if(data_struct){
  581. sint32* ptr = (sint32*)GetStructPointer(data_struct);
  582. ptr[index] = data;
  583. if(data != 0 && data != -1)
  584. data_struct->SetIsSet(true);
  585. }
  586. }
  587. void PacketStruct::setDataType(DataStruct* data_struct, sint64 data, int32 index){
  588. if(data_struct){
  589. sint64* ptr = (sint64*)GetStructPointer(data_struct);
  590. ptr[index] = data;
  591. if(data > 0)
  592. data_struct->SetIsSet(true);
  593. }
  594. }
  595. void PacketStruct::setDataType(DataStruct* data_struct, char data, int32 index){
  596. if(data_struct){
  597. char* ptr = (char*)GetStructPointer(data_struct);
  598. ptr[index] = data;
  599. if(data > 0)
  600. data_struct->SetIsSet(true);
  601. }
  602. }
  603. void PacketStruct::setDataType(DataStruct* data_struct, int8 data, int32 index){
  604. if(data_struct){
  605. int8* ptr = (int8*)GetStructPointer(data_struct);
  606. ptr[index] = data;
  607. if(data > 0)
  608. data_struct->SetIsSet(true);
  609. }
  610. }
  611. void PacketStruct::setDataType(DataStruct* data_struct, int16 data, int32 index){
  612. if(data_struct){
  613. int16* ptr = (int16*)GetStructPointer(data_struct);
  614. ptr[index] = data;
  615. if(data > 0)
  616. data_struct->SetIsSet(true);
  617. }
  618. }
  619. void PacketStruct::setDataType(DataStruct* data_struct, int32 data, int32 index){
  620. if(data_struct){
  621. int32* ptr = (int32*)GetStructPointer(data_struct);
  622. ptr[index] = data;
  623. if(data > 0)
  624. data_struct->SetIsSet(true);
  625. }
  626. }
  627. void PacketStruct::setDataType(DataStruct* data_struct, int64 data, int32 index){
  628. if(data_struct){
  629. int64* ptr = (int64*)GetStructPointer(data_struct);
  630. ptr[index] = data;
  631. if(data > 0)
  632. data_struct->SetIsSet(true);
  633. }
  634. }
  635. void PacketStruct::setDataType(DataStruct* data_struct, float data, int32 index){
  636. if(data_struct){
  637. float* ptr = (float*)GetStructPointer(data_struct);
  638. ptr[index] = data;
  639. if(data > 0)
  640. data_struct->SetIsSet(true);
  641. }
  642. }
  643. void PacketStruct::setDataType(DataStruct* data_struct, double data, int32 index){
  644. if(data_struct){
  645. double* ptr = (double*)GetStructPointer(data_struct);
  646. ptr[index] = data;
  647. if(data > 0)
  648. data_struct->SetIsSet(true);
  649. }
  650. }
  651. void PacketStruct::setData(DataStruct* data_struct, EQ2_8BitString* input_string, int32 index, bool use_second_type){
  652. if(data_struct){
  653. EQ2_8BitString* tmp = (EQ2_8BitString*)GetStructPointer(data_struct);
  654. tmp->data = input_string->data;
  655. tmp->size = input_string->data.length();
  656. if(input_string->data.length() > 0)
  657. data_struct->SetIsSet(true);
  658. }
  659. }
  660. void PacketStruct::setData(DataStruct* data_struct, EQ2_16BitString* input_string, int32 index, bool use_second_type){
  661. if(data_struct){
  662. EQ2_16BitString* tmp = (EQ2_16BitString*)GetStructPointer(data_struct);
  663. tmp->data = input_string->data;
  664. tmp->size = input_string->data.length();
  665. if(input_string->data.length() > 0)
  666. data_struct->SetIsSet(true);
  667. }
  668. }
  669. void PacketStruct::setData(DataStruct* data_struct, EQ2_32BitString* input_string, int32 index, bool use_second_type){
  670. if(data_struct){
  671. EQ2_32BitString* tmp = (EQ2_32BitString*)GetStructPointer(data_struct);
  672. tmp->data = input_string->data;
  673. tmp->size = input_string->data.length();
  674. if(input_string->data.length() > 0)
  675. data_struct->SetIsSet(true);
  676. }
  677. }
  678. void PacketStruct::add(DataStruct* data){
  679. structs.push_back(data);
  680. struct_map[data->GetStringName()] = data;
  681. switch(data->GetType()){
  682. case DATA_STRUCT_INT8:{
  683. struct_data[data] = new int8[data->GetLength()];
  684. int8* ptr = (int8*)GetStructPointer(data);
  685. if(data->GetLength() > 1){
  686. int8 default_val = data->GetDefaultValue();
  687. memset(ptr, default_val, data->GetLength()*sizeof(int8));
  688. }
  689. else
  690. ptr[0] = 0;
  691. break;
  692. }
  693. case DATA_STRUCT_INT16:{
  694. struct_data[data] = new int16[data->GetLength()];
  695. int16* ptr = (int16*)GetStructPointer(data);
  696. if(data->GetLength() > 1){
  697. int8 default_val = data->GetDefaultValue();
  698. memset(ptr, default_val, data->GetLength()*sizeof(int16));
  699. }
  700. else
  701. ptr[0] = 0;
  702. break;
  703. }
  704. case DATA_STRUCT_INT32:{
  705. struct_data[data] = new int32[data->GetLength()];
  706. int32* ptr = (int32*)GetStructPointer(data);
  707. if(data->GetLength() > 1){
  708. int8 default_val = data->GetDefaultValue();
  709. memset(ptr, default_val, data->GetLength()*sizeof(int32));
  710. }
  711. else
  712. ptr[0] = 0;
  713. break;
  714. }
  715. case DATA_STRUCT_INT64:{
  716. struct_data[data] = new int64[data->GetLength()];
  717. int64* ptr = (int64*)GetStructPointer(data);
  718. if(data->GetLength() > 1){
  719. int8 default_val = data->GetDefaultValue();
  720. memset(ptr, default_val, data->GetLength()*sizeof(int64));
  721. }
  722. else
  723. ptr[0] = 0;
  724. break;
  725. }
  726. case DATA_STRUCT_SINT8:{
  727. struct_data[data] = new sint8[data->GetLength()];
  728. sint8* ptr = (sint8*)GetStructPointer(data);
  729. if(data->GetLength() > 1)
  730. memset(ptr, 0, data->GetLength()*sizeof(sint8));
  731. else
  732. ptr[0] = 0;
  733. break;
  734. }
  735. case DATA_STRUCT_SINT16:{
  736. struct_data[data] = new sint16[data->GetLength()];
  737. sint16* ptr = (sint16*)GetStructPointer(data);
  738. if(data->GetLength() > 1)
  739. memset(ptr, 0, data->GetLength()*sizeof(sint16));
  740. else
  741. ptr[0] = 0;
  742. break;
  743. }
  744. case DATA_STRUCT_SINT32:{
  745. struct_data[data] = new sint32[data->GetLength()];
  746. sint32* ptr = (sint32*)GetStructPointer(data);
  747. if(data->GetLength() > 1)
  748. memset(ptr, 0, data->GetLength()*sizeof(sint32));
  749. else
  750. ptr[0] = 0;
  751. break;
  752. }
  753. case DATA_STRUCT_SINT64:{
  754. struct_data[data] = new sint64[data->GetLength()];
  755. sint64* ptr = (sint64*)GetStructPointer(data);
  756. if(data->GetLength() > 1)
  757. memset(ptr, 0, data->GetLength()*sizeof(sint64));
  758. else
  759. ptr[0] = 0;
  760. break;
  761. }
  762. case DATA_STRUCT_CHAR:{
  763. struct_data[data] = new char[data->GetLength()];
  764. char* ptr = (char*)GetStructPointer(data);
  765. if(data->GetLength() > 1)
  766. memset(ptr, 0, data->GetLength()*sizeof(char));
  767. else
  768. ptr[0] = 0;
  769. break;
  770. }
  771. case DATA_STRUCT_FLOAT:{
  772. struct_data[data] = new float[data->GetLength()];
  773. float* ptr = (float*)GetStructPointer(data);
  774. if(data->GetLength() > 1)
  775. memset(ptr, 0, data->GetLength()*sizeof(float));
  776. else
  777. ptr[0] = 0;
  778. break;
  779. }
  780. case DATA_STRUCT_DOUBLE:{
  781. struct_data[data] = new double[data->GetLength()];
  782. double* ptr = (double*)GetStructPointer(data);
  783. if(data->GetLength() > 1)
  784. memset(ptr, 0, data->GetLength()*sizeof(double));
  785. else
  786. ptr[0] = 0;
  787. break;
  788. }
  789. case DATA_STRUCT_ARRAY:{
  790. data->SetLength(0);
  791. break;
  792. }
  793. case DATA_STRUCT_EQ2_8BIT_STRING:{
  794. string name2 = data->GetStringName();
  795. for(int32 i=1;i<data->GetLength();i++){
  796. DataStruct* new_data = new DataStruct(data);
  797. char blah[10] = {0};
  798. sprintf(blah,"%i",i);
  799. name2.append("_").append(blah);
  800. new_data->SetName(name2.c_str());
  801. new_data->SetLength(1);
  802. EQ2_8BitString* tmp = new EQ2_8BitString;
  803. tmp->size = 0;
  804. struct_data[new_data] = tmp;
  805. structs.push_back(new_data);
  806. }
  807. data->SetLength(1);
  808. EQ2_8BitString* tmp = new EQ2_8BitString;
  809. tmp->size = 0;
  810. struct_data[data] = tmp;
  811. break;
  812. }
  813. case DATA_STRUCT_EQ2_16BIT_STRING:{
  814. string name2 = data->GetStringName();
  815. for(int32 i=1;i<data->GetLength();i++){
  816. DataStruct* new_data = new DataStruct(data);
  817. char blah[10] = {0};
  818. sprintf(blah,"%i",i);
  819. name2.append("_").append(blah);
  820. new_data->SetName(name2.c_str());
  821. new_data->SetLength(1);
  822. EQ2_16BitString* tmp = new EQ2_16BitString;
  823. tmp->size = 0;
  824. struct_data[new_data] = tmp;
  825. structs.push_back(new_data);
  826. }
  827. data->SetLength(1);
  828. EQ2_16BitString* tmp = new EQ2_16BitString;
  829. tmp->size = 0;
  830. struct_data[data] = tmp;
  831. break;
  832. }
  833. case DATA_STRUCT_EQ2_32BIT_STRING:{
  834. string name2 = data->GetStringName();
  835. for(int32 i=1;i<data->GetLength();i++){
  836. DataStruct* new_data = new DataStruct(data);
  837. char blah[10] = {0};
  838. sprintf(blah,"%i",i);
  839. name2.append("_").append(blah);
  840. new_data->SetName(name2.c_str());
  841. new_data->SetLength(1);
  842. EQ2_32BitString* tmp = new EQ2_32BitString;
  843. tmp->size = 0;
  844. struct_data[new_data] = tmp;
  845. structs.push_back(new_data);
  846. }
  847. data->SetLength(1);
  848. EQ2_32BitString* tmp = new EQ2_32BitString;
  849. tmp->size = 0;
  850. struct_data[data] = tmp;
  851. break;
  852. }
  853. case DATA_STRUCT_COLOR:{
  854. struct_data[data] = new EQ2_Color[data->GetLength()];
  855. EQ2_Color* ptr = (EQ2_Color*)GetStructPointer(data);
  856. for(int16 i=0;i<data->GetLength();i++){
  857. ptr[i].red = 0;
  858. ptr[i].blue = 0;
  859. ptr[i].green = 0;
  860. }
  861. break;
  862. }
  863. case DATA_STRUCT_EQUIPMENT:{
  864. struct_data[data] = new EQ2_EquipmentItem[data->GetLength()];
  865. EQ2_EquipmentItem* ptr = (EQ2_EquipmentItem*)GetStructPointer(data);
  866. for(int16 i=0;i<data->GetLength();i++){
  867. memset(&ptr[i].color, 0, sizeof(ptr[i].color));
  868. memset(&ptr[i].highlight, 0, sizeof(ptr[i].highlight));
  869. ptr[i].type = 0;
  870. }
  871. break;
  872. }
  873. case DATA_STRUCT_ITEM:{
  874. struct_data[data] = new uchar[10000];
  875. char* ptr = (char*)GetStructPointer(data);
  876. memset(ptr, 0, 10000);
  877. break;
  878. }
  879. }
  880. }
  881. void PacketStruct::remove(DataStruct* data){
  882. vector<DataStruct*>::iterator itr;
  883. for(itr=structs.begin();itr!=structs.end();itr++) {
  884. if(data == (*itr)){
  885. structs.erase(itr);
  886. safe_delete(data);
  887. return;
  888. }
  889. }
  890. }
  891. DataStruct* PacketStruct::findStruct(const char* name, int32 index){
  892. return findStruct(name, index, index);
  893. }
  894. DataStruct* PacketStruct::findStruct(const char* name, int32 index1, int32 index2){
  895. DataStruct* data = 0;
  896. if(struct_map.count(string(name)) > 0){
  897. data = struct_map[string(name)];
  898. if(data && index2 < data->GetLength())
  899. return data;
  900. }
  901. vector<DataStruct*>::iterator itr;
  902. PacketStruct* packet = 0;
  903. vector<PacketStruct*>::iterator itr2;
  904. string name2 = string(name);
  905. if(index1 < 0xFFFF){
  906. char blah[10] = {0};
  907. sprintf(blah,"_%i",index1);
  908. name2.append(blah);
  909. }
  910. if(struct_map.count(name2) > 0){
  911. data = struct_map[name2];
  912. if(data && index2 < data->GetLength())
  913. return data;
  914. }
  915. for(itr2=arrays.begin();itr2!=arrays.end();itr2++) {
  916. packet = *itr2;
  917. data = packet->findStruct(name,index1, index2);
  918. if(data != 0)
  919. return data;
  920. }
  921. return 0;
  922. }
  923. void PacketStruct::remove(const char* name){
  924. DataStruct* data = 0;
  925. vector<DataStruct*>::iterator itr;
  926. for(itr=structs.begin();itr!=structs.end();itr++) {
  927. data = *itr;
  928. if(strcmp(name, data->GetName()) == 0){
  929. structs.erase(itr);
  930. safe_delete(data);
  931. return;
  932. }
  933. }
  934. }
  935. string* PacketStruct::serializeString(){
  936. serializePacket();
  937. return getDataString();
  938. }
  939. void PacketStruct::setSmallString(DataStruct* data_struct, const char* text, int32 index){
  940. EQ2_8BitString* string_data = new EQ2_8BitString;
  941. string_data->data = string(text);
  942. string_data->size = string_data->data.length();
  943. setData(data_struct, string_data, index);
  944. safe_delete(string_data);
  945. }
  946. void PacketStruct::setMediumString(DataStruct* data_struct, const char* text, int32 index){
  947. EQ2_16BitString* string_data = new EQ2_16BitString;
  948. string_data->data = string(text);
  949. string_data->size = string_data->data.length();
  950. setData(data_struct, string_data, index);
  951. safe_delete(string_data);
  952. }
  953. void PacketStruct::setLargeString(DataStruct* data_struct, const char* text, int32 index){
  954. EQ2_32BitString* string_data = new EQ2_32BitString;
  955. string_data->data = string(text);
  956. string_data->size = string_data->data.length();
  957. setData(data_struct, string_data, index);
  958. safe_delete(string_data);
  959. }
  960. void PacketStruct::setSmallStringByName(const char* name, const char* text, int32 index){
  961. setSmallString(findStruct(name, index), text, index);
  962. }
  963. void PacketStruct::setMediumStringByName(const char* name, const char* text, int32 index){
  964. setMediumString(findStruct(name, index), text, index);
  965. }
  966. void PacketStruct::setLargeStringByName(const char* name, const char* text, int32 index){
  967. setLargeString(findStruct(name, index), text, index);
  968. }
  969. bool PacketStruct::GetVariableIsSet(const char* name){
  970. DataStruct* ds2 = findStruct(name, 0);
  971. if(!ds2 || !ds2->IsSet())
  972. return false;
  973. return true;
  974. }
  975. bool PacketStruct::GetVariableIsNotSet(const char* name){
  976. DataStruct* ds2 = findStruct(name, 0);
  977. if(ds2 && ds2->IsSet())
  978. return false;
  979. return true;
  980. }
  981. bool PacketStruct::LoadPacketData(uchar* data, int32 data_len){
  982. loadedSuccessfully = true;
  983. DataStruct* data_struct = 0;
  984. try{
  985. InitializeLoadData(data, data_len);
  986. vector<DataStruct*>::iterator itr;
  987. for(itr=structs.begin();itr!=structs.end();itr++) {
  988. data_struct = *itr;
  989. if(!data_struct->AddToStruct())
  990. continue;
  991. if(data_struct->GetIfSet() && data_struct->GetIfSetVariable()){
  992. string varname = string(data_struct->GetIfSetVariable());
  993. if(varname.find(",") < 0xFFFFFFFF){
  994. vector<string>* varnames = SplitString(varname, ',');
  995. if(varnames){
  996. bool should_continue = true;
  997. for(int32 i=0;i<varnames->size();i++){
  998. if(GetVariableIsSet(varnames->at(i).c_str())){
  999. should_continue = false;
  1000. break;
  1001. }
  1002. }
  1003. safe_delete(varnames);
  1004. if(should_continue)
  1005. continue;
  1006. }
  1007. }
  1008. else{
  1009. // Check to see if the variable contains %i, if it does assume we are in an array
  1010. // and get the current index from the end of the data struct's name
  1011. char name[250] = {0};
  1012. if (varname.find("%i") < 0xFFFFFFFF) {
  1013. vector<string>* varnames = SplitString(data_struct->GetName(), '_');
  1014. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1015. sprintf(name, varname.c_str(), index);
  1016. }
  1017. else
  1018. strcpy(name, varname.c_str());
  1019. if(!GetVariableIsSet(name))
  1020. continue;
  1021. }
  1022. }
  1023. if(data_struct->GetIfNotSet() && data_struct->GetIfNotSetVariable()){
  1024. string varname = string(data_struct->GetIfNotSetVariable());
  1025. if(varname.find(",") < 0xFFFFFFFF){
  1026. vector<string>* varnames = SplitString(varname, ',');
  1027. if(varnames){
  1028. bool should_continue = false;
  1029. for(int32 i=0;i<varnames->size();i++){
  1030. if(!GetVariableIsNotSet(varnames->at(i).c_str())){
  1031. should_continue = true;
  1032. break;
  1033. }
  1034. }
  1035. safe_delete(varnames);
  1036. if(should_continue)
  1037. continue;
  1038. }
  1039. }
  1040. else{
  1041. // Check to see if the variable contains %i, if it does assume we are in an array
  1042. // and get the current index from the end of the data struct's name
  1043. char name[250] = {0};
  1044. if (varname.find("%i") < 0xFFFFFFFF) {
  1045. vector<string>* varnames = SplitString(data_struct->GetName(), '_');
  1046. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1047. sprintf(name, varname.c_str(), index);
  1048. }
  1049. else
  1050. strcpy(name, varname.c_str());
  1051. if(!GetVariableIsNotSet(name))
  1052. continue;
  1053. }
  1054. }
  1055. // Quick implementaion of IfVariableNotEquals
  1056. // probably not what it was intended for as it currently just checks to see if the given variable equals 1
  1057. // should probably change it so you can define what the variable should or shouldn't equal
  1058. //
  1059. // ie: IfVariableNotEquals="stat_type_%i=1"
  1060. // would be a check to make sure that stat_type_%i does not equal 1 and if it does exclude this element
  1061. if (data_struct->GetIfNotEquals() && data_struct->GetIfNotEqualsVariable()) {
  1062. // Get the variable name
  1063. string varname = string(data_struct->GetIfNotEqualsVariable());
  1064. char name[250] = {0};
  1065. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1066. // index and replace it
  1067. if (varname.find("%i") < 0xFFFFFFFF) {
  1068. // Get the current index by getting the number at the end of the name
  1069. vector<string>* varnames = SplitString(data_struct->GetName(), '_');
  1070. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1071. string substr = "stat_type";
  1072. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1073. // adorn_stat_subtype 18 chars
  1074. string temp = varname.substr(12);
  1075. char temp2[20]={0};
  1076. int index2 = atoi(temp.c_str());
  1077. itoa(index2, temp2, 10);
  1078. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1079. }
  1080. sprintf(name, varname.c_str(), index);
  1081. safe_delete(varnames);
  1082. }
  1083. else
  1084. strcpy(name, varname.c_str());
  1085. // Get the data for the variable
  1086. int16 value = 0;
  1087. DataStruct* data_struct2 = findStruct(name, 0);
  1088. value = getType_int16(data_struct2);
  1089. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1090. if (value == 1)
  1091. continue;
  1092. }
  1093. // copy and paste of the code above for IfEquals
  1094. if (data_struct->GetIfEquals() && data_struct->GetIfEqualsVariable()) {
  1095. // Get the variable name
  1096. string varname = string(data_struct->GetIfEqualsVariable());
  1097. char name[250] = {0};
  1098. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1099. // index and replace it
  1100. if (varname.find("%i") < 0xFFFFFFFF) {
  1101. // Get the current index by getting the number at the end of the name
  1102. vector<string>* varnames = SplitString(data_struct->GetName(), '_');
  1103. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1104. string substr = "stat_type";
  1105. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1106. // adorn_stat_subtype 18 chars
  1107. string temp = varname.substr(12);
  1108. char temp2[20]={0};
  1109. int index2 = atoi(temp.c_str());
  1110. itoa(index2, temp2, 10);
  1111. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1112. }
  1113. sprintf(name, varname.c_str(), index);
  1114. safe_delete(varnames);
  1115. }
  1116. else
  1117. strcpy(name, varname.c_str());
  1118. // Get the data for the variable
  1119. int16 value = 0;
  1120. DataStruct* data_struct2 = findStruct(name, 0);
  1121. value = getType_int16(data_struct2);
  1122. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1123. if (value != 1)
  1124. continue;
  1125. }
  1126. // The following is tailored to items as they are the only structs that use type2
  1127. // if more type2's are needed outside of the item stats array we need to think up an element
  1128. // to determine when to use type2 over type.
  1129. // Added checks for set stats and adorn stats - Zcoretri
  1130. bool useType2 = false;
  1131. if (data_struct->GetType2() > 0) {
  1132. int16 type = 0;
  1133. char name[250] = {0};
  1134. vector<string>* varnames = SplitString(data_struct->GetName(), '_');
  1135. string struct_name = data_struct->GetName();
  1136. if (struct_name.find("set") < 0xFFFFFFFF) {
  1137. string tmp = "set_stat_type";
  1138. struct_name.replace(0, 9, tmp);
  1139. sprintf(name, "%s", struct_name.c_str());
  1140. }
  1141. else if (struct_name.find("adorn") < 0xFFFFFFFF) {
  1142. string tmp = "adorn_stat_type";
  1143. struct_name.replace(0, 9, tmp);
  1144. sprintf(name, "%s", struct_name.c_str());
  1145. }
  1146. else {
  1147. // set name to stat_type_# (where # is the current index of the array we are in)
  1148. sprintf(name, "%s_%s", "stat_type", varnames->at(varnames->size()-1).c_str());
  1149. }
  1150. // Look up the value for stat_type
  1151. DataStruct* data_struct2 = findStruct(name, 0);
  1152. type = getType_int16(data_struct2);
  1153. // If stat_type == 6 we use a float, else we use sint16
  1154. if (type != 6)
  1155. useType2 = true;
  1156. safe_delete(varnames);
  1157. }
  1158. if (!StructLoadData(data_struct, GetStructPointer(data_struct), data_struct->GetLength(), useType2))
  1159. {
  1160. loadedSuccessfully = false;
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. catch(...){
  1166. loadedSuccessfully = false;
  1167. }
  1168. return loadedSuccessfully;
  1169. }
  1170. bool PacketStruct::StructLoadData(DataStruct* data_struct, void* data, int32 len, bool useType2){
  1171. int8 type = 0;
  1172. if (useType2) {
  1173. type = data_struct->GetType2();
  1174. // Need to change the data the struct expects to type2
  1175. data_struct->SetType(type);
  1176. LogWrite(PACKET__DEBUG, 7, "Items", "Using type2 = %i", type);
  1177. }
  1178. else
  1179. type = data_struct->GetType();
  1180. switch(type){
  1181. case DATA_STRUCT_INT8:
  1182. LoadData((int8*)data, len);
  1183. data_struct->SetIsSet(*((int8*)data) > 0);
  1184. break;
  1185. case DATA_STRUCT_INT16:
  1186. if(data_struct->GetOversized() > 0){
  1187. LoadData((int8*)data, len);
  1188. if(getType_int16(data_struct) == data_struct->GetOversizedByte()){
  1189. LoadData((int16*)data, len);
  1190. }
  1191. }
  1192. else{
  1193. LoadData((int16*)data, len);
  1194. }
  1195. data_struct->SetIsSet(*((int16*)data) > 0);
  1196. break;
  1197. case DATA_STRUCT_INT32:
  1198. if(data_struct->GetOversized() > 0){
  1199. LoadData((int8*)data, len);
  1200. if(getType_int32(data_struct) == data_struct->GetOversizedByte()){
  1201. LoadData((int32*)data, len);
  1202. }
  1203. else
  1204. LoadData((int8*)data, len);
  1205. }
  1206. else{
  1207. LoadData((int32*)data, len);
  1208. }
  1209. data_struct->SetIsSet(*((int32*)data) > 0);
  1210. break;
  1211. case DATA_STRUCT_INT64:
  1212. LoadData((int64*)data, len);
  1213. data_struct->SetIsSet(*((int64*)data) > 0);
  1214. break;
  1215. case DATA_STRUCT_SINT8:
  1216. LoadData((sint8*)data, len);
  1217. data_struct->SetIsSet(*((sint8*)data) > 0);
  1218. break;
  1219. case DATA_STRUCT_SINT16:
  1220. if(data_struct->GetOversized() > 0){
  1221. LoadData((sint8*)data, len);
  1222. sint8 val = (sint8)getType_sint16(data_struct);
  1223. if(val < 0) //necessary because when using memcpy from a smaller data type to a larger one, the sign is lost
  1224. setData(data_struct, val, 0);
  1225. if(getType_sint16(data_struct) == data_struct->GetOversizedByte())
  1226. LoadData((sint16*)data, len);
  1227. }
  1228. else
  1229. LoadData((sint16*)data, len);
  1230. data_struct->SetIsSet(*((sint16*)data) > 0);
  1231. break;
  1232. case DATA_STRUCT_SINT32:
  1233. LoadData((sint32*)data, len);
  1234. data_struct->SetIsSet(*((sint32*)data) > 0);
  1235. break;
  1236. case DATA_STRUCT_SINT64:
  1237. LoadData((sint64*)data, len);
  1238. data_struct->SetIsSet(*((sint64*)data) > 0);
  1239. break;
  1240. case DATA_STRUCT_CHAR:
  1241. LoadData((char*)data, len);
  1242. data_struct->SetIsSet(data > 0);
  1243. break;
  1244. case DATA_STRUCT_FLOAT:
  1245. LoadData((float*)data, len);
  1246. data_struct->SetIsSet(*((float*)data) > 0);
  1247. break;
  1248. case DATA_STRUCT_DOUBLE:
  1249. LoadData((double*)data, len);
  1250. data_struct->SetIsSet(*((double*)data) > 0);
  1251. break;
  1252. case DATA_STRUCT_EQ2_8BIT_STRING:{
  1253. LoadDataString((EQ2_8BitString*)data);
  1254. data_struct->SetIsSet(((EQ2_8BitString*)data)->data.length() > 0);
  1255. break;
  1256. }
  1257. case DATA_STRUCT_EQ2_16BIT_STRING:{
  1258. LoadDataString((EQ2_16BitString*)data);
  1259. data_struct->SetIsSet(((EQ2_16BitString*)data)->data.length() > 0);
  1260. break;
  1261. }
  1262. case DATA_STRUCT_EQ2_32BIT_STRING:{
  1263. LoadDataString((EQ2_32BitString*)data);
  1264. data_struct->SetIsSet(((EQ2_32BitString*)data)->data.length() > 0);
  1265. break;
  1266. }
  1267. case DATA_STRUCT_COLOR:{
  1268. if(strcmp(GetName(),"CreateCharacter") == 0 || strcmp(GetName(), "WS_SubmitCharCust") == 0)
  1269. CreateEQ2Color((EQ2_Color*)data);
  1270. else
  1271. LoadData((EQ2_Color*)data, len);
  1272. break;
  1273. }
  1274. case DATA_STRUCT_EQUIPMENT:{
  1275. LoadData((EQ2_EquipmentItem*)data);
  1276. break;
  1277. }
  1278. case DATA_STRUCT_ARRAY:{
  1279. int32 size = GetArraySize(data_struct,0);
  1280. if(size > 0xFFFF){
  1281. LogWrite(PACKET__WARNING, 1, "Packet", "Possible corrupt packet while loading struct array, orig array size: %u in struct name %s, data name %s", size, GetName(), (data_struct && data_struct->GetName()) ? data_struct->GetName() : "??");
  1282. size = 1;
  1283. return false;
  1284. }
  1285. PacketStruct* ps = GetPacketStructByName(data_struct->GetName());
  1286. if(ps && ps->GetSubPacketSize() != size){
  1287. if(data_struct->GetMaxArraySize() > 0 && size > data_struct->GetMaxArraySize())
  1288. size = data_struct->GetMaxArraySize();
  1289. ps->reAddAll(size);
  1290. }
  1291. if(ps && size > 0){
  1292. //for(int i=0;i<size && (GetLoadLen()-GetLoadPos()) > 0;i++){
  1293. ps->LoadPacketData(GetLoadBuffer()+GetLoadPos(), GetLoadLen()-GetLoadPos());
  1294. SetLoadPos(GetLoadPos() + ps->GetLoadPos());
  1295. //}
  1296. }
  1297. break;
  1298. }
  1299. default:{
  1300. data_struct->SetIsSet(false);
  1301. }
  1302. }
  1303. return true;
  1304. }
  1305. PacketStruct* PacketStruct::GetPacketStructByName(const char* name){
  1306. PacketStruct* ps = 0;
  1307. vector<PacketStruct*>::iterator itr;
  1308. for(itr=arrays.begin();itr!=arrays.end();itr++) {
  1309. ps = *itr;
  1310. if(strcmp(ps->GetName(),name) == 0)
  1311. return ps;
  1312. ps = ps->GetPacketStructByName(name);
  1313. if(ps)
  1314. return ps;
  1315. }
  1316. return 0;
  1317. }
  1318. void PacketStruct::reAddAll(int32 length){
  1319. vector<DataStruct*>::iterator itr;
  1320. DataStruct* ds = 0;
  1321. PacketStruct* ps = 0;
  1322. vector<PacketStruct*>::iterator packet_itr;
  1323. if(orig_structs.size() == 0)
  1324. orig_structs = structs;
  1325. else
  1326. deleteDataStructs(&structs);
  1327. structs.clear();
  1328. if(orig_packets.size() == 0 && arrays.size() > 0)
  1329. orig_packets = arrays;
  1330. else{
  1331. for(packet_itr=arrays.begin();packet_itr!=arrays.end();packet_itr++) {
  1332. ps = *packet_itr;
  1333. safe_delete(ps);
  1334. }
  1335. }
  1336. arrays.clear();
  1337. for(int16 i=0;i<length;i++){
  1338. for(packet_itr=orig_packets.begin();packet_itr!=orig_packets.end();packet_itr++) {
  1339. ps = *packet_itr;
  1340. PacketStruct* new_packet = new PacketStruct(ps, true);
  1341. char tmp[20] = {0};
  1342. sprintf(tmp,"_%i",i);
  1343. string name = string(new_packet->GetName());
  1344. name.append(tmp);
  1345. new_packet->SetName(name.c_str());
  1346. add(new_packet);
  1347. }
  1348. for(itr=orig_structs.begin();itr!=orig_structs.end();itr++){
  1349. ds = *itr;
  1350. DataStruct* new_data = new DataStruct(ds);
  1351. char tmp[20] = {0};
  1352. sprintf(tmp,"_%i",i);
  1353. string name = new_data->GetStringName();
  1354. if(IsSubPacket() && parent->IsSubPacket()){
  1355. string parent_name = string(GetName());
  1356. try{
  1357. if(parent_name.rfind("_") < 0xFFFFFFFF)
  1358. sprintf(tmp,"%i_%i",atoi(parent_name.substr(parent_name.rfind("_")+1).c_str()), i);
  1359. }
  1360. catch(...){
  1361. sprintf(tmp,"_%i",i);
  1362. }
  1363. }
  1364. name.append(tmp);
  1365. new_data->SetName(name.c_str());
  1366. if(new_data->GetType() == DATA_STRUCT_ARRAY){
  1367. string old_size_arr = string(new_data->GetArraySizeVariable());
  1368. new_data->SetArraySizeVariable(old_size_arr.append(tmp).c_str());
  1369. }
  1370. add(new_data);
  1371. }
  1372. }
  1373. sub_packet_size = length;
  1374. }
  1375. int32 PacketStruct::GetArraySize(DataStruct* data_struct, int32 index){
  1376. if(data_struct){
  1377. const char* name = data_struct->GetArraySizeVariable();
  1378. return GetArraySize(name, index);
  1379. }
  1380. return 0;
  1381. }
  1382. int32 PacketStruct::GetArraySize(const char* name, int32 index){
  1383. int32 ret = 0;
  1384. DataStruct* ds = findStruct(name, index);
  1385. if(ds){
  1386. if(ds->GetType() == DATA_STRUCT_INT8){
  1387. int8* tmp = (int8*)GetStructPointer(ds);
  1388. ret = *tmp;
  1389. }
  1390. else if(ds->GetType() == DATA_STRUCT_INT16){
  1391. int16* tmp = (int16*)GetStructPointer(ds);
  1392. ret = *tmp;
  1393. }
  1394. else if(ds->GetType() == DATA_STRUCT_INT32){
  1395. int32* tmp = (int32*)GetStructPointer(ds);
  1396. ret = *tmp;
  1397. }
  1398. else if(ds->GetType() == DATA_STRUCT_INT64){
  1399. int64* tmp = (int64*)GetStructPointer(ds);
  1400. ret = *tmp;
  1401. }
  1402. }
  1403. return ret;
  1404. }
  1405. void PacketStruct::UpdateArrayByArrayLength(DataStruct* data_struct, int32 index, int32 size){
  1406. if(data_struct){
  1407. PacketStruct* packet = 0;
  1408. DataStruct* data = 0;
  1409. vector<DataStruct*>::iterator itr;
  1410. for(itr=structs.begin();itr!=structs.end();itr++) {
  1411. data = *itr;
  1412. if(strcmp(data->GetArraySizeVariable(), data_struct->GetName()) == 0){
  1413. packet = GetPacketStructByName(data->GetName());
  1414. if(packet)
  1415. packet->reAddAll(size);
  1416. return;
  1417. }
  1418. }
  1419. vector<PacketStruct*>::iterator itr2;
  1420. for(itr2=arrays.begin();itr2!=arrays.end();itr2++) {
  1421. packet = *itr2;
  1422. packet->UpdateArrayByArrayLength(data_struct, index, size);
  1423. }
  1424. }
  1425. }
  1426. void PacketStruct::UpdateArrayByArrayLengthName(const char* name, int32 index, int32 size){
  1427. UpdateArrayByArrayLength(findStruct(name, index), index, size);
  1428. }
  1429. int32 PacketStruct::GetArraySizeByName(const char* name, int32 index){
  1430. DataStruct* ds1 = findStruct(name, index);
  1431. return GetArraySize(ds1, index);
  1432. }
  1433. void PacketStruct::serializePacket(bool clear){
  1434. if(clear)
  1435. Clear();
  1436. bool client_cmd = false;
  1437. string client_data;
  1438. #ifndef LOGIN
  1439. if(GetOpcode() == OP_ClientCmdMsg && strlen(GetOpcodeType()) > 0 && !IsSubPacket())
  1440. client_cmd = true;
  1441. #endif
  1442. DataStruct* data = 0;
  1443. vector<DataStruct*>::iterator itr;
  1444. for(itr=structs.begin();itr!=structs.end();itr++) {
  1445. data = *itr;
  1446. if(data->AddToStruct()){
  1447. if(data->GetIfSet() && data->GetIfSetVariable()){
  1448. string varname = string(data->GetIfSetVariable());
  1449. if(varname.find(",") < 0xFFFFFFFF){
  1450. vector<string>* varnames = SplitString(varname, ',');
  1451. if(varnames){
  1452. bool should_continue = true;
  1453. for(int32 i=0;i<varnames->size();i++){
  1454. if(GetVariableIsSet(varnames->at(i).c_str())){
  1455. should_continue = false;
  1456. break;
  1457. }
  1458. }
  1459. safe_delete(varnames);
  1460. if(should_continue)
  1461. continue;
  1462. }
  1463. }
  1464. else{
  1465. if(!GetVariableIsSet(varname.c_str()))
  1466. continue;
  1467. }
  1468. }
  1469. if(data->GetIfNotSet() && data->GetIfNotSetVariable()){
  1470. string varname = string(data->GetIfNotSetVariable());
  1471. if(varname.find(",") < 0xFFFFFFFF){
  1472. vector<string>* varnames = SplitString(varname, ',');
  1473. if(varnames){
  1474. bool should_continue = false;
  1475. for(int32 i=0;i<varnames->size();i++){
  1476. if(!GetVariableIsNotSet(varnames->at(i).c_str())){
  1477. should_continue = true;
  1478. break;
  1479. }
  1480. }
  1481. safe_delete(varnames);
  1482. if(should_continue)
  1483. continue;
  1484. }
  1485. }
  1486. else{
  1487. if(!GetVariableIsNotSet(varname.c_str()))
  1488. continue;
  1489. }
  1490. }
  1491. // Quick implementaion of IfVariableNotEquals
  1492. // probably not what it was intended for as it currently just checks to see if the given variable equals 1
  1493. // should probably change it so you can define what the variable should or shouldn't equal
  1494. //
  1495. // ie: IfVariableNotEquals="stat_type_%i=1"
  1496. // would be a check to make sure that stat_type_%i does not equal 1 and if it does exclude this element
  1497. if (data->GetIfNotEquals() && data->GetIfNotEqualsVariable()) {
  1498. // Get the variable name
  1499. string varname = string(data->GetIfNotEqualsVariable());
  1500. char name[250] = {0};
  1501. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1502. // index and replace it
  1503. if (varname.find("%i") < 0xFFFFFFFF) {
  1504. // Get the current index by getting the number at the end of the name
  1505. vector<string>* varnames = SplitString(data->GetName(), '_');
  1506. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1507. string substr = "stat_type";
  1508. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1509. // adorn_stat_subtype 18 chars
  1510. string temp = varname.substr(12);
  1511. char temp2[20]={0};
  1512. int index2 = atoi(temp.c_str());
  1513. itoa(index2, temp2, 10);
  1514. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1515. }
  1516. sprintf(name, varname.c_str(), index);
  1517. safe_delete(varnames);
  1518. }
  1519. else
  1520. strcpy(name, varname.c_str());
  1521. // Get the data for the variable
  1522. int16 value = 0;
  1523. DataStruct* data_struct2 = findStruct(name, 0);
  1524. value = getType_int16(data_struct2);
  1525. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1526. if (value == 1)
  1527. continue;
  1528. }
  1529. // copy and paste of the code above for IfEquals
  1530. if (data->GetIfEquals() && data->GetIfEqualsVariable()) {
  1531. // Get the variable name
  1532. string varname = string(data->GetIfEqualsVariable());
  1533. char name[250] = {0};
  1534. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1535. // index and replace it
  1536. if (varname.find("%i") < 0xFFFFFFFF) {
  1537. // Get the current index by getting the number at the end of the name
  1538. vector<string>* varnames = SplitString(data->GetName(), '_');
  1539. int index = 0;
  1540. if (varnames)
  1541. index = atoi(varnames->at(varnames->size() - 1).c_str());
  1542. string substr = "stat_type";
  1543. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1544. // adorn_stat_subtype 18 chars
  1545. string temp = varname.substr(12);
  1546. char temp2[20]={0};
  1547. int index2 = atoi(temp.c_str());
  1548. itoa(index2, temp2, 10);
  1549. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1550. }
  1551. sprintf(name, varname.c_str(), index);
  1552. safe_delete(varnames);
  1553. }
  1554. else
  1555. strcpy(name, varname.c_str());
  1556. // Get the data for the variable
  1557. int16 value = 0;
  1558. DataStruct* data_struct2 = findStruct(name, 0);
  1559. value = getType_int16(data_struct2);
  1560. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1561. if (value != 1)
  1562. continue;
  1563. }
  1564. if(client_cmd)
  1565. AddSerializedData(data, 0, &client_data);
  1566. else
  1567. AddSerializedData(data);
  1568. }
  1569. }
  1570. #ifndef LOGIN
  1571. if(client_cmd){
  1572. EmuOpcode sub_opcode = EQOpcodeManager[0]->NameSearch(GetOpcodeType());
  1573. int16 opcode_val = 0;
  1574. if(sub_opcode == OP_Unknown){
  1575. try{
  1576. opcode_val = atoi(GetOpcodeType());
  1577. }
  1578. catch(...){}
  1579. if(opcode_val == 0)
  1580. LogWrite(PACKET__ERROR, 0, "Packet", "PACKET NOT SENT CORRECTLY! Unable to get Emu Opcode from: '%s'", GetOpcodeType());
  1581. }
  1582. Clear();
  1583. int32 size = client_data.length() + 3; //gotta add the opcode and oversized
  1584. int8 oversized = 0xFF;
  1585. int16 OpcodeVersion = GetOpcodeVersion(client_version);
  1586. if(opcode_val == 0)
  1587. opcode_val = EQOpcodeManager[OpcodeVersion]->EmuToEQ(sub_opcode);
  1588. if(opcode_val == EQOpcodeManager[OpcodeVersion]->EmuToEQ(OP_EqExamineInfoCmd))
  1589. size += (size - 9);
  1590. StructAddData(size, sizeof(int32), 0);
  1591. StructAddData(oversized, sizeof(int8), 0);
  1592. StructAddData(opcode_val, sizeof(int16), 0);
  1593. AddData(client_data);
  1594. }
  1595. #endif
  1596. }
  1597. int32 PacketStruct::GetTotalPacketSize(){
  1598. int32 retSize = 0;
  1599. DataStruct* data = 0;
  1600. vector<DataStruct*>::iterator itr;
  1601. EQ2_8BitString* tmp1 = 0;
  1602. EQ2_16BitString* tmp2 = 0;
  1603. EQ2_32BitString* tmp3 = 0;
  1604. for(itr=structs.begin();itr!=structs.end();itr++) {
  1605. data = *itr;
  1606. switch(data->GetType()){
  1607. case DATA_STRUCT_INT8:
  1608. case DATA_STRUCT_SINT8:
  1609. case DATA_STRUCT_CHAR:
  1610. retSize+=(1*data->GetLength());
  1611. break;
  1612. case DATA_STRUCT_SINT16:
  1613. case DATA_STRUCT_INT16:
  1614. retSize+=(2*data->GetLength());
  1615. break;
  1616. case DATA_STRUCT_INT32:
  1617. case DATA_STRUCT_SINT32:
  1618. case DATA_STRUCT_FLOAT:
  1619. case DATA_STRUCT_DOUBLE:
  1620. retSize+=(4*data->GetLength());
  1621. break;
  1622. case DATA_STRUCT_SINT64:
  1623. case DATA_STRUCT_INT64:
  1624. retSize+=(8*data->GetLength());
  1625. break;
  1626. case DATA_STRUCT_EQ2_8BIT_STRING:
  1627. tmp1 = (EQ2_8BitString*)GetStructPointer(data);
  1628. if(tmp1){
  1629. for(int16 i=0;i<data->GetLength();i++)
  1630. retSize += tmp1[i].data.length();
  1631. }
  1632. retSize+=(1*data->GetLength());
  1633. break;
  1634. case DATA_STRUCT_EQ2_16BIT_STRING:{
  1635. tmp2 = (EQ2_16BitString*)GetStructPointer(data);
  1636. if(tmp2){
  1637. for(int16 i=0;i<data->GetLength();i++)
  1638. retSize += tmp2[i].data.length();
  1639. }
  1640. retSize+=(2*data->GetLength());
  1641. break;
  1642. }
  1643. case DATA_STRUCT_EQ2_32BIT_STRING:{
  1644. tmp3 = (EQ2_32BitString*)GetStructPointer(data);
  1645. if(tmp3){
  1646. for(int16 i=0;i<data->GetLength();i++)
  1647. retSize += tmp3[i].data.length();
  1648. }
  1649. retSize+=(4*data->GetLength());
  1650. break;
  1651. }
  1652. case DATA_STRUCT_ARRAY:{
  1653. int32 size = GetArraySize(data,0);
  1654. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1655. if(ps && ps->GetSubPacketSize() != size){
  1656. ps->reAddAll(size);
  1657. }
  1658. if(ps && size > 0)
  1659. retSize+= ps->GetTotalPacketSize();
  1660. break;
  1661. }
  1662. case DATA_STRUCT_COLOR:{
  1663. retSize+=((sizeof(int8)*3)*data->GetLength());
  1664. break;
  1665. }
  1666. case DATA_STRUCT_EQUIPMENT:{
  1667. retSize+=((((sizeof(int8)*3)*2) + sizeof(int16))*data->GetLength());
  1668. break;
  1669. }
  1670. }
  1671. }
  1672. return retSize;
  1673. }
  1674. void PacketStruct::AddSerializedData(DataStruct* data, int32 index, string* datastring){
  1675. switch(data->GetAddType()){
  1676. case DATA_STRUCT_INT8:
  1677. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1678. break;
  1679. case DATA_STRUCT_INT16:
  1680. if(data->GetOversized() > 0){
  1681. if(*((int16*)GetStructPointer(data)) >= data->GetOversized()){
  1682. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1683. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1684. }
  1685. else
  1686. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1687. }
  1688. else
  1689. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1690. break;
  1691. case DATA_STRUCT_INT32:
  1692. if(data->GetOversized() > 0){
  1693. if(*((int32*)GetStructPointer(data)) >= data->GetOversized()){
  1694. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1695. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1696. }
  1697. else
  1698. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1699. }
  1700. else
  1701. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1702. break;
  1703. case DATA_STRUCT_INT64:
  1704. StructAddData((int64*)GetStructPointer(data), data->GetLength(), sizeof(int64), datastring);
  1705. break;
  1706. case DATA_STRUCT_SINT8:
  1707. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1708. break;
  1709. case DATA_STRUCT_SINT16:
  1710. if(data->GetOversized() > 0){
  1711. sint16 val = *((sint16*)GetStructPointer(data));
  1712. if(val >= data->GetOversized() || val <= (data->GetOversized()*-1)){
  1713. StructAddData(data->GetOversizedByte(), sizeof(sint8), datastring);
  1714. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1715. }
  1716. else
  1717. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1718. }
  1719. else
  1720. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1721. break;
  1722. case DATA_STRUCT_SINT32:
  1723. StructAddData((sint32*)GetStructPointer(data), data->GetLength(), sizeof(sint32), datastring);
  1724. break;
  1725. case DATA_STRUCT_SINT64:
  1726. StructAddData((sint64*)GetStructPointer(data), data->GetLength(), sizeof(sint64), datastring);
  1727. break;
  1728. case DATA_STRUCT_CHAR:
  1729. StructAddData((char*)GetStructPointer(data), data->GetLength(), sizeof(char), datastring);
  1730. break;
  1731. case DATA_STRUCT_FLOAT:
  1732. StructAddData((float*)GetStructPointer(data), data->GetLength(), sizeof(float), datastring);
  1733. break;
  1734. case DATA_STRUCT_DOUBLE:
  1735. StructAddData((double*)GetStructPointer(data), data->GetLength(), sizeof(double), datastring);
  1736. break;
  1737. case DATA_STRUCT_EQ2_8BIT_STRING:{
  1738. for(int16 i=0;i<data->GetLength();i++){
  1739. EQ2_8BitString* ds = (EQ2_8BitString*)GetStructPointer(data);
  1740. AddDataString(ds[i], datastring);
  1741. }
  1742. break;
  1743. }
  1744. case DATA_STRUCT_EQ2_16BIT_STRING:{
  1745. for(int16 i=0;i<data->GetLength();i++){
  1746. EQ2_16BitString* ds = (EQ2_16BitString*)GetStructPointer(data);
  1747. AddDataString(ds[i], datastring);
  1748. }
  1749. break;
  1750. }
  1751. case DATA_STRUCT_EQ2_32BIT_STRING:{
  1752. for(int16 i=0;i<data->GetLength();i++){
  1753. EQ2_32BitString* ds = (EQ2_32BitString*)GetStructPointer(data);
  1754. AddDataString(ds[i], datastring);
  1755. }
  1756. break;
  1757. }
  1758. case DATA_STRUCT_ARRAY:{
  1759. int32 size = GetArraySize(data,0);
  1760. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1761. if(ps && ps->GetSubPacketSize() != size){
  1762. ps->reAddAll(size);
  1763. }
  1764. if(ps && size > 0){
  1765. ps->serializePacket();
  1766. string data = *(ps->getDataString());
  1767. AddData(data, datastring);
  1768. }
  1769. break;
  1770. }
  1771. case DATA_STRUCT_COLOR:{
  1772. StructAddData((EQ2_Color*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_Color), datastring);
  1773. break;
  1774. }
  1775. case DATA_STRUCT_EQUIPMENT:{
  1776. StructAddData((EQ2_EquipmentItem*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_EquipmentItem), datastring);
  1777. break;
  1778. }
  1779. case DATA_STRUCT_ITEM:{
  1780. //DumpPacket(ptr, data->GetItemSize());
  1781. AddCharArray((char*)GetStructPointer(data), data->GetItemSize(), datastring);
  1782. break;
  1783. }
  1784. }
  1785. }
  1786. int8 PacketStruct::getType_int8_ByName(const char* name, int32 index, bool force){
  1787. DataStruct* data_struct = findStruct(name, index);
  1788. return getType_int8(data_struct, index, force);
  1789. }
  1790. int16 PacketStruct::getType_int16_ByName(const char* name, int32 index, bool force){
  1791. DataStruct* data_struct = findStruct(name, index);
  1792. return getType_int16(data_struct, index, force);
  1793. }
  1794. int32 PacketStruct::getType_int32_ByName(const char* name, int32 index, bool force){
  1795. DataStruct* data_struct = findStruct(name, index);
  1796. return getType_int32(data_struct, index, force);
  1797. }
  1798. int64 PacketStruct::getType_int64_ByName(const char* name, int32 index, bool force){
  1799. DataStruct* data_struct = findStruct(name, index);
  1800. return getType_int64(data_struct, index, force);
  1801. }
  1802. sint8 PacketStruct::getType_sint8_ByName(const char* name, int32 index, bool force){
  1803. DataStruct* data_struct = findStruct(name, index);
  1804. return getType_sint8(data_struct, index, force);
  1805. }
  1806. sint16 PacketStruct::getType_sint16_ByName(const char* name, int32 index, bool force){
  1807. DataStruct* data_struct = findStruct(name, index);
  1808. return getType_sint16(data_struct, index, force);
  1809. }
  1810. sint32 PacketStruct::getType_sint32_ByName(const char* name, int32 index, bool force){
  1811. DataStruct* data_struct = findStruct(name, index);
  1812. return getType_sint32(data_struct, index, force);
  1813. }
  1814. sint64 PacketStruct::getType_sint64_ByName(const char* name, int32 index, bool force){
  1815. DataStruct* data_struct = findStruct(name, index);
  1816. return getType_sint64(data_struct, index, force);
  1817. }
  1818. float PacketStruct::getType_float_ByName(const char* name, int32 index, bool force){
  1819. DataStruct* data_struct = findStruct(name, index);
  1820. return getType_float(data_struct, index, force);
  1821. }
  1822. double PacketStruct::getType_double_ByName(const char* name, int32 index, bool force){
  1823. DataStruct* data_struct = findStruct(name, index);
  1824. return getType_double(data_struct, index, force);
  1825. }
  1826. char PacketStruct::getType_char_ByName(const char* name, int32 index, bool force){
  1827. DataStruct* data_struct = findStruct(name, index);
  1828. return getType_char(data_struct, index, force);
  1829. }
  1830. EQ2_8BitString PacketStruct::getType_EQ2_8BitString_ByName(const char* name, int32 index, bool force){
  1831. DataStruct* data_struct = findStruct(name, index);
  1832. return getType_EQ2_8BitString(data_struct, index, force);
  1833. }
  1834. EQ2_16BitString PacketStruct::getType_EQ2_16BitString_ByName(const char* name, int32 index, bool force){
  1835. DataStruct* data_struct = findStruct(name, index);
  1836. return getType_EQ2_16BitString(data_struct, index, force);
  1837. }
  1838. EQ2_32BitString PacketStruct::getType_EQ2_32BitString_ByName(const char* name, int32 index, bool force){
  1839. DataStruct* data_struct = findStruct(name, index);
  1840. return getType_EQ2_32BitString(data_struct, index, force);
  1841. }
  1842. EQ2_Color PacketStruct::getType_EQ2_Color_ByName(const char* name, int32 index, bool force){
  1843. DataStruct* data_struct = findStruct(name, index);
  1844. return getType_EQ2_Color(data_struct, index, force);
  1845. }
  1846. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem_ByName(const char* name, int32 index, bool force){
  1847. DataStruct* data_struct = findStruct(name, index);
  1848. return getType_EQ2_EquipmentItem(data_struct, index, force);
  1849. }
  1850. int8 PacketStruct::getType_int8(DataStruct* data_struct, int32 index, bool force){
  1851. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT8) || force)){
  1852. int8* ptr = (int8*)GetStructPointer(data_struct);
  1853. return ptr[index];
  1854. }
  1855. return 0;
  1856. }
  1857. int16 PacketStruct::getType_int16(DataStruct* data_struct, int32 index, bool force){
  1858. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT16) || force)){
  1859. int16* ptr = (int16*)GetStructPointer(data_struct);
  1860. return ptr[index];
  1861. }
  1862. return 0;
  1863. }
  1864. int32 PacketStruct::getType_int32(DataStruct* data_struct, int32 index, bool force){
  1865. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT32) || force)){
  1866. int32* ptr = (int32*)GetStructPointer(data_struct);
  1867. return ptr[index];
  1868. }
  1869. return 0;
  1870. }
  1871. int64 PacketStruct::getType_int64(DataStruct* data_struct, int32 index, bool force){
  1872. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT64) || force)){
  1873. int64* ptr = (int64*)GetStructPointer(data_struct);
  1874. return ptr[index];
  1875. }
  1876. return 0;
  1877. }
  1878. sint8 PacketStruct::getType_sint8(DataStruct* data_struct, int32 index, bool force){
  1879. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT8) || force)){
  1880. sint8* ptr = (sint8*)GetStructPointer(data_struct);
  1881. return ptr[index];
  1882. }
  1883. return 0;
  1884. }
  1885. sint16 PacketStruct::getType_sint16(DataStruct* data_struct, int32 index, bool force){
  1886. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT16) || force)){
  1887. sint16* ptr = (sint16*)GetStructPointer(data_struct);
  1888. return ptr[index];
  1889. }
  1890. return 0;
  1891. }
  1892. sint32 PacketStruct::getType_sint32(DataStruct* data_struct, int32 index, bool force){
  1893. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT32) || force)){
  1894. sint32* ptr = (sint32*)GetStructPointer(data_struct);
  1895. return ptr[index];
  1896. }
  1897. return 0;
  1898. }
  1899. sint64 PacketStruct::getType_sint64(DataStruct* data_struct, int32 index, bool force){
  1900. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT64) || force)){
  1901. sint64* ptr = (sint64*)GetStructPointer(data_struct);
  1902. return ptr[index];
  1903. }
  1904. return 0;
  1905. }
  1906. float PacketStruct::getType_float(DataStruct* data_struct, int32 index, bool force){
  1907. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_FLOAT) || force)){
  1908. float* ptr = (float*)GetStructPointer(data_struct);
  1909. return ptr[index];
  1910. }
  1911. return 0;
  1912. }
  1913. double PacketStruct::getType_double(DataStruct* data_struct, int32 index, bool force){
  1914. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_DOUBLE) || force)){
  1915. double* ptr = (double*)GetStructPointer(data_struct);
  1916. return ptr[index];
  1917. }
  1918. return 0;
  1919. }
  1920. char PacketStruct::getType_char(DataStruct* data_struct, int32 index, bool force){
  1921. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_CHAR) || force)){
  1922. char* ptr = (char*)GetStructPointer(data_struct);
  1923. return ptr[index];
  1924. }
  1925. return 0;
  1926. }
  1927. EQ2_8BitString PacketStruct::getType_EQ2_8BitString(DataStruct* data_struct, int32 index, bool force){
  1928. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_8BIT_STRING) || force)){
  1929. EQ2_8BitString* ptr = (EQ2_8BitString*)GetStructPointer(data_struct);
  1930. return ptr[index];
  1931. }
  1932. EQ2_8BitString ret;
  1933. ret.size = 0;
  1934. return ret;
  1935. }
  1936. EQ2_16BitString PacketStruct::getType_EQ2_16BitString(DataStruct* data_struct, int32 index, bool force){
  1937. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_16BIT_STRING) || force)){
  1938. EQ2_16BitString* ptr = (EQ2_16BitString*)GetStructPointer(data_struct);
  1939. return ptr[index];
  1940. }
  1941. EQ2_16BitString ret;
  1942. ret.size = 0;
  1943. return ret;
  1944. }
  1945. EQ2_32BitString PacketStruct::getType_EQ2_32BitString(DataStruct* data_struct, int32 index, bool force){
  1946. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_32BIT_STRING) || force)){
  1947. EQ2_32BitString* ptr = (EQ2_32BitString*)GetStructPointer(data_struct);
  1948. return ptr[index];
  1949. }
  1950. EQ2_32BitString ret;
  1951. ret.size = 0;
  1952. return ret;
  1953. }
  1954. EQ2_Color PacketStruct::getType_EQ2_Color(DataStruct* data_struct, int32 index, bool force){
  1955. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_COLOR) || force)){
  1956. EQ2_Color* ptr = (EQ2_Color*)GetStructPointer(data_struct);
  1957. return ptr[index];
  1958. }
  1959. EQ2_Color ret;
  1960. ret.blue = 0;
  1961. ret.red = 0;
  1962. ret.green = 0;
  1963. return ret;
  1964. }
  1965. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem(DataStruct* data_struct, int32 index, bool force){
  1966. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQUIPMENT) || force)){
  1967. EQ2_EquipmentItem* ptr = (EQ2_EquipmentItem*)GetStructPointer(data_struct);
  1968. return ptr[index];
  1969. }
  1970. EQ2_EquipmentItem ret;
  1971. ret.color.blue = 0;
  1972. ret.color.red = 0;
  1973. ret.color.green = 0;
  1974. ret.highlight.blue = 0;
  1975. ret.highlight.red = 0;
  1976. ret.highlight.green = 0;
  1977. ret.type = 0;
  1978. return ret;
  1979. }
  1980. bool PacketStruct::SetOpcode(const char* new_opcode){
  1981. opcode = EQOpcodeManager[0]->NameSearch(new_opcode);
  1982. if(opcode == OP_Unknown){
  1983. #ifndef MINILOGIN
  1984. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode named '%s', this struct cannot be serialized directly.", GetName(), new_opcode);
  1985. #endif
  1986. return false;
  1987. }
  1988. return true;
  1989. }
  1990. EQ2Packet* PacketStruct::serialize(){
  1991. serializePacket();
  1992. if(GetOpcode() != OP_Unknown)
  1993. return new EQ2Packet(GetOpcode(), getData(), getDataSize());
  1994. else{
  1995. #ifndef MINILOGIN
  1996. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode and cannot be serialized directly.", GetName());
  1997. #endif
  1998. return 0;
  1999. }
  2000. }
  2001. EQ2Packet* PacketStruct::serializeCountPacket(int16 version, int8 offset, uchar* orig_packet, uchar* xor_packet){
  2002. string* packet_data = serializeString();
  2003. uchar* data = (uchar*)packet_data->c_str();
  2004. int32 size = packet_data->size();
  2005. uchar* packed_data = new uchar[size + 20];
  2006. memset(packed_data, 0, size + 20);
  2007. if(orig_packet && xor_packet){
  2008. memcpy(xor_packet, data + 6, size - 6 - offset);
  2009. Encode(xor_packet, orig_packet, size - 6 - offset);
  2010. size = Pack(packed_data, xor_packet, size - 6 - offset, size+20, version);
  2011. }
  2012. else
  2013. size = Pack(packed_data, data + 6, packet_data->size()-6 - offset, packet_data->size()+20, version);
  2014. uchar* combined = new uchar[size + sizeof(int16)+offset];
  2015. memset(combined, 0, size + sizeof(int16)+offset);
  2016. uchar* ptr = combined;
  2017. memcpy(ptr, data, sizeof(int16));
  2018. ptr += sizeof(int16);
  2019. memcpy(ptr, packed_data, size);
  2020. if(offset > 0){
  2021. ptr += size;
  2022. uchar* ptr2 = data;
  2023. ptr2 += packet_data->size() - offset;
  2024. memcpy(ptr, ptr2, offset);
  2025. }
  2026. EQ2Packet* app = new EQ2Packet(GetOpcode(), combined, size + sizeof(int16)+offset);
  2027. safe_delete_array(packed_data);
  2028. safe_delete_array(combined);
  2029. return app;
  2030. }
  2031. bool PacketStruct::IsSubPacket(){
  2032. return sub_packet;
  2033. }
  2034. void PacketStruct::IsSubPacket(bool new_val){
  2035. sub_packet = new_val;
  2036. }
  2037. int32 PacketStruct::GetSubPacketSize(){
  2038. return sub_packet_size;
  2039. }
  2040. void PacketStruct::SetSubPacketSize(int32 new_size){
  2041. sub_packet_size = new_size;
  2042. }
  2043. void* PacketStruct::GetStructPointer(DataStruct* data_struct, bool erase){
  2044. try{
  2045. void* tmp = struct_data[data_struct];
  2046. if(tmp != 0){
  2047. if(erase)
  2048. struct_data.erase(data_struct);
  2049. return tmp;
  2050. }
  2051. else{
  2052. PacketStruct* packet = 0;
  2053. vector<PacketStruct*>::iterator itr2;
  2054. for(itr2=arrays.begin();itr2!=arrays.end();itr2++) {
  2055. packet = *itr2;
  2056. if(packet){
  2057. tmp = packet->GetStructPointer(data_struct, erase);
  2058. if( tmp != 0)
  2059. return tmp;
  2060. }
  2061. }
  2062. }
  2063. }
  2064. catch(...){
  2065. cout << "Caught Exception...\n";
  2066. }
  2067. return 0;
  2068. }
  2069. vector<DataStruct*> PacketStruct::GetDataStructs(){
  2070. vector<DataStruct*> ret;
  2071. DataStruct* ds = 0;
  2072. vector<DataStruct*>::iterator itr;
  2073. for(itr=structs.begin();itr!=structs.end();itr++){
  2074. ds = *itr;
  2075. if(ds->GetType() == DATA_STRUCT_ARRAY){
  2076. int32 size = GetArraySize(ds,0);
  2077. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2078. if(ps && ps->GetSubPacketSize() != size){
  2079. ps->reAddAll(size);
  2080. }
  2081. if(ps){
  2082. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2083. vector<DataStruct*>::iterator itr2;
  2084. for(itr2=ret2.begin();itr2!=ret2.end();itr2++){
  2085. ret.push_back(*itr2);
  2086. }
  2087. }
  2088. }
  2089. else if(ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY){
  2090. int32 size = GetArraySize(ds,0);
  2091. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2092. if(ps && ps->GetSubPacketSize() != size){
  2093. ps->reAddAll(size);
  2094. }
  2095. if(ps){
  2096. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2097. vector<DataStruct*>::iterator itr2;
  2098. for(itr2=ret2.begin();itr2!=ret2.end();itr2++){
  2099. ret.push_back(*itr2);
  2100. }
  2101. }
  2102. }
  2103. else
  2104. ret.push_back(ds);
  2105. }
  2106. return ret;
  2107. }
  2108. void PacketStruct::PrintPacket(){
  2109. DataStruct* ds = 0;
  2110. vector<DataStruct*>::iterator itr;
  2111. for(itr=structs.begin();itr!=structs.end();itr++){
  2112. ds = *itr;
  2113. if(!ds->AddToStruct())
  2114. continue;
  2115. for(int16 i=0;i<ds->GetLength();i++){
  2116. cout << "Name: " << ds->GetName() << " \tIndex: " << i << " \tType: ";
  2117. switch(ds->GetType()){
  2118. case DATA_STRUCT_INT8:
  2119. printf("int8\t\tData: %i", getType_int8_ByName(ds->GetName(), i));
  2120. break;
  2121. case DATA_STRUCT_INT16:
  2122. printf("int16\t\tData: %i", getType_int16_ByName(ds->GetName(), i));
  2123. break;
  2124. case DATA_STRUCT_INT32:
  2125. printf("int32\t\tData: %u", getType_int32_ByName(ds->GetName(), i));
  2126. break;
  2127. case DATA_STRUCT_INT64:
  2128. printf("int64\t\tData: %llu", getType_int64_ByName(ds->GetName(), i));
  2129. break;
  2130. case DATA_STRUCT_SINT8:
  2131. printf("sint8\t\tData: %i", getType_sint8_ByName(ds->GetName(), i));
  2132. break;
  2133. case DATA_STRUCT_SINT16:
  2134. printf("sint16\t\tData: %i", getType_sint16_ByName(ds->GetName(), i));
  2135. break;
  2136. case DATA_STRUCT_SINT32:
  2137. printf("sint32\t\tData: %i", getType_sint32_ByName(ds->GetName(), i));
  2138. break;
  2139. case DATA_STRUCT_SINT64:
  2140. printf("sint64\t\tData: %lli", getType_sint64_ByName(ds->GetName(), i));
  2141. break;
  2142. case DATA_STRUCT_CHAR:
  2143. printf("char\t\tData: %c", getType_char_ByName(ds->GetName(), i));
  2144. break;
  2145. case DATA_STRUCT_FLOAT:
  2146. printf("float\t\tData: %f", getType_float_ByName(ds->GetName(), i));
  2147. break;
  2148. case DATA_STRUCT_DOUBLE:
  2149. printf("double\t\tData: %f", getType_double_ByName(ds->GetName(), i));
  2150. break;
  2151. case DATA_STRUCT_EQ2_8BIT_STRING:
  2152. printf("EQ2_8BitString\tData: %s", getType_EQ2_8BitString_ByName(ds->GetName(), i).data.c_str());
  2153. break;
  2154. case DATA_STRUCT_EQ2_16BIT_STRING:
  2155. printf("EQ2_16BitString\tData: %s", getType_EQ2_16BitString_ByName(ds->GetName(), i).data.c_str());
  2156. break;
  2157. case DATA_STRUCT_EQ2_32BIT_STRING:{
  2158. printf("EQ2_32BitString\tData: %s", getType_EQ2_32BitString_ByName(ds->GetName(), i).data.c_str());
  2159. break;
  2160. }
  2161. case DATA_STRUCT_ARRAY:{
  2162. int32 size = GetArraySize(ds,0);
  2163. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2164. if(ps && ps->GetSubPacketSize() != size){
  2165. ps->reAddAll(size);
  2166. }
  2167. if(ps){
  2168. cout << "Array:\tData: \n";
  2169. ps->PrintPacket();
  2170. }
  2171. break;
  2172. }
  2173. case DATA_STRUCT_COLOR:{
  2174. cout.unsetf( ios_base::dec );
  2175. cout.setf( ios_base::hex );
  2176. printf("EQ2_Color\tData: ");
  2177. EQ2_Color tmp = getType_EQ2_Color_ByName(ds->GetName(), i);
  2178. printf("R: %i", tmp.red);
  2179. printf(", G: %i", tmp.green);
  2180. printf(", B: %i", tmp.blue);
  2181. break;
  2182. }
  2183. case DATA_STRUCT_EQUIPMENT:{
  2184. cout.unsetf( ios_base::dec );
  2185. cout.setf( ios_base::hex );
  2186. printf("EQ2_EquipmentItem\tData: ");
  2187. EQ2_EquipmentItem tmp = getType_EQ2_EquipmentItem_ByName(ds->GetName(), i);
  2188. printf("type: ");
  2189. printf(" ,color R: %i", tmp.color.red);
  2190. printf(" ,color G: %i", tmp.color.green);
  2191. printf(" ,color B: %i", tmp.color.blue);
  2192. printf(" ,hl R: %i", tmp.highlight.red);
  2193. printf(" ,hl G: %i", tmp.highlight.green);
  2194. printf(" ,hl B: %i", tmp.highlight.blue);
  2195. break;
  2196. }
  2197. }
  2198. cout << endl;
  2199. }
  2200. if(ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY){
  2201. int32 size = GetArraySize(ds,0);
  2202. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2203. if(ps && ps->GetSubPacketSize() != size){
  2204. ps->reAddAll(size);
  2205. }
  2206. if(ps){
  2207. cout << "Array:\tData: \n";
  2208. ps->PrintPacket();
  2209. }
  2210. }
  2211. }
  2212. }
  2213. void PacketStruct::LoadFromPacketStruct(PacketStruct* packet, char* substruct_name){
  2214. vector<DataStruct*>::iterator itr;
  2215. DataStruct* ds = 0;
  2216. char name[512];
  2217. //scatman (1/30/2012): these declarations are here to get rid of a linux compile error "taking address of temporary"
  2218. EQ2_8BitString str8;
  2219. EQ2_16BitString str16;
  2220. EQ2_32BitString str32;
  2221. EQ2_EquipmentItem equip;
  2222. for(itr=structs.begin();itr!=structs.end();itr++){
  2223. ds = *itr;
  2224. for(int16 i=0;i<ds->GetLength();i++){
  2225. memset(name, 0, sizeof(name));
  2226. if(substruct_name)
  2227. snprintf(name, sizeof(name), "%s_%s_0", substruct_name, ds->GetName());
  2228. else
  2229. strncpy(name, ds->GetName(), sizeof(name));
  2230. name[sizeof(name) - 1] = '\0';
  2231. switch(ds->GetType()){
  2232. case DATA_STRUCT_INT8:
  2233. setData(ds, packet->getType_int8_ByName(name, i), i);
  2234. break;
  2235. case DATA_STRUCT_SINT8:
  2236. setData(ds, packet->getType_sint8_ByName(name, i), i);
  2237. break;
  2238. case DATA_STRUCT_CHAR:
  2239. setData(ds, packet->getType_char_ByName(name, i), i);
  2240. break;
  2241. case DATA_STRUCT_SINT16:
  2242. setData(ds, packet->getType_sint16_ByName(name, i), i);
  2243. break;
  2244. case DATA_STRUCT_INT16:
  2245. setData(ds, packet->getType_int16_ByName(name, i), i);
  2246. break;
  2247. case DATA_STRUCT_INT32:
  2248. setData(ds, packet->getType_int32_ByName(name, i), i);
  2249. break;
  2250. case DATA_STRUCT_INT64:
  2251. setData(ds, packet->getType_int64_ByName(name, i), i);
  2252. break;
  2253. case DATA_STRUCT_SINT32:
  2254. setData(ds, packet->getType_sint32_ByName(name, i), i);
  2255. break;
  2256. case DATA_STRUCT_SINT64:
  2257. setData(ds, packet->getType_sint64_ByName(name, i), i);
  2258. break;
  2259. case DATA_STRUCT_FLOAT:
  2260. setData(ds, packet->getType_float_ByName(name, i), i);
  2261. break;
  2262. case DATA_STRUCT_DOUBLE:
  2263. setData(ds, packet->getType_double_ByName(name, i), i);
  2264. break;
  2265. case DATA_STRUCT_EQ2_8BIT_STRING:
  2266. str8 = packet->getType_EQ2_8BitString_ByName(name, i);
  2267. setData(ds, &str8, i);
  2268. break;
  2269. case DATA_STRUCT_EQ2_16BIT_STRING:
  2270. str16 = packet->getType_EQ2_16BitString_ByName(name, i);
  2271. setData(ds, &str16, i);
  2272. break;
  2273. case DATA_STRUCT_EQ2_32BIT_STRING:
  2274. str32 = packet->getType_EQ2_32BitString_ByName(name, i);
  2275. setData(ds, &str32, i);
  2276. break;
  2277. case DATA_STRUCT_ARRAY:{
  2278. int32 size = GetArraySize(ds, 0);
  2279. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2280. if(ps && size > 0)
  2281. ps->LoadFromPacketStruct(packet, substruct_name);
  2282. break;
  2283. }
  2284. case DATA_STRUCT_COLOR:
  2285. setColor(ds, packet->getType_EQ2_Color_ByName(name, i), i);
  2286. break;
  2287. case DATA_STRUCT_EQUIPMENT:
  2288. equip = packet->getType_EQ2_EquipmentItem_ByName(name, i);
  2289. setEquipmentByName(ds->GetName(), &equip, i);
  2290. break;
  2291. default:
  2292. break;
  2293. }
  2294. }
  2295. }
  2296. }
  2297. #ifdef WORLD
  2298. void PacketStruct::setItem(DataStruct* ds, Item* item, Player* player, int32 index, sint8 offset){
  2299. if(!ds)
  2300. return;
  2301. uchar* ptr = (uchar*)GetStructPointer(ds);
  2302. PacketStruct* packet = item->PrepareItem(client_version);
  2303. if(packet){
  2304. int16 item_version = GetItemPacketType(packet->GetVersion());
  2305. item->serialize(packet, true, player, item_version);
  2306. string* generic_string_data = packet->serializeString();
  2307. int32 size = generic_string_data->length();
  2308. if(size <= 13)
  2309. return;
  2310. size -= (9+offset);
  2311. if(item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2))
  2312. size = (size*2)-5;
  2313. uchar* out_data = new uchar[size+1];
  2314. uchar* out_ptr = out_data;
  2315. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (9 + offset), generic_string_data->length() - (9 + offset));
  2316. //without these it will prompt for your character name
  2317. if(offset == 0 || offset == -1 || offset == 2){
  2318. out_data[0] = 1;
  2319. }
  2320. //
  2321. out_ptr += generic_string_data->length() - (10 + offset);
  2322. if(item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2)){
  2323. out_data[4] = 0x80;
  2324. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (13 + offset), generic_string_data->length() -(13+offset));
  2325. }
  2326. ds->SetItemSize(size);
  2327. //DumpPacket(out_data, size);
  2328. memcpy(ptr, out_data, size);
  2329. safe_delete_array(out_data);
  2330. delete packet;
  2331. }
  2332. //DumpPacket(ptr2, ds->GetItemSize());
  2333. }
  2334. void PacketStruct::setItemByName(const char* name, Item* item, Player* player, int32 index, sint8 offset){
  2335. setItem(findStruct(name, index), item, player, index, offset);
  2336. }
  2337. void PacketStruct::setItemArrayDataByName(const char* name, Item* item, Player* player, int32 index1, int32 index2, sint8 offset){
  2338. char tmp[10] = {0};
  2339. sprintf(tmp,"_%i",index1);
  2340. string name2 = string(name).append(tmp);
  2341. setItem(findStruct(name2.c_str(), index1, index2), item, player, index2, offset);
  2342. }
  2343. void PacketStruct::ResetData() {
  2344. vector<DataStruct*>::iterator itr;
  2345. for (itr = structs.begin(); itr != structs.end(); itr++){
  2346. DataStruct* ds = *itr;
  2347. void* ptr = GetStructPointer(ds);
  2348. if (!ptr)
  2349. continue;
  2350. switch (ds->GetType())
  2351. {
  2352. case DATA_STRUCT_EQ2_8BIT_STRING:{
  2353. EQ2_8BitString* real_ptr = (EQ2_8BitString*)ptr;
  2354. real_ptr->size = 0;
  2355. real_ptr->data.clear();
  2356. break;
  2357. }
  2358. case DATA_STRUCT_EQ2_16BIT_STRING:{
  2359. EQ2_16BitString* real_ptr = (EQ2_16BitString*)ptr;
  2360. real_ptr->size = 0;
  2361. real_ptr->data.clear();
  2362. break;
  2363. }
  2364. case DATA_STRUCT_EQ2_32BIT_STRING:{
  2365. EQ2_32BitString* real_ptr = (EQ2_32BitString*)ptr;
  2366. real_ptr->size = 0;
  2367. real_ptr->data.clear();
  2368. break;
  2369. }
  2370. case DATA_STRUCT_EQUIPMENT:{
  2371. memset(ptr, 0, sizeof(EQ2_EquipmentItem) * ds->GetLength());
  2372. break;
  2373. }
  2374. case DATA_STRUCT_DOUBLE:{
  2375. memset(ptr, 0, sizeof(double) * ds->GetLength());
  2376. break;
  2377. }
  2378. case DATA_STRUCT_FLOAT:{
  2379. memset(ptr, 0, sizeof(float) * ds->GetLength());
  2380. break;
  2381. }
  2382. case DATA_STRUCT_INT8:{
  2383. memset(ptr, 0, sizeof(int8) * ds->GetLength());
  2384. break;
  2385. }
  2386. case DATA_STRUCT_INT16:{
  2387. memset(ptr, 0, sizeof(int16) * ds->GetLength());
  2388. break;
  2389. }
  2390. case DATA_STRUCT_INT32:{
  2391. memset(ptr, 0, sizeof(int32) * ds->GetLength());
  2392. break;
  2393. }
  2394. case DATA_STRUCT_INT64:{
  2395. memset(ptr, 0, sizeof(int64) * ds->GetLength());
  2396. break;
  2397. }
  2398. case DATA_STRUCT_SINT8:{
  2399. memset(ptr, 0, sizeof(sint8) * ds->GetLength());
  2400. break;
  2401. }
  2402. case DATA_STRUCT_SINT16:{
  2403. memset(ptr, 0, sizeof(sint16) * ds->GetLength());
  2404. break;
  2405. }
  2406. case DATA_STRUCT_SINT32:{
  2407. memset(ptr, 0, sizeof(sint32) * ds->GetLength());
  2408. break;
  2409. }
  2410. case DATA_STRUCT_SINT64:{
  2411. memset(ptr, 0, sizeof(sint64) * ds->GetLength());
  2412. break;
  2413. }
  2414. case DATA_STRUCT_ITEM:{
  2415. memset(ptr, 0, 10000 * ds->GetLength());
  2416. break;
  2417. }
  2418. case DATA_STRUCT_CHAR:{
  2419. memset(ptr, 0, sizeof(char) * ds->GetLength());
  2420. break;
  2421. }
  2422. case DATA_STRUCT_COLOR:{
  2423. memset(ptr, 0, sizeof(EQ2_Color) * ds->GetLength());
  2424. break;
  2425. }
  2426. }
  2427. }
  2428. vector<PacketStruct*>::iterator itr2;
  2429. for (itr2 = arrays.begin(); itr2 != arrays.end(); itr2++)
  2430. (*itr2)->ResetData();
  2431. }
  2432. #endif