PacketStruct.cpp 84 KB

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