PacketStruct.cpp 80 KB

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