Skip to content

Commit

Permalink
gltfpack: Switch from manual UTF8 conversion to a manifest
Browse files Browse the repository at this point in the history
Since CMake supports manifest files as sources, it's a little easier to
specify UTF-8 as the ANSI encoding via that; this is embedded into .exe
and is used for command line arguments as well so the rest of the code
just works.
  • Loading branch information
zeux committed Sep 14, 2024
1 parent 6177c59 commit cc8822d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ set(GLTF_SOURCES
gltf/write.cpp
)

if(WIN32)
list(APPEND GLTF_SOURCES gltf/gltfpack.manifest)
endif()

if(MSVC)
add_compile_options(/W4)
else()
Expand Down
39 changes: 6 additions & 33 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,12 @@ unsigned int textureMask(const char* arg)
}

#ifndef GLTFFUZZ
int run(int argc, char** argv)
int main(int argc, char** argv)
{
#if !defined(__wasi__)
setlocale(LC_ALL, "C"); // disable locale specific convention for number parsing/printing
#endif

meshopt_encodeVertexVersion(0);
meshopt_encodeIndexVersion(1);

Expand Down Expand Up @@ -1629,43 +1633,12 @@ int run(int argc, char** argv)
}
#endif

#if !defined(_WIN32) && !defined(__wasi__)
int main(int argc, char** argv)
{
setlocale(LC_ALL, "C"); // disable locale specific convention for number parsing/printing
return run(argc, argv);
}
#endif

#ifdef _WIN32
int wmain(int argc, wchar_t* argv[])
{
setlocale(LC_ALL, "en_us.utf8"); // use utf8 as ansi codepage

std::vector<char*> args;
for (int i = 0; i < argc; ++i)
{
std::mbstate_t state = {};
const wchar_t* argw = argv[i];
size_t len = wcsrtombs(NULL, &argw, 0, &state);
if (len == size_t(-1))
return -1;

char* argu = new char[len + 1];
wcsrtombs(argu, &argw, len + 1, &state);
args.push_back(argu);
}

return run(int(args.size()), args.data());
}
#endif

#ifdef __wasi__
extern "C" int pack(int argc, char** argv)
{
chdir("/gltfpack-$pwd");

int result = run(argc, argv);
int result = main(argc, argv);
fflush(NULL);
return result;
}
Expand Down
9 changes: 9 additions & 0 deletions gltf/gltfpack.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity type="win32" name="..." version="6.0.0.0"/>
<application>
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
</windowsSettings>
</application>
</assembly>

0 comments on commit cc8822d

Please sign in to comment.