Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataManager: add DefaultScriptReader #2624

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions gframe/data_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

}
5 changes: 5 additions & 0 deletions gframe/data_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down