Switch compiler to MSVC (#16)

* Initial working cmake version
Code style changes according to CLion suggestions
Changed logging to use template instead of macros
---------
Co-authored-by: asesidaa <1061472754@qq.com>

* Reformat everything, updated readme for MSVC
* Warnings annoy me deeply
* Update artifact export
* Simplifying file hierarchy
Build result will now be in dist
This commit is contained in:
KIT!
2024-11-27 21:07:17 +01:00
committed by GitHub
parent b5d9488df6
commit 91a9f9ad25
47 changed files with 1448 additions and 1341 deletions
+71 -59
View File
@@ -1,114 +1,114 @@
#include <codecvt>
#include "helpers.h"
#include <windows.h>
static void
toml_myfree (void *p) {
if (p) {
char *pp = (char *)p;
const char *pp = static_cast<char *> (p);
delete[] pp;
}
}
toml_table_t *
openConfig (std::filesystem::path path) {
if (!std::filesystem::exists (path) || !path.has_filename ()) {
LogMessage (LOG_LEVEL_WARN, (std::string (path.string ()) + ": file does not exist").c_str ());
return 0;
openConfig (const std::filesystem::path &path) {
if (!exists (path) || !path.has_filename ()) {
LogMessage (LogLevel::WARN, std::string (path.string ()) + ": file does not exist");
return nullptr;
}
std::ifstream stream (path);
if (!stream.is_open ()) {
LogMessage (LOG_LEVEL_WARN, ("Could not open " + std::string (path.string ())).c_str ());
return 0;
LogMessage (LogLevel::WARN, ("Could not open " + std::string (path.string ())).c_str ());
return nullptr;
}
stream.seekg (0, stream.end);
size_t length = stream.tellg ();
const size_t length = stream.tellg ();
stream.seekg (0, stream.beg);
char *buf = (char *)calloc (length + 1, sizeof (char));
const auto buf = static_cast<char *> (calloc (length + 1, sizeof (char)));
stream.read (buf, length);
char errorbuf[200];
toml_table_t *config = toml_parse (buf, errorbuf, 200);
char errorBuffer[200];
toml_table_t *config = toml_parse (buf, errorBuffer, 200);
stream.close ();
free (buf);
if (!config) {
LogMessage (LOG_LEVEL_WARN, (path.string () + ": " + errorbuf).c_str ());
return 0;
LogMessage (LogLevel::WARN, (path.string () + ": " + errorBuffer).c_str ());
return nullptr;
}
return config;
}
toml_table_t *
openConfigSection (toml_table_t *config, const std::string &sectionName) {
openConfigSection (const toml_table_t *config, const std::string &sectionName) {
toml_table_t *section = toml_table_in (config, sectionName.c_str ());
if (!section) {
LogMessage (LOG_LEVEL_ERROR, ("Cannot find section " + sectionName).c_str ());
return 0;
LogMessage (LogLevel::ERROR, ("Cannot find section " + sectionName).c_str ());
return nullptr;
}
return section;
}
bool
readConfigBool (toml_table_t *table, const std::string &key, bool notFoundValue) {
toml_datum_t data = toml_bool_in (table, key.c_str ());
if (!data.ok) {
LogMessage (LOG_LEVEL_WARN, ("Could not find Boolean named " + key).c_str ());
readConfigBool (const toml_table_t *table, const std::string &key, const bool notFoundValue) {
const auto [ok, u] = toml_bool_in (table, key.c_str ());
if (!ok) {
LogMessage (LogLevel::WARN, ("Could not find Boolean named " + key).c_str ());
return notFoundValue;
}
return (bool)data.u.b;
return static_cast<bool> (u.b);
}
int64_t
readConfigInt (toml_table_t *table, const std::string &key, int64_t notFoundValue) {
toml_datum_t data = toml_int_in (table, key.c_str ());
if (!data.ok) {
LogMessage (LOG_LEVEL_WARN, ("Could not find Int named " + key).c_str ());
i64
readConfigInt (const toml_table_t *table, const std::string &key, const i64 notFoundValue) {
const auto [ok, u] = toml_int_in (table, key.c_str ());
if (!ok) {
LogMessage (LogLevel::WARN, ("Could not find Int named " + key).c_str ());
return notFoundValue;
}
return data.u.i;
return u.i;
}
const std::string
readConfigString (toml_table_t *table, const std::string &key, const std::string &notFoundValue) {
toml_datum_t data = toml_string_in (table, key.c_str ());
if (!data.ok) {
LogMessage (LOG_LEVEL_WARN, ("Could not find String named " + key).c_str ());
std::string
readConfigString (const toml_table_t *table, const std::string &key, const std::string &notFoundValue) {
const auto [ok, u] = toml_string_in (table, key.c_str ());
if (!ok) {
LogMessage (LogLevel::WARN, ("Could not find String named " + key).c_str ());
return notFoundValue;
}
std::string str = data.u.s;
toml_myfree (data.u.s);
std::string str = u.s;
toml_myfree (u.s);
return str;
}
std::vector<int64_t>
readConfigIntArray (toml_table_t *table, const std::string &key, std::vector<int64_t> notFoundValue) {
toml_array_t *array = toml_array_in (table, key.c_str ());
std::vector<i64>
readConfigIntArray (const toml_table_t *table, const std::string &key, std::vector<i64> notFoundValue) {
const toml_array_t *array = toml_array_in (table, key.c_str ());
if (!array) {
LogMessage (LOG_LEVEL_WARN, ("Could not find int Array named " + key).c_str ());
LogMessage (LogLevel::WARN, ("Could not find int Array named " + key).c_str ());
return notFoundValue;
}
std::vector<int64_t> datas;
std::vector<i64> ret;
for (int i = 0;; i++) {
toml_datum_t data = toml_int_at (array, i);
if (!data.ok) break;
datas.push_back (data.u.i);
auto [ok, u] = toml_int_at (array, i);
if (!ok) break;
ret.push_back (u.i);
}
return datas;
return ret;
}
std::wstring
replace (const std::wstring orignStr, const std::wstring oldStr, const std::wstring newStr) {
size_t pos = 0;
std::wstring tempStr = orignStr;
std::wstring::size_type newStrLen = newStr.length ();
std::wstring::size_type oldStrLen = oldStr.length ();
replace (const std::wstring &orignStr, const std::wstring &oldStr, const std::wstring &newStr) {
size_t pos = 0;
std::wstring tempStr = orignStr;
const std::wstring::size_type newStrLen = newStr.length ();
const std::wstring::size_type oldStrLen = oldStr.length ();
while (true) {
pos = tempStr.find (oldStr, pos);
if (pos == std::wstring::npos) break;
@@ -121,11 +121,11 @@ replace (const std::wstring orignStr, const std::wstring oldStr, const std::wstr
}
std::string
replace (const std::string orignStr, const std::string oldStr, const std::string newStr) {
size_t pos = 0;
std::string tempStr = orignStr;
std::string::size_type newStrLen = newStr.length ();
std::string::size_type oldStrLen = oldStr.length ();
replace (const std::string &orignStr, const std::string &oldStr, const std::string &newStr) {
size_t pos = 0;
std::string tempStr = orignStr;
const std::string::size_type newStrLen = newStr.length ();
const std::string::size_type oldStrLen = oldStr.length ();
while (true) {
pos = tempStr.find (oldStr, pos);
if (pos == std::string::npos) break;
@@ -138,7 +138,7 @@ replace (const std::string orignStr, const std::string oldStr, const std::string
}
const char *
GameVersionToString (GameVersion version) {
GameVersionToString (const GameVersion version) {
switch (version) {
case GameVersion::JPN00: return "JPN00";
case GameVersion::JPN08: return "JPN08";
@@ -149,7 +149,7 @@ GameVersionToString (GameVersion version) {
}
const char *
languageStr (int language) {
languageStr (const int language) {
switch (language) {
case 1: return "en_us";
case 2: return "cn_tw";
@@ -161,12 +161,24 @@ languageStr (int language) {
std::string
ConvertWideToUtf8 (const std::wstring &wstr) {
std::wstring_convert<std::codecvt_utf8<wchar_t> > converter;
return converter.to_bytes (wstr);
if (wstr.empty ()) return {};
// Determine the size of the resulting UTF-8 string
const int utf8Size = WideCharToMultiByte (CP_UTF8, 0, wstr.c_str (), -1, nullptr, 0, nullptr, nullptr);
if (utf8Size <= 0) {
LogMessage (LogLevel::ERROR, "Failed to convert wide string to UTF-8");
return {};
}
// Allocate buffer and perform the conversion
std::string utf8Str (utf8Size, '\0'); // -1 to exclude null terminator
WideCharToMultiByte (CP_UTF8, 0, wstr.c_str (), -1, data (utf8Str), utf8Size, nullptr, nullptr);
return utf8Str;
}
bool
AreAllBytesZero (const uint8_t *array, size_t offset, size_t length) {
AreAllBytesZero (const u8 *array, const size_t offset, const size_t length) {
for (size_t i = 0; i < length; ++i)
if (array[offset + i] != 0x00) return false;
return true;