diff --git a/inferno/src/inferno/io/file.cpp b/inferno/src/inferno/io/file.cpp index 9b39e89..2a23784 100644 --- a/inferno/src/inferno/io/file.cpp +++ b/inferno/src/inferno/io/file.cpp @@ -6,7 +6,7 @@ namespace Inferno { - std::string File::read(const std::string &path) + std::shared_ptr File::raw(const std::string& path) { // Create input stream object and open file std::ifstream ifstream(path); @@ -26,14 +26,22 @@ namespace Inferno { } // Allocate memory filled with zeros - auto buffer = std::make_unique(length); + auto buffer = std::shared_ptr(new char[length + 1]); // Fill buffer with file contents ifstream.read(buffer.get(), length); ifstream.close(); + // Null termination + buffer[length] = '\0'; + + return buffer; + } + + std::string File::read(const std::string &path) + { // Create string from the buffer and return - return std::string(buffer.get(), length); + return std::string(raw(path).get()); } int32_t File::length(const std::string& path, std::ifstream& ifstream) diff --git a/inferno/src/inferno/io/file.h b/inferno/src/inferno/io/file.h index aa65ad6..d522219 100644 --- a/inferno/src/inferno/io/file.h +++ b/inferno/src/inferno/io/file.h @@ -3,6 +3,7 @@ #include // std::ifstream, std::ofstream #include // std::setfill, std::setw +#include // std::shared_ptr #include // std::string #include "inferno/assert.h" @@ -13,6 +14,7 @@ namespace Inferno { class File { public: + static std::shared_ptr raw(const std::string& path); static std::string read(const std::string& path); static int32_t length(const std::string& path, std::ifstream& ifstream); diff --git a/inferno/src/inferno/io/gltffile.cpp b/inferno/src/inferno/io/gltffile.cpp index 264516a..35c55dd 100644 --- a/inferno/src/inferno/io/gltffile.cpp +++ b/inferno/src/inferno/io/gltffile.cpp @@ -7,6 +7,7 @@ #include "inferno/io/gltffile.h" #include "inferno/io/log.h" #include "inferno/util/string.h" +#include "inferno/util/json.h" namespace Inferno { @@ -18,9 +19,10 @@ namespace Inferno { return glb(path); } else if (extension.compare(".gltf") == 0) { - ASSERT_NOT_REACHED(); + return gltf(path); } + ASSERT(false, "GltfFile unknown file extension!"); return {}; } @@ -98,4 +100,23 @@ namespace Inferno { return { chunkData, chunkLengthInt }; } + std::pair, std::shared_ptr> GltfFile::gltf(const std::string& path) + { + auto jsonContent = File::raw(path); + + json json = json::parse(jsonContent.get()); + ASSERT(Json::hasProperty(json, "buffers"), "GltfFile missing required property 'buffers'"); + + std::shared_ptr binaryContent { nullptr }; + for (auto& [key, buffer] : json["buffers"].items()) { + auto uri = Json::parseStringProperty(buffer, "uri", true); + ASSERT(uri, "GltfFile missing required property 'uri'"); + ASSERT(uri.value().find("data:", 0) == std::string::npos, "GltfFile embedded binary data is unsupported"); + binaryContent = File::raw("assets/gltf/" + uri.value()); + break; + } + + return { jsonContent, binaryContent }; + } + } // namespace Inferno diff --git a/inferno/src/inferno/io/gltffile.h b/inferno/src/inferno/io/gltffile.h index b24cf2f..2a582fa 100644 --- a/inferno/src/inferno/io/gltffile.h +++ b/inferno/src/inferno/io/gltffile.h @@ -15,6 +15,8 @@ namespace Inferno { private: static std::pair, std::shared_ptr> glb(const std::string& path); static std::pair, uint32_t> readChunk(std::ifstream& ifstream, uint32_t offset, uint32_t type); + + static std::pair, std::shared_ptr> gltf(const std::string& path); }; } // namespace Inferno