diff --git a/gframe/data_manager.cpp b/gframe/data_manager.cpp index 22eb54cbb9..41c15fa6a4 100644 --- a/gframe/data_manager.cpp +++ b/gframe/data_manager.cpp @@ -392,19 +392,26 @@ uint32 DataManager::CardReader(uint32 code, card_data* pData) { } unsigned char* DataManager::ScriptReaderEx(const char* script_name, int* slen) { // default script name: ./script/c%d.lua - char first[256]{}; - char second[256]{}; + if (std::strncmp(script_name, "./script", 8) != 0) + return DefaultScriptReader(script_name, slen); + char expansions_path[1024]{}; + std::snprintf(expansions_path, sizeof expansions_path, "./expansions/%s", script_name + 2); if(mainGame->gameConf.prefer_expansion_script) { - snprintf(first, sizeof first, "expansions/%s", script_name + 2); - snprintf(second, sizeof second, "%s", script_name + 2); + if (ScriptReader(script_name, slen)) + return scriptBuffer; + else if (DefaultScriptReader(expansions_path, slen)) + return scriptBuffer; + else if (DefaultScriptReader(script_name, slen)) + return scriptBuffer; } else { - snprintf(first, sizeof first, "%s", script_name + 2); - snprintf(second, sizeof second, "expansions/%s", script_name + 2); + if (DefaultScriptReader(script_name, slen)) + return scriptBuffer; + else if (ScriptReader(script_name, slen)) + return scriptBuffer; + else if (DefaultScriptReader(expansions_path, slen)) + return scriptBuffer; } - if(ScriptReader(first, slen)) - return scriptBuffer; - else - return ScriptReader(second, slen); + return nullptr; } unsigned char* DataManager::ScriptReader(const char* script_name, int* slen) { #ifdef _WIN32 @@ -426,5 +433,18 @@ unsigned char* DataManager::ScriptReader(const char* script_name, int* slen) { *slen = (int)size; return scriptBuffer; } +unsigned char* DataManager::DefaultScriptReader(const char* script_name, int* slen) { + wchar_t fname[256]{}; + BufferIO::DecodeUTF8(script_name, fname); + FILE* fp = myfopen(fname, "rb"); + if (!fp) + return nullptr; + size_t len = std::fread(scriptBuffer, 1, sizeof scriptBuffer, fp); + std::fclose(fp); + if (len >= sizeof scriptBuffer) + return nullptr; + *slen = (int)len; + return scriptBuffer; +} } diff --git a/gframe/data_manager.h b/gframe/data_manager.h index f488d81fd3..4e5f9db844 100644 --- a/gframe/data_manager.h +++ b/gframe/data_manager.h @@ -54,7 +54,12 @@ class DataManager { static const wchar_t* unknown_string; static uint32 CardReader(uint32, card_data*); static unsigned char* ScriptReaderEx(const char* script_name, int* slen); + + //read by IFileSystem static unsigned char* ScriptReader(const char* script_name, int* slen); + //read by fread + static unsigned char* DefaultScriptReader(const char* script_name, int* slen); + static IFileSystem* FileSystem; private: