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

read datas in subdirectory #2223

Open
wants to merge 5 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
70 changes: 57 additions & 13 deletions gframe/data_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,23 @@ bool DataManager::LoadDB(const wchar_t* wfile) {
spmemvfs_env_fini();
return true;
}
void DataManager::LoadExpansionDB(const wchar_t* wpath) {
FileSystem::TraversalDir(wpath, [this, wpath](const wchar_t* name, bool isdir) {
if(!isdir && wcsrchr(name, '.') && !mywcsncasecmp(wcsrchr(name, '.'), L".cdb", 4)) {
wchar_t fpath[1024];
myswprintf(fpath, L"%ls/%ls", wpath, name);
LoadDB(fpath);
}
});
}
bool DataManager::LoadStrings(const char* file) {
#ifdef _WIN32
wchar_t fname[1024];
BufferIO::DecodeUTF8(file, fname);
FILE* fp = _wfopen(fname, L"r");
#else
FILE* fp = fopen(file, "r");
#endif // _WIN32
if(!fp)
return false;
char linebuf[256];
Expand Down Expand Up @@ -137,6 +152,15 @@ void DataManager::ReadStringConfLine(const char* linebuf) {
_setnameStrings[value] = strBuffer;
}
}
void DataManager::LoadExpansionStrings(const char* upath) {
FileSystem::TraversalDir(upath, [this, upath](const char* name, bool isdir) {
if(!isdir && strcmp(name, "system.conf") && strcmp(name, "lflist.conf") && strrchr(name, '.') && !mystrncasecmp(strrchr(name, '.'), ".conf", 5)) {
char fpath[1024];
sprintf(fpath, "%s/%s", upath, name);
LoadStrings(fpath);
}
});
}
bool DataManager::Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt) {
BufferIO::DecodeUTF8(sqlite3_errmsg(pDB->handle), strBuffer);
if(pStmt)
Expand Down Expand Up @@ -350,20 +374,40 @@ int DataManager::CardReader(int code, void* pData) {
return 0;
}
byte* DataManager::ScriptReaderEx(const char* script_name, int* slen) {
// default script name: ./script/c%d.lua
char first[256];
char second[256];
if(mainGame->gameConf.prefer_expansion_script) {
sprintf(first, "expansions/%s", script_name + 2);
sprintf(second, "%s", script_name + 2);
} else {
sprintf(first, "%s", script_name + 2);
sprintf(second, "expansions/%s", script_name + 2);
bool prefer = mainGame->gameConf.prefer_expansion_script ? true : false;
if(!prefer) {
char sname1[256];
sprintf(sname1, "%s", script_name + 2);
if(ScriptReader(sname1, slen))
return scriptBuffer;
}
if(ScriptReader(first, slen))
return scriptBuffer;
else
return ScriptReader(second, slen);
byte* buffer = ScriptReaderExDirectry("expansions", script_name, slen);
if(buffer)
return buffer;
bool find = false;
FileSystem::TraversalDir("./expansions", [script_name, slen, &buffer, &find](const char* name, bool isdir) {
if(!find && isdir && strcmp(name, ".") && strcmp(name, "..") && strcmp(name, "pics") && strcmp(name, "script")) {
char fpath[1024];
sprintf(fpath, "expansions/%s", name);
buffer = ScriptReaderExDirectry(fpath, script_name, slen);
if(buffer)
find = true;
}
});
if(find)
return buffer;
if(prefer) {
char sname2[256];
sprintf(sname2, "%s", script_name + 2);
if(ScriptReader(sname2, slen))
return scriptBuffer;
}
return 0;
}
byte* DataManager::ScriptReaderExDirectry(const char* path, const char* script_name, int* slen) {
char sname[256];
sprintf(sname, "%s/%s", path, script_name + 2); //default script name: ./script/c%d.lua
return ScriptReader(sname, slen);
}
byte* DataManager::ScriptReader(const char* script_name, int* slen) {
#ifdef _WIN32
Expand Down
3 changes: 3 additions & 0 deletions gframe/data_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class DataManager {
public:
DataManager(): _datas(8192), _strings(8192) {}
bool LoadDB(const wchar_t* wfile);
void LoadExpansionDB(const wchar_t* wpath);
bool LoadStrings(const char* file);
bool LoadStrings(IReadFile* reader);
void ReadStringConfLine(const char* linebuf);
void LoadExpansionStrings(const char* upath);
bool Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt = 0);
bool GetData(int code, CardData* pData);
code_pointer GetCodePointer(int code);
Expand Down Expand Up @@ -56,6 +58,7 @@ class DataManager {
static const wchar_t* unknown_string;
static int CardReader(int, void*);
static byte* ScriptReaderEx(const char* script_name, int* slen);
static byte* ScriptReaderExDirectry(const char* path, const char* script_name, int* slen);
static byte* ScriptReader(const char* script_name, int* slen);
static IFileSystem* FileSystem;
};
Expand Down
93 changes: 62 additions & 31 deletions gframe/deck_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,77 @@ DeckManager deckManager;

void DeckManager::LoadLFListSingle(const char* path) {
LFList* cur = nullptr;
#ifdef _WIN32
wchar_t fpath[1024];
BufferIO::DecodeUTF8(path, fpath);
FILE* fp = _wfopen(fpath, L"r");
#else
FILE* fp = fopen(path, "r");
#endif // _WIN32
char linebuf[256];
wchar_t strBuffer[256];
if(fp) {
while(fgets(linebuf, 256, fp)) {
if(linebuf[0] == '#')
continue;
if(linebuf[0] == '!') {
int sa = BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
while(strBuffer[sa - 1] == L'\r' || strBuffer[sa - 1] == L'\n' ) sa--;
strBuffer[sa] = 0;
LFList newlist;
_lfList.push_back(newlist);
cur = &_lfList[_lfList.size() - 1];
cur->listName = strBuffer;
cur->hash = 0x7dfcee6a;
continue;
}
int p = 0;
while(linebuf[p] != ' ' && linebuf[p] != '\t' && linebuf[p] != 0) p++;
if(linebuf[p] == 0)
continue;
linebuf[p++] = 0;
int sa = p;
int code = atoi(linebuf);
if(code == 0)
continue;
while(linebuf[p] == ' ' || linebuf[p] == '\t') p++;
while(linebuf[p] != ' ' && linebuf[p] != '\t' && linebuf[p] != 0) p++;
linebuf[p] = 0;
int count = atoi(&linebuf[sa]);
if(!cur) continue;
cur->content[code] = count;
cur->hash = cur->hash ^ ((code << 18) | (code >> 14)) ^ ((code << (27 + count)) | (code >> (5 - count)));
cur = ReadLFListSingle(linebuf, cur);
}
fclose(fp);
}
}
void DeckManager::LoadLFListSingle(IReadFile* reader) {
LFList* cur = nullptr;
char ch[2] = " ";
char linebuf[256] = "";
while(reader->read(&ch[0], 1)) {
if(ch[0] == '\0')
break;
strcat(linebuf, ch);
if(ch[0] == '\n') {
cur = ReadLFListSingle(linebuf, cur);
linebuf[0] = '\0';
}
}
reader->drop();
}
LFList* DeckManager::ReadLFListSingle(char* linebuf, LFList* cur) {
wchar_t strBuffer[256];
if(linebuf[0] == '#')
return cur;
if(linebuf[0] == '!') {
int sa = BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
while(strBuffer[sa - 1] == L'\r' || strBuffer[sa - 1] == L'\n' ) sa--;
strBuffer[sa] = 0;
auto lit = std::find_if(_lfList.begin(), _lfList.end(), [&strBuffer](const ygo::LFList& list) {
return wcscmp(list.listName.c_str(), strBuffer) == 0;
});
if(lit == _lfList.end()) {
LFList newlist;
_lfList.push_back(newlist);
cur = &_lfList[_lfList.size() - 1];
cur->listName = strBuffer;
cur->hash = 0x7dfcee6a;
} else {
cur = lit._Ptr;
}
return cur;
}
int p = 0;
while(linebuf[p] != ' ' && linebuf[p] != '\t' && linebuf[p] != 0) p++;
if(linebuf[p] == 0)
return cur;
linebuf[p++] = 0;
int sa = p;
int code = atoi(linebuf);
if(code == 0)
return cur;
while(linebuf[p] == ' ' || linebuf[p] == '\t') p++;
while(linebuf[p] != ' ' && linebuf[p] != '\t' && linebuf[p] != 0) p++;
linebuf[p] = 0;
int count = atoi(&linebuf[sa]);
if(!cur) return cur;
cur->content[code] = count;
cur->hash = cur->hash ^ ((code << 18) | (code >> 14)) ^ ((code << (27 + count)) | (code >> (5 - count)));
return cur;
}
void DeckManager::LoadLFList() {
LoadLFListSingle("expansions/lflist.conf");
LoadLFListSingle("lflist.conf");
LFList nolimit;
nolimit.listName = L"N/A";
Expand Down
2 changes: 2 additions & 0 deletions gframe/deck_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DeckManager {
std::vector<LFList> _lfList;

void LoadLFListSingle(const char* path);
void LoadLFListSingle(IReadFile* reader);
LFList* ReadLFListSingle(char* linebuf, LFList* cur);
void LoadLFList();
const wchar_t* GetLFListName(int lfhash);
const std::unordered_map<int, int>* GetLFListContent(int lfhash);
Expand Down
32 changes: 24 additions & 8 deletions gframe/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ bool Game::Initialize() {
menuHandler.prev_sel = -1;
memset(&dInfo, 0, sizeof(DuelInfo));
memset(chatTiming, 0, sizeof(chatTiming));
deckManager.LoadLFList();
driver = device->getVideoDriver();
driver->setTextureCreationFlag(irr::video::ETCF_CREATE_MIP_MAPS, false);
driver->setTextureCreationFlag(irr::video::ETCF_OPTIMIZED_FOR_QUALITY, true);
Expand All @@ -68,7 +67,7 @@ bool Game::Initialize() {
ErrorLog("Failed to load strings!");
return false;
}
dataManager.LoadStrings("./expansions/strings.conf");
deckManager.LoadLFList();
env = device->getGUIEnvironment();
numFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 16);
adFont = irr::gui::CGUITTFont::createTTFont(env, gameConf.numfont, 12);
Expand Down Expand Up @@ -919,12 +918,10 @@ void Game::SetStaticText(irr::gui::IGUIStaticText* pControl, u32 cWidth, irr::gu
pControl->setText(dataManager.strBuffer);
}
void Game::LoadExpansions() {
dataManager.LoadExpansionDB(L"./expansions");
dataManager.LoadStrings("./expansions/strings.conf");
deckManager.LoadLFListSingle("expansions/lflist.conf");
FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) {
if(!isdir && wcsrchr(name, '.') && !mywcsncasecmp(wcsrchr(name, '.'), L".cdb", 4)) {
wchar_t fpath[1024];
myswprintf(fpath, L"./expansions/%ls", name);
dataManager.LoadDB(fpath);
}
if(!isdir && wcsrchr(name, '.') && !mywcsncasecmp(wcsrchr(name, '.'), L".zip", 4)) {
wchar_t fpath[1024];
myswprintf(fpath, L"./expansions/%ls", name);
Expand All @@ -936,6 +933,17 @@ void Game::LoadExpansions() {
dataManager.FileSystem->addFileArchive(upath, true, false);
#endif
}
if(isdir && wcscmp(name, L".") && wcscmp(name, L"..") && wcscmp(name, L"pics") && wcscmp(name, L"script")) {
wchar_t fpath[1024];
char upath[1024];
myswprintf(fpath, L"./expansions/%ls", name);
dataManager.LoadExpansionDB(fpath);
BufferIO::EncodeUTF8(fpath, upath);
dataManager.LoadExpansionStrings(upath);
myswprintf(fpath, L"./expansions/%ls/lflist.conf", name);
BufferIO::EncodeUTF8(fpath, upath);
deckManager.LoadLFListSingle(upath);
}
});
for(u32 i = 0; i < DataManager::FileSystem->getFileArchiveCount(); ++i) {
const IFileList* archive = DataManager::FileSystem->getFileArchive(i)->getFileList();
Expand All @@ -949,14 +957,22 @@ void Game::LoadExpansions() {
#endif
if(wcsrchr(fname, '.') && !mywcsncasecmp(wcsrchr(fname, '.'), L".cdb", 4))
dataManager.LoadDB(fname);
if(wcsrchr(fname, '.') && !mywcsncasecmp(wcsrchr(fname, '.'), L".conf", 5)) {
if(wcscmp(fname, L"system.conf") && wcscmp(fname, L"lflist.conf") && wcsrchr(fname, '.') && !mywcsncasecmp(wcsrchr(fname, '.'), L".conf", 5)) {
#ifdef _WIN32
IReadFile* reader = DataManager::FileSystem->createAndOpenFile(fname);
#else
IReadFile* reader = DataManager::FileSystem->createAndOpenFile(uname);
#endif
dataManager.LoadStrings(reader);
}
if(!wcscmp(fname, L"lflist.conf")) {
#ifdef _WIN32
IReadFile* reader = DataManager::FileSystem->createAndOpenFile(fname);
#else
IReadFile* reader = DataManager::FileSystem->createAndOpenFile(uname);
#endif
deckManager.LoadLFListSingle(reader);
}
}
}
}
Expand Down
Loading