PacketStruct.cpp 80 KB

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