9
3

ConfigReader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "ConfigReader.h"
  17. #include "Log.h"
  18. ConfigReader::~ConfigReader(){
  19. MStructs.lock();
  20. DestroyStructs();
  21. MStructs.unlock();
  22. }
  23. PacketStruct* ConfigReader::getStructByVersion(const char* name, int16 version){
  24. PacketStruct* packet = 0;
  25. PacketStruct* newpacket = 0;
  26. MStructs.lock();
  27. vector<PacketStruct*>* struct_versions = structs[string(name)];
  28. if(struct_versions){
  29. vector<PacketStruct*>::iterator iter;
  30. for(iter = struct_versions->begin(); iter != struct_versions->end(); iter++){
  31. packet = *iter;
  32. if(packet && packet->GetVersion() == version){
  33. newpacket = new PacketStruct(packet, version);
  34. break;
  35. }
  36. }
  37. }
  38. MStructs.unlock();
  39. if(!newpacket)
  40. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find struct named '%s' with version: %i", name, version);
  41. return newpacket;
  42. }
  43. void ConfigReader::ReloadStructs(){
  44. MStructs.lock();
  45. DestroyStructs();
  46. for(int32 i=0;i<load_files.size();i++)
  47. processXML_Elements(load_files[i].c_str());
  48. MStructs.unlock();
  49. }
  50. void ConfigReader::DestroyStructs(){
  51. map<string, vector<PacketStruct*>*>::iterator struct_iterator;
  52. for(struct_iterator=structs.begin();struct_iterator!=structs.end();struct_iterator++) {
  53. vector<PacketStruct*>* versions = struct_iterator->second;
  54. vector<PacketStruct*>::iterator version_iter;
  55. if(versions){
  56. for(version_iter = versions->begin(); version_iter != versions->end(); version_iter++){
  57. safe_delete(*version_iter);
  58. }
  59. }
  60. safe_delete(versions);
  61. }
  62. structs.clear();
  63. }
  64. PacketStruct* ConfigReader::getStruct(const char* name, int16 version){
  65. PacketStruct* latest_version = 0;
  66. PacketStruct* new_latest_version = 0;
  67. MStructs.lock();
  68. vector<PacketStruct*>* struct_versions = structs[string(name)];
  69. if(struct_versions){
  70. vector<PacketStruct*>::iterator iter;
  71. for(iter = struct_versions->begin(); iter != struct_versions->end(); iter++){
  72. if((*iter)->GetVersion() <= version && (!latest_version || (*iter)->GetVersion() > latest_version->GetVersion()))
  73. latest_version = *iter;
  74. }
  75. if (latest_version) {
  76. if (latest_version->GetOpcode() != OP_Unknown && latest_version->GetOpcodeValue(version) == 0xFFFF) {
  77. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find valid opcode for Packet Struct '%s' and client version %d", latest_version->GetName(), version);
  78. }
  79. else if(strlen(latest_version->GetOpcodeType()) == 0 || latest_version->GetOpcode() != OP_Unknown)
  80. new_latest_version = new PacketStruct(latest_version, version);
  81. }
  82. }
  83. MStructs.unlock();
  84. if(!new_latest_version && !latest_version)
  85. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find struct named '%s'", name);
  86. return new_latest_version;
  87. }
  88. int16 ConfigReader::GetStructVersion(const char* name, int16 version){
  89. MStructs.lock();
  90. vector<PacketStruct*>* struct_versions = structs[string(name)];
  91. int16 ret = 0;
  92. if(struct_versions){
  93. vector<PacketStruct*>::iterator iter;
  94. PacketStruct* latest_version = 0;
  95. for(iter = struct_versions->begin(); iter != struct_versions->end(); iter++){
  96. if(!latest_version || ( (*iter)->GetVersion() > latest_version->GetVersion() && (*iter)->GetVersion() <= version) )
  97. latest_version = *iter;
  98. }
  99. if(latest_version)
  100. ret = latest_version->GetVersion();
  101. }
  102. MStructs.unlock();
  103. return ret;
  104. }
  105. void ConfigReader::addStruct(const char* name, int16 version, PacketStruct* new_struct){
  106. string strname(name);
  107. vector<PacketStruct*>* struct_versions = structs[strname];
  108. if(struct_versions)
  109. struct_versions->push_back(new_struct);
  110. else{
  111. struct_versions = new vector<PacketStruct*>;
  112. struct_versions->push_back(new_struct);
  113. structs[strname] = struct_versions;
  114. }
  115. }
  116. bool ConfigReader::LoadFile(const char* name){
  117. load_files.push_back(name);
  118. return processXML_Elements(name);
  119. }
  120. bool ConfigReader::processXML_Elements(const char* fileName){
  121. XMLNode xMainNode=XMLNode::openFileHelper(fileName,"EQ2Emulator");
  122. if(xMainNode.isEmpty())
  123. return false;
  124. for(int i=0;i<xMainNode.nChildNode("Struct");i++){
  125. const char* struct_name = xMainNode.getChildNode("Struct", i).getAttribute("Name");
  126. const char* str_version = xMainNode.getChildNode("Struct", i).getAttribute("ClientVersion");
  127. const char* opcode_name = xMainNode.getChildNode("Struct", i).getAttribute("OpcodeName");
  128. const char* opcode_type = xMainNode.getChildNode("Struct", i).getAttribute("OpcodeType");
  129. if(!struct_name || !str_version)
  130. {
  131. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid struct, all structs must include at least a Name and ClientVersion!");
  132. continue;
  133. }
  134. int16 version = 1;
  135. try
  136. {
  137. version = atoi(str_version);
  138. }
  139. catch(...)
  140. {
  141. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid version for struct named '%s': '%s'", struct_name, str_version);
  142. continue;
  143. }
  144. PacketStruct* new_struct = new PacketStruct();
  145. new_struct->SetName(struct_name);
  146. if(opcode_type)
  147. new_struct->SetOpcodeType(opcode_type);
  148. if(opcode_name){
  149. if(!new_struct->SetOpcode(opcode_name)){
  150. safe_delete(new_struct);
  151. continue;
  152. }
  153. }
  154. new_struct->SetVersion(version);
  155. loadDataStruct(new_struct, xMainNode.getChildNode("Struct", i));
  156. addStruct(struct_name, version, new_struct);
  157. }
  158. return true;
  159. }
  160. void ConfigReader::loadDataStruct(PacketStruct* packet, XMLNode parentNode, bool array_packet){
  161. for(int x=0;x<parentNode.nChildNode();x++){
  162. const char* name = parentNode.getChildNode("Data", x).getAttribute("ElementName");
  163. const char* type = parentNode.getChildNode("Data", x).getAttribute("Type");
  164. const char* size = parentNode.getChildNode("Data", x).getAttribute("Size");
  165. const char* type2 = parentNode.getChildNode("Data", x).getAttribute("Type2");
  166. const char* array_size = parentNode.getChildNode("Data", x).getAttribute("ArraySizeVariable");
  167. const char* max_array = parentNode.getChildNode("Data", x).getAttribute("MaxArraySize");
  168. const char* substruct = parentNode.getChildNode("Data", x).getAttribute("Substruct");
  169. const char* default_value = parentNode.getChildNode("Data", x).getAttribute("DefaultByteValue");
  170. const char* oversized = parentNode.getChildNode("Data", x).getAttribute("OversizedValue");
  171. const char* oversized_byte = parentNode.getChildNode("Data", x).getAttribute("OversizedByte");
  172. const char* if_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableSet");
  173. const char* if_not_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableNotSet");
  174. const char* if_equals_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableEquals");
  175. const char* if_not_equals_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableNotEquals");
  176. const char* optional = parentNode.getChildNode("Data", x).getAttribute("Optional");
  177. const char* if_flag_not_set_variable = parentNode.getChildNode("Data", x).getAttribute("IfFlagNotSet");
  178. const char* if_flag_set_variable = parentNode.getChildNode("Data", x).getAttribute("IfFlagSet");
  179. //const char* type2criteria = parentNode.getChildNode("Data", x).getAttribute("Type2Criteria"); // JA: LE added to PacketAnalyzer for Items parsing - 12.2012
  180. //const char* criteria = parentNode.getChildNode("Data", x).getAttribute("Criteria"); // JA: LE added to PacketAnalyzer for Items parsing - 12.2012
  181. int8 max_array_size = 0;
  182. try{
  183. if(max_array)
  184. max_array_size = atoi(max_array);
  185. }
  186. catch(...){}
  187. int16 num_size = 1;
  188. try{
  189. if(size)
  190. num_size = atoi(size);
  191. }
  192. catch(...){}
  193. int8 byte_val = 0;
  194. try{
  195. if(default_value)
  196. byte_val = atoi(default_value);
  197. }
  198. catch(...){}
  199. if(substruct && name){
  200. PacketStruct* substruct_packet = getStruct(substruct, packet->GetVersion());
  201. if(substruct_packet){
  202. vector<DataStruct*>::iterator itr;
  203. vector<DataStruct*>* structs = substruct_packet->getStructs();
  204. DataStruct* ds = 0;
  205. int i = 0;
  206. char tmp[10] = {0};
  207. for(i=0;i<num_size;i++){
  208. sprintf(tmp,"%i",i);
  209. for(itr=structs->begin();itr!=structs->end();itr++) {
  210. ds = *itr;
  211. string new_name;
  212. if(array_packet)
  213. new_name = string(name).append("_").append(ds->GetStringName());
  214. else
  215. new_name = string(name).append("_").append(ds->GetStringName()).append("_").append(tmp);
  216. DataStruct* ds2 = new DataStruct(new_name.c_str(), ds->GetType(),ds->GetLength(), ds->GetType2());
  217. if(!array_packet && strlen(ds->GetArraySizeVariable()) > 1)
  218. ds2->SetArraySizeVariable(string(name).append("_").append(ds->GetArraySizeVariable()).append("_").append(tmp).c_str());
  219. ds2->SetOversized(ds->GetOversized());
  220. ds2->SetOversizedByte(ds->GetOversizedByte());
  221. ds2->SetDefaultValue(ds->GetDefaultValue());
  222. ds2->SetMaxArraySize(ds->GetMaxArraySize());
  223. ds2->SetIfSetVariable(ds->GetIfSetVariable());
  224. ds2->SetIfNotSetVariable(ds->GetIfNotSetVariable());
  225. ds2->SetIfEqualsVariable(ds->GetIfEqualsVariable());
  226. ds2->SetIfNotEqualsVariable(ds->GetIfNotEqualsVariable());
  227. ds2->SetIfFlagNotSetVariable(ds->GetIfFlagNotSetVariable());
  228. ds2->SetIfFlagSetVariable(ds->GetIfFlagSetVariable());
  229. ds2->SetIsOptional(ds->IsOptional());
  230. packet->add(ds2);
  231. }
  232. }
  233. if(!array_packet){
  234. i--;
  235. substruct_packet->renameSubstructArray(name, i);
  236. //ds2->SetArraySizeVariable((char*)string(name).append("_").append(ds->GetArraySizeVariable()).append("_").append(tmp).c_str());
  237. packet->addPacketArrays(substruct_packet);
  238. }
  239. safe_delete(substruct_packet);
  240. }
  241. continue;
  242. }
  243. else if(type && strncasecmp(type,"Array", 5)==0 && array_size){
  244. PacketStruct* new_packet = new PacketStruct;
  245. new_packet->SetName(name);
  246. new_packet->IsSubPacket(true);
  247. new_packet->SetVersion(packet->GetVersion());
  248. loadDataStruct(new_packet, parentNode.getChildNode("Data", x), true);
  249. packet->add(new_packet);
  250. }
  251. if(!name || !type)
  252. {
  253. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid Data Element, all elements must include at least an ElementName and Type!");
  254. LogWrite(MISC__WARNING, 0, "Misc", "\tStruct: '%s', version: %i", parentNode.getAttribute("Name"), parentNode.getAttribute("ClientVersion"));
  255. continue;
  256. }
  257. DataStruct* ds = new DataStruct(name, type, num_size, type2);
  258. int8 oversized_value = 0;
  259. int8 oversized_byte_value = 255;
  260. if(oversized){
  261. try{
  262. oversized_value = atoi(oversized);
  263. }
  264. catch(...){}
  265. }
  266. if(oversized_byte){
  267. try{
  268. oversized_byte_value = atoi(oversized_byte);
  269. }
  270. catch(...){}
  271. }
  272. ds->SetOversizedByte(oversized_byte_value);
  273. ds->SetOversized(oversized_value);
  274. ds->SetMaxArraySize(max_array_size);
  275. if(array_size)
  276. ds->SetArraySizeVariable(array_size);
  277. ds->SetDefaultValue(byte_val);
  278. ds->SetIfSetVariable(if_variable);
  279. ds->SetIfNotSetVariable(if_not_variable);
  280. ds->SetIfEqualsVariable(if_equals_variable);
  281. ds->SetIfNotEqualsVariable(if_not_equals_variable);
  282. ds->SetIfFlagNotSetVariable(if_flag_not_set_variable);
  283. ds->SetIfFlagSetVariable(if_flag_set_variable);
  284. if (optional && strlen(optional) > 0 && (strcmp("true", optional) == 0 || strcmp("TRUE", optional) == 0 || strcmp("True", optional) == 0))
  285. ds->SetIsOptional(true);
  286. packet->add(ds);
  287. }
  288. }