Skip to content

Commit 21c20c8

Browse files
committed
fix unicode characters like Umlaute üäÜÖßéè
1 parent 1692a55 commit 21c20c8

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ endif()
1717

1818
set(VERSION_MAJOR 1)
1919
set(VERSION_MINOR 6)
20-
set(VERSION_PATCH 2)
20+
set(VERSION_PATCH 3)
2121
set(VERSION_TWEAK 0)
2222

2323
set(VERSION_RC "${CMAKE_CURRENT_BINARY_DIR}/version.rc")

header/Defines.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ inline void Message(const char* format, ...)
2929
inline void Info(const char* format, ...)
3030
{
3131
#ifdef _DEBUG
32+
const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
33+
[[maybe_unused]] const auto success = SetConsoleTextAttribute(hConsole, 0); // white
3234
va_list args;
3335
va_start(args, format);
3436
vprintf(format, args);
@@ -39,8 +41,8 @@ inline void Info(const char* format, ...)
3941
inline void Warning(const char* format, ...)
4042
{
4143
#ifdef _DEBUG
42-
static HANDLE hConsole = GetStdHandle(STD_ERROR_HANDLE);
43-
[[maybe_unused]] static auto success = SetConsoleTextAttribute(hConsole, 6);
44+
const HANDLE hConsole = GetStdHandle(STD_ERROR_HANDLE);
45+
[[maybe_unused]] const auto success = SetConsoleTextAttribute(hConsole, 6); // yellow
4446
va_list args;
4547
va_start(args, format);
4648
vfprintf(stderr, format, args);

header/Utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ namespace utils
1212
vec.erase(found);
1313
}
1414
}
15+
16+
inline std::wstring utf8_to_wstring(const std::string& utf8str) {
17+
if (utf8str.empty()) return {};
18+
19+
// Calculate the number of wide characters needed for the conversion
20+
const int count = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, nullptr, 0);
21+
if (count == 0) throw std::runtime_error("Failed to convert UTF-8 to UTF-16");
22+
23+
std::wstring wstr(count, 0);
24+
MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, wstr.data(), count);
25+
26+
return wstr;
27+
}
1528
}

modules/ModfileLoader.ixx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import <filesystem>;
1515
import ModfileLoader.TpfReader;
1616

1717
export class ModfileLoader {
18-
std::string file_name;
18+
std::filesystem::path file_name;
1919
const std::string TPF_PASSWORD{
2020
0x73, 0x2A, 0x63, 0x7D, 0x5F, 0x0A, static_cast<char>(0xA6), static_cast<char>(0xBD),
2121
0x7D, 0x65, 0x7E, 0x67, 0x61, 0x2A, 0x7F, 0x7F,
@@ -26,7 +26,7 @@ export class ModfileLoader {
2626
};
2727

2828
public:
29-
ModfileLoader(const std::string& fileName);
29+
ModfileLoader(const std::filesystem::path& fileName);
3030

3131
std::vector<TexEntry> GetContents();
3232

@@ -39,15 +39,15 @@ private:
3939
void LoadEntries(libzippp::ZipArchive& archive, std::vector<TexEntry>& entries);
4040
};
4141

42-
ModfileLoader::ModfileLoader(const std::string& fileName)
42+
ModfileLoader::ModfileLoader(const std::filesystem::path& fileName)
4343
{
44-
file_name = std::filesystem::absolute(fileName).string();
44+
file_name = std::filesystem::absolute(fileName);
4545
}
4646

4747
std::vector<TexEntry> ModfileLoader::GetContents()
4848
{
4949
try {
50-
return file_name.ends_with(".tpf") ? GetTpfContents() : GetFileContents();
50+
return file_name.wstring().ends_with(L".tpf") ? GetTpfContents() : GetFileContents();
5151
}
5252
catch (const std::exception&) {
5353
Warning("Failed to open mod file: %s\n", file_name.c_str());
@@ -81,7 +81,7 @@ std::vector<TexEntry> ModfileLoader::GetFileContents()
8181
{
8282
std::vector<TexEntry> entries;
8383

84-
libzippp::ZipArchive zip_archive(file_name);
84+
libzippp::ZipArchive zip_archive(file_name.string());
8585
zip_archive.open();
8686
LoadEntries(zip_archive, entries);
8787
zip_archive.close();

modules/ModfileLoader_TpfReader.ixx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
export module ModfileLoader.TpfReader;
22

3+
import <filesystem>;
34
import <string>;
45
import <fstream>;
56
import <vector>;
67

78
export class TpfReader {
89
public:
9-
TpfReader(const std::string& path)
10+
TpfReader(const std::filesystem::path& path)
1011
{
1112
file_stream = std::ifstream(path, std::ios::binary);
1213
if (!file_stream.seekg(0, std::ios::end).good() || !file_stream.seekg(0, std::ios::beg).good()) {

modules/TextureClient.ixx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ gsl::owner<TextureFileStruct*> AddFile(TexEntry& entry, const bool compress, con
164164
return texture_file_struct;
165165
}
166166

167-
std::vector<gsl::owner<TextureFileStruct*>> ProcessModfile(const std::string& modfile, const std::filesystem::path& dll_path, const bool compress)
167+
std::vector<gsl::owner<TextureFileStruct*>> ProcessModfile(const std::filesystem::path& modfile, const std::filesystem::path& dll_path, const bool compress)
168168
{
169169
const auto hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
170170
if (FAILED(hr)) return {};
@@ -192,25 +192,29 @@ std::vector<gsl::owner<TextureFileStruct*>> ProcessModfile(const std::string& mo
192192

193193
void TextureClient::LoadModsFromFile(const char* source)
194194
{
195-
static std::vector<std::string> loaded_modfiles{};
195+
static std::vector<std::filesystem::path> loaded_modfiles{};
196196
Message("Initialize: searching in %s\n", source);
197197

198-
std::ifstream file(source);
198+
std::locale::global(std::locale(""));
199+
std::ifstream file(source, std::ios::binary);
199200
if (!file.is_open()) {
200201
Warning("LoadModsFromFile: failed to open modlist.txt for reading; aborting!!!");
201202
return;
202203
}
203204
Message("Initialize: found modlist.txt. Reading\n");
204205
std::string line;
205-
std::vector<std::string> modfiles;
206+
std::vector<std::filesystem::path> modfiles;
206207
while (std::getline(file, line)) {
207208
// Remove newline character
208209
line.erase(std::ranges::remove(line, '\r').begin(), line.end());
209210
line.erase(std::ranges::remove(line, '\n').begin(), line.end());
210211

211-
if (!std::ranges::contains(loaded_modfiles, line)) {
212-
modfiles.push_back(line);
213-
loaded_modfiles.push_back(line);
212+
const auto wline = utils::utf8_to_wstring(line);
213+
const auto fsline = std::filesystem::path(wline);
214+
215+
if (!std::ranges::contains(loaded_modfiles, fsline)) {
216+
modfiles.push_back(fsline);
217+
loaded_modfiles.push_back(fsline);
214218
}
215219
}
216220
auto files_size = 0u;

0 commit comments

Comments
 (0)