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 (std::string const &dataDirPath, bool debugOutput)
 
bool handleArgs (int argc, char **argv, int &mapnum, int &tileX, int &tileY, std::string &configFilePath, bool &silent, char *&file, unsigned int &threads)
 
int finish (char const *message, int returnValue)
 
int main (int argc, char **argv)
 

Function Documentation

◆ checkDirectories()

bool checkDirectories ( std::string const &  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 ( char const *  message,
int  returnValue 
)
156{
157 printf("%s", message);
158 getchar(); // Wait for user input
159 return returnValue;
160}

Referenced by main().

◆ handleArgs()

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

References MMAP::executableDirectoryPath().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)
163{
164 unsigned int threads = std::thread::hardware_concurrency();
165 int mapnum = -1;
166 int tileX = -1, tileY = -1;
167 bool silent = false;
168 char* file = nullptr;
169 std::string configFilePath = "mmaps-config.yaml";
170 bool validParam = handleArgs(argc, argv, mapnum,
171 tileX, tileY, configFilePath, silent, file, threads);
172
173 if (!validParam)
174 return silent ? -1 : finish("You have specified invalid parameters", -1);
175
176 auto config = Config::FromFile(configFilePath);
177 if (!config)
178 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);
179
180 if (mapnum == -1 && config->IsDebugOutputEnabled())
181 {
182 if (silent)
183 return -2;
184
185 printf("You have specified debug output, but didn't specify a map to generate.\n");
186 printf("This will generate debug output for ALL maps.\n");
187 printf("Are you sure you want to continue? (y/n) ");
188 if (getchar() != 'y')
189 return 0;
190 }
191
192 if (!checkDirectories(config->DataDirPath(), config->IsDebugOutputEnabled()))
193 return silent ? -3 : finish("Press ENTER to close...", -3);
194
195 MapBuilder builder(&config.value(), mapnum, threads);
196
197 uint32 start = getMSTime();
198 if (file)
199 builder.buildMeshFromFile(file);
200 else if (tileX > -1 && tileY > -1 && mapnum >= 0)
201 builder.buildSingleTile(mapnum, tileX, tileY);
202 else if (mapnum >= 0)
203 builder.buildMaps(uint32(mapnum));
204 else
205 builder.buildMaps({});
206
207 if (!silent)
208 printf("Finished. MMAPS were built in %s\n", secsToTimeString(GetMSTimeDiffToNow(start) / 1000).c_str());
209 return 0;
210}
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(char const *message, int returnValue)
Definition PathGenerator.cpp:155
bool handleArgs(int argc, char **argv, int &mapnum, int &tileX, int &tileY, std::string &configFilePath, bool &silent, char *&file, unsigned int &threads)
Definition PathGenerator.cpp:60
bool checkDirectories(std::string const &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().