ConfigReader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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(!latest_version || ( (*iter)->GetVersion() > latest_version->GetVersion() && (*iter)->GetVersion() <= version) )
  73. latest_version = *iter;
  74. }
  75. if(latest_version)
  76. new_latest_version = new PacketStruct(latest_version, version);
  77. }
  78. MStructs.unlock();
  79. if(!new_latest_version)
  80. LogWrite(PACKET__ERROR, 0, "Packet", "Could not find struct named '%s'", name);
  81. return new_latest_version;
  82. }
  83. int16 ConfigReader::GetStructVersion(const char* name, int16 version){
  84. MStructs.lock();
  85. vector<PacketStruct*>* struct_versions = structs[string(name)];
  86. int16 ret = 0;
  87. if(struct_versions){
  88. vector<PacketStruct*>::iterator iter;
  89. PacketStruct* latest_version = 0;
  90. for(iter = struct_versions->begin(); iter != struct_versions->end(); iter++){
  91. if(!latest_version || ( (*iter)->GetVersion() > latest_version->GetVersion() && (*iter)->GetVersion() <= version) )
  92. latest_version = *iter;
  93. }
  94. if(latest_version)
  95. ret = latest_version->GetVersion();
  96. }
  97. MStructs.unlock();
  98. return ret;
  99. }
  100. void ConfigReader::addStruct(const char* name, int16 version, PacketStruct* new_struct){
  101. string strname(name);
  102. vector<PacketStruct*>* struct_versions = structs[strname];
  103. if(struct_versions)
  104. struct_versions->push_back(new_struct);
  105. else{
  106. struct_versions = new vector<PacketStruct*>;
  107. struct_versions->push_back(new_struct);
  108. structs[strname] = struct_versions;
  109. }
  110. }
  111. bool ConfigReader::LoadFile(const char* name){
  112. load_files.push_back(name);
  113. return processXML_Elements(name);
  114. }
  115. bool ConfigReader::processXML_Elements(const char* fileName){
  116. XMLNode xMainNode=XMLNode::openFileHelper(fileName,"EQ2Emulator");
  117. if(xMainNode.isEmpty())
  118. return false;
  119. for(int i=0;i<xMainNode.nChildNode("Struct");i++){
  120. const char* struct_name = xMainNode.getChildNode("Struct", i).getAttribute("Name");
  121. const char* str_version = xMainNode.getChildNode("Struct", i).getAttribute("ClientVersion");
  122. const char* opcode_name = xMainNode.getChildNode("Struct", i).getAttribute("OpcodeName");
  123. const char* opcode_type = xMainNode.getChildNode("Struct", i).getAttribute("OpcodeType");
  124. if(!struct_name || !str_version)
  125. {
  126. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid struct, all structs must include at least a Name and ClientVersion!");
  127. continue;
  128. }
  129. int16 version = 1;
  130. try
  131. {
  132. version = atoi(str_version);
  133. }
  134. catch(...)
  135. {
  136. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid version for struct named '%s': '%s'", struct_name, str_version);
  137. continue;
  138. }
  139. PacketStruct* new_struct = new PacketStruct();
  140. new_struct->SetName(struct_name);
  141. if(opcode_type)
  142. new_struct->SetOpcodeType(opcode_type);
  143. if(opcode_name){
  144. if(!new_struct->SetOpcode(opcode_name)){
  145. safe_delete(new_struct);
  146. continue;
  147. }
  148. }
  149. new_struct->SetVersion(version);
  150. loadDataStruct(new_struct, xMainNode.getChildNode("Struct", i));
  151. addStruct(struct_name, version, new_struct);
  152. }
  153. return true;
  154. }
  155. void ConfigReader::loadDataStruct(PacketStruct* packet, XMLNode parentNode, bool array_packet){
  156. for(int x=0;x<parentNode.nChildNode();x++){
  157. const char* name = parentNode.getChildNode("Data", x).getAttribute("ElementName");
  158. const char* type = parentNode.getChildNode("Data", x).getAttribute("Type");
  159. const char* size = parentNode.getChildNode("Data", x).getAttribute("Size");
  160. const char* type2 = parentNode.getChildNode("Data", x).getAttribute("Type2");
  161. const char* array_size = parentNode.getChildNode("Data", x).getAttribute("ArraySizeVariable");
  162. const char* max_array = parentNode.getChildNode("Data", x).getAttribute("MaxArraySize");
  163. const char* substruct = parentNode.getChildNode("Data", x).getAttribute("Substruct");
  164. const char* default_value = parentNode.getChildNode("Data", x).getAttribute("DefaultByteValue");
  165. const char* oversized = parentNode.getChildNode("Data", x).getAttribute("OversizedValue");
  166. const char* oversized_byte = parentNode.getChildNode("Data", x).getAttribute("OversizedByte");
  167. const char* if_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableSet");
  168. const char* if_not_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableNotSet");
  169. const char* if_equals_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableEquals");
  170. const char* if_not_equals_variable = parentNode.getChildNode("Data", x).getAttribute("IfVariableNotEquals");
  171. //const char* type2criteria = parentNode.getChildNode("Data", x).getAttribute("Type2Criteria"); // JA: LE added to PacketAnalyzer for Items parsing - 12.2012
  172. //const char* criteria = parentNode.getChildNode("Data", x).getAttribute("Criteria"); // JA: LE added to PacketAnalyzer for Items parsing - 12.2012
  173. int8 max_array_size = 0;
  174. try{
  175. if(max_array)
  176. max_array_size = atoi(max_array);
  177. }
  178. catch(...){}
  179. int16 num_size = 1;
  180. try{
  181. if(size)
  182. num_size = atoi(size);
  183. }
  184. catch(...){}
  185. int8 byte_val = 0;
  186. try{
  187. if(default_value)
  188. byte_val = atoi(default_value);
  189. }
  190. catch(...){}
  191. if(substruct && name){
  192. PacketStruct* substruct_packet = getStruct(substruct, packet->GetVersion());
  193. if(substruct_packet){
  194. vector<DataStruct*>::iterator itr;
  195. vector<DataStruct*>* structs = substruct_packet->getStructs();
  196. DataStruct* ds = 0;
  197. int i = 0;
  198. char tmp[10] = {0};
  199. for(i=0;i<num_size;i++){
  200. sprintf(tmp,"%i",i);
  201. for(itr=structs->begin();itr!=structs->end();itr++) {
  202. ds = *itr;
  203. string new_name;
  204. if(array_packet)
  205. new_name = string(name).append("_").append(ds->GetStringName());
  206. else
  207. new_name = string(name).append("_").append(ds->GetStringName()).append("_").append(tmp);
  208. DataStruct* ds2 = new DataStruct(new_name.c_str(), ds->GetType(),ds->GetLength(), ds->GetType2());
  209. if(!array_packet && strlen(ds->GetArraySizeVariable()) > 1)
  210. ds2->SetArraySizeVariable(string(name).append("_").append(ds->GetArraySizeVariable()).append("_").append(tmp).c_str());
  211. ds2->SetOversized(ds->GetOversized());
  212. ds2->SetOversizedByte(ds->GetOversizedByte());
  213. ds2->SetDefaultValue(ds->GetDefaultValue());
  214. ds2->SetMaxArraySize(ds->GetMaxArraySize());
  215. ds2->SetIfSetVariable(ds->GetIfSetVariable());
  216. ds2->SetIfNotSetVariable(ds->GetIfNotSetVariable());
  217. ds2->SetIfEqualsVariable(ds->GetIfEqualsVariable());
  218. ds2->SetIfNotEqualsVariable(ds->GetIfNotEqualsVariable());
  219. packet->add(ds2);
  220. }
  221. }
  222. if(!array_packet){
  223. i--;
  224. substruct_packet->renameSubstructArray(name, i);
  225. //ds2->SetArraySizeVariable((char*)string(name).append("_").append(ds->GetArraySizeVariable()).append("_").append(tmp).c_str());
  226. packet->addPacketArrays(substruct_packet);
  227. }
  228. safe_delete(substruct_packet);
  229. }
  230. continue;
  231. }
  232. else if(type && strncasecmp(type,"Array", 5)==0 && array_size){
  233. PacketStruct* new_packet = new PacketStruct;
  234. new_packet->SetName(name);
  235. new_packet->IsSubPacket(true);
  236. new_packet->SetVersion(packet->GetVersion());
  237. loadDataStruct(new_packet, parentNode.getChildNode("Data", x), true);
  238. packet->add(new_packet);
  239. }
  240. if(!name || !type)
  241. {
  242. LogWrite(MISC__WARNING, 0, "Misc", "Ignoring invalid Data Element, all elements must include at least an ElementName and Type!");
  243. LogWrite(MISC__WARNING, 0, "Misc", "\tStruct: '%s', version: %i", parentNode.getAttribute("Name"), parentNode.getAttribute("ClientVersion"));
  244. continue;
  245. }
  246. DataStruct* ds = new DataStruct(name, type, num_size, type2);
  247. int8 oversized_value = 0;
  248. int8 oversized_byte_value = 255;
  249. if(oversized){
  250. try{
  251. oversized_value = atoi(oversized);
  252. }
  253. catch(...){}
  254. }
  255. if(oversized_byte){
  256. try{
  257. oversized_byte_value = atoi(oversized_byte);
  258. }
  259. catch(...){}
  260. }
  261. ds->SetOversizedByte(oversized_byte_value);
  262. ds->SetOversized(oversized_value);
  263. ds->SetMaxArraySize(max_array_size);
  264. if(array_size)
  265. ds->SetArraySizeVariable(array_size);
  266. ds->SetDefaultValue(byte_val);
  267. ds->SetIfSetVariable(if_variable);
  268. ds->SetIfNotSetVariable(if_not_variable);
  269. ds->SetIfEqualsVariable(if_equals_variable);
  270. ds->SetIfNotEqualsVariable(if_not_equals_variable);
  271. packet->add(ds);
  272. }
  273. }