Minor game detect refactoring

This commit is contained in:
mkrsym1 2023-08-10 01:00:24 +03:00
parent 0bfab4f682
commit e9d2130105
3 changed files with 21 additions and 11 deletions

View File

@ -2,6 +2,8 @@
#include <stdint.h> #include <stdint.h>
#define UTILS_COUNT(arr) (sizeof(arr) / sizeof(*arr))
int utils_path_exists(const wchar_t *filePath); int utils_path_exists(const wchar_t *filePath);
uint32_t utils_file_crc32c(const wchar_t *filePath); uint32_t utils_file_crc32c(const wchar_t *filePath);

View File

@ -1,32 +1,39 @@
#include <msg.h> #include <msg.h>
#include <utils.h>
#include <game.h> #include <game.h>
typedef void (*fill_fn)(struct game_data *buf); typedef void (*fill_fn)(struct game_data *buf);
struct name_fn_pair { struct name_fn_pair {
const char *name; const wchar_t *name;
fill_fn fill; fill_fn fill;
}; };
const struct name_fn_pair GAMES[] = { const struct name_fn_pair GAMES[] = {
{ "bh3.exe", &hi3_fill_data }, { L"BH3", &hi3_fill_data },
{ "starrail.exe", &hsr_fill_data } { L"StarRail", &hsr_fill_data }
}; };
void game_detect(struct game_data *buf) { void game_detect(struct game_data *buf) {
char exePath[MAX_PATH]; wchar_t exePath[MAX_PATH];
GetModuleFileNameA(NULL, exePath, MAX_PATH); GetModuleFileNameW(NULL, exePath, MAX_PATH);
char *exeName = strrchr(exePath, '\\') + 1; // Leave only the basename
strlwr(exeName); wchar_t *exeName = wcsrchr(exePath, L'\\') + 1;
for (size_t i = 0; i < sizeof(GAMES) / sizeof(struct name_fn_pair); i++) { // Cut off extension (.exe)
if (strcmp(exeName, GAMES[i].name) == 0) { wchar_t *extensionDot = wcsrchr(exeName, L'.');
if (extensionDot != NULL) {
*extensionDot = L'\0';
}
for (size_t i = 0; i < UTILS_COUNT(GAMES); i++) {
if (wcsicmp(exeName, GAMES[i].name) == 0) {
GAMES[i].fill(buf); GAMES[i].fill(buf);
return; return;
} }
} }
msg_err_a("Unknown game: %s", exeName); msg_err_w(L"Unknown game: %ls", exeName);
} }

View File

@ -78,9 +78,10 @@ void hsr_fill_data(struct game_data *buf) {
uint32_t crc = utils_file_crc32c(L"UnityPlayer.dll"); uint32_t crc = utils_file_crc32c(L"UnityPlayer.dll");
enum hsr_region id = HSR_INVALID; enum hsr_region id = HSR_INVALID;
for (size_t i = 0; i < sizeof(HSR_REGIONS) / sizeof(struct crc_region_pair); i++) { for (size_t i = 0; i < UTILS_COUNT(HSR_REGIONS); i++) {
if (HSR_REGIONS[i].crc == crc) { if (HSR_REGIONS[i].crc == crc) {
id = HSR_REGIONS[i].id; id = HSR_REGIONS[i].id;
break;
} }
} }