Skip to content

Commit

Permalink
Extract the JSON writing into a writeJson function
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Jan 28, 2024
1 parent 0c43e0e commit c33cda1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
11 changes: 10 additions & 1 deletion include/fastgltf/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ namespace fastgltf {
std::filesystem::path getBufferFilePath(const Asset& asset, std::size_t index);
std::filesystem::path getImageFilePath(const Asset& asset, std::size_t index, MimeType mimeType);

std::string writeJson(const Asset& asset);

public:
/**
* Sets the base folder where images URIs should be based of from,
Expand All @@ -858,9 +860,13 @@ namespace fastgltf {
void setImagePath(std::filesystem::path folder);

/**
* Converts the Asset into a glTF JSON string.
* Generates a glTF JSON string from the given asset.
*/
Expected<ExportResult<std::string>> writeGltfJson(const Asset& asset, ExportOptions options = ExportOptions::None);

/**
* Generates a glTF binary (GLB) blob from the given asset.
*/
Expected<ExportResult<std::vector<std::byte>>> writeGltfBinary(const Asset& asset, ExportOptions options = ExportOptions::None);
};

Expand All @@ -874,6 +880,9 @@ namespace fastgltf {
using Exporter::writeGltfBinary;

public:
/**
* Writes a glTF JSON string generated from the given asset to the file pointed to by target.
*/
Error writeGltfJson(const Asset& asset, std::filesystem::path target, ExportOptions options = ExportOptions::None);
Error writeGltfBinary(const Asset& asset, std::filesystem::path target, ExportOptions options = ExportOptions::None);
};
Expand Down
50 changes: 27 additions & 23 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4321,18 +4321,7 @@ fs::path fg::Exporter::getImageFilePath(const Asset& asset, std::size_t index, M
return imageFolder / (name + std::to_string(index) + std::string(extension));
}

fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const Asset& asset, ExportOptions nOptions) {
bufferPaths.clear();
imagePaths.clear();
options = nOptions;

if (hasBit(options, ExportOptions::ValidateAsset)) {
auto validation = validate(asset);
if (validation != Error::None) {
return Expected<ExportResult<std::string>>{errorCode};
}
}

std::string fg::Exporter::writeJson(const fastgltf::Asset &asset) {
// Fairly rudimentary approach of just composing the JSON string using a std::string.
std::string outputString;

Expand Down Expand Up @@ -4372,6 +4361,23 @@ fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const As
prettyPrintJson(outputString);
}

return outputString;
}

fg::Expected<fg::ExportResult<std::string>> fg::Exporter::writeGltfJson(const Asset& asset, ExportOptions nOptions) {
bufferPaths.clear();
imagePaths.clear();
options = nOptions;

if (hasBit(options, ExportOptions::ValidateAsset)) {
auto validation = validate(asset);
if (validation != Error::None) {
return Expected<ExportResult<std::string>>{errorCode};
}
}

// Fairly rudimentary approach of just composing the JSON string using a std::string.
std::string outputString = writeJson(asset);
if (errorCode != Error::None) {
return Expected<ExportResult<std::string>> { errorCode };
}
Expand All @@ -4392,21 +4398,19 @@ fg::Expected<fg::ExportResult<std::vector<std::byte>>> fg::Exporter::writeGltfBi
options &= (~ExportOptions::PrettyPrintJson);

ExportResult<std::vector<std::byte>> result;

// Generate the JSON.
auto expected = writeGltfJson(asset, options);
if (expected.error() != Error::None) {
return Expected<ExportResult<std::vector<std::byte>>> { expected.error() };
auto json = writeJson(asset);
if (errorCode != Error::None) {
return Expected<ExportResult<std::vector<std::byte>>> { errorCode };
}
auto& json = expected.get();
result.bufferPaths = std::move(json.bufferPaths);
result.imagePaths = std::move(json.imagePaths);

result.bufferPaths = std::move(bufferPaths);
result.imagePaths = std::move(imagePaths);

const bool withEmbeddedBuffer = !asset.buffers.empty()
&& std::get_if<sources::Vector>(&asset.buffers.front().data) != nullptr // We only support writing Vectors as embedded buffers
&& asset.buffers.front().byteLength < std::numeric_limits<decltype(BinaryGltfChunk::chunkLength)>::max();

std::size_t binarySize = sizeof(BinaryGltfHeader) + sizeof(BinaryGltfChunk) + json.output.size();
std::size_t binarySize = sizeof(BinaryGltfHeader) + sizeof(BinaryGltfChunk) + json.size();
if (withEmbeddedBuffer) {
binarySize += sizeof(BinaryGltfChunk) + asset.buffers.front().byteLength;
}
Expand All @@ -4425,10 +4429,10 @@ fg::Expected<fg::ExportResult<std::vector<std::byte>>> fg::Exporter::writeGltfBi

BinaryGltfChunk jsonChunk;
jsonChunk.chunkType = binaryGltfJsonChunkMagic;
jsonChunk.chunkLength = json.output.size();
jsonChunk.chunkLength = json.size();
write(&jsonChunk, sizeof jsonChunk);

write(json.output.data(), json.output.size() * sizeof(decltype(json.output)::value_type));
write(json.data(), json.size() * sizeof(decltype(json)::value_type));

if (withEmbeddedBuffer) {
const auto& buffer = asset.buffers.front();
Expand Down

0 comments on commit c33cda1

Please sign in to comment.