Improve handling of input path and map backend

This commit is contained in:
sfan5 2025-02-19 11:57:54 +01:00
parent c93948c200
commit 5016bca232
4 changed files with 51 additions and 13 deletions

View File

@ -372,35 +372,51 @@ std::set<std::string> TileGenerator::getSupportedBackends()
void TileGenerator::openDb(const std::string &input_path)
{
if (dir_exists(input_path.c_str())) {
// ok
} else if (file_exists(input_path.c_str())) {
throw std::runtime_error("Input path is a file, it should point to the world folder instead");
} else {
throw std::runtime_error("Input path does not exist");
}
std::string input = input_path;
if (input.back() != PATH_SEPARATOR)
input += PATH_SEPARATOR;
std::string backend = m_backend;
if (backend.empty()) {
std::ifstream ifs(input + "world.mt");
if(!ifs.good())
std::string backend = m_backend;
if (backend.empty() && !ifs.good()) {
throw std::runtime_error("Failed to open world.mt");
} else if (backend.empty()) {
backend = read_setting_default("backend", ifs, "sqlite3");
ifs.close();
}
if (backend == "sqlite3")
if (backend == "dummy") {
throw std::runtime_error("This map uses the dummy backend and contains no data");
} else if (backend == "sqlite3") {
m_db = new DBSQLite3(input);
#if USE_POSTGRESQL
else if (backend == "postgresql")
} else if (backend == "postgresql") {
m_db = new DBPostgreSQL(input);
#endif
#if USE_LEVELDB
else if (backend == "leveldb")
} else if (backend == "leveldb") {
m_db = new DBLevelDB(input);
#endif
#if USE_REDIS
else if (backend == "redis")
} else if (backend == "redis") {
m_db = new DBRedis(input);
#endif
else
} else {
throw std::runtime_error(std::string("Unknown map backend: ") + backend);
}
if (!read_setting_default("readonly_backend", ifs, "").empty()) {
std::cerr << "Warning: Map with readonly_backend is not supported. "
"The result may be incomplete." << std::endl;
}
// Determine how we're going to traverse the database (heuristic)
if (m_exhaustiveSearch == EXH_AUTO) {

View File

@ -15,7 +15,7 @@ static void usage()
{
const std::pair<const char*, const char*> options[] = {
{"-i/--input", "<world_path>"},
{"-o/--output", "<output_image.png>"},
{"-o/--output", "<output_image>"},
{"--bgcolor", "<color>"},
{"--scalecolor", "<color>"},
{"--playercolor", "<color>"},
@ -38,7 +38,7 @@ static void usage()
{"--dumpblock", "x,y,z"},
};
const char *top_text =
"minetestmapper -i <world_path> -o <output_image.png> [options]\n"
"minetestmapper -i <world_path> -o <output_image> [options]\n"
"Generate an overview image of a Luanti map.\n"
"\n"
"Options:\n";

View File

@ -1,11 +1,15 @@
#include <stdexcept>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include "util.h"
static std::string trim(const std::string &s)
{
auto isspace = [] (char c) -> bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n'; };
auto isspace = [] (char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
};
size_t front = 0;
while (isspace(s[front]))
@ -20,6 +24,7 @@ static std::string trim(const std::string &s)
std::string read_setting(const std::string &name, std::istream &is)
{
char linebuf[512];
is.seekg(0);
while (is.good()) {
is.getline(linebuf, sizeof(linebuf));
@ -53,3 +58,16 @@ std::string read_setting_default(const std::string &name, std::istream &is,
return def;
}
}
bool file_exists(const char *path)
{
struct stat s{};
// check for !dir to allow symlinks or such
return stat(path, &s) == 0 && (s.st_mode & S_IFDIR) != S_IFDIR;
}
bool dir_exists(const char *path)
{
struct stat s{};
return stat(path, &s) == 0 && (s.st_mode & S_IFDIR) == S_IFDIR;
}

View File

@ -19,3 +19,7 @@ std::string read_setting(const std::string &name, std::istream &is);
std::string read_setting_default(const std::string &name, std::istream &is,
const std::string &def);
bool file_exists(const char *path);
bool dir_exists(const char *path);