diff --git a/src/cmd2F.c b/src/cmd2F.c index 86d5497..16bc18f 100644 --- a/src/cmd2F.c +++ b/src/cmd2F.c @@ -42,11 +42,14 @@ #include "ald_manager.h" #include "LittleEndian.h" #include "hacks.h" +#include "filecheck.h" /* 選択 Window OPEN 時 callback */ static int cb_sel_init_page = 0; static int cb_sel_init_address = 0; +static const char REGREADSTRING_RESULT[] = "*regReadString*"; + extern INPUTSTRING_PARAM mi_param; extern void commandH(); @@ -1441,7 +1444,10 @@ void commands2F87() { int eBaneStrNum = getCaliValue(); int eResultStrNum = getCaliValue(); int *vResult = getCaliVariable(); - + + // This command is used to check if the game is installed, so we always + // return 1 with the dummy string REGREADSTRING_RESULT. + svar_set(eResultStrNum, REGREADSTRING_RESULT); *vResult = 1; DEBUG_COMMAND("regReadString %d,%d,%d,%d:", @@ -1451,8 +1457,17 @@ void commands2F87() { void commands2F88() { int eFileNameStrNum = getCaliValue(); int *vResult = getCaliVariable(); - - *vResult = 1; + + const char *fname = svar_get(eFileNameStrNum); + if (strcmp(fname, REGREADSTRING_RESULT) == 0) { + // If this command is called with the result of regReadString, the game + // is checking if the game CD is inserted. We return 1 in this case. + *vResult = 1; + } else { + char *fname_utf8 = sjis2utf(fname); + *vResult = fc_exists(fname_utf8) ? 1 : 0; + free(fname_utf8); + } DEBUG_COMMAND("fileCheckExist %d,%d:", eFileNameStrNum, *vResult); } diff --git a/src/filecheck.c b/src/filecheck.c index 7cdb1af..9c5bba7 100644 --- a/src/filecheck.c +++ b/src/filecheck.c @@ -29,11 +29,10 @@ #include #include #include +#include #ifdef _WIN32 #include #include -#undef min -#undef max #endif #include "filecheck.h" @@ -126,6 +125,18 @@ char *fc_get_path(const char *fname_utf8) { return get_fullpath(saveDataPath, fname_utf8); } +bool fc_exists(const char *fname_utf8) +{ + char *path = fc_get_path(fname_utf8); + wchar_t wpath[PATH_MAX + 1]; + bool result = false; + if (MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, wpath, PATH_MAX + 1)) { + result = _waccess(wpath, F_OK) != -1; + } + free(path); + return result; +} + FILE *fc_open(const char *fname_utf8, char type) { char *path = fc_get_path(fname_utf8); FILE *fp = fopen_utf8(path, type); @@ -162,6 +173,14 @@ char *fc_get_path(const char *fname_utf8) { return get_fullpath(saveDataPath, fname_utf8); } +bool fc_exists(const char *fname_utf8) +{ + char *path = fc_get_path(fname_utf8); + bool result = access(path, F_OK) != -1; + free(path); + return result; +} + FILE *fc_open(const char *fname_utf8, char type) { char *fullpath = fc_search(fname_utf8, saveDataPath); if (!fullpath) { diff --git a/src/filecheck.h b/src/filecheck.h index 1135419..85e3d2d 100644 --- a/src/filecheck.h +++ b/src/filecheck.h @@ -24,9 +24,12 @@ #ifndef __FILECHECK_H__ #define __FILECHECK_H__ -extern void fc_init(const char *name); -extern char *fc_get_path(const char *fname_utf8); -extern FILE *fc_open(const char *fname_utf8, char type); -extern void fc_backup_oldfile(const char *filename); +#include + +void fc_init(const char *name); +char *fc_get_path(const char *fname_utf8); +bool fc_exists(const char *fname_utf8); +FILE *fc_open(const char *fname_utf8, char type); +void fc_backup_oldfile(const char *filename); #endif /* !__FILECHECK_H__ */