From 1c20a36a8b78b9ee87d01d3f98ca66ebf62964c8 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Sun, 7 Aug 2022 12:33:01 +0200 Subject: [PATCH] complete reverse of open file by mode --- src/game/game.cpp | 8 ++-- src/game/game.hpp | 2 +- src/module/file_system.cpp | 79 ++++++++++++++++++++++++++++++++++---- src/std_include.hpp | 1 + 4 files changed, 78 insertions(+), 12 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index 79d145f..b4c5f8a 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -789,10 +789,10 @@ namespace game return (*fs_searchpaths != nullptr); } - void* fs_handle_for_file_dedicated(FsThread thread) + int fs_handle_for_file_dedicated(FsThread thread) { static DWORD func = 0x5245F0; - void* result = nullptr; + auto result = 0; __asm { @@ -806,14 +806,14 @@ namespace game return result; } - void* FS_HandleForFile(FsThread thread) + int FS_HandleForFile(FsThread thread) { if (is_dedi()) { return fs_handle_for_file_dedicated(thread); } - return reinterpret_cast + return reinterpret_cast (SELECT_VALUE(0x46B1C0, 0x5AEE50, 0x0))(thread); } diff --git a/src/game/game.hpp b/src/game/game.hpp index c94ca58..bc10265 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -286,7 +286,7 @@ namespace game void FS_FCloseFile(int h); bool FS_Initialized(); - void* FS_HandleForFile(FsThread thread); + int FS_HandleForFile(FsThread thread); int FS_FOpenFileReadForThread(const char* filename, int* file, FsThread thread); int FS_CreatePath(char* OSPath); void FS_CheckFileSystemStarted(); diff --git a/src/module/file_system.cpp b/src/module/file_system.cpp index 7d0f554..2dd56bd 100644 --- a/src/module/file_system.cpp +++ b/src/module/file_system.cpp @@ -27,14 +27,27 @@ static unsigned int file_write(const void* ptr, unsigned int len, FILE* stream) static FILE* file_open_append_text(const char* filename) { - FILE* file; - const auto err = fopen_s(&file, filename, "at"); - if (err == 0) + errno = 0; + auto* file = fopen(filename, "at"); + if (file) { return file; } - printf("Couldn't open file: %s\n", filename); + printf("Couldn't open file: %s %s\n", filename, strerror(errno)); + return nullptr; +} + +static FILE* file_open_write_binary(const char* filename) +{ + errno = 0; + auto* file = fopen(filename, "wb"); + if (file) + { + return file; + } + + printf("Couldn't open file: %s %s\n", filename, strerror(errno)); return nullptr; } @@ -127,7 +140,7 @@ static game::native::FsThread get_current_thread() return game::native::FS_THREAD_INVALID; } -static void* handle_for_file_current_thread() +static int handle_for_file_current_thread() { return game::native::FS_HandleForFile(get_current_thread()); } @@ -155,7 +168,7 @@ static int open_file_append(const char* filename) return 0; } - auto h = reinterpret_cast(handle_for_file_current_thread()); + auto h = handle_for_file_current_thread(); game::native::fsh[h].zipFile = nullptr; strncpy_s(game::native::fsh[h].name, filename, _TRUNCATE); game::native::fsh[h].handleFiles.file.o = f; @@ -170,12 +183,56 @@ static int open_file_append(const char* filename) return h; } +static int get_handle_and_open_file(const char* filename, const char* ospath, game::native::FsThread thread) +{ + auto* fp = file_open_write_binary(ospath); + if (!fp) + { + return 0; + } + + const auto f = game::native::FS_HandleForFile(thread); + game::native::fsh[f].zipFile = NULL; + game::native::fsh[f].handleFiles.file.o = fp; + + strncpy_s(game::native::fsh[f].name, filename, _TRUNCATE); + game::native::fsh[f].handleSync = 0; + + return f; +} + +static int open_file_write_to_dir_for_thread(const char* filename, const char* dir, const char* osbasepath, game::native::FsThread thread) +{ + char ospath[MAX_PATH]{}; + + game::native::FS_CheckFileSystemStarted(); + + const char* basepath = (*fs_homepath)->current.string; + build_os_path_for_thread(basepath, dir, filename, ospath, game::native::FS_THREAD_MAIN); + + if ((*fs_debug)->current.integer) + { + printf("FS_FOpenFileWriteToDirForThread: %s\n", ospath); + } + + if (game::native::FS_CreatePath(ospath)) + { + return 0; + } + + return get_handle_and_open_file(filename, ospath, thread); +} + +static int open_file_write(const char* filename) +{ + return open_file_write_to_dir_for_thread(filename, game::native::fs_gamedir, "", game::native::FS_THREAD_MAIN); +} + static const char* sys_default_install_path_stub() { static auto current_path = std::filesystem::current_path().string(); return current_path.data(); } - int file_system::open_file_by_mode(const char* qpath, int* f, game::native::fsMode_t mode) { auto r = 6969; @@ -187,6 +244,14 @@ int file_system::open_file_by_mode(const char* qpath, int* f, game::native::fsMo *game::native::com_fileAccessed = TRUE; r = game::native::FS_FOpenFileReadForThread(qpath, f, game::native::FS_THREAD_MAIN); break; + case game::native::FS_WRITE: + *f = open_file_write(qpath); + r = 0; + if (!*f) + { + r = -1; + } + break; case game::native::FS_APPEND_SYNC: sync = 1; case game::native::FS_APPEND: diff --git a/src/std_include.hpp b/src/std_include.hpp index 7342d87..14bf1fb 100644 --- a/src/std_include.hpp +++ b/src/std_include.hpp @@ -17,6 +17,7 @@ #pragma warning(disable: 28020) #define WIN32_LEAN_AND_MEAN +#define _CRT_SECURE_NO_WARNINGS #include #include