AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
PathGenerator.cpp File Reference
#include "Config.h"
#include "MapBuilder.h"
#include "PathCommon.h"
#include "Timer.h"
#include "Util.h"
#include <boost/filesystem.hpp>

Go to the source code of this file.

Functions

bool checkDirectories (const std::string &dataDirPath, bool debugOutput)
 
bool handleArgs (int argc, char **argv, int &mapnum, int &tileX, int &tileY, std::string &configFilePath, bool &silent, char *&offMeshInputPath, char *&file, unsigned int &threads)
 
int finish (const char *message, int returnValue)
 
int main (int argc, char **argv)
 

Function Documentation

◆ checkDirectories()

bool checkDirectories ( const std::string &  dataDirPath,
bool  debugOutput 
)
28{
29 std::vector<std::string> dirFiles;
30
31 if (getDirContents(dirFiles, (std::filesystem::path(dataDirPath) / "maps").string()) == LISTFILE_DIRECTORY_NOT_FOUND || dirFiles.empty())
32 {
33 printf("'maps' directory is empty or does not exist\n");
34 return false;
35 }
36
37 dirFiles.clear();
38 if (getDirContents(dirFiles, (std::filesystem::path(dataDirPath) / "vmaps").string(), "*.vmtree") == LISTFILE_DIRECTORY_NOT_FOUND || dirFiles.empty())
39 {
40 printf("'vmaps' directory is empty or does not exist\n");
41 return false;
42 }
43
44 dirFiles.clear();
45 if (getDirContents(dirFiles, (std::filesystem::path(dataDirPath) / "mmaps").string()) == LISTFILE_DIRECTORY_NOT_FOUND)
46 {
47 return boost::filesystem::create_directory((std::filesystem::path(dataDirPath) / "mmaps").string());
48 }
49
50 dirFiles.clear();
51 if (debugOutput && getDirContents(dirFiles, (std::filesystem::path(dataDirPath) / "meshes").string()) == LISTFILE_DIRECTORY_NOT_FOUND)
52 {
53 printf("'meshes' directory does not exist creating...\n");
54 return boost::filesystem::create_directory((std::filesystem::path(dataDirPath) / "meshes").string());
55 }
56
57 return true;
58}
@ LISTFILE_DIRECTORY_NOT_FOUND
Definition PathCommon.h:78
ListFilesResult getDirContents(std::vector< std::string > &fileList, std::string dirpath=".", std::string filter="*")
Definition PathCommon.h:82

References MMAP::getDirContents(), and MMAP::LISTFILE_DIRECTORY_NOT_FOUND.

Referenced by main().

◆ finish()

int finish ( const char *  message,
int  returnValue 
)
165{
166 printf("%s", message);
167 getchar(); // Wait for user input
168 return returnValue;
169}

Referenced by main().

◆ handleArgs()

bool handleArgs ( int  argc,
char **  argv,
int &  mapnum,
int &  tileX,
int &  tileY,
std::string &  configFilePath,
bool &  silent,
char *&  offMeshInputPath,
char *&  file,
unsigned int &  threads 
)
69{
70 bool hasCustomConfigPath = false;
71 char* param = nullptr;
72 for (int i = 1; i < argc; ++i)
73 {
74 if (strcmp(argv[i], "--config") == 0)
75 {
76 param = argv[++i];
77 if (!param)
78 return false;
79
80 hasCustomConfigPath = true;
81 configFilePath = param;
82 }
83 else if (strcmp(argv[i], "--threads") == 0)
84 {
85 param = argv[++i];
86 if (!param)
87 return false;
88 threads = static_cast<unsigned int>(std::max(0, atoi(param)));
89 }
90 else if (strcmp(argv[i], "--file") == 0)
91 {
92 param = argv[++i];
93 if (!param)
94 return false;
95 file = param;
96 }
97 else if (strcmp(argv[i], "--tile") == 0)
98 {
99 param = argv[++i];
100 if (!param)
101 return false;
102
103 char* stileX = strtok(param, ",");
104 char* stileY = strtok(nullptr, ",");
105 int tilex = atoi(stileX);
106 int tiley = atoi(stileY);
107
108 if ((tilex > 0 && tilex < 64) || (tilex == 0 && strcmp(stileX, "0") == 0))
109 tileX = tilex;
110 if ((tiley > 0 && tiley < 64) || (tiley == 0 && strcmp(stileY, "0") == 0))
111 tileY = tiley;
112
113 if (tileX < 0 || tileY < 0)
114 {
115 printf("invalid tile coords.\n");
116 return false;
117 }
118 }
119 else if (strcmp(argv[i], "--silent") == 0)
120 {
121 silent = true;
122 }
123 else if (strcmp(argv[i], "--offMeshInput") == 0)
124 {
125 param = argv[++i];
126 if (!param)
127 return false;
128
129 offMeshInputPath = param;
130 }
131 else
132 {
133 int map = atoi(argv[i]);
134 if (map > 0 || (map == 0 && (strcmp(argv[i], "0") == 0)))
135 mapnum = map;
136 else
137 {
138 printf("invalid map id\n");
139 return false;
140 }
141 }
142 }
143
144 if (!hasCustomConfigPath)
145 {
146 FILE* f = fopen(configFilePath.c_str(), "r");
147 if (!f)
148 {
149 auto execRelPath = std::filesystem::path(executableDirectoryPath())/configFilePath;
150 f = fopen(execRelPath.string().c_str(), "r");
151 if (!f)
152 {
153 printf("Failed to load configuration. Ensure that 'mmaps-config.yaml' exists in the current directory or specify its path using the --config option.'\n");
154 return false;
155 }
156 configFilePath = execRelPath.string();
157 }
158 fclose(f);
159 }
160
161 return true;
162}
std::string executableDirectoryPath()
Definition PathCommon.h:39

References MMAP::executableDirectoryPath().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)
172{
173 unsigned int threads = std::thread::hardware_concurrency();
174 int mapnum = -1;
175 int tileX = -1, tileY = -1;
176 bool silent = false;
177 char* offMeshInputPath = nullptr;
178 char* file = nullptr;
179 std::string configFilePath = "mmaps-config.yaml";
180 bool validParam = handleArgs(argc, argv, mapnum,
181 tileX, tileY, configFilePath, silent, offMeshInputPath, file, threads);
182
183 if (!validParam)
184 return silent ? -1 : finish("You have specified invalid parameters", -1);
185
186 auto config = Config::FromFile(configFilePath);
187 if (!config)
188 return silent ? -1 : finish("Failed to load configuration. Ensure that 'mmaps-config.yaml' exists in the current directory or specify its path using the --config option.", -1);
189
190 if (mapnum == -1 && config->IsDebugOutputEnabled())
191 {
192 if (silent)
193 return -2;
194
195 printf("You have specified debug output, but didn't specify a map to generate.\n");
196 printf("This will generate debug output for ALL maps.\n");
197 printf("Are you sure you want to continue? (y/n) ");
198 if (getchar() != 'y')
199 return 0;
200 }
201
202 if (!checkDirectories(config->DataDirPath(), config->IsDebugOutputEnabled()))
203 return silent ? -3 : finish("Press ENTER to close...", -3);
204
205 MapBuilder builder(&config.value(), mapnum, offMeshInputPath, threads);
206
207 uint32 start = getMSTime();
208 if (file)
209 builder.buildMeshFromFile(file);
210 else if (tileX > -1 && tileY > -1 && mapnum >= 0)
211 builder.buildSingleTile(mapnum, tileX, tileY);
212 else if (mapnum >= 0)
213 builder.buildMaps(uint32(mapnum));
214 else
215 builder.buildMaps({});
216
217 if (!silent)
218 printf("Finished. MMAPS were built in %s\n", secsToTimeString(GetMSTimeDiffToNow(start) / 1000).c_str());
219 return 0;
220}
std::uint32_t uint32
Definition Define.h:107
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:131
uint32 getMSTime()
Definition Timer.h:103
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
Definition Util.cpp:73
static std::optional< Config > FromFile(std::string_view configFile)
Definition Config.cpp:63
Definition MapBuilder.h:123
int finish(const char *message, int returnValue)
Definition PathGenerator.cpp:164
bool handleArgs(int argc, char **argv, int &mapnum, int &tileX, int &tileY, std::string &configFilePath, bool &silent, char *&offMeshInputPath, char *&file, unsigned int &threads)
Definition PathGenerator.cpp:60
bool checkDirectories(const std::string &dataDirPath, bool debugOutput)
Definition PathGenerator.cpp:27

References MMAP::MapBuilder::buildMaps(), MMAP::MapBuilder::buildMeshFromFile(), MMAP::MapBuilder::buildSingleTile(), checkDirectories(), finish(), MMAP::Config::FromFile(), getMSTime(), GetMSTimeDiffToNow(), handleArgs(), and secsToTimeString().