9
3

PacketStruct.cpp 76 KB

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