PacketStruct.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  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, bool create_color) {
  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, create_color))
  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, bool create_color) {
  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(true);
  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. // lets not do this again, DoF behaves differently than AoM, DoF is not compatible with CreateEQ2Color
  1375. //if (strcmp(GetName(), "CreateCharacter") == 0 || strcmp(GetName(), "WS_SubmitCharCust") == 0)
  1376. if(create_color)
  1377. CreateEQ2Color((EQ2_Color*)data);
  1378. else
  1379. LoadData((EQ2_Color*)data, len);
  1380. break;
  1381. }
  1382. case DATA_STRUCT_EQUIPMENT: {
  1383. LoadData((EQ2_EquipmentItem*)data);
  1384. break;
  1385. }
  1386. case DATA_STRUCT_ARRAY: {
  1387. int32 size = GetArraySize(data_struct, 0);
  1388. if (size > 0xFFFF || size > GetLoadLen()-GetLoadPos()) {
  1389. LogWrite(PACKET__WARNING, 1, "Packet", "Possible corrupt packet while loading struct array, orig array size: %u in struct name %s, data name %s, load_len %u, load_pos %u", size, GetName(), (data_struct && data_struct->GetName()) ? data_struct->GetName() : "??", GetLoadLen(), GetLoadPos());
  1390. return false;
  1391. }
  1392. PacketStruct* ps = GetPacketStructByName(data_struct->GetName());
  1393. if (ps && ps->GetSubPacketSize() != size) {
  1394. if (data_struct->GetMaxArraySize() > 0 && size > data_struct->GetMaxArraySize())
  1395. size = data_struct->GetMaxArraySize();
  1396. ps->reAddAll(size);
  1397. }
  1398. if (ps && size > 0) {
  1399. //for(int i=0;i<size && (GetLoadLen()-GetLoadPos()) > 0;i++){
  1400. if(ps->LoadPacketData(GetLoadBuffer() + GetLoadPos(), GetLoadLen() - GetLoadPos(), create_color)) {
  1401. SetLoadPos(GetLoadPos() + ps->GetLoadPos());
  1402. }
  1403. //}
  1404. }
  1405. break;
  1406. }
  1407. default: {
  1408. data_struct->SetIsSet(false);
  1409. }
  1410. }
  1411. return true;
  1412. }
  1413. PacketStruct* PacketStruct::GetPacketStructByName(const char* name) {
  1414. PacketStruct* ps = 0;
  1415. vector<PacketStruct*>::iterator itr;
  1416. for (itr = arrays.begin(); itr != arrays.end(); itr++) {
  1417. ps = *itr;
  1418. if (strcmp(ps->GetName(), name) == 0)
  1419. return ps;
  1420. ps = ps->GetPacketStructByName(name);
  1421. if (ps)
  1422. return ps;
  1423. }
  1424. return 0;
  1425. }
  1426. void PacketStruct::reAddAll(int32 length) {
  1427. vector<DataStruct*>::iterator itr;
  1428. DataStruct* ds = 0;
  1429. PacketStruct* ps = 0;
  1430. vector<PacketStruct*>::iterator packet_itr;
  1431. if (orig_structs.size() == 0)
  1432. orig_structs = structs;
  1433. else
  1434. deleteDataStructs(&structs);
  1435. structs.clear();
  1436. if (orig_packets.size() == 0 && arrays.size() > 0)
  1437. orig_packets = arrays;
  1438. else {
  1439. for (packet_itr = arrays.begin(); packet_itr != arrays.end(); packet_itr++) {
  1440. ps = *packet_itr;
  1441. safe_delete(ps);
  1442. }
  1443. }
  1444. arrays.clear();
  1445. for (int16 i = 0; i < length; i++) {
  1446. for (packet_itr = orig_packets.begin(); packet_itr != orig_packets.end(); packet_itr++) {
  1447. ps = *packet_itr;
  1448. PacketStruct* new_packet = new PacketStruct(ps, true);
  1449. char tmp[20] = { 0 };
  1450. sprintf(tmp, "_%i", i);
  1451. string name = string(new_packet->GetName());
  1452. name.append(tmp);
  1453. new_packet->SetName(name.c_str());
  1454. add(new_packet);
  1455. }
  1456. for (itr = orig_structs.begin(); itr != orig_structs.end(); itr++) {
  1457. ds = *itr;
  1458. DataStruct* new_data = new DataStruct(ds);
  1459. char tmp[20] = { 0 };
  1460. sprintf(tmp, "_%i", i);
  1461. string name = new_data->GetStringName();
  1462. if (IsSubPacket() && parent->IsSubPacket()) {
  1463. string parent_name = string(GetName());
  1464. try {
  1465. if (parent_name.rfind("_") < 0xFFFFFFFF)
  1466. sprintf(tmp, "%i_%i", atoi(parent_name.substr(parent_name.rfind("_") + 1).c_str()), i);
  1467. }
  1468. catch (...) {
  1469. sprintf(tmp, "_%i", i);
  1470. }
  1471. }
  1472. name.append(tmp);
  1473. new_data->SetName(name.c_str());
  1474. if (new_data->GetType() == DATA_STRUCT_ARRAY) {
  1475. string old_size_arr = string(new_data->GetArraySizeVariable());
  1476. new_data->SetArraySizeVariable(old_size_arr.append(tmp).c_str());
  1477. }
  1478. add(new_data);
  1479. }
  1480. }
  1481. sub_packet_size = length;
  1482. }
  1483. int32 PacketStruct::GetArraySize(DataStruct* data_struct, int32 index) {
  1484. if (data_struct) {
  1485. const char* name = data_struct->GetArraySizeVariable();
  1486. return GetArraySize(name, index);
  1487. }
  1488. return 0;
  1489. }
  1490. int32 PacketStruct::GetArraySize(const char* name, int32 index) {
  1491. int32 ret = 0;
  1492. DataStruct* ds = findStruct(name, index);
  1493. if (ds) {
  1494. if (ds->GetType() == DATA_STRUCT_INT8) {
  1495. int8* tmp = (int8*)GetStructPointer(ds);
  1496. ret = *tmp;
  1497. }
  1498. else if (ds->GetType() == DATA_STRUCT_INT16) {
  1499. int16* tmp = (int16*)GetStructPointer(ds);
  1500. ret = *tmp;
  1501. }
  1502. else if (ds->GetType() == DATA_STRUCT_INT32) {
  1503. int32* tmp = (int32*)GetStructPointer(ds);
  1504. ret = *tmp;
  1505. }
  1506. else if (ds->GetType() == DATA_STRUCT_INT64) {
  1507. int64* tmp = (int64*)GetStructPointer(ds);
  1508. ret = *tmp;
  1509. }
  1510. }
  1511. return ret;
  1512. }
  1513. void PacketStruct::UpdateArrayByArrayLength(DataStruct* data_struct, int32 index, int32 size) {
  1514. if (data_struct) {
  1515. PacketStruct* packet = 0;
  1516. DataStruct* data = 0;
  1517. vector<DataStruct*>::iterator itr;
  1518. for (itr = structs.begin(); itr != structs.end(); itr++) {
  1519. data = *itr;
  1520. if (strcmp(data->GetArraySizeVariable(), data_struct->GetName()) == 0) {
  1521. packet = GetPacketStructByName(data->GetName());
  1522. if (packet)
  1523. packet->reAddAll(size);
  1524. return;
  1525. }
  1526. }
  1527. vector<PacketStruct*>::iterator itr2;
  1528. for (itr2 = arrays.begin(); itr2 != arrays.end(); itr2++) {
  1529. packet = *itr2;
  1530. packet->UpdateArrayByArrayLength(data_struct, index, size);
  1531. }
  1532. }
  1533. }
  1534. void PacketStruct::UpdateArrayByArrayLengthName(const char* name, int32 index, int32 size) {
  1535. UpdateArrayByArrayLength(findStruct(name, index), index, size);
  1536. }
  1537. int32 PacketStruct::GetArraySizeByName(const char* name, int32 index) {
  1538. DataStruct* ds1 = findStruct(name, index);
  1539. return GetArraySize(ds1, index);
  1540. }
  1541. int16 PacketStruct::GetOpcodeValue(int16 client_version) {
  1542. int16 opcode = 0xFFFF;
  1543. bool client_cmd = false;
  1544. int16 OpcodeVersion = 0;
  1545. #ifndef LOGIN
  1546. if (GetOpcode() == OP_ClientCmdMsg && strlen(GetOpcodeType()) > 0 && !IsSubPacket())
  1547. client_cmd = true;
  1548. #endif
  1549. if (client_cmd) {
  1550. EmuOpcode sub_opcode = EQOpcodeManager[0]->NameSearch(GetOpcodeType());
  1551. if (sub_opcode != OP_Unknown) { //numbers should be used at OpcodeTypes, define them!
  1552. OpcodeVersion = GetOpcodeVersion(client_version);
  1553. if (EQOpcodeManager.count(OpcodeVersion) > 0) {
  1554. opcode = EQOpcodeManager[OpcodeVersion]->EmuToEQ(sub_opcode);
  1555. if (opcode == 0xCDCD) {
  1556. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find valid opcode for opcode: %s and client_version: %i", EQOpcodeManager[OpcodeVersion]->EmuToName(sub_opcode), client_version);
  1557. }
  1558. }
  1559. }
  1560. }
  1561. else {
  1562. OpcodeVersion = GetOpcodeVersion(client_version);
  1563. if (EQOpcodeManager.count(OpcodeVersion) > 0) {
  1564. opcode = EQOpcodeManager[OpcodeVersion]->EmuToEQ(GetOpcode());
  1565. if (opcode == 0xCDCD) {
  1566. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find valid opcode for opcode: %s and client_version: %i", EQOpcodeManager[OpcodeVersion]->EmuToName(GetOpcode()), client_version);
  1567. }
  1568. }
  1569. }
  1570. #ifndef LOGIN
  1571. if(opcode == 0)
  1572. opcode = 0xFFFF;
  1573. #endif
  1574. return opcode;
  1575. }
  1576. void PacketStruct::serializePacket(bool clear) {
  1577. if (clear)
  1578. Clear();
  1579. bool client_cmd = false;
  1580. string client_data;
  1581. #ifndef LOGIN
  1582. if (GetOpcode() == OP_ClientCmdMsg && strlen(GetOpcodeType()) > 0 && !IsSubPacket())
  1583. client_cmd = true;
  1584. #endif
  1585. DataStruct* data = 0;
  1586. vector<DataStruct*>::iterator itr;
  1587. for (itr = structs.begin(); itr != structs.end(); itr++) {
  1588. data = *itr;
  1589. if (data->IsOptional())//this would be false if the datastruct WAS optional, but was set
  1590. continue;
  1591. if (data->AddToStruct()) {
  1592. if (data->GetIfFlagNotSet() && CheckFlagExists(data->GetIfFlagNotSetVariable()))
  1593. continue;
  1594. if (data->GetIfFlagSet() && !CheckFlagExists(data->GetIfFlagSetVariable()))
  1595. continue;
  1596. if (data->GetIfSet() && data->GetIfSetVariable()) {
  1597. string varname = string(data->GetIfSetVariable());
  1598. if (varname.find(",") < 0xFFFFFFFF) {
  1599. vector<string>* varnames = SplitString(varname, ',');
  1600. if (varnames) {
  1601. bool should_continue = true;
  1602. for (int32 i = 0; i < varnames->size(); i++) {
  1603. if (GetVariableIsSet(varnames->at(i).c_str())) {
  1604. should_continue = false;
  1605. break;
  1606. }
  1607. }
  1608. safe_delete(varnames);
  1609. if (should_continue)
  1610. continue;
  1611. }
  1612. }
  1613. else {
  1614. if (!GetVariableIsSet(varname.c_str()))
  1615. continue;
  1616. }
  1617. }
  1618. if (data->GetIfNotSet() && data->GetIfNotSetVariable()) {
  1619. string varname = string(data->GetIfNotSetVariable());
  1620. if (varname.find(",") < 0xFFFFFFFF) {
  1621. vector<string>* varnames = SplitString(varname, ',');
  1622. if (varnames) {
  1623. bool should_continue = false;
  1624. for (int32 i = 0; i < varnames->size(); i++) {
  1625. if (!GetVariableIsNotSet(varnames->at(i).c_str())) {
  1626. should_continue = true;
  1627. break;
  1628. }
  1629. }
  1630. safe_delete(varnames);
  1631. if (should_continue)
  1632. continue;
  1633. }
  1634. }
  1635. else {
  1636. // Check to see if the variable contains %i, if it does assume we are in an array
  1637. // and get the current index from the end of the data struct's name
  1638. char name[250] = { 0 };
  1639. if (varname.find("%i") < 0xFFFFFFFF) {
  1640. vector<string>* varnames = SplitString(varname, '_');
  1641. vector<string>* indexes = SplitString(data->GetName(), '_');
  1642. int index = 0;
  1643. if (indexes->size() > 0)
  1644. index = atoi(indexes->at(indexes->size() - 1).c_str());
  1645. sprintf(name, varname.c_str(), index);
  1646. }
  1647. else
  1648. strcpy(name, varname.c_str());
  1649. if (!GetVariableIsNotSet(name))
  1650. continue;
  1651. }
  1652. }
  1653. // Quick implementaion of IfVariableNotEquals
  1654. // probably not what it was intended for as it currently just checks to see if the given variable equals 1
  1655. // should probably change it so you can define what the variable should or shouldn't equal
  1656. //
  1657. // ie: IfVariableNotEquals="stat_type_%i=1"
  1658. // would be a check to make sure that stat_type_%i does not equal 1 and if it does exclude this element
  1659. if (data->GetIfNotEquals() && data->GetIfNotEqualsVariable()) {
  1660. // Get the variable name
  1661. string varname = string(data->GetIfNotEqualsVariable());
  1662. char name[250] = { 0 };
  1663. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1664. // index and replace it
  1665. if (varname.find("%i") < 0xFFFFFFFF) {
  1666. // Get the current index by getting the number at the end of the name
  1667. vector<string>* varnames = SplitString(data->GetName(), '_');
  1668. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1669. string substr = "stat_type";
  1670. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1671. // adorn_stat_subtype 18 chars
  1672. string temp = varname.substr(12);
  1673. char temp2[20] = { 0 };
  1674. int index2 = atoi(temp.c_str());
  1675. itoa(index2, temp2, 10);
  1676. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1677. }
  1678. sprintf(name, varname.c_str(), index);
  1679. safe_delete(varnames);
  1680. }
  1681. else
  1682. strcpy(name, varname.c_str());
  1683. // Get the data for the variable
  1684. int16 value = 0;
  1685. DataStruct* data_struct2 = findStruct(name, 0);
  1686. value = getType_int16(data_struct2);
  1687. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1688. if (value == 1)
  1689. continue;
  1690. }
  1691. // copy and paste of the code above for IfEquals
  1692. if (data->GetIfEquals() && data->GetIfEqualsVariable()) {
  1693. // Get the variable name
  1694. string varname = string(data->GetIfEqualsVariable());
  1695. char name[250] = { 0 };
  1696. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1697. // index and replace it
  1698. if (varname.find("%i") < 0xFFFFFFFF) {
  1699. // Get the current index by getting the number at the end of the name
  1700. vector<string>* varnames = SplitString(data->GetName(), '_');
  1701. int index = 0;
  1702. if (varnames)
  1703. index = atoi(varnames->at(varnames->size() - 1).c_str());
  1704. string substr = "stat_type";
  1705. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1706. // adorn_stat_subtype 18 chars
  1707. string temp = varname.substr(12);
  1708. char temp2[20] = { 0 };
  1709. int index2 = atoi(temp.c_str());
  1710. itoa(index2, temp2, 10);
  1711. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1712. }
  1713. sprintf(name, varname.c_str(), index);
  1714. safe_delete(varnames);
  1715. }
  1716. else
  1717. strcpy(name, varname.c_str());
  1718. // Get the data for the variable
  1719. int16 value = 0;
  1720. DataStruct* data_struct2 = findStruct(name, 0);
  1721. value = getType_int16(data_struct2);
  1722. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1723. if (value != 1)
  1724. continue;
  1725. }
  1726. if (client_cmd)
  1727. AddSerializedData(data, 0, &client_data);
  1728. else
  1729. AddSerializedData(data);
  1730. }
  1731. }
  1732. #ifndef LOGIN
  1733. if (client_cmd) {
  1734. int16 opcode_val = GetOpcodeValue(client_version);
  1735. Clear();
  1736. int32 size = client_data.length() + 3; //gotta add the opcode and oversized
  1737. int8 oversized = 0xFF;
  1738. int16 OpcodeVersion = GetOpcodeVersion(client_version);
  1739. if (opcode_val == EQOpcodeManager[OpcodeVersion]->EmuToEQ(OP_EqExamineInfoCmd) && client_version > 546)
  1740. size += (size - 9);
  1741. if (client_version <= 283) {
  1742. if (size >= 255) {
  1743. StructAddData(oversized, sizeof(int8), 0);
  1744. StructAddData(size, sizeof(int16), 0);
  1745. }
  1746. else {
  1747. StructAddData(size, sizeof(int8), 0);
  1748. }
  1749. StructAddData(oversized, sizeof(int8), 0);
  1750. StructAddData(opcode_val, sizeof(int16), 0);
  1751. }
  1752. else {
  1753. StructAddData(size, sizeof(int32), 0);
  1754. StructAddData(oversized, sizeof(int8), 0);
  1755. StructAddData(opcode_val, sizeof(int16), 0);
  1756. }
  1757. AddData(client_data);
  1758. }
  1759. #endif
  1760. }
  1761. int32 PacketStruct::GetTotalPacketSize() {
  1762. int32 retSize = 0;
  1763. DataStruct* data = 0;
  1764. vector<DataStruct*>::iterator itr;
  1765. EQ2_8BitString* tmp1 = 0;
  1766. EQ2_16BitString* tmp2 = 0;
  1767. EQ2_32BitString* tmp3 = 0;
  1768. for (itr = structs.begin(); itr != structs.end(); itr++) {
  1769. data = *itr;
  1770. switch (data->GetType()) {
  1771. case DATA_STRUCT_INT8:
  1772. case DATA_STRUCT_SINT8:
  1773. case DATA_STRUCT_CHAR:
  1774. retSize += (1 * data->GetLength());
  1775. break;
  1776. case DATA_STRUCT_SINT16:
  1777. case DATA_STRUCT_INT16:
  1778. retSize += (2 * data->GetLength());
  1779. break;
  1780. case DATA_STRUCT_INT32:
  1781. case DATA_STRUCT_SINT32:
  1782. case DATA_STRUCT_FLOAT:
  1783. case DATA_STRUCT_DOUBLE:
  1784. retSize += (4 * data->GetLength());
  1785. break;
  1786. case DATA_STRUCT_SINT64:
  1787. case DATA_STRUCT_INT64:
  1788. retSize += (8 * data->GetLength());
  1789. break;
  1790. case DATA_STRUCT_EQ2_8BIT_STRING:
  1791. tmp1 = (EQ2_8BitString*)GetStructPointer(data);
  1792. if (tmp1) {
  1793. for (int16 i = 0; i < data->GetLength(); i++)
  1794. retSize += tmp1[i].data.length();
  1795. }
  1796. retSize += (1 * data->GetLength());
  1797. break;
  1798. case DATA_STRUCT_EQ2_16BIT_STRING: {
  1799. tmp2 = (EQ2_16BitString*)GetStructPointer(data);
  1800. if (tmp2) {
  1801. for (int16 i = 0; i < data->GetLength(); i++)
  1802. retSize += tmp2[i].data.length();
  1803. }
  1804. retSize += (2 * data->GetLength());
  1805. break;
  1806. }
  1807. case DATA_STRUCT_EQ2_32BIT_STRING: {
  1808. tmp3 = (EQ2_32BitString*)GetStructPointer(data);
  1809. if (tmp3) {
  1810. for (int16 i = 0; i < data->GetLength(); i++)
  1811. retSize += tmp3[i].data.length();
  1812. }
  1813. retSize += (4 * data->GetLength());
  1814. break;
  1815. }
  1816. case DATA_STRUCT_ARRAY: {
  1817. int32 size = GetArraySize(data, 0);
  1818. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1819. if (ps && ps->GetSubPacketSize() != size) {
  1820. ps->reAddAll(size);
  1821. }
  1822. if (ps && size > 0)
  1823. retSize += ps->GetTotalPacketSize();
  1824. break;
  1825. }
  1826. case DATA_STRUCT_COLOR: {
  1827. retSize += ((sizeof(int8) * 3) * data->GetLength());
  1828. break;
  1829. }
  1830. case DATA_STRUCT_EQUIPMENT: {
  1831. retSize += ((((sizeof(int8) * 3) * 2) + sizeof(int16)) * data->GetLength());
  1832. break;
  1833. }
  1834. }
  1835. }
  1836. return retSize;
  1837. }
  1838. void PacketStruct::AddSerializedData(DataStruct* data, int32 index, string* datastring) {
  1839. switch (data->GetAddType()) {
  1840. case DATA_STRUCT_INT8:
  1841. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1842. break;
  1843. case DATA_STRUCT_INT16:
  1844. if (data->GetOversized() > 0) {
  1845. if (*((int16*)GetStructPointer(data)) >= data->GetOversized()) {
  1846. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1847. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1848. }
  1849. else
  1850. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1851. }
  1852. else
  1853. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1854. break;
  1855. case DATA_STRUCT_INT32:
  1856. if (data->GetOversized() > 0) {
  1857. if (*((int32*)GetStructPointer(data)) >= data->GetOversized()) {
  1858. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1859. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1860. }
  1861. else
  1862. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1863. }
  1864. else
  1865. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1866. break;
  1867. case DATA_STRUCT_INT64:
  1868. StructAddData((int64*)GetStructPointer(data), data->GetLength(), sizeof(int64), datastring);
  1869. break;
  1870. case DATA_STRUCT_SINT8:
  1871. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1872. break;
  1873. case DATA_STRUCT_SINT16:
  1874. if (data->GetOversized() > 0) {
  1875. sint16 val = *((sint16*)GetStructPointer(data));
  1876. if (val >= data->GetOversized() || val <= (data->GetOversized() * -1)) {
  1877. StructAddData(data->GetOversizedByte(), sizeof(sint8), datastring);
  1878. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1879. }
  1880. else
  1881. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1882. }
  1883. else
  1884. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1885. break;
  1886. case DATA_STRUCT_SINT32:
  1887. StructAddData((sint32*)GetStructPointer(data), data->GetLength(), sizeof(sint32), datastring);
  1888. break;
  1889. case DATA_STRUCT_SINT64:
  1890. StructAddData((sint64*)GetStructPointer(data), data->GetLength(), sizeof(sint64), datastring);
  1891. break;
  1892. case DATA_STRUCT_CHAR:
  1893. StructAddData((char*)GetStructPointer(data), data->GetLength(), sizeof(char), datastring);
  1894. break;
  1895. case DATA_STRUCT_FLOAT:
  1896. StructAddData((float*)GetStructPointer(data), data->GetLength(), sizeof(float), datastring);
  1897. break;
  1898. case DATA_STRUCT_DOUBLE:
  1899. StructAddData((double*)GetStructPointer(data), data->GetLength(), sizeof(double), datastring);
  1900. break;
  1901. case DATA_STRUCT_EQ2_8BIT_STRING: {
  1902. for (int16 i = 0; i < data->GetLength(); i++) {
  1903. EQ2_8BitString* ds = (EQ2_8BitString*)GetStructPointer(data);
  1904. AddDataString(ds[i], datastring);
  1905. }
  1906. break;
  1907. }
  1908. case DATA_STRUCT_EQ2_16BIT_STRING: {
  1909. for (int16 i = 0; i < data->GetLength(); i++) {
  1910. EQ2_16BitString* ds = (EQ2_16BitString*)GetStructPointer(data);
  1911. AddDataString(ds[i], datastring);
  1912. }
  1913. break;
  1914. }
  1915. case DATA_STRUCT_EQ2_32BIT_STRING: {
  1916. for (int16 i = 0; i < data->GetLength(); i++) {
  1917. EQ2_32BitString* ds = (EQ2_32BitString*)GetStructPointer(data);
  1918. AddDataString(ds[i], datastring);
  1919. }
  1920. break;
  1921. }
  1922. case DATA_STRUCT_ARRAY: {
  1923. int32 size = GetArraySize(data, 0);
  1924. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1925. if (ps && ps->GetSubPacketSize() != size) {
  1926. ps->reAddAll(size);
  1927. }
  1928. if (ps && size > 0) {
  1929. ps->serializePacket();
  1930. string data = *(ps->getDataString());
  1931. AddData(data, datastring);
  1932. }
  1933. break;
  1934. }
  1935. case DATA_STRUCT_COLOR: {
  1936. StructAddData((EQ2_Color*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_Color), datastring);
  1937. break;
  1938. }
  1939. case DATA_STRUCT_EQUIPMENT: {
  1940. StructAddData((EQ2_EquipmentItem*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_EquipmentItem), datastring);
  1941. break;
  1942. }
  1943. case DATA_STRUCT_ITEM: {
  1944. //DumpPacket((uchar*)GetStructPointer(data), data->GetItemSize());
  1945. AddCharArray((char*)GetStructPointer(data), data->GetItemSize(), datastring);
  1946. break;
  1947. }
  1948. }
  1949. }
  1950. int8 PacketStruct::getType_int8_ByName(const char* name, int32 index, bool force) {
  1951. DataStruct* data_struct = findStruct(name, index);
  1952. return getType_int8(data_struct, index, force);
  1953. }
  1954. int16 PacketStruct::getType_int16_ByName(const char* name, int32 index, bool force) {
  1955. DataStruct* data_struct = findStruct(name, index);
  1956. return getType_int16(data_struct, index, force);
  1957. }
  1958. int32 PacketStruct::getType_int32_ByName(const char* name, int32 index, bool force) {
  1959. DataStruct* data_struct = findStruct(name, index);
  1960. return getType_int32(data_struct, index, force);
  1961. }
  1962. int64 PacketStruct::getType_int64_ByName(const char* name, int32 index, bool force) {
  1963. DataStruct* data_struct = findStruct(name, index);
  1964. return getType_int64(data_struct, index, force);
  1965. }
  1966. sint8 PacketStruct::getType_sint8_ByName(const char* name, int32 index, bool force) {
  1967. DataStruct* data_struct = findStruct(name, index);
  1968. return getType_sint8(data_struct, index, force);
  1969. }
  1970. sint16 PacketStruct::getType_sint16_ByName(const char* name, int32 index, bool force) {
  1971. DataStruct* data_struct = findStruct(name, index);
  1972. return getType_sint16(data_struct, index, force);
  1973. }
  1974. sint32 PacketStruct::getType_sint32_ByName(const char* name, int32 index, bool force) {
  1975. DataStruct* data_struct = findStruct(name, index);
  1976. return getType_sint32(data_struct, index, force);
  1977. }
  1978. sint64 PacketStruct::getType_sint64_ByName(const char* name, int32 index, bool force) {
  1979. DataStruct* data_struct = findStruct(name, index);
  1980. return getType_sint64(data_struct, index, force);
  1981. }
  1982. float PacketStruct::getType_float_ByName(const char* name, int32 index, bool force) {
  1983. DataStruct* data_struct = findStruct(name, index);
  1984. return getType_float(data_struct, index, force);
  1985. }
  1986. double PacketStruct::getType_double_ByName(const char* name, int32 index, bool force) {
  1987. DataStruct* data_struct = findStruct(name, index);
  1988. return getType_double(data_struct, index, force);
  1989. }
  1990. char PacketStruct::getType_char_ByName(const char* name, int32 index, bool force) {
  1991. DataStruct* data_struct = findStruct(name, index);
  1992. return getType_char(data_struct, index, force);
  1993. }
  1994. EQ2_8BitString PacketStruct::getType_EQ2_8BitString_ByName(const char* name, int32 index, bool force) {
  1995. DataStruct* data_struct = findStruct(name, index);
  1996. return getType_EQ2_8BitString(data_struct, index, force);
  1997. }
  1998. EQ2_16BitString PacketStruct::getType_EQ2_16BitString_ByName(const char* name, int32 index, bool force) {
  1999. DataStruct* data_struct = findStruct(name, index);
  2000. return getType_EQ2_16BitString(data_struct, index, force);
  2001. }
  2002. EQ2_32BitString PacketStruct::getType_EQ2_32BitString_ByName(const char* name, int32 index, bool force) {
  2003. DataStruct* data_struct = findStruct(name, index);
  2004. return getType_EQ2_32BitString(data_struct, index, force);
  2005. }
  2006. EQ2_Color PacketStruct::getType_EQ2_Color_ByName(const char* name, int32 index, bool force) {
  2007. DataStruct* data_struct = findStruct(name, index);
  2008. return getType_EQ2_Color(data_struct, index, force);
  2009. }
  2010. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem_ByName(const char* name, int32 index, bool force) {
  2011. DataStruct* data_struct = findStruct(name, index);
  2012. return getType_EQ2_EquipmentItem(data_struct, index, force);
  2013. }
  2014. int8 PacketStruct::getType_int8(DataStruct* data_struct, int32 index, bool force) {
  2015. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_INT8) || force)) {
  2016. int8* ptr = (int8*)GetStructPointer(data_struct);
  2017. return ptr[index];
  2018. }
  2019. return 0;
  2020. }
  2021. int16 PacketStruct::getType_int16(DataStruct* data_struct, int32 index, bool force) {
  2022. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_INT16) || force)) {
  2023. int16* ptr = (int16*)GetStructPointer(data_struct);
  2024. return ptr[index];
  2025. }
  2026. return 0;
  2027. }
  2028. int32 PacketStruct::getType_int32(DataStruct* data_struct, int32 index, bool force) {
  2029. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_INT32) || force)) {
  2030. int32* ptr = (int32*)GetStructPointer(data_struct);
  2031. return ptr[index];
  2032. }
  2033. return 0;
  2034. }
  2035. int64 PacketStruct::getType_int64(DataStruct* data_struct, int32 index, bool force) {
  2036. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_INT64) || force)) {
  2037. int64* ptr = (int64*)GetStructPointer(data_struct);
  2038. return ptr[index];
  2039. }
  2040. return 0;
  2041. }
  2042. sint8 PacketStruct::getType_sint8(DataStruct* data_struct, int32 index, bool force) {
  2043. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT8) || force)) {
  2044. sint8* ptr = (sint8*)GetStructPointer(data_struct);
  2045. return ptr[index];
  2046. }
  2047. return 0;
  2048. }
  2049. sint16 PacketStruct::getType_sint16(DataStruct* data_struct, int32 index, bool force) {
  2050. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT16) || force)) {
  2051. sint16* ptr = (sint16*)GetStructPointer(data_struct);
  2052. return ptr[index];
  2053. }
  2054. return 0;
  2055. }
  2056. sint32 PacketStruct::getType_sint32(DataStruct* data_struct, int32 index, bool force) {
  2057. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT32) || force)) {
  2058. sint32* ptr = (sint32*)GetStructPointer(data_struct);
  2059. return ptr[index];
  2060. }
  2061. return 0;
  2062. }
  2063. sint64 PacketStruct::getType_sint64(DataStruct* data_struct, int32 index, bool force) {
  2064. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT64) || force)) {
  2065. sint64* ptr = (sint64*)GetStructPointer(data_struct);
  2066. return ptr[index];
  2067. }
  2068. return 0;
  2069. }
  2070. float PacketStruct::getType_float(DataStruct* data_struct, int32 index, bool force) {
  2071. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_FLOAT) || force)) {
  2072. float* ptr = (float*)GetStructPointer(data_struct);
  2073. return ptr[index];
  2074. }
  2075. return 0;
  2076. }
  2077. double PacketStruct::getType_double(DataStruct* data_struct, int32 index, bool force) {
  2078. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_DOUBLE) || force)) {
  2079. double* ptr = (double*)GetStructPointer(data_struct);
  2080. return ptr[index];
  2081. }
  2082. return 0;
  2083. }
  2084. char PacketStruct::getType_char(DataStruct* data_struct, int32 index, bool force) {
  2085. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_CHAR) || force)) {
  2086. char* ptr = (char*)GetStructPointer(data_struct);
  2087. return ptr[index];
  2088. }
  2089. return 0;
  2090. }
  2091. EQ2_8BitString PacketStruct::getType_EQ2_8BitString(DataStruct* data_struct, int32 index, bool force) {
  2092. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_8BIT_STRING) || force)) {
  2093. EQ2_8BitString* ptr = (EQ2_8BitString*)GetStructPointer(data_struct);
  2094. return ptr[index];
  2095. }
  2096. EQ2_8BitString ret;
  2097. ret.size = 0;
  2098. return ret;
  2099. }
  2100. EQ2_16BitString PacketStruct::getType_EQ2_16BitString(DataStruct* data_struct, int32 index, bool force) {
  2101. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_16BIT_STRING) || force)) {
  2102. EQ2_16BitString* ptr = (EQ2_16BitString*)GetStructPointer(data_struct);
  2103. return ptr[index];
  2104. }
  2105. EQ2_16BitString ret;
  2106. ret.size = 0;
  2107. return ret;
  2108. }
  2109. EQ2_32BitString PacketStruct::getType_EQ2_32BitString(DataStruct* data_struct, int32 index, bool force) {
  2110. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_32BIT_STRING) || force)) {
  2111. EQ2_32BitString* ptr = (EQ2_32BitString*)GetStructPointer(data_struct);
  2112. return ptr[index];
  2113. }
  2114. EQ2_32BitString ret;
  2115. ret.size = 0;
  2116. return ret;
  2117. }
  2118. EQ2_Color PacketStruct::getType_EQ2_Color(DataStruct* data_struct, int32 index, bool force) {
  2119. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_COLOR) || force)) {
  2120. EQ2_Color* ptr = (EQ2_Color*)GetStructPointer(data_struct);
  2121. return ptr[index];
  2122. }
  2123. EQ2_Color ret;
  2124. ret.blue = 0;
  2125. ret.red = 0;
  2126. ret.green = 0;
  2127. return ret;
  2128. }
  2129. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem(DataStruct* data_struct, int32 index, bool force) {
  2130. if (data_struct && ((data_struct->GetType() == DATA_STRUCT_EQUIPMENT) || force)) {
  2131. EQ2_EquipmentItem* ptr = (EQ2_EquipmentItem*)GetStructPointer(data_struct);
  2132. return ptr[index];
  2133. }
  2134. EQ2_EquipmentItem ret;
  2135. ret.color.blue = 0;
  2136. ret.color.red = 0;
  2137. ret.color.green = 0;
  2138. ret.highlight.blue = 0;
  2139. ret.highlight.red = 0;
  2140. ret.highlight.green = 0;
  2141. ret.type = 0;
  2142. return ret;
  2143. }
  2144. bool PacketStruct::SetOpcode(const char* new_opcode) {
  2145. opcode = EQOpcodeManager[0]->NameSearch(new_opcode);
  2146. if (opcode == OP_Unknown) {
  2147. #ifndef MINILOGIN
  2148. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode named '%s', this struct cannot be serialized directly.", GetName(), new_opcode);
  2149. #endif
  2150. return false;
  2151. }
  2152. return true;
  2153. }
  2154. EQ2Packet* PacketStruct::serialize() {
  2155. serializePacket();
  2156. if (GetOpcode() != OP_Unknown)
  2157. return new EQ2Packet(GetOpcode(), getData(), getDataSize());
  2158. else {
  2159. #ifndef MINILOGIN
  2160. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode and cannot be serialized directly.", GetName());
  2161. #endif
  2162. return 0;
  2163. }
  2164. }
  2165. EQ2Packet* PacketStruct::serializeCountPacket(int16 version, int8 offset, uchar* orig_packet, uchar* xor_packet) {
  2166. string* packet_data = serializeString();
  2167. uchar* data = (uchar*)packet_data->c_str();
  2168. int32 size = packet_data->size();
  2169. uchar* packed_data = new uchar[size + 1000]; // this size + 20 is poorly defined, depending on the packet data, we could use a additional length of 1K+
  2170. memset(packed_data, 0, size + 1000);
  2171. if (orig_packet && xor_packet) {
  2172. memcpy(xor_packet, data + 6, size - 6 - offset);
  2173. Encode(xor_packet, orig_packet, size - 6 - offset);
  2174. size = Pack(packed_data, xor_packet, size - 6 - offset, size + 1000, version);
  2175. }
  2176. else
  2177. size = Pack(packed_data, data + 6, packet_data->size() - 6 - offset, packet_data->size() + 1000, version);
  2178. uchar* combined = new uchar[size + sizeof(int16) + offset];
  2179. memset(combined, 0, size + sizeof(int16) + offset);
  2180. uchar* ptr = combined;
  2181. memcpy(ptr, data, sizeof(int16));
  2182. ptr += sizeof(int16);
  2183. memcpy(ptr, packed_data, size);
  2184. if (offset > 0) {
  2185. ptr += size;
  2186. uchar* ptr2 = data;
  2187. ptr2 += packet_data->size() - offset;
  2188. memcpy(ptr, ptr2, offset);
  2189. }
  2190. EQ2Packet* app = new EQ2Packet(GetOpcode(), combined, size + sizeof(int16) + offset);
  2191. safe_delete_array(packed_data);
  2192. safe_delete_array(combined);
  2193. return app;
  2194. }
  2195. bool PacketStruct::IsSubPacket() {
  2196. return sub_packet;
  2197. }
  2198. void PacketStruct::IsSubPacket(bool new_val) {
  2199. sub_packet = new_val;
  2200. }
  2201. int32 PacketStruct::GetSubPacketSize() {
  2202. return sub_packet_size;
  2203. }
  2204. void PacketStruct::SetSubPacketSize(int32 new_size) {
  2205. sub_packet_size = new_size;
  2206. }
  2207. void* PacketStruct::GetStructPointer(DataStruct* data_struct, bool erase) {
  2208. try {
  2209. map<DataStruct*, void*>::iterator tmpitr = struct_data.find(data_struct);
  2210. if (tmpitr != struct_data.end()) {
  2211. if (erase)
  2212. struct_data.erase(data_struct);
  2213. return tmpitr->second;
  2214. }
  2215. else {
  2216. PacketStruct* packet = 0;
  2217. vector<PacketStruct*>::iterator itr2;
  2218. for (itr2 = arrays.begin(); itr2 != arrays.end(); itr2++) {
  2219. packet = *itr2;
  2220. if (packet) {
  2221. void* tmp = packet->GetStructPointer(data_struct, erase);
  2222. if (tmp != 0)
  2223. return tmp;
  2224. }
  2225. }
  2226. }
  2227. }
  2228. catch (...) {
  2229. cout << "Caught Exception...\n";
  2230. }
  2231. return 0;
  2232. }
  2233. vector<DataStruct*> PacketStruct::GetDataStructs() {
  2234. vector<DataStruct*> ret;
  2235. DataStruct* ds = 0;
  2236. vector<DataStruct*>::iterator itr;
  2237. for (itr = structs.begin(); itr != structs.end(); itr++) {
  2238. ds = *itr;
  2239. if (ds->GetType() == DATA_STRUCT_ARRAY) {
  2240. int32 size = GetArraySize(ds, 0);
  2241. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2242. if (ps && ps->GetSubPacketSize() != size) {
  2243. ps->reAddAll(size);
  2244. }
  2245. if (ps) {
  2246. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2247. vector<DataStruct*>::iterator itr2;
  2248. for (itr2 = ret2.begin(); itr2 != ret2.end(); itr2++) {
  2249. ret.push_back(*itr2);
  2250. }
  2251. }
  2252. }
  2253. else if (ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY) {
  2254. int32 size = GetArraySize(ds, 0);
  2255. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2256. if (ps && ps->GetSubPacketSize() != size) {
  2257. ps->reAddAll(size);
  2258. }
  2259. if (ps) {
  2260. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2261. vector<DataStruct*>::iterator itr2;
  2262. for (itr2 = ret2.begin(); itr2 != ret2.end(); itr2++) {
  2263. ret.push_back(*itr2);
  2264. }
  2265. }
  2266. }
  2267. else
  2268. ret.push_back(ds);
  2269. }
  2270. return ret;
  2271. }
  2272. void PacketStruct::PrintPacket() {
  2273. DataStruct* ds = 0;
  2274. vector<DataStruct*>::iterator itr;
  2275. for (itr = structs.begin(); itr != structs.end(); itr++) {
  2276. ds = *itr;
  2277. if (!ds->AddToStruct())
  2278. continue;
  2279. for (int16 i = 0; i < ds->GetLength(); i++) {
  2280. cout << "Name: " << ds->GetName() << " \tIndex: " << i << " \tType: ";
  2281. switch (ds->GetType()) {
  2282. case DATA_STRUCT_INT8:
  2283. printf("int8\t\tData: %i", getType_int8_ByName(ds->GetName(), i));
  2284. break;
  2285. case DATA_STRUCT_INT16:
  2286. printf("int16\t\tData: %i", getType_int16_ByName(ds->GetName(), i));
  2287. break;
  2288. case DATA_STRUCT_INT32:
  2289. printf("int32\t\tData: %u", getType_int32_ByName(ds->GetName(), i));
  2290. break;
  2291. case DATA_STRUCT_INT64:
  2292. printf("int64\t\tData: %llu", getType_int64_ByName(ds->GetName(), i));
  2293. break;
  2294. case DATA_STRUCT_SINT8:
  2295. printf("sint8\t\tData: %i", getType_sint8_ByName(ds->GetName(), i));
  2296. break;
  2297. case DATA_STRUCT_SINT16:
  2298. printf("sint16\t\tData: %i", getType_sint16_ByName(ds->GetName(), i));
  2299. break;
  2300. case DATA_STRUCT_SINT32:
  2301. printf("sint32\t\tData: %i", getType_sint32_ByName(ds->GetName(), i));
  2302. break;
  2303. case DATA_STRUCT_SINT64:
  2304. printf("sint64\t\tData: %lli", getType_sint64_ByName(ds->GetName(), i));
  2305. break;
  2306. case DATA_STRUCT_CHAR:
  2307. printf("char\t\tData: %c", getType_char_ByName(ds->GetName(), i));
  2308. break;
  2309. case DATA_STRUCT_FLOAT:
  2310. printf("float\t\tData: %f", getType_float_ByName(ds->GetName(), i));
  2311. break;
  2312. case DATA_STRUCT_DOUBLE:
  2313. printf("double\t\tData: %f", getType_double_ByName(ds->GetName(), i));
  2314. break;
  2315. case DATA_STRUCT_EQ2_8BIT_STRING:
  2316. printf("EQ2_8BitString\tData: %s", getType_EQ2_8BitString_ByName(ds->GetName(), i).data.c_str());
  2317. break;
  2318. case DATA_STRUCT_EQ2_16BIT_STRING:
  2319. printf("EQ2_16BitString\tData: %s", getType_EQ2_16BitString_ByName(ds->GetName(), i).data.c_str());
  2320. break;
  2321. case DATA_STRUCT_EQ2_32BIT_STRING: {
  2322. printf("EQ2_32BitString\tData: %s", getType_EQ2_32BitString_ByName(ds->GetName(), i).data.c_str());
  2323. break;
  2324. }
  2325. case DATA_STRUCT_ITEM: {
  2326. if (ds->GetItemSize() > 0) {
  2327. DumpPacket((uchar*)GetStructPointer(ds), ds->GetItemSize());
  2328. }
  2329. break;
  2330. }
  2331. case DATA_STRUCT_ARRAY: {
  2332. int32 size = GetArraySize(ds, 0);
  2333. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2334. if (ps && ps->GetSubPacketSize() != size) {
  2335. ps->reAddAll(size);
  2336. }
  2337. if (ps) {
  2338. cout << "Array:\tData: \n";
  2339. ps->PrintPacket();
  2340. }
  2341. break;
  2342. }
  2343. case DATA_STRUCT_COLOR: {
  2344. cout.unsetf(ios_base::dec);
  2345. cout.setf(ios_base::hex);
  2346. printf("EQ2_Color\tData: ");
  2347. EQ2_Color tmp = getType_EQ2_Color_ByName(ds->GetName(), i);
  2348. printf("R: %i", tmp.red);
  2349. printf(", G: %i", tmp.green);
  2350. printf(", B: %i", tmp.blue);
  2351. break;
  2352. }
  2353. case DATA_STRUCT_EQUIPMENT: {
  2354. cout.unsetf(ios_base::dec);
  2355. cout.setf(ios_base::hex);
  2356. printf("EQ2_EquipmentItem\tData: ");
  2357. EQ2_EquipmentItem tmp = getType_EQ2_EquipmentItem_ByName(ds->GetName(), i);
  2358. printf("type: ");
  2359. printf(" ,color R: %i", tmp.color.red);
  2360. printf(" ,color G: %i", tmp.color.green);
  2361. printf(" ,color B: %i", tmp.color.blue);
  2362. printf(" ,hl R: %i", tmp.highlight.red);
  2363. printf(" ,hl G: %i", tmp.highlight.green);
  2364. printf(" ,hl B: %i", tmp.highlight.blue);
  2365. break;
  2366. }
  2367. }
  2368. cout << endl;
  2369. }
  2370. if (ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY) {
  2371. int32 size = GetArraySize(ds, 0);
  2372. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2373. if (ps && ps->GetSubPacketSize() != size) {
  2374. ps->reAddAll(size);
  2375. }
  2376. if (ps) {
  2377. cout << "Array:\tData: \n";
  2378. ps->PrintPacket();
  2379. }
  2380. }
  2381. }
  2382. }
  2383. void PacketStruct::LoadFromPacketStruct(PacketStruct* packet, char* substruct_name) {
  2384. vector<DataStruct*>::iterator itr;
  2385. DataStruct* ds = 0;
  2386. char name[512];
  2387. //scatman (1/30/2012): these declarations are here to get rid of a linux compile error "taking address of temporary"
  2388. EQ2_8BitString str8;
  2389. EQ2_16BitString str16;
  2390. EQ2_32BitString str32;
  2391. EQ2_EquipmentItem equip;
  2392. for (itr = structs.begin(); itr != structs.end(); itr++) {
  2393. ds = *itr;
  2394. for (int16 i = 0; i < ds->GetLength(); i++) {
  2395. memset(name, 0, sizeof(name));
  2396. if (substruct_name)
  2397. snprintf(name, sizeof(name), "%s_%s_0", substruct_name, ds->GetName());
  2398. else
  2399. strncpy(name, ds->GetName(), sizeof(name));
  2400. name[sizeof(name) - 1] = '\0';
  2401. switch (ds->GetType()) {
  2402. case DATA_STRUCT_INT8:
  2403. setData(ds, packet->getType_int8_ByName(name, i), i);
  2404. break;
  2405. case DATA_STRUCT_SINT8:
  2406. setData(ds, packet->getType_sint8_ByName(name, i), i);
  2407. break;
  2408. case DATA_STRUCT_CHAR:
  2409. setData(ds, packet->getType_char_ByName(name, i), i);
  2410. break;
  2411. case DATA_STRUCT_SINT16:
  2412. setData(ds, packet->getType_sint16_ByName(name, i), i);
  2413. break;
  2414. case DATA_STRUCT_INT16:
  2415. setData(ds, packet->getType_int16_ByName(name, i), i);
  2416. break;
  2417. case DATA_STRUCT_INT32:
  2418. setData(ds, packet->getType_int32_ByName(name, i), i);
  2419. break;
  2420. case DATA_STRUCT_INT64:
  2421. setData(ds, packet->getType_int64_ByName(name, i), i);
  2422. break;
  2423. case DATA_STRUCT_SINT32:
  2424. setData(ds, packet->getType_sint32_ByName(name, i), i);
  2425. break;
  2426. case DATA_STRUCT_SINT64:
  2427. setData(ds, packet->getType_sint64_ByName(name, i), i);
  2428. break;
  2429. case DATA_STRUCT_FLOAT:
  2430. setData(ds, packet->getType_float_ByName(name, i), i);
  2431. break;
  2432. case DATA_STRUCT_DOUBLE:
  2433. setData(ds, packet->getType_double_ByName(name, i), i);
  2434. break;
  2435. case DATA_STRUCT_EQ2_8BIT_STRING:
  2436. str8 = packet->getType_EQ2_8BitString_ByName(name, i);
  2437. setData(ds, &str8, i);
  2438. break;
  2439. case DATA_STRUCT_EQ2_16BIT_STRING:
  2440. str16 = packet->getType_EQ2_16BitString_ByName(name, i);
  2441. setData(ds, &str16, i);
  2442. break;
  2443. case DATA_STRUCT_EQ2_32BIT_STRING:
  2444. str32 = packet->getType_EQ2_32BitString_ByName(name, i);
  2445. setData(ds, &str32, i);
  2446. break;
  2447. case DATA_STRUCT_ARRAY: {
  2448. int32 size = GetArraySize(ds, 0);
  2449. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2450. if (ps && size > 0)
  2451. ps->LoadFromPacketStruct(packet, substruct_name);
  2452. break;
  2453. }
  2454. case DATA_STRUCT_COLOR:
  2455. setColor(ds, packet->getType_EQ2_Color_ByName(name, i), i);
  2456. break;
  2457. case DATA_STRUCT_EQUIPMENT:
  2458. equip = packet->getType_EQ2_EquipmentItem_ByName(name, i);
  2459. setEquipmentByName(ds->GetName(), &equip, i);
  2460. break;
  2461. default:
  2462. break;
  2463. }
  2464. }
  2465. }
  2466. }
  2467. #ifdef WORLD
  2468. void PacketStruct::setItem(DataStruct* ds, Item* item, Player* player, int32 index, sint8 offset, bool loot_item, bool make_empty_item_packet) {
  2469. if (!ds)
  2470. return;
  2471. uchar* ptr = (uchar*)GetStructPointer(ds);
  2472. if(!item) {
  2473. if(make_empty_item_packet) {
  2474. if(client_version <= 546) {
  2475. // for player inspection this will offset the parts of the packet that have no items
  2476. 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};
  2477. bogusItemBuffer[World::newValue] = 0x00;
  2478. int sizeOfArray = sizeof(bogusItemBuffer) / sizeof(bogusItemBuffer[0]);
  2479. ds->SetItemSize(sizeOfArray);
  2480. memcpy(ptr, bogusItemBuffer, sizeOfArray);
  2481. }
  2482. else {
  2483. // for player inspection this will offset the parts of the packet that have no items
  2484. 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};
  2485. int sizeOfArray = sizeof(bogusItemBuffer) / sizeof(bogusItemBuffer[0]);
  2486. ds->SetItemSize(sizeOfArray);
  2487. memcpy(ptr, bogusItemBuffer, sizeOfArray);
  2488. }
  2489. }
  2490. return;
  2491. }
  2492. PacketStruct* packet = item->PrepareItem(client_version, false, loot_item);
  2493. if (packet) {
  2494. int16 item_version = GetItemPacketType(packet->GetVersion());
  2495. // 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
  2496. item->serialize(packet, true, player, item_version, 0, (packet->GetVersion() <= 546) ? loot_item : false);
  2497. string* generic_string_data = packet->serializeString();
  2498. int32 size = generic_string_data->length(); // had to set to 81
  2499. int32 actual_length = size;
  2500. if(offset > 12) {
  2501. size += 6; // end padding for the specialized item (used for player inspection)
  2502. }
  2503. //DumpPacket((uchar*)generic_string_data->c_str(), size);
  2504. if (size <= 13)
  2505. {
  2506. ds->SetIsSet(false);
  2507. return;
  2508. }
  2509. size -= (9 + offset);
  2510. if (client_version > 546 && item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2))
  2511. size = (size * 2) - 5;
  2512. uchar* out_data = new uchar[size + 1];
  2513. memset(out_data, 0, size+1);
  2514. uchar* out_ptr = out_data;
  2515. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (9 + offset), actual_length - (9 + offset));
  2516. //DumpPacket((uchar*)generic_string_data->c_str() + (9 + offset), size);
  2517. //without these it will prompt for your character name
  2518. if (offset == 0 || offset == -1 || offset == 2) {
  2519. if (client_version <= 546 && item->details.count > 0)
  2520. out_data[0] = item->details.count;
  2521. else
  2522. out_data[0] = 1;
  2523. }
  2524. //
  2525. out_ptr += generic_string_data->length() - (10 + offset);
  2526. if (client_version > 546 && item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2)) {
  2527. out_data[4] = 0x80;
  2528. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (13 + offset), generic_string_data->length() - (13 + offset));
  2529. }
  2530. ds->SetItemSize(size);
  2531. // printf("Item Name: %s\n",item->name.c_str());
  2532. // DumpPacket(out_data, size);
  2533. memcpy(ptr, out_data, size);
  2534. safe_delete_array(out_data);
  2535. delete packet;
  2536. }
  2537. //DumpPacket(ptr2, ds->GetItemSize());
  2538. }
  2539. void PacketStruct::setItemByName(const char* name, Item* item, Player* player, int32 index, sint8 offset, bool loot_item, bool make_empty_item_packet) {
  2540. setItem(findStruct(name, index), item, player, index, offset, loot_item, make_empty_item_packet);
  2541. }
  2542. void PacketStruct::setItemArrayDataByName(const char* name, Item* item, Player* player, int32 index1, int32 index2, sint8 offset, bool loot_item, bool make_empty_item_packet) {
  2543. char tmp[10] = { 0 };
  2544. sprintf(tmp, "_%i", index1);
  2545. string name2 = string(name).append(tmp);
  2546. setItem(findStruct(name2.c_str(), index1, index2), item, player, index2, offset, loot_item, make_empty_item_packet);
  2547. }
  2548. void PacketStruct::ResetData() {
  2549. vector<DataStruct*>::iterator itr;
  2550. for (itr = structs.begin(); itr != structs.end(); itr++) {
  2551. DataStruct* ds = *itr;
  2552. void* ptr = GetStructPointer(ds);
  2553. if (!ptr)
  2554. continue;
  2555. switch (ds->GetType())
  2556. {
  2557. case DATA_STRUCT_EQ2_8BIT_STRING: {
  2558. EQ2_8BitString* real_ptr = (EQ2_8BitString*)ptr;
  2559. real_ptr->size = 0;
  2560. real_ptr->data.clear();
  2561. break;
  2562. }
  2563. case DATA_STRUCT_EQ2_16BIT_STRING: {
  2564. EQ2_16BitString* real_ptr = (EQ2_16BitString*)ptr;
  2565. real_ptr->size = 0;
  2566. real_ptr->data.clear();
  2567. break;
  2568. }
  2569. case DATA_STRUCT_EQ2_32BIT_STRING: {
  2570. EQ2_32BitString* real_ptr = (EQ2_32BitString*)ptr;
  2571. real_ptr->size = 0;
  2572. real_ptr->data.clear();
  2573. break;
  2574. }
  2575. case DATA_STRUCT_EQUIPMENT: {
  2576. memset(ptr, 0, sizeof(EQ2_EquipmentItem) * ds->GetLength());
  2577. break;
  2578. }
  2579. case DATA_STRUCT_DOUBLE: {
  2580. memset(ptr, 0, sizeof(double) * ds->GetLength());
  2581. break;
  2582. }
  2583. case DATA_STRUCT_FLOAT: {
  2584. memset(ptr, 0, sizeof(float) * ds->GetLength());
  2585. break;
  2586. }
  2587. case DATA_STRUCT_INT8: {
  2588. memset(ptr, 0, sizeof(int8) * ds->GetLength());
  2589. break;
  2590. }
  2591. case DATA_STRUCT_INT16: {
  2592. memset(ptr, 0, sizeof(int16) * ds->GetLength());
  2593. break;
  2594. }
  2595. case DATA_STRUCT_INT32: {
  2596. memset(ptr, 0, sizeof(int32) * ds->GetLength());
  2597. break;
  2598. }
  2599. case DATA_STRUCT_INT64: {
  2600. memset(ptr, 0, sizeof(int64) * ds->GetLength());
  2601. break;
  2602. }
  2603. case DATA_STRUCT_SINT8: {
  2604. memset(ptr, 0, sizeof(sint8) * ds->GetLength());
  2605. break;
  2606. }
  2607. case DATA_STRUCT_SINT16: {
  2608. memset(ptr, 0, sizeof(sint16) * ds->GetLength());
  2609. break;
  2610. }
  2611. case DATA_STRUCT_SINT32: {
  2612. memset(ptr, 0, sizeof(sint32) * ds->GetLength());
  2613. break;
  2614. }
  2615. case DATA_STRUCT_SINT64: {
  2616. memset(ptr, 0, sizeof(sint64) * ds->GetLength());
  2617. break;
  2618. }
  2619. case DATA_STRUCT_ITEM: {
  2620. memset(ptr, 0, 10000 * ds->GetLength());
  2621. break;
  2622. }
  2623. case DATA_STRUCT_CHAR: {
  2624. memset(ptr, 0, sizeof(char) * ds->GetLength());
  2625. break;
  2626. }
  2627. case DATA_STRUCT_COLOR: {
  2628. memset(ptr, 0, sizeof(EQ2_Color) * ds->GetLength());
  2629. break;
  2630. }
  2631. }
  2632. }
  2633. vector<PacketStruct*>::iterator itr2;
  2634. for (itr2 = arrays.begin(); itr2 != arrays.end(); itr2++)
  2635. (*itr2)->ResetData();
  2636. }
  2637. #endif