mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 01:24:53 +01:00
Refactor info generation
This commit is contained in:
parent
8bbc75d42f
commit
db59b73856
4 changed files with 144 additions and 56 deletions
|
@ -3,6 +3,7 @@ link_directories(${ZLIB_LIBRARY_DIRS} ${LIBPNG_LIBRARY_DIRS})
|
|||
|
||||
add_executable(MinedMap
|
||||
MinedMap.cpp
|
||||
Info.cpp
|
||||
NBT/Tag.cpp
|
||||
Resource/Biome.cpp
|
||||
Resource/BlockType.cpp
|
||||
|
|
82
src/Info.cpp
Normal file
82
src/Info.cpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include "Info.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
namespace MinedMap {
|
||||
|
||||
void Info::writeJSON(const char *filename) {
|
||||
const std::string tmpfile = std::string(filename) + ".tmp";
|
||||
|
||||
FILE *f = fopen(tmpfile.c_str(), "w");
|
||||
if (!f) {
|
||||
std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(f, "{\n");
|
||||
fprintf(f, " \"info\" : {\n");
|
||||
fprintf(f, " \"minX\" : %i,\n", minX);
|
||||
fprintf(f, " \"maxX\" : %i,\n", maxX);
|
||||
fprintf(f, " \"minZ\" : %i,\n", minZ);
|
||||
fprintf(f, " \"maxZ\" : %i\n", maxZ);
|
||||
fprintf(f, " },\n");
|
||||
fprintf(f, " \"regions\" : [\n");
|
||||
|
||||
for (int z = minZ; z <= maxZ; z++) {
|
||||
fprintf(f, " [");
|
||||
|
||||
for (int x = minX; x <= maxX; x++) {
|
||||
fprintf(f, "%s", regions.count(std::make_pair(x, z)) ? "true" : "false");
|
||||
|
||||
if (x < maxX)
|
||||
fprintf(f, ", ");
|
||||
}
|
||||
|
||||
if (z < maxZ)
|
||||
fprintf(f, "],\n");
|
||||
else
|
||||
fprintf(f, "]\n");
|
||||
}
|
||||
|
||||
fprintf(f, " ]\n");
|
||||
fprintf(f, "}\n");
|
||||
|
||||
fclose(f);
|
||||
|
||||
if (std::rename(tmpfile.c_str(), filename) < 0) {
|
||||
std::fprintf(stderr, "Unable to save %s: %s\n", filename, std::strerror(errno));
|
||||
unlink(tmpfile.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
57
src/Info.hpp
Normal file
57
src/Info.hpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace MinedMap {
|
||||
|
||||
class Info {
|
||||
private:
|
||||
std::set<std::pair<int, int>> regions;
|
||||
int minX, maxX, minZ, maxZ;
|
||||
|
||||
public:
|
||||
Info() : minX(INT_MAX), maxX(INT_MIN), minZ(INT_MAX), maxZ(INT_MIN) {}
|
||||
|
||||
void addRegion(int x, int z) {
|
||||
regions.insert(std::make_pair(x, z));
|
||||
|
||||
if (x < minX) minX = x;
|
||||
if (x > maxX) maxX = x;
|
||||
if (z < minZ) minZ = z;
|
||||
if (z > maxZ) maxZ = z;
|
||||
}
|
||||
|
||||
void writeJSON(const char *filename);
|
||||
};
|
||||
|
||||
}
|
|
@ -24,8 +24,8 @@
|
|||
*/
|
||||
|
||||
|
||||
#include "Info.hpp"
|
||||
#include "World/Region.hpp"
|
||||
#include "NBT/ListTag.hpp"
|
||||
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
|
@ -153,52 +153,6 @@ static bool checkFilename(const char *name, int *x, int *z) {
|
|||
|
||||
}
|
||||
|
||||
static void writeInfo(const std::string &filename, const std::set<std::pair<int, int>> ®ions, int minX, int maxX, int minZ, int maxZ) {
|
||||
const std::string tmpfile = filename + ".tmp";
|
||||
|
||||
FILE *f = fopen(tmpfile.c_str(), "w");
|
||||
if (!f) {
|
||||
std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
fprintf(f, "{\n");
|
||||
fprintf(f, " \"info\" : {\n");
|
||||
fprintf(f, " \"minX\" : %i,\n", minX);
|
||||
fprintf(f, " \"maxX\" : %i,\n", maxX);
|
||||
fprintf(f, " \"minZ\" : %i,\n", minZ);
|
||||
fprintf(f, " \"maxZ\" : %i\n", maxZ);
|
||||
fprintf(f, " },\n");
|
||||
fprintf(f, " \"regions\" : [\n");
|
||||
|
||||
for (int z = minZ; z <= maxZ; z++) {
|
||||
fprintf(f, " [");
|
||||
|
||||
for (int x = minX; x <= maxX; x++) {
|
||||
fprintf(f, "%s", regions.count(std::make_pair(x, z)) ? "true" : "false");
|
||||
|
||||
if (x < maxX)
|
||||
fprintf(f, ", ");
|
||||
}
|
||||
|
||||
if (z < maxZ)
|
||||
fprintf(f, "],\n");
|
||||
else
|
||||
fprintf(f, "]\n");
|
||||
}
|
||||
|
||||
fprintf(f, " ]\n");
|
||||
fprintf(f, "}\n");
|
||||
|
||||
fclose(f);
|
||||
|
||||
if (std::rename(tmpfile.c_str(), filename.c_str()) < 0) {
|
||||
std::fprintf(stderr, "Unable to save %s: %s\n", filename.c_str(), std::strerror(errno));
|
||||
unlink(tmpfile.c_str());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 3) {
|
||||
std::fprintf(stderr, "Usage: %s <data directory> <output directory>\n", argv[0]);
|
||||
|
@ -216,8 +170,7 @@ int main(int argc, char *argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
std::set<std::pair<int, int>> regions;
|
||||
int minX = INT_MAX, maxX = INT_MIN, minZ = INT_MAX, maxZ = INT_MIN;
|
||||
Info info;
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) != nullptr) {
|
||||
|
@ -225,12 +178,7 @@ int main(int argc, char *argv[]) {
|
|||
if (!checkFilename(entry->d_name, &x, &z))
|
||||
continue;
|
||||
|
||||
if (x < minX) minX = x;
|
||||
if (x > maxX) maxX = x;
|
||||
if (z < minZ) minZ = z;
|
||||
if (z > maxZ) maxZ = z;
|
||||
|
||||
regions.insert(std::make_pair(x, z));
|
||||
info.addRegion(x, z);
|
||||
|
||||
std::string name(entry->d_name);
|
||||
doRegion(inputdir + "/" + name, outputdir + "/" + name.substr(0, name.length()-3) + "png");
|
||||
|
@ -238,7 +186,7 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
closedir(dir);
|
||||
|
||||
writeInfo(outputdir + "/info.json", regions, minX, maxX, minZ, maxZ);
|
||||
info.writeJSON((outputdir + "/info.json").c_str());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue