PacketStruct.cpp 84 KB

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