PacketStruct.cpp 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  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. // Check to see if the variable contains %i, if it does assume we are in an array
  1487. // and get the current index from the end of the data struct's name
  1488. char name[250] = { 0 };
  1489. if (varname.find("%i") < 0xFFFFFFFF) {
  1490. vector<string>* varnames = SplitString(varname, '_');
  1491. vector<string>* indexes = SplitString(data->GetName(), '_');
  1492. int index = 0;
  1493. if (indexes->size() > 0 )
  1494. index = atoi(indexes->at(indexes->size() - 1).c_str());
  1495. sprintf(name, varname.c_str(), index);
  1496. }
  1497. else
  1498. strcpy(name, varname.c_str());
  1499. if(!GetVariableIsNotSet(name))
  1500. continue;
  1501. }
  1502. }
  1503. // Quick implementaion of IfVariableNotEquals
  1504. // probably not what it was intended for as it currently just checks to see if the given variable equals 1
  1505. // should probably change it so you can define what the variable should or shouldn't equal
  1506. //
  1507. // ie: IfVariableNotEquals="stat_type_%i=1"
  1508. // would be a check to make sure that stat_type_%i does not equal 1 and if it does exclude this element
  1509. if (data->GetIfNotEquals() && data->GetIfNotEqualsVariable()) {
  1510. // Get the variable name
  1511. string varname = string(data->GetIfNotEqualsVariable());
  1512. char name[250] = {0};
  1513. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1514. // index and replace it
  1515. if (varname.find("%i") < 0xFFFFFFFF) {
  1516. // Get the current index by getting the number at the end of the name
  1517. vector<string>* varnames = SplitString(data->GetName(), '_');
  1518. int index = atoi(varnames->at(varnames->size() - 1).c_str());
  1519. string substr = "stat_type";
  1520. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1521. // adorn_stat_subtype 18 chars
  1522. string temp = varname.substr(12);
  1523. char temp2[20]={0};
  1524. int index2 = atoi(temp.c_str());
  1525. itoa(index2, temp2, 10);
  1526. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1527. }
  1528. sprintf(name, varname.c_str(), index);
  1529. safe_delete(varnames);
  1530. }
  1531. else
  1532. strcpy(name, varname.c_str());
  1533. // Get the data for the variable
  1534. int16 value = 0;
  1535. DataStruct* data_struct2 = findStruct(name, 0);
  1536. value = getType_int16(data_struct2);
  1537. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1538. if (value == 1)
  1539. continue;
  1540. }
  1541. // copy and paste of the code above for IfEquals
  1542. if (data->GetIfEquals() && data->GetIfEqualsVariable()) {
  1543. // Get the variable name
  1544. string varname = string(data->GetIfEqualsVariable());
  1545. char name[250] = {0};
  1546. // Check to see if the variable has %i in the name, if it does assume we are in an array and get the current
  1547. // index and replace it
  1548. if (varname.find("%i") < 0xFFFFFFFF) {
  1549. // Get the current index by getting the number at the end of the name
  1550. vector<string>* varnames = SplitString(data->GetName(), '_');
  1551. int index = 0;
  1552. if (varnames)
  1553. index = atoi(varnames->at(varnames->size() - 1).c_str());
  1554. string substr = "stat_type";
  1555. if (strncmp(varname.c_str(), substr.c_str(), strlen(substr.c_str())) == 0) {
  1556. // adorn_stat_subtype 18 chars
  1557. string temp = varname.substr(12);
  1558. char temp2[20]={0};
  1559. int index2 = atoi(temp.c_str());
  1560. itoa(index2, temp2, 10);
  1561. varname = varname.substr(0, 12).append(temp2).append("_%i");
  1562. }
  1563. sprintf(name, varname.c_str(), index);
  1564. safe_delete(varnames);
  1565. }
  1566. else
  1567. strcpy(name, varname.c_str());
  1568. // Get the data for the variable
  1569. int16 value = 0;
  1570. DataStruct* data_struct2 = findStruct(name, 0);
  1571. value = getType_int16(data_struct2);
  1572. // Hack for items as it is the only struct that currently uses IfVariableNotEquals
  1573. if (value != 1)
  1574. continue;
  1575. }
  1576. if(client_cmd)
  1577. AddSerializedData(data, 0, &client_data);
  1578. else
  1579. AddSerializedData(data);
  1580. }
  1581. }
  1582. #ifndef LOGIN
  1583. if(client_cmd){
  1584. EmuOpcode sub_opcode = EQOpcodeManager[0]->NameSearch(GetOpcodeType());
  1585. int16 opcode_val = 0;
  1586. if(sub_opcode == OP_Unknown){
  1587. try{
  1588. opcode_val = atoi(GetOpcodeType());
  1589. }
  1590. catch(...){}
  1591. if(opcode_val == 0)
  1592. LogWrite(PACKET__ERROR, 0, "Packet", "PACKET NOT SENT CORRECTLY! Unable to get Emu Opcode from: '%s'", GetOpcodeType());
  1593. }
  1594. Clear();
  1595. int32 size = client_data.length() + 3; //gotta add the opcode and oversized
  1596. int8 oversized = 0xFF;
  1597. int16 OpcodeVersion = GetOpcodeVersion(client_version);
  1598. if(opcode_val == 0)
  1599. opcode_val = EQOpcodeManager[OpcodeVersion]->EmuToEQ(sub_opcode);
  1600. if(opcode_val == EQOpcodeManager[OpcodeVersion]->EmuToEQ(OP_EqExamineInfoCmd))
  1601. size += (size - 9);
  1602. StructAddData(size, sizeof(int32), 0);
  1603. StructAddData(oversized, sizeof(int8), 0);
  1604. StructAddData(opcode_val, sizeof(int16), 0);
  1605. AddData(client_data);
  1606. }
  1607. #endif
  1608. }
  1609. int32 PacketStruct::GetTotalPacketSize(){
  1610. int32 retSize = 0;
  1611. DataStruct* data = 0;
  1612. vector<DataStruct*>::iterator itr;
  1613. EQ2_8BitString* tmp1 = 0;
  1614. EQ2_16BitString* tmp2 = 0;
  1615. EQ2_32BitString* tmp3 = 0;
  1616. for(itr=structs.begin();itr!=structs.end();itr++) {
  1617. data = *itr;
  1618. switch(data->GetType()){
  1619. case DATA_STRUCT_INT8:
  1620. case DATA_STRUCT_SINT8:
  1621. case DATA_STRUCT_CHAR:
  1622. retSize+=(1*data->GetLength());
  1623. break;
  1624. case DATA_STRUCT_SINT16:
  1625. case DATA_STRUCT_INT16:
  1626. retSize+=(2*data->GetLength());
  1627. break;
  1628. case DATA_STRUCT_INT32:
  1629. case DATA_STRUCT_SINT32:
  1630. case DATA_STRUCT_FLOAT:
  1631. case DATA_STRUCT_DOUBLE:
  1632. retSize+=(4*data->GetLength());
  1633. break;
  1634. case DATA_STRUCT_SINT64:
  1635. case DATA_STRUCT_INT64:
  1636. retSize+=(8*data->GetLength());
  1637. break;
  1638. case DATA_STRUCT_EQ2_8BIT_STRING:
  1639. tmp1 = (EQ2_8BitString*)GetStructPointer(data);
  1640. if(tmp1){
  1641. for(int16 i=0;i<data->GetLength();i++)
  1642. retSize += tmp1[i].data.length();
  1643. }
  1644. retSize+=(1*data->GetLength());
  1645. break;
  1646. case DATA_STRUCT_EQ2_16BIT_STRING:{
  1647. tmp2 = (EQ2_16BitString*)GetStructPointer(data);
  1648. if(tmp2){
  1649. for(int16 i=0;i<data->GetLength();i++)
  1650. retSize += tmp2[i].data.length();
  1651. }
  1652. retSize+=(2*data->GetLength());
  1653. break;
  1654. }
  1655. case DATA_STRUCT_EQ2_32BIT_STRING:{
  1656. tmp3 = (EQ2_32BitString*)GetStructPointer(data);
  1657. if(tmp3){
  1658. for(int16 i=0;i<data->GetLength();i++)
  1659. retSize += tmp3[i].data.length();
  1660. }
  1661. retSize+=(4*data->GetLength());
  1662. break;
  1663. }
  1664. case DATA_STRUCT_ARRAY:{
  1665. int32 size = GetArraySize(data,0);
  1666. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1667. if(ps && ps->GetSubPacketSize() != size){
  1668. ps->reAddAll(size);
  1669. }
  1670. if(ps && size > 0)
  1671. retSize+= ps->GetTotalPacketSize();
  1672. break;
  1673. }
  1674. case DATA_STRUCT_COLOR:{
  1675. retSize+=((sizeof(int8)*3)*data->GetLength());
  1676. break;
  1677. }
  1678. case DATA_STRUCT_EQUIPMENT:{
  1679. retSize+=((((sizeof(int8)*3)*2) + sizeof(int16))*data->GetLength());
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. return retSize;
  1685. }
  1686. void PacketStruct::AddSerializedData(DataStruct* data, int32 index, string* datastring){
  1687. switch(data->GetAddType()){
  1688. case DATA_STRUCT_INT8:
  1689. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1690. break;
  1691. case DATA_STRUCT_INT16:
  1692. if(data->GetOversized() > 0){
  1693. if(*((int16*)GetStructPointer(data)) >= data->GetOversized()){
  1694. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1695. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1696. }
  1697. else
  1698. StructAddData((int8*)GetStructPointer(data), data->GetLength(), sizeof(int8), datastring);
  1699. }
  1700. else
  1701. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1702. break;
  1703. case DATA_STRUCT_INT32:
  1704. if(data->GetOversized() > 0){
  1705. if(*((int32*)GetStructPointer(data)) >= data->GetOversized()){
  1706. StructAddData(data->GetOversizedByte(), sizeof(int8), datastring);
  1707. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1708. }
  1709. else
  1710. StructAddData((int16*)GetStructPointer(data), data->GetLength(), sizeof(int16), datastring);
  1711. }
  1712. else
  1713. StructAddData((int32*)GetStructPointer(data), data->GetLength(), sizeof(int32), datastring);
  1714. break;
  1715. case DATA_STRUCT_INT64:
  1716. StructAddData((int64*)GetStructPointer(data), data->GetLength(), sizeof(int64), datastring);
  1717. break;
  1718. case DATA_STRUCT_SINT8:
  1719. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1720. break;
  1721. case DATA_STRUCT_SINT16:
  1722. if(data->GetOversized() > 0){
  1723. sint16 val = *((sint16*)GetStructPointer(data));
  1724. if(val >= data->GetOversized() || val <= (data->GetOversized()*-1)){
  1725. StructAddData(data->GetOversizedByte(), sizeof(sint8), datastring);
  1726. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1727. }
  1728. else
  1729. StructAddData((sint8*)GetStructPointer(data), data->GetLength(), sizeof(sint8), datastring);
  1730. }
  1731. else
  1732. StructAddData((sint16*)GetStructPointer(data), data->GetLength(), sizeof(sint16), datastring);
  1733. break;
  1734. case DATA_STRUCT_SINT32:
  1735. StructAddData((sint32*)GetStructPointer(data), data->GetLength(), sizeof(sint32), datastring);
  1736. break;
  1737. case DATA_STRUCT_SINT64:
  1738. StructAddData((sint64*)GetStructPointer(data), data->GetLength(), sizeof(sint64), datastring);
  1739. break;
  1740. case DATA_STRUCT_CHAR:
  1741. StructAddData((char*)GetStructPointer(data), data->GetLength(), sizeof(char), datastring);
  1742. break;
  1743. case DATA_STRUCT_FLOAT:
  1744. StructAddData((float*)GetStructPointer(data), data->GetLength(), sizeof(float), datastring);
  1745. break;
  1746. case DATA_STRUCT_DOUBLE:
  1747. StructAddData((double*)GetStructPointer(data), data->GetLength(), sizeof(double), datastring);
  1748. break;
  1749. case DATA_STRUCT_EQ2_8BIT_STRING:{
  1750. for(int16 i=0;i<data->GetLength();i++){
  1751. EQ2_8BitString* ds = (EQ2_8BitString*)GetStructPointer(data);
  1752. AddDataString(ds[i], datastring);
  1753. }
  1754. break;
  1755. }
  1756. case DATA_STRUCT_EQ2_16BIT_STRING:{
  1757. for(int16 i=0;i<data->GetLength();i++){
  1758. EQ2_16BitString* ds = (EQ2_16BitString*)GetStructPointer(data);
  1759. AddDataString(ds[i], datastring);
  1760. }
  1761. break;
  1762. }
  1763. case DATA_STRUCT_EQ2_32BIT_STRING:{
  1764. for(int16 i=0;i<data->GetLength();i++){
  1765. EQ2_32BitString* ds = (EQ2_32BitString*)GetStructPointer(data);
  1766. AddDataString(ds[i], datastring);
  1767. }
  1768. break;
  1769. }
  1770. case DATA_STRUCT_ARRAY:{
  1771. int32 size = GetArraySize(data,0);
  1772. PacketStruct* ps = GetPacketStructByName(data->GetName());
  1773. if(ps && ps->GetSubPacketSize() != size){
  1774. ps->reAddAll(size);
  1775. }
  1776. if(ps && size > 0){
  1777. ps->serializePacket();
  1778. string data = *(ps->getDataString());
  1779. AddData(data, datastring);
  1780. }
  1781. break;
  1782. }
  1783. case DATA_STRUCT_COLOR:{
  1784. StructAddData((EQ2_Color*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_Color), datastring);
  1785. break;
  1786. }
  1787. case DATA_STRUCT_EQUIPMENT:{
  1788. StructAddData((EQ2_EquipmentItem*)GetStructPointer(data), data->GetLength(), sizeof(EQ2_EquipmentItem), datastring);
  1789. break;
  1790. }
  1791. case DATA_STRUCT_ITEM:{
  1792. //DumpPacket(ptr, data->GetItemSize());
  1793. AddCharArray((char*)GetStructPointer(data), data->GetItemSize(), datastring);
  1794. break;
  1795. }
  1796. }
  1797. }
  1798. int8 PacketStruct::getType_int8_ByName(const char* name, int32 index, bool force){
  1799. DataStruct* data_struct = findStruct(name, index);
  1800. return getType_int8(data_struct, index, force);
  1801. }
  1802. int16 PacketStruct::getType_int16_ByName(const char* name, int32 index, bool force){
  1803. DataStruct* data_struct = findStruct(name, index);
  1804. return getType_int16(data_struct, index, force);
  1805. }
  1806. int32 PacketStruct::getType_int32_ByName(const char* name, int32 index, bool force){
  1807. DataStruct* data_struct = findStruct(name, index);
  1808. return getType_int32(data_struct, index, force);
  1809. }
  1810. int64 PacketStruct::getType_int64_ByName(const char* name, int32 index, bool force){
  1811. DataStruct* data_struct = findStruct(name, index);
  1812. return getType_int64(data_struct, index, force);
  1813. }
  1814. sint8 PacketStruct::getType_sint8_ByName(const char* name, int32 index, bool force){
  1815. DataStruct* data_struct = findStruct(name, index);
  1816. return getType_sint8(data_struct, index, force);
  1817. }
  1818. sint16 PacketStruct::getType_sint16_ByName(const char* name, int32 index, bool force){
  1819. DataStruct* data_struct = findStruct(name, index);
  1820. return getType_sint16(data_struct, index, force);
  1821. }
  1822. sint32 PacketStruct::getType_sint32_ByName(const char* name, int32 index, bool force){
  1823. DataStruct* data_struct = findStruct(name, index);
  1824. return getType_sint32(data_struct, index, force);
  1825. }
  1826. sint64 PacketStruct::getType_sint64_ByName(const char* name, int32 index, bool force){
  1827. DataStruct* data_struct = findStruct(name, index);
  1828. return getType_sint64(data_struct, index, force);
  1829. }
  1830. float PacketStruct::getType_float_ByName(const char* name, int32 index, bool force){
  1831. DataStruct* data_struct = findStruct(name, index);
  1832. return getType_float(data_struct, index, force);
  1833. }
  1834. double PacketStruct::getType_double_ByName(const char* name, int32 index, bool force){
  1835. DataStruct* data_struct = findStruct(name, index);
  1836. return getType_double(data_struct, index, force);
  1837. }
  1838. char PacketStruct::getType_char_ByName(const char* name, int32 index, bool force){
  1839. DataStruct* data_struct = findStruct(name, index);
  1840. return getType_char(data_struct, index, force);
  1841. }
  1842. EQ2_8BitString PacketStruct::getType_EQ2_8BitString_ByName(const char* name, int32 index, bool force){
  1843. DataStruct* data_struct = findStruct(name, index);
  1844. return getType_EQ2_8BitString(data_struct, index, force);
  1845. }
  1846. EQ2_16BitString PacketStruct::getType_EQ2_16BitString_ByName(const char* name, int32 index, bool force){
  1847. DataStruct* data_struct = findStruct(name, index);
  1848. return getType_EQ2_16BitString(data_struct, index, force);
  1849. }
  1850. EQ2_32BitString PacketStruct::getType_EQ2_32BitString_ByName(const char* name, int32 index, bool force){
  1851. DataStruct* data_struct = findStruct(name, index);
  1852. return getType_EQ2_32BitString(data_struct, index, force);
  1853. }
  1854. EQ2_Color PacketStruct::getType_EQ2_Color_ByName(const char* name, int32 index, bool force){
  1855. DataStruct* data_struct = findStruct(name, index);
  1856. return getType_EQ2_Color(data_struct, index, force);
  1857. }
  1858. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem_ByName(const char* name, int32 index, bool force){
  1859. DataStruct* data_struct = findStruct(name, index);
  1860. return getType_EQ2_EquipmentItem(data_struct, index, force);
  1861. }
  1862. int8 PacketStruct::getType_int8(DataStruct* data_struct, int32 index, bool force){
  1863. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT8) || force)){
  1864. int8* ptr = (int8*)GetStructPointer(data_struct);
  1865. return ptr[index];
  1866. }
  1867. return 0;
  1868. }
  1869. int16 PacketStruct::getType_int16(DataStruct* data_struct, int32 index, bool force){
  1870. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT16) || force)){
  1871. int16* ptr = (int16*)GetStructPointer(data_struct);
  1872. return ptr[index];
  1873. }
  1874. return 0;
  1875. }
  1876. int32 PacketStruct::getType_int32(DataStruct* data_struct, int32 index, bool force){
  1877. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT32) || force)){
  1878. int32* ptr = (int32*)GetStructPointer(data_struct);
  1879. return ptr[index];
  1880. }
  1881. return 0;
  1882. }
  1883. int64 PacketStruct::getType_int64(DataStruct* data_struct, int32 index, bool force){
  1884. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_INT64) || force)){
  1885. int64* ptr = (int64*)GetStructPointer(data_struct);
  1886. return ptr[index];
  1887. }
  1888. return 0;
  1889. }
  1890. sint8 PacketStruct::getType_sint8(DataStruct* data_struct, int32 index, bool force){
  1891. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT8) || force)){
  1892. sint8* ptr = (sint8*)GetStructPointer(data_struct);
  1893. return ptr[index];
  1894. }
  1895. return 0;
  1896. }
  1897. sint16 PacketStruct::getType_sint16(DataStruct* data_struct, int32 index, bool force){
  1898. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT16) || force)){
  1899. sint16* ptr = (sint16*)GetStructPointer(data_struct);
  1900. return ptr[index];
  1901. }
  1902. return 0;
  1903. }
  1904. sint32 PacketStruct::getType_sint32(DataStruct* data_struct, int32 index, bool force){
  1905. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT32) || force)){
  1906. sint32* ptr = (sint32*)GetStructPointer(data_struct);
  1907. return ptr[index];
  1908. }
  1909. return 0;
  1910. }
  1911. sint64 PacketStruct::getType_sint64(DataStruct* data_struct, int32 index, bool force){
  1912. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_SINT64) || force)){
  1913. sint64* ptr = (sint64*)GetStructPointer(data_struct);
  1914. return ptr[index];
  1915. }
  1916. return 0;
  1917. }
  1918. float PacketStruct::getType_float(DataStruct* data_struct, int32 index, bool force){
  1919. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_FLOAT) || force)){
  1920. float* ptr = (float*)GetStructPointer(data_struct);
  1921. return ptr[index];
  1922. }
  1923. return 0;
  1924. }
  1925. double PacketStruct::getType_double(DataStruct* data_struct, int32 index, bool force){
  1926. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_DOUBLE) || force)){
  1927. double* ptr = (double*)GetStructPointer(data_struct);
  1928. return ptr[index];
  1929. }
  1930. return 0;
  1931. }
  1932. char PacketStruct::getType_char(DataStruct* data_struct, int32 index, bool force){
  1933. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_CHAR) || force)){
  1934. char* ptr = (char*)GetStructPointer(data_struct);
  1935. return ptr[index];
  1936. }
  1937. return 0;
  1938. }
  1939. EQ2_8BitString PacketStruct::getType_EQ2_8BitString(DataStruct* data_struct, int32 index, bool force){
  1940. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_8BIT_STRING) || force)){
  1941. EQ2_8BitString* ptr = (EQ2_8BitString*)GetStructPointer(data_struct);
  1942. return ptr[index];
  1943. }
  1944. EQ2_8BitString ret;
  1945. ret.size = 0;
  1946. return ret;
  1947. }
  1948. EQ2_16BitString PacketStruct::getType_EQ2_16BitString(DataStruct* data_struct, int32 index, bool force){
  1949. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_16BIT_STRING) || force)){
  1950. EQ2_16BitString* ptr = (EQ2_16BitString*)GetStructPointer(data_struct);
  1951. return ptr[index];
  1952. }
  1953. EQ2_16BitString ret;
  1954. ret.size = 0;
  1955. return ret;
  1956. }
  1957. EQ2_32BitString PacketStruct::getType_EQ2_32BitString(DataStruct* data_struct, int32 index, bool force){
  1958. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQ2_32BIT_STRING) || force)){
  1959. EQ2_32BitString* ptr = (EQ2_32BitString*)GetStructPointer(data_struct);
  1960. return ptr[index];
  1961. }
  1962. EQ2_32BitString ret;
  1963. ret.size = 0;
  1964. return ret;
  1965. }
  1966. EQ2_Color PacketStruct::getType_EQ2_Color(DataStruct* data_struct, int32 index, bool force){
  1967. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_COLOR) || force)){
  1968. EQ2_Color* ptr = (EQ2_Color*)GetStructPointer(data_struct);
  1969. return ptr[index];
  1970. }
  1971. EQ2_Color ret;
  1972. ret.blue = 0;
  1973. ret.red = 0;
  1974. ret.green = 0;
  1975. return ret;
  1976. }
  1977. EQ2_EquipmentItem PacketStruct::getType_EQ2_EquipmentItem(DataStruct* data_struct, int32 index, bool force){
  1978. if(data_struct && ((data_struct->GetType() == DATA_STRUCT_EQUIPMENT) || force)){
  1979. EQ2_EquipmentItem* ptr = (EQ2_EquipmentItem*)GetStructPointer(data_struct);
  1980. return ptr[index];
  1981. }
  1982. EQ2_EquipmentItem ret;
  1983. ret.color.blue = 0;
  1984. ret.color.red = 0;
  1985. ret.color.green = 0;
  1986. ret.highlight.blue = 0;
  1987. ret.highlight.red = 0;
  1988. ret.highlight.green = 0;
  1989. ret.type = 0;
  1990. return ret;
  1991. }
  1992. bool PacketStruct::SetOpcode(const char* new_opcode){
  1993. opcode = EQOpcodeManager[0]->NameSearch(new_opcode);
  1994. if(opcode == OP_Unknown){
  1995. #ifndef MINILOGIN
  1996. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode named '%s', this struct cannot be serialized directly.", GetName(), new_opcode);
  1997. #endif
  1998. return false;
  1999. }
  2000. return true;
  2001. }
  2002. EQ2Packet* PacketStruct::serialize(){
  2003. serializePacket();
  2004. if(GetOpcode() != OP_Unknown)
  2005. return new EQ2Packet(GetOpcode(), getData(), getDataSize());
  2006. else{
  2007. #ifndef MINILOGIN
  2008. LogWrite(PACKET__ERROR, 0, "Packet", "Warning: PacketStruct '%s' uses an unknown opcode and cannot be serialized directly.", GetName());
  2009. #endif
  2010. return 0;
  2011. }
  2012. }
  2013. EQ2Packet* PacketStruct::serializeCountPacket(int16 version, int8 offset, uchar* orig_packet, uchar* xor_packet){
  2014. string* packet_data = serializeString();
  2015. uchar* data = (uchar*)packet_data->c_str();
  2016. int32 size = packet_data->size();
  2017. uchar* packed_data = new uchar[size + 20];
  2018. memset(packed_data, 0, size + 20);
  2019. if(orig_packet && xor_packet){
  2020. memcpy(xor_packet, data + 6, size - 6 - offset);
  2021. Encode(xor_packet, orig_packet, size - 6 - offset);
  2022. size = Pack(packed_data, xor_packet, size - 6 - offset, size+20, version);
  2023. }
  2024. else
  2025. size = Pack(packed_data, data + 6, packet_data->size()-6 - offset, packet_data->size()+20, version);
  2026. uchar* combined = new uchar[size + sizeof(int16)+offset];
  2027. memset(combined, 0, size + sizeof(int16)+offset);
  2028. uchar* ptr = combined;
  2029. memcpy(ptr, data, sizeof(int16));
  2030. ptr += sizeof(int16);
  2031. memcpy(ptr, packed_data, size);
  2032. if(offset > 0){
  2033. ptr += size;
  2034. uchar* ptr2 = data;
  2035. ptr2 += packet_data->size() - offset;
  2036. memcpy(ptr, ptr2, offset);
  2037. }
  2038. EQ2Packet* app = new EQ2Packet(GetOpcode(), combined, size + sizeof(int16)+offset);
  2039. safe_delete_array(packed_data);
  2040. safe_delete_array(combined);
  2041. return app;
  2042. }
  2043. bool PacketStruct::IsSubPacket(){
  2044. return sub_packet;
  2045. }
  2046. void PacketStruct::IsSubPacket(bool new_val){
  2047. sub_packet = new_val;
  2048. }
  2049. int32 PacketStruct::GetSubPacketSize(){
  2050. return sub_packet_size;
  2051. }
  2052. void PacketStruct::SetSubPacketSize(int32 new_size){
  2053. sub_packet_size = new_size;
  2054. }
  2055. void* PacketStruct::GetStructPointer(DataStruct* data_struct, bool erase){
  2056. try{
  2057. void* tmp = struct_data[data_struct];
  2058. if(tmp != 0){
  2059. if(erase)
  2060. struct_data.erase(data_struct);
  2061. return tmp;
  2062. }
  2063. else{
  2064. PacketStruct* packet = 0;
  2065. vector<PacketStruct*>::iterator itr2;
  2066. for(itr2=arrays.begin();itr2!=arrays.end();itr2++) {
  2067. packet = *itr2;
  2068. if(packet){
  2069. tmp = packet->GetStructPointer(data_struct, erase);
  2070. if( tmp != 0)
  2071. return tmp;
  2072. }
  2073. }
  2074. }
  2075. }
  2076. catch(...){
  2077. cout << "Caught Exception...\n";
  2078. }
  2079. return 0;
  2080. }
  2081. vector<DataStruct*> PacketStruct::GetDataStructs(){
  2082. vector<DataStruct*> ret;
  2083. DataStruct* ds = 0;
  2084. vector<DataStruct*>::iterator itr;
  2085. for(itr=structs.begin();itr!=structs.end();itr++){
  2086. ds = *itr;
  2087. if(ds->GetType() == DATA_STRUCT_ARRAY){
  2088. int32 size = GetArraySize(ds,0);
  2089. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2090. if(ps && ps->GetSubPacketSize() != size){
  2091. ps->reAddAll(size);
  2092. }
  2093. if(ps){
  2094. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2095. vector<DataStruct*>::iterator itr2;
  2096. for(itr2=ret2.begin();itr2!=ret2.end();itr2++){
  2097. ret.push_back(*itr2);
  2098. }
  2099. }
  2100. }
  2101. else if(ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY){
  2102. int32 size = GetArraySize(ds,0);
  2103. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2104. if(ps && ps->GetSubPacketSize() != size){
  2105. ps->reAddAll(size);
  2106. }
  2107. if(ps){
  2108. vector<DataStruct*> ret2 = ps->GetDataStructs();
  2109. vector<DataStruct*>::iterator itr2;
  2110. for(itr2=ret2.begin();itr2!=ret2.end();itr2++){
  2111. ret.push_back(*itr2);
  2112. }
  2113. }
  2114. }
  2115. else
  2116. ret.push_back(ds);
  2117. }
  2118. return ret;
  2119. }
  2120. void PacketStruct::PrintPacket(){
  2121. DataStruct* ds = 0;
  2122. vector<DataStruct*>::iterator itr;
  2123. for(itr=structs.begin();itr!=structs.end();itr++){
  2124. ds = *itr;
  2125. if(!ds->AddToStruct())
  2126. continue;
  2127. for(int16 i=0;i<ds->GetLength();i++){
  2128. cout << "Name: " << ds->GetName() << " \tIndex: " << i << " \tType: ";
  2129. switch(ds->GetType()){
  2130. case DATA_STRUCT_INT8:
  2131. printf("int8\t\tData: %i", getType_int8_ByName(ds->GetName(), i));
  2132. break;
  2133. case DATA_STRUCT_INT16:
  2134. printf("int16\t\tData: %i", getType_int16_ByName(ds->GetName(), i));
  2135. break;
  2136. case DATA_STRUCT_INT32:
  2137. printf("int32\t\tData: %u", getType_int32_ByName(ds->GetName(), i));
  2138. break;
  2139. case DATA_STRUCT_INT64:
  2140. printf("int64\t\tData: %llu", getType_int64_ByName(ds->GetName(), i));
  2141. break;
  2142. case DATA_STRUCT_SINT8:
  2143. printf("sint8\t\tData: %i", getType_sint8_ByName(ds->GetName(), i));
  2144. break;
  2145. case DATA_STRUCT_SINT16:
  2146. printf("sint16\t\tData: %i", getType_sint16_ByName(ds->GetName(), i));
  2147. break;
  2148. case DATA_STRUCT_SINT32:
  2149. printf("sint32\t\tData: %i", getType_sint32_ByName(ds->GetName(), i));
  2150. break;
  2151. case DATA_STRUCT_SINT64:
  2152. printf("sint64\t\tData: %lli", getType_sint64_ByName(ds->GetName(), i));
  2153. break;
  2154. case DATA_STRUCT_CHAR:
  2155. printf("char\t\tData: %c", getType_char_ByName(ds->GetName(), i));
  2156. break;
  2157. case DATA_STRUCT_FLOAT:
  2158. printf("float\t\tData: %f", getType_float_ByName(ds->GetName(), i));
  2159. break;
  2160. case DATA_STRUCT_DOUBLE:
  2161. printf("double\t\tData: %f", getType_double_ByName(ds->GetName(), i));
  2162. break;
  2163. case DATA_STRUCT_EQ2_8BIT_STRING:
  2164. printf("EQ2_8BitString\tData: %s", getType_EQ2_8BitString_ByName(ds->GetName(), i).data.c_str());
  2165. break;
  2166. case DATA_STRUCT_EQ2_16BIT_STRING:
  2167. printf("EQ2_16BitString\tData: %s", getType_EQ2_16BitString_ByName(ds->GetName(), i).data.c_str());
  2168. break;
  2169. case DATA_STRUCT_EQ2_32BIT_STRING:{
  2170. printf("EQ2_32BitString\tData: %s", getType_EQ2_32BitString_ByName(ds->GetName(), i).data.c_str());
  2171. break;
  2172. }
  2173. case DATA_STRUCT_ARRAY:{
  2174. int32 size = GetArraySize(ds,0);
  2175. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2176. if(ps && ps->GetSubPacketSize() != size){
  2177. ps->reAddAll(size);
  2178. }
  2179. if(ps){
  2180. cout << "Array:\tData: \n";
  2181. ps->PrintPacket();
  2182. }
  2183. break;
  2184. }
  2185. case DATA_STRUCT_COLOR:{
  2186. cout.unsetf( ios_base::dec );
  2187. cout.setf( ios_base::hex );
  2188. printf("EQ2_Color\tData: ");
  2189. EQ2_Color tmp = getType_EQ2_Color_ByName(ds->GetName(), i);
  2190. printf("R: %i", tmp.red);
  2191. printf(", G: %i", tmp.green);
  2192. printf(", B: %i", tmp.blue);
  2193. break;
  2194. }
  2195. case DATA_STRUCT_EQUIPMENT:{
  2196. cout.unsetf( ios_base::dec );
  2197. cout.setf( ios_base::hex );
  2198. printf("EQ2_EquipmentItem\tData: ");
  2199. EQ2_EquipmentItem tmp = getType_EQ2_EquipmentItem_ByName(ds->GetName(), i);
  2200. printf("type: ");
  2201. printf(" ,color R: %i", tmp.color.red);
  2202. printf(" ,color G: %i", tmp.color.green);
  2203. printf(" ,color B: %i", tmp.color.blue);
  2204. printf(" ,hl R: %i", tmp.highlight.red);
  2205. printf(" ,hl G: %i", tmp.highlight.green);
  2206. printf(" ,hl B: %i", tmp.highlight.blue);
  2207. break;
  2208. }
  2209. }
  2210. cout << endl;
  2211. }
  2212. if(ds->GetLength() == 0 && ds->GetType() == DATA_STRUCT_ARRAY){
  2213. int32 size = GetArraySize(ds,0);
  2214. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2215. if(ps && ps->GetSubPacketSize() != size){
  2216. ps->reAddAll(size);
  2217. }
  2218. if(ps){
  2219. cout << "Array:\tData: \n";
  2220. ps->PrintPacket();
  2221. }
  2222. }
  2223. }
  2224. }
  2225. void PacketStruct::LoadFromPacketStruct(PacketStruct* packet, char* substruct_name){
  2226. vector<DataStruct*>::iterator itr;
  2227. DataStruct* ds = 0;
  2228. char name[512];
  2229. //scatman (1/30/2012): these declarations are here to get rid of a linux compile error "taking address of temporary"
  2230. EQ2_8BitString str8;
  2231. EQ2_16BitString str16;
  2232. EQ2_32BitString str32;
  2233. EQ2_EquipmentItem equip;
  2234. for(itr=structs.begin();itr!=structs.end();itr++){
  2235. ds = *itr;
  2236. for(int16 i=0;i<ds->GetLength();i++){
  2237. memset(name, 0, sizeof(name));
  2238. if(substruct_name)
  2239. snprintf(name, sizeof(name), "%s_%s_0", substruct_name, ds->GetName());
  2240. else
  2241. strncpy(name, ds->GetName(), sizeof(name));
  2242. name[sizeof(name) - 1] = '\0';
  2243. switch(ds->GetType()){
  2244. case DATA_STRUCT_INT8:
  2245. setData(ds, packet->getType_int8_ByName(name, i), i);
  2246. break;
  2247. case DATA_STRUCT_SINT8:
  2248. setData(ds, packet->getType_sint8_ByName(name, i), i);
  2249. break;
  2250. case DATA_STRUCT_CHAR:
  2251. setData(ds, packet->getType_char_ByName(name, i), i);
  2252. break;
  2253. case DATA_STRUCT_SINT16:
  2254. setData(ds, packet->getType_sint16_ByName(name, i), i);
  2255. break;
  2256. case DATA_STRUCT_INT16:
  2257. setData(ds, packet->getType_int16_ByName(name, i), i);
  2258. break;
  2259. case DATA_STRUCT_INT32:
  2260. setData(ds, packet->getType_int32_ByName(name, i), i);
  2261. break;
  2262. case DATA_STRUCT_INT64:
  2263. setData(ds, packet->getType_int64_ByName(name, i), i);
  2264. break;
  2265. case DATA_STRUCT_SINT32:
  2266. setData(ds, packet->getType_sint32_ByName(name, i), i);
  2267. break;
  2268. case DATA_STRUCT_SINT64:
  2269. setData(ds, packet->getType_sint64_ByName(name, i), i);
  2270. break;
  2271. case DATA_STRUCT_FLOAT:
  2272. setData(ds, packet->getType_float_ByName(name, i), i);
  2273. break;
  2274. case DATA_STRUCT_DOUBLE:
  2275. setData(ds, packet->getType_double_ByName(name, i), i);
  2276. break;
  2277. case DATA_STRUCT_EQ2_8BIT_STRING:
  2278. str8 = packet->getType_EQ2_8BitString_ByName(name, i);
  2279. setData(ds, &str8, i);
  2280. break;
  2281. case DATA_STRUCT_EQ2_16BIT_STRING:
  2282. str16 = packet->getType_EQ2_16BitString_ByName(name, i);
  2283. setData(ds, &str16, i);
  2284. break;
  2285. case DATA_STRUCT_EQ2_32BIT_STRING:
  2286. str32 = packet->getType_EQ2_32BitString_ByName(name, i);
  2287. setData(ds, &str32, i);
  2288. break;
  2289. case DATA_STRUCT_ARRAY:{
  2290. int32 size = GetArraySize(ds, 0);
  2291. PacketStruct* ps = GetPacketStructByName(ds->GetName());
  2292. if(ps && size > 0)
  2293. ps->LoadFromPacketStruct(packet, substruct_name);
  2294. break;
  2295. }
  2296. case DATA_STRUCT_COLOR:
  2297. setColor(ds, packet->getType_EQ2_Color_ByName(name, i), i);
  2298. break;
  2299. case DATA_STRUCT_EQUIPMENT:
  2300. equip = packet->getType_EQ2_EquipmentItem_ByName(name, i);
  2301. setEquipmentByName(ds->GetName(), &equip, i);
  2302. break;
  2303. default:
  2304. break;
  2305. }
  2306. }
  2307. }
  2308. }
  2309. #ifdef WORLD
  2310. void PacketStruct::setItem(DataStruct* ds, Item* item, Player* player, int32 index, sint8 offset){
  2311. if(!ds)
  2312. return;
  2313. uchar* ptr = (uchar*)GetStructPointer(ds);
  2314. PacketStruct* packet = item->PrepareItem(client_version);
  2315. if(packet){
  2316. int16 item_version = GetItemPacketType(packet->GetVersion());
  2317. item->serialize(packet, true, player, item_version);
  2318. string* generic_string_data = packet->serializeString();
  2319. int32 size = generic_string_data->length();
  2320. if(size <= 13)
  2321. return;
  2322. size -= (9+offset);
  2323. if(item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2))
  2324. size = (size*2)-5;
  2325. uchar* out_data = new uchar[size+1];
  2326. uchar* out_ptr = out_data;
  2327. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (9 + offset), generic_string_data->length() - (9 + offset));
  2328. //without these it will prompt for your character name
  2329. if(offset == 0 || offset == -1 || offset == 2){
  2330. out_data[0] = 1;
  2331. }
  2332. //
  2333. out_ptr += generic_string_data->length() - (10 + offset);
  2334. if(item->IsBag() == false && item->IsBauble() == false && item->IsFood() == false && (offset == 0 || offset == -1 || offset == 2)){
  2335. out_data[4] = 0x80;
  2336. memcpy(out_ptr, (uchar*)generic_string_data->c_str() + (13 + offset), generic_string_data->length() -(13+offset));
  2337. }
  2338. ds->SetItemSize(size);
  2339. //DumpPacket(out_data, size);
  2340. memcpy(ptr, out_data, size);
  2341. safe_delete_array(out_data);
  2342. delete packet;
  2343. }
  2344. //DumpPacket(ptr2, ds->GetItemSize());
  2345. }
  2346. void PacketStruct::setItemByName(const char* name, Item* item, Player* player, int32 index, sint8 offset){
  2347. setItem(findStruct(name, index), item, player, index, offset);
  2348. }
  2349. void PacketStruct::setItemArrayDataByName(const char* name, Item* item, Player* player, int32 index1, int32 index2, sint8 offset){
  2350. char tmp[10] = {0};
  2351. sprintf(tmp,"_%i",index1);
  2352. string name2 = string(name).append(tmp);
  2353. setItem(findStruct(name2.c_str(), index1, index2), item, player, index2, offset);
  2354. }
  2355. void PacketStruct::ResetData() {
  2356. vector<DataStruct*>::iterator itr;
  2357. for (itr = structs.begin(); itr != structs.end(); itr++){
  2358. DataStruct* ds = *itr;
  2359. void* ptr = GetStructPointer(ds);
  2360. if (!ptr)
  2361. continue;
  2362. switch (ds->GetType())
  2363. {
  2364. case DATA_STRUCT_EQ2_8BIT_STRING:{
  2365. EQ2_8BitString* real_ptr = (EQ2_8BitString*)ptr;
  2366. real_ptr->size = 0;
  2367. real_ptr->data.clear();
  2368. break;
  2369. }
  2370. case DATA_STRUCT_EQ2_16BIT_STRING:{
  2371. EQ2_16BitString* real_ptr = (EQ2_16BitString*)ptr;
  2372. real_ptr->size = 0;
  2373. real_ptr->data.clear();
  2374. break;
  2375. }
  2376. case DATA_STRUCT_EQ2_32BIT_STRING:{
  2377. EQ2_32BitString* real_ptr = (EQ2_32BitString*)ptr;
  2378. real_ptr->size = 0;
  2379. real_ptr->data.clear();
  2380. break;
  2381. }
  2382. case DATA_STRUCT_EQUIPMENT:{
  2383. memset(ptr, 0, sizeof(EQ2_EquipmentItem) * ds->GetLength());
  2384. break;
  2385. }
  2386. case DATA_STRUCT_DOUBLE:{
  2387. memset(ptr, 0, sizeof(double) * ds->GetLength());
  2388. break;
  2389. }
  2390. case DATA_STRUCT_FLOAT:{
  2391. memset(ptr, 0, sizeof(float) * ds->GetLength());
  2392. break;
  2393. }
  2394. case DATA_STRUCT_INT8:{
  2395. memset(ptr, 0, sizeof(int8) * ds->GetLength());
  2396. break;
  2397. }
  2398. case DATA_STRUCT_INT16:{
  2399. memset(ptr, 0, sizeof(int16) * ds->GetLength());
  2400. break;
  2401. }
  2402. case DATA_STRUCT_INT32:{
  2403. memset(ptr, 0, sizeof(int32) * ds->GetLength());
  2404. break;
  2405. }
  2406. case DATA_STRUCT_INT64:{
  2407. memset(ptr, 0, sizeof(int64) * ds->GetLength());
  2408. break;
  2409. }
  2410. case DATA_STRUCT_SINT8:{
  2411. memset(ptr, 0, sizeof(sint8) * ds->GetLength());
  2412. break;
  2413. }
  2414. case DATA_STRUCT_SINT16:{
  2415. memset(ptr, 0, sizeof(sint16) * ds->GetLength());
  2416. break;
  2417. }
  2418. case DATA_STRUCT_SINT32:{
  2419. memset(ptr, 0, sizeof(sint32) * ds->GetLength());
  2420. break;
  2421. }
  2422. case DATA_STRUCT_SINT64:{
  2423. memset(ptr, 0, sizeof(sint64) * ds->GetLength());
  2424. break;
  2425. }
  2426. case DATA_STRUCT_ITEM:{
  2427. memset(ptr, 0, 10000 * ds->GetLength());
  2428. break;
  2429. }
  2430. case DATA_STRUCT_CHAR:{
  2431. memset(ptr, 0, sizeof(char) * ds->GetLength());
  2432. break;
  2433. }
  2434. case DATA_STRUCT_COLOR:{
  2435. memset(ptr, 0, sizeof(EQ2_Color) * ds->GetLength());
  2436. break;
  2437. }
  2438. }
  2439. }
  2440. vector<PacketStruct*>::iterator itr2;
  2441. for (itr2 = arrays.begin(); itr2 != arrays.end(); itr2++)
  2442. (*itr2)->ResetData();
  2443. }
  2444. #endif