region_map.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "region_map.h"
  2. #include "region_map_v1.h"
  3. #include "../../common/Log.h"
  4. #ifdef WIN32
  5. #define _snprintf snprintf
  6. #include <WinSock2.h>
  7. #include <windows.h>
  8. #endif
  9. #include <boost/regex.hpp>
  10. #include <boost/filesystem.hpp>
  11. #include <boost/foreach.hpp>
  12. #include <boost/algorithm/string.hpp>
  13. #include <algorithm>
  14. #include <cctype>
  15. #include <stdio.h>
  16. #include <fstream>
  17. #include <string.h>
  18. /**
  19. * @param name
  20. * @return
  21. */
  22. inline bool file_exists(const std::string& name) {
  23. std::ifstream f(name.c_str());
  24. return f.good();
  25. }
  26. /**
  27. * @param zone_name
  28. * @return
  29. */
  30. RegionMap* RegionMap::LoadRegionMapfile(std::string filename, std::string zone_name) {
  31. std::string loadedFile = "Regions/";
  32. loadedFile += filename;
  33. loadedFile += ".EQ2Region";
  34. FILE* f = fopen(loadedFile.c_str(), "rb");
  35. LogWrite(REGION__DEBUG, 7, "Region", "Attempting load of %s", filename.c_str());
  36. if (!f)
  37. {
  38. LogWrite(REGION__ERROR, 7, "Region", "Failed to load of %s", filename.c_str());
  39. return nullptr;
  40. }
  41. // Read the string for the zone file name this was created for
  42. int8 strSize;
  43. char name[256];
  44. fread(&strSize, sizeof(int8), 1, f);
  45. LogWrite(REGION__DEBUG, 7, "Region", "strSize = %u", strSize);
  46. size_t len = fread(&name, sizeof(char), strSize, f);
  47. name[len] = '\0';
  48. LogWrite(REGION__DEBUG, 7, "Region", "name = %s", name);
  49. string inFileName(name);
  50. boost::algorithm::to_lower(inFileName);
  51. string zoneNameLwr(zone_name);
  52. boost::algorithm::to_lower(zoneNameLwr);
  53. std::size_t found = inFileName.find(zoneNameLwr);
  54. // Make sure file contents are for the correct zone
  55. if (found == std::string::npos) {
  56. fclose(f);
  57. LogWrite(REGION__ERROR, 0, "Region", "RegionMap::LoadRegionMapfile() map contents (%s) do not match its name (%s).", inFileName, zoneNameLwr.c_str());
  58. return nullptr;
  59. }
  60. int32 regionMapVersion;
  61. fread(&regionMapVersion, sizeof(int32), 1, f);
  62. LogWrite(REGION__INFO, 0, "Region", "Loading %s RegionMapVersion = %u", name, regionMapVersion);
  63. RegionMapV1* regionmap = new RegionMapV1();
  64. regionmap->Load(f, zoneNameLwr, regionMapVersion);
  65. return regionmap;
  66. }
  67. void RegionMapRange::AddVersionRange(std::string zoneName) {
  68. boost::filesystem::path targetDir("Regions/");
  69. // crash fix since the dir isn't present
  70. if(!boost::filesystem::is_directory(targetDir))
  71. {
  72. LogWrite(REGION__ERROR, 7, "Region", "Unable to find directory %s", targetDir.c_str());
  73. return;
  74. }
  75. boost::filesystem::recursive_directory_iterator iter(targetDir), eod;
  76. boost::smatch base_match;
  77. std::string formula = "(.*\\/|.*\\\\)((" + zoneName + ")(\\-([0-9]+)\\-([0-9]+))?)\\.EQ2Region$";
  78. boost::regex re(formula.c_str());
  79. LogWrite(REGION__INFO, 0, "Region", "Region Formula to match: %s\n", formula.c_str());
  80. BOOST_FOREACH(boost::filesystem::path
  81. const & i, make_pair(iter, eod)) {
  82. if (is_regular_file(i)) {
  83. std::string fileName(i.string());
  84. if (boost::regex_match(fileName, base_match, re)) {
  85. boost::ssub_match base_sub_match = base_match[2];
  86. boost::ssub_match base_sub_match2 = base_match[5];
  87. boost::ssub_match base_sub_match3 = base_match[6];
  88. std::string baseMatch(base_sub_match.str().c_str());
  89. std::string baseMatch2(base_sub_match2.str().c_str());
  90. std::string baseMatch3(base_sub_match3.str().c_str());
  91. LogWrite(REGION__INFO, 0, "Region", "Region to Load: %s, size: %i, string: %s, min: %s, max: %s\n", fileName.c_str(), base_match.size(), baseMatch.c_str(), baseMatch2.c_str(), baseMatch3.c_str());
  92. RegionMap * regionmap = RegionMap::LoadRegionMapfile(base_sub_match.str().c_str(), zoneName);
  93. int32 min_version = 0, max_version = 0;
  94. if (strlen(base_sub_match2.str().c_str()) > 0)
  95. min_version = atoul(base_sub_match2.str().c_str());
  96. if (strlen(base_sub_match2.str().c_str()) > 0)
  97. max_version = atoul(base_sub_match3.str().c_str());
  98. version_map.insert(std::make_pair(new VersionRange(min_version, max_version), regionmap));
  99. }
  100. }
  101. }
  102. }