Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access files using UTF-8 encoding #54

Closed
wants to merge 14 commits into from
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ find_package(Threads REQUIRED)
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_BINARY_DIR}")

# stuff we get from Conan
find_package(Boost 1.76 COMPONENTS system filesystem program_options REQUIRED )
find_package(Boost 1.76 COMPONENTS system filesystem program_options nowide REQUIRED )
find_package(ZLIB MODULE REQUIRED)
find_package(fmt MODULE REQUIRED)
find_package(Iconv MODULE REQUIRED)
Expand Down Expand Up @@ -209,6 +209,7 @@ target_link_libraries(libFBX2glTF
${DRACO_LIB}
Boost::system
Boost::filesystem
Boost::nowide
optimized ${FBXSDK_LIBRARY}
debug ${FBXSDK_LIBRARY_DEBUG}
fmt::fmt
Expand Down
15 changes: 12 additions & 3 deletions src/FBX2glTF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

#include <fstream>
#include <boost/nowide/fstream.hpp>
#include <boost/nowide/filesystem.hpp>
#include <boost/nowide/cstdio.hpp>
#include <iostream>
#include <map>
#include <unordered_map>
Expand All @@ -20,9 +22,14 @@
#include "utils/File_Utils.hpp"
#include "utils/String_Utils.hpp"

// in Fbx2Raw.cpp
extern std::string NativeToUTF8(const std::string& str);

bool verboseOutput = false;

int main(int argc, char* argv[]) {
boost::nowide::nowide_filesystem();

GltfOptions gltfOptions;

CLI::App app{
Expand Down Expand Up @@ -310,6 +317,8 @@ int main(int argc, char* argv[]) {
if (outputPath.empty()) {
// if -o is not given, default to the basename of the .fbx
outputPath = "./" + FileUtils::GetFileBase(inputPath);
} else {
outputPath = NativeToUTF8(outputPath);
}
// the output folder in .gltf mode, not used for .glb
std::string outputFolder;
Expand Down Expand Up @@ -362,7 +371,7 @@ int main(int argc, char* argv[]) {
raw.Condense(gltfOptions.maxSkinningWeights, gltfOptions.normalizeSkinningWeights);
raw.TransformGeometry(gltfOptions.computeNormals);

std::ofstream outStream; // note: auto-flushes in destructor
boost::nowide::ofstream outStream; // note: auto-flushes in destructor
const auto streamStart = outStream.tellp();

outStream.open(modelPath, std::ios::trunc | std::ios::ate | std::ios::out | std::ios::binary);
Expand Down Expand Up @@ -395,7 +404,7 @@ int main(int argc, char* argv[]) {
assert(!outputFolder.empty());

const std::string binaryPath = outputFolder + extBufferFilename;
FILE* fp = fopen(binaryPath.c_str(), "wb");
FILE* fp = boost::nowide::fopen(binaryPath.c_str(), "wb");
if (fp == nullptr) {
fmt::fprintf(stderr, "ERROR:: Couldn't open file '%s' for writing.\n", binaryPath);
return 1;
Expand Down
11 changes: 6 additions & 5 deletions src/fbx/Fbx2Raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

float scaleFactor;

static std::string NativeToUTF8(const std::string& str) {
std::string NativeToUTF8(const std::string& str) {
#if _WIN32
char* u8cstr = nullptr;
#if (_UNICODE || UNICODE)
Expand Down Expand Up @@ -1131,13 +1131,14 @@ bool LoadFBXFile(
FbxManager* pManager = FbxManager::Create();

if (!options.fbxTempDir.empty()) {
pManager->GetXRefManager().AddXRefProject("embeddedFileProject", options.fbxTempDir.c_str());
const auto& fbxTempDir = NativeToUTF8(options.fbxTempDir);
pManager->GetXRefManager().AddXRefProject("embeddedFileProject", fbxTempDir.c_str());
FbxXRefManager::sEmbeddedFileProject = "embeddedFileProject";
pManager->GetXRefManager().AddXRefProject("configurationProject", options.fbxTempDir.c_str());
pManager->GetXRefManager().AddXRefProject("configurationProject", fbxTempDir.c_str());
FbxXRefManager::sConfigurationProject = "configurationProject";
pManager->GetXRefManager().AddXRefProject("localizationProject", options.fbxTempDir.c_str());
pManager->GetXRefManager().AddXRefProject("localizationProject", fbxTempDir.c_str());
FbxXRefManager::sLocalizationProject = "localizationProject";
pManager->GetXRefManager().AddXRefProject("temporaryFileProject", options.fbxTempDir.c_str());
pManager->GetXRefManager().AddXRefProject("temporaryFileProject", fbxTempDir.c_str());
FbxXRefManager::sTemporaryFileProject = "temporaryFileProject";
}

Expand Down
3 changes: 2 additions & 1 deletion src/gltf/GltfModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <boost/nowide/fstream.hpp>
#include "GltfModel.hpp"

std::shared_ptr<BufferViewData> GltfModel::GetAlignedBufferView(
Expand Down Expand Up @@ -44,7 +45,7 @@ std::shared_ptr<BufferViewData> GltfModel::AddBufferViewForFile(
}

std::shared_ptr<BufferViewData> result;
std::ifstream file(filename, std::ios::binary | std::ios::ate);
boost::nowide::ifstream file(filename, std::ios::binary | std::ios::ate);
if (file) {
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
Expand Down
12 changes: 10 additions & 2 deletions src/gltf/Raw2Gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <cassert>
#include <cstdint>
#include <fstream>
#include <boost/nowide/fstream.hpp>
#include <iostream>

#include <stb_image.h>
Expand Down Expand Up @@ -77,7 +77,7 @@ static const std::vector<TriangleIndex> getIndexArray(const RawModel& raw) {
}

ModelData* Raw2Gltf(
std::ofstream& gltfOutStream,
boost::nowide::ofstream& gltfOutStream,
const std::string& outputFolder,
const RawModel& raw,
const GltfOptions& options) {
Expand Down Expand Up @@ -155,6 +155,14 @@ ModelData* Raw2Gltf(

for (int i = 0; i < raw.GetAnimationCount(); i++) {
const RawAnimation& animation = raw.GetAnimation(i);

if (animation.channels.empty()) {
fmt::printf(
"Animation '%s' has no channels, skipped\n",
animation.name.c_str()
);
continue;
}

auto accessor = gltf->AddAccessorAndView(buffer, GLT_FLOAT, animation.times);
accessor->min = {*std::min_element(std::begin(animation.times), std::end(animation.times))};
Expand Down
3 changes: 2 additions & 1 deletion src/gltf/Raw2Gltf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <memory>
#include <string>
#include <boost/nowide/fstream.hpp>

// This can be a macro under Windows, confusing Draco
#undef ERROR
Expand Down Expand Up @@ -203,7 +204,7 @@ struct ModelData {
};

ModelData* Raw2Gltf(
std::ofstream& gltfOutStream,
boost::nowide::ofstream& gltfOutStream,
const std::string& outputFolder,
const RawModel& raw,
const GltfOptions& options);
3 changes: 2 additions & 1 deletion src/gltf/TextureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "TextureBuilder.hpp"

#include <boost/nowide/cstdio.hpp>
#include <stb_image.h>
#include <stb_image_write.h>

Expand Down Expand Up @@ -140,7 +141,7 @@ std::shared_ptr<TextureData> TextureBuilder::combine(
} else {
const std::string imageFilename = mergedFilename + (".png");
const std::string imagePath = outputFolder + imageFilename;
FILE* fp = fopen(imagePath.c_str(), "wb");
FILE* fp = boost::nowide::fopen(imagePath.c_str(), "wb");
if (fp == nullptr) {
fmt::printf("Warning:: Couldn't write file '%s' for writing.\n", imagePath);
return nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/File_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "File_Utils.hpp"

#include <fstream>
#include <boost/nowide/fstream.hpp>
#include <set>
#include <string>
#include <vector>
Expand Down Expand Up @@ -57,7 +57,7 @@ bool CreatePath(const std::string path) {
}

bool CopyFile(const std::string& srcFilename, const std::string& dstFilename, bool createPath) {
std::ifstream srcFile(srcFilename, std::ios::binary);
boost::nowide::ifstream srcFile(srcFilename, std::ios::binary);
if (!srcFile) {
fmt::printf("Warning: Couldn't open file %s for reading.\n", srcFilename);
return false;
Expand All @@ -72,7 +72,7 @@ bool CopyFile(const std::string& srcFilename, const std::string& dstFilename, bo
return false;
}

std::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc);
boost::nowide::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc);
if (!dstFile) {
fmt::printf("Warning: Couldn't open file %s for writing.\n", dstFilename);
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/utils/Image_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
#include <algorithm>
#include <string>

#define STBI_WINDOWS_UTF8
#define STB_IMAGE_IMPLEMENTATION

#include <stb_image.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION

#include <stb_image_write.h>

namespace ImageUtils {
Expand Down