Added UTF-8 path support on Windows.

Ref issue #82.
This commit is contained in:
Muzychenko Andrey 2021-11-06 19:22:56 +03:00
parent ecdf802d68
commit 683204519c
8 changed files with 39 additions and 9 deletions

View File

@ -297,7 +297,7 @@ void DatFile::Finalize()
// PINBALL2.MID is an alternative font provided in 3DPB data
// Scaled down because it is too large for top text box
/*auto file = pinball::make_path_name("PINBALL2.MID");
auto fileHandle = fopen(file.c_str(), "rb");
auto fileHandle = fopenu(file.c_str(), "rb");
fseek(fileHandle, 0, SEEK_END);
auto fileSize = static_cast<uint32_t>(ftell(fileHandle));
auto rcData = reinterpret_cast<MsgFont*>(new uint8_t[fileSize]);

View File

@ -57,7 +57,7 @@ void Sound::PlaySound(Mix_Chunk* wavePtr, int time)
Mix_Chunk* Sound::LoadWaveFile(const std::string& lpName)
{
auto wavFile = fopen(lpName.c_str(), "r");
auto wavFile = fopenu(lpName.c_str(), "r");
if (!wavFile)
return nullptr;
fclose(wavFile);

View File

@ -34,6 +34,25 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
{
return MainActual(lpCmdLine);
}
// fopen to _wfopen adapter, for UTF-8 paths
FILE* fopenu(const char* path, const char* opt)
{
wchar_t* wideArgs[2]{};
for (auto& arg : wideArgs)
{
auto src = wideArgs[0] ? opt : path;
auto length = MultiByteToWideChar(CP_UTF8, 0, src, -1, nullptr, 0);
arg = new wchar_t[length];
MultiByteToWideChar(CP_UTF8, 0, src, -1, arg, length);
}
auto fileHandle = _wfopen(wideArgs[0], wideArgs[1]);
for (auto arg : wideArgs)
delete[] arg;
return fileHandle;
}
#endif
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu

View File

@ -158,7 +158,7 @@ int loader::get_sound_id(int groupIndex)
}
auto filePath = pinball::make_path_name(fileName);
auto file = fopen(filePath.c_str(), "rb");
auto file = fopenu(filePath.c_str(), "rb");
if (file)
{
fread(&wavHeader, 1, sizeof wavHeader, file);

View File

@ -101,11 +101,12 @@ Mix_Music* midi::load_track(std::string fileName)
if (i == 0)
{
auto filePath = basePath + ".MID";
auto fileHandle = fopen(filePath.c_str(), "rb");
auto fileHandle = fopenu(filePath.c_str(), "rb");
if (fileHandle)
{
fclose(fileHandle);
audio = Mix_LoadMUS(filePath.c_str());
auto rw = SDL_RWFromFile(filePath.c_str(), "rb");
audio = Mix_LoadMUS_RW(rw, 1);
}
}
else
@ -115,7 +116,7 @@ Mix_Music* midi::load_track(std::string fileName)
{
// Dump converted MIDI file
/*auto filePath = basePath + ".midi";
FILE* fileHandle = fopen(filePath.c_str(), "wb");
FILE* fileHandle = fopenu(filePath.c_str(), "wb");
fwrite(midi->data(), 1, midi->size(), fileHandle);
fclose(fileHandle);*/
@ -164,7 +165,7 @@ bool midi::play_track(Mix_Music* midi)
/// <returns>Vector that contains MIDI file</returns>
std::vector<uint8_t>* midi::MdsToMidi(std::string file)
{
auto fileHandle = fopen(file.c_str(), "rb");
auto fileHandle = fopenu(file.c_str(), "rb");
if (!fileHandle)
return nullptr;

View File

@ -16,7 +16,7 @@ DatFile* partman::load_records(LPCSTR lpFileName, bool fullTiltMode)
dat8BitBmpHeader bmpHeader{};
dat16BitBmpHeader zMapHeader{};
auto fileHandle = fopen(lpFileName, "rb");
auto fileHandle = fopenu(lpFileName, "rb");
if (fileHandle == nullptr)
return nullptr;

View File

@ -83,4 +83,14 @@ int Sign(T val)
return (T(0) < val) - (val < T(0));
}
// UTF-8 path adapter for fopen on Windows, implemented in SpaceCadetPinball.cpp
#ifdef _WIN32
extern FILE* fopenu(const char* path, const char* opt);
#else
inline FILE* fopenu(const char* path, const char* opt)
{
return fopen(path, opt);
}
#endif
#endif //PCH_H

View File

@ -82,7 +82,7 @@ int winmain::WinMain(LPCSTR lpCmdLine)
{
auto datFileName = datFileNames[i];
auto datFilePath = pinball::make_path_name(datFileName);
auto datFile = fopen(datFilePath.c_str(), "r");
auto datFile = fopenu(datFilePath.c_str(), "r");
if (datFile)
{
fclose(datFile);