PacketStruct.cpp 81 KB

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