Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/utility/fix_embedded_tilesets.cpp
blob: 8f49db7c89360e35a2fa3854bfa36f590de69552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "StarFile.hpp"
#include "StarLogging.hpp"
#include "StarRootLoader.hpp"
#include "StarTilesetDatabase.hpp"

using namespace Star;

void removeCommonPrefix(StringList& a, StringList& b) {
  // Remove elements from a and b until there is one that differs.
  while (a.size() > 0 && b.size() > 0 && a[0] == b[0]) {
    a.eraseAt(0);
    b.eraseAt(0);
  }
}

String createRelativePath(String fromFile, String toFile) {
  if (!File::isDirectory(fromFile))
    fromFile = File::dirName(fromFile);
  fromFile = File::fullPath(fromFile);
  toFile = File::fullPath(toFile);

  StringList fromParts = fromFile.splitAny("/\\");
  StringList toParts = toFile.splitAny("/\\");
  removeCommonPrefix(fromParts, toParts);

  StringList relativeParts;
  for (String part : fromParts)
    relativeParts.append("..");
  relativeParts.appendAll(toParts);

  return relativeParts.join("/");
}

Maybe<Json> repairTileset(Json tileset, String const& mapPath, String const& tilesetPath) {
  if (tileset.contains("source"))
    return {};
  size_t firstGid = tileset.getUInt("firstgid");
  String tilesetName = tileset.getString("name");
  String tilesetFileName = File::relativeTo(tilesetPath, tilesetName + ".json");
  if (!File::exists(tilesetFileName))
    throw StarException::format("Tileset {} does not exist. Can't repair {}", tilesetFileName, mapPath);
  return {JsonObject{{"firstgid", firstGid}, {"source", createRelativePath(mapPath, tilesetFileName)}}};
}

Maybe<Json> repair(Json mapJson, String const& mapPath, String const& tilesetPath) {
  JsonArray tilesets = mapJson.getArray("tilesets");
  bool changed = false;
  for (size_t i = 0; i < tilesets.size(); ++i) {
    if (Maybe<Json> tileset = repairTileset(tilesets[i], mapPath, tilesetPath)) {
      tilesets[i] = *tileset;
      changed = true;
    }
  }
  if (!changed)
    return {};
  return mapJson.set("tilesets", tilesets);
}

void forEachRecursiveFileMatch(String const& dirName, String const& filenameSuffix, function<void(String)> func) {
  for (pair<String, bool> entry : File::dirList(dirName)) {
    if (entry.second)
      forEachRecursiveFileMatch(File::relativeTo(dirName, entry.first), filenameSuffix, func);
    else if (entry.first.endsWith(filenameSuffix))
      func(File::relativeTo(dirName, entry.first));
  }
}

void fixEmbeddedTilesets(String const& searchRoot, String const& tilesetPath) {
  forEachRecursiveFileMatch(searchRoot, ".json", [tilesetPath](String const& path) {
      Json json = Json::parseJson(File::readFileString(path));
      if (json.contains("tilesets")) {
        if (Maybe<Json> fixed = repair(json, path, tilesetPath)) {
          File::writeFile(fixed->repr(2, true), path);
          Logger::info("Repaired {}", path);
        }
      }
    });
}

int main(int argc, char* argv[]) {
  try {
    RootLoader rootLoader({{}, {}, {}, LogLevel::Info, false, {}});
    rootLoader.setSummary("Replaces embedded tilesets in Tiled JSON files with references to external tilesets. Assumes tilesets are available in the packed assets.");
    rootLoader.addArgument("searchRoot", OptionParser::Required);
    rootLoader.addArgument("tilesetsPath", OptionParser::Required);

    RootUPtr root;
    OptionParser::Options options;
    tie(root, options) = rootLoader.commandInitOrDie(argc, argv);

    String searchRoot = options.arguments[0];
    String tilesetPath = options.arguments[1];

    fixEmbeddedTilesets(searchRoot, tilesetPath);

    return 0;
  } catch (std::exception const& e) {
    cerrf("exception caught: {}\n", outputException(e, true));
    return 1;
  }
}