2#include "JsonSerialization.h"
4#include "Foundation/Logging/Logger.h"
5#include "Foundation/Platform/IO.h"
9namespace fs = std::filesystem;
13 using namespace Cogs::Core::TerrainProvider;
17 uint64_t combine(uint32_t a, uint32_t b)
19 return (uint64_t(a) << 32) | uint64_t(b);
23 std::string buildPath(
const std::string& pathPrefix,
unsigned i,
unsigned j,
unsigned level, TerrainProvider::MimeType& kind)
27 case TerrainProvider::MimeType::None: suffix =
".none";
break;
28 case TerrainProvider::MimeType::Png: suffix =
".png";
break;
29 case TerrainProvider::MimeType::Gif: suffix =
".gif";
break;
30 case TerrainProvider::MimeType::Jpeg: suffix =
".jpg";
break;
31 case TerrainProvider::MimeType::Tiff: suffix =
".tif";
break;
32 case TerrainProvider::MimeType::F32: suffix =
".f32";
break;
33 case TerrainProvider::MimeType::RGBA8:suffix =
".rgba";
break;
39 std::string filename = std::to_string(j) + suffix;
41 return Cogs::IO::combine(pathPrefix, Cogs::IO::combine(std::to_string(level), Cogs::IO::combine(std::to_string(i), filename)));
49 assert(providerConf->cacheKey != NoString);
51 mimeType = providerConf->mimeType;
52 if (mimeType == MimeType::None) {
53 LOG_ERROR(logger,
"No mimetype set.");
59 rootPath = IO::combine(cachesPath.
to_string(), Strings::getC(providerConf->cacheKey));
60 offline = providerConf->offline;
63 if (!fs::exists(rootPath, ec) && !ec) {
65 LOG_ERROR(logger,
"Path '%s' does not exist.", rootPath.c_str());
69 if (!fs::create_directories(rootPath, ec) && !ec) {
70 LOG_ERROR(logger,
"Failed to create path '%s'", rootPath.c_str());
74 LOG_ERROR(logger,
"Failure when creating path %s: %s", rootPath.c_str(), ec.message().c_str());
80 LOG_ERROR(logger,
"Failure when checking path %s: %s", rootPath.c_str(), ec.message().c_str());
86 auto paramPath = IO::combine(rootPath,
"RasterSourceParameters.json");
87 auto paramFile = IO::readFileSync(paramPath);
90 auto cacheConf = deserializeRastersourceConfig(context,
StringView((
const char*)paramFile->ptr, paramFile->size));
92 if (!cacheConf)
return false;
94 if (!providerConf->compatible(cacheConf.get())) {
95 LOG_ERROR(logger,
"Incompatible configurations");
102 LOG_ERROR(logger,
"Failed to read %s", paramPath.c_str());
107 if (!fs::is_empty(rootPath)) {
108 LOG_ERROR(logger,
"%s is not empty and does not contain a rastersource config file", rootPath.c_str());
113 if (!serializeConfig(buf, providerConf)) {
114 LOG_ERROR(logger,
"Failed to serialize rastersource");
117 if (!IO::writeBinaryFile(paramPath, buf.data(), buf.size())) {
118 LOG_ERROR(logger,
"Error writing %s", paramPath.c_str());
126 LOG_DEBUG(logger,
"Using disk cache at %s%s", rootPath.c_str(), offline ?
" (offline)" :
"");
134 if (!initialized)
return false;
137 auto tilePath = buildPath(rootPath,
id.i,
id.j,
id.level, kind);
138 std::ifstream in(tilePath, std::ios::binary | std::ios::in | std::ios::ate);
143 in.seekg(0, std::ios::end);
144 size_t file_size = in.tellg();
145 contents.resize(file_size,
false);
147 in.seekg(0, std::ios::beg);
148 in.read((
char*)contents.data(), contents.size());
151 return contents.size() != 0;
156 if (contents.size() == 0 && debugLog.
size() == 0)
return true;
160 auto tilePath = buildPath(rootPath,
id.i,
id.j,
id.level, kind);
161 auto parentPath = fs::path(tilePath).parent_path();
163 if (!fs::exists(parentPath, ec) && !ec) {
165 if (!fs::create_directories(parentPath, ec) && !ec) {
166 LOG_ERROR(logger,
"Failed to create path: %s", parentPath.generic_string().c_str());
170 LOG_ERROR(logger,
"Failure when creating path %s: %s", parentPath.generic_string().c_str(), ec.message().c_str());
175 LOG_ERROR(logger,
"Failure checking tile path: %s", ec.message().c_str());
180 if (contents.size() != 0 && kind != MimeType::None) {
182 if (kind != mimeType) {
183 LOG_ERROR(logger,
"Mismatched mime-type, cache mime-type=%s, tile mime-type=%s",
184 Strings::getC(mimeTypeString(mimeType)),
185 Strings::getC(mimeTypeString(kind)));
189 if (!IO::writeBinaryFile(tilePath, contents.data(), contents.size())) {
190 LOG_ERROR(logger,
"Error writing %s", tilePath.c_str());
195 if (!debugLog.
empty()) {
196 auto logPath = tilePath +
".txt";
197 if (!IO::writeBinaryFile(logPath, debugLog.
data(), debugLog.
size())) {
198 LOG_ERROR(logger,
"Error writing debug log %s", logPath.c_str());
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Log implementation class.
Provides a weakly referenced view over the contents of a string.
constexpr const char * data() const noexcept
Get the sequence of characters referenced by the string view.
constexpr size_t size() const noexcept
Get the size of the string.
constexpr bool empty() const noexcept
Check if the string is empty.
std::string to_string() const
String conversion method.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
constexpr Log getLogger(const char(&name)[LEN]) noexcept