mirror of
https://github.com/minetest/minetestmapper.git
synced 2025-02-23 07:20:24 +01:00
Improve handling of input path and map backend
This commit is contained in:
parent
c93948c200
commit
5016bca232
@ -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::ifstream ifs(input + "world.mt");
|
||||
|
||||
std::string backend = m_backend;
|
||||
if (backend.empty()) {
|
||||
std::ifstream ifs(input + "world.mt");
|
||||
if(!ifs.good())
|
||||
throw std::runtime_error("Failed to open world.mt");
|
||||
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) {
|
||||
|
@ -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";
|
||||
|
20
src/util.cpp
20
src/util.cpp
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user