region_map.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "region_map.h"
  2. #include "region_map_v1.h"
  3. #include "../../common/Log.h"
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <fstream>
  9. /**
  10. * @param name
  11. * @return
  12. */
  13. inline bool file_exists(const std::string& name) {
  14. std::ifstream f(name.c_str());
  15. return f.good();
  16. }
  17. /**
  18. * @param zone_name
  19. * @return
  20. */
  21. RegionMap* RegionMap::LoadRegionMapfile(std::string zone_name) {
  22. std::string filename = "Regions/";
  23. filename += zone_name;
  24. filename += ".EQ2Region";
  25. FILE* f = fopen(filename.c_str(), "rb");
  26. LogWrite(REGION__DEBUG, 7, "Region", "Attempting load of %s", filename.c_str());
  27. if (!f)
  28. {
  29. LogWrite(REGION__ERROR, 7, "Region", "Failed to load of %s", filename.c_str());
  30. return nullptr;
  31. }
  32. // Read the string for the zone file name this was created for
  33. int8 strSize;
  34. char name[256];
  35. fread(&strSize, sizeof(int8), 1, f);
  36. LogWrite(REGION__DEBUG, 7, "Region", "strSize = %u", strSize);
  37. size_t len = fread(&name, sizeof(char), strSize, f);
  38. name[len] = '\0';
  39. LogWrite(REGION__DEBUG, 7, "Region", "name = %s", name);
  40. string fileName(name);
  41. std::size_t found = fileName.find(zone_name);
  42. // Make sure file contents are for the correct zone
  43. if (found == std::string::npos) {
  44. fclose(f);
  45. LogWrite(REGION__ERROR, 0, "Region", "WaterMap::LoadWaterMapfile() map contents (%s) do not match its name (%s).", &name, zone_name.c_str());
  46. return nullptr;
  47. }
  48. int32 regionMapVersion;
  49. fread(&regionMapVersion, sizeof(int32), 1, f);
  50. LogWrite(REGION__INFO, 0, "Region", "Loading %s RegionMapVersion = %u", name, regionMapVersion);
  51. RegionMapV1* regionmap = new RegionMapV1();
  52. regionmap->Load(f);
  53. return regionmap;
  54. }