9
3

PacketStruct.cpp 76 KB

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