Skip to content

Commit

Permalink
On Windows, use Unicode/UTF-8 for stat().
Browse files Browse the repository at this point in the history
  • Loading branch information
NagyD committed May 29, 2021
1 parent 7433af8 commit d9c79b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/ChangeLog.txt
Expand Up @@ -641,3 +641,5 @@ DONE: In new replays, use deprecation_number = 2. On playback, waste the RNG cyc
Older replays have deprecation_number <= 1, and we don't waste the RNG cycle when playing them back.
DONE: When backing offscreen (to the left) from the first guard on level 7 (among others), simulate the glitch from DOS PoP, which causes the prince to fall through the floor.
Details: See https://github.com/NagyD/SDLPoP/issues/229
FIXED: On Windows, use Unicode/UTF-8 for stat().
So SDLPoP can load levels from mod folders when a replay file restarts the level or advances to the next level.
1 change: 1 addition & 0 deletions src/options.c
Expand Up @@ -714,6 +714,7 @@ void load_mod_options() {
char folder_name[POP_MAX_PATH];
snprintf_check(folder_name, sizeof(folder_name), "%s/%s", mods_folder, levelset_name);
const char* located_folder_name = locate_file(folder_name);
//printf("located_folder_name = %s\n", located_folder_name);
bool ok = false;
struct stat info;
if (stat(located_folder_name, &info) == 0) {
Expand Down
11 changes: 11 additions & 0 deletions src/proto.h
Expand Up @@ -513,19 +513,30 @@ void sdlperror(const char* header);
bool file_exists(const char* filename);
#define locate_file(filename) locate_file_(filename, alloca(POP_MAX_PATH), POP_MAX_PATH)
const char* locate_file_(const char* filename, char* path_buffer, int buffer_size);

#ifdef _WIN32

FILE* fopen_UTF8(const char* filename, const char* mode);
#define fopen fopen_UTF8

int chdir_UTF8(const char* path);
#define chdir chdir_UTF8

int mkdir_UTF8(const char* path);
#define mkdir mkdir_UTF8

int access_UTF8(const char* filename_UTF8, int mode);
#ifdef access
#undef access
#endif
#define access access_UTF8

int stat_UTF8(const char *filename_UTF8, struct stat *_Stat);
// We define a function-like macro, because `stat` is also the name of the type, and we don't want to redefine that.
#define stat(filename_UTF8, _Stat) stat_UTF8(filename_UTF8, _Stat)

#endif //_WIN32

directory_listing_type* create_directory_listing_and_find_first_file(const char* directory, const char* extension);
char* get_current_filename_from_directory_listing(directory_listing_type* data);
bool find_next_file(directory_listing_type* data);
Expand Down
9 changes: 9 additions & 0 deletions src/seg009.c
Expand Up @@ -108,6 +108,15 @@ int access_UTF8(const char* filename_UTF8, int mode) {
SDL_free(filename_UTF16);
return result;
}

int stat_UTF8(const char *filename_UTF8, struct stat *_Stat) {
WCHAR* filename_UTF16 = WIN_UTF8ToString(filename_UTF8);
// There is a _wstat() function as well, but it expects the second argument to be a different type than stat().
int result = wstat(filename_UTF16, _Stat);
SDL_free(filename_UTF16);
return result;
}

#endif //_WIN32

// OS abstraction for listing directory contents
Expand Down

0 comments on commit d9c79b5

Please sign in to comment.