9
3

PacketStruct.cpp 84 KB

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