server: import the duel server (nanahira/ygopro server at 52c868ce)
The server tree of https://code.moenext.com/nanahira/ygopro.git, branch server, commit 52c868ce071ea0f2fcb0cf39bf2fb4151d12b90c, stock: gframe/, premake5.lua, .ci/, LICENSE, and gframe/ygopro.rc + .ico (upstream keeps them in resource/ and copies them in before building). No game assets, no scripts, no ocgcore submodule: the core is ../core. The third-party sources upstream's Windows CI downloads are vendored here at the positions its premake expects, each with its premake fragment from upstream's premake/ folder inside it: lua 5.4.8, sqlite 3.53.3, libevent 2.1.13-stable (tests, samples and autotools helpers left out; the files its Windows prebuild copies into include/ left out), zlib 1.3.2 (contrib and examples left out), xz 5.8.3 (src/liblzma + src/common only), and irrlicht 1.9 from mycard at 20f86d25: include/ plus the source/Irrlicht headers and the 11 .cpp files the server's zip reader project compiles. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
26f9baf9b4
commit
da27435cdb
1032 files changed
+746904
No files matched your search
@@ -0,0 +1,582 @@
|
||||
#include <algorithm>
|
||||
#include "deck_manager.h"
|
||||
#include "data_manager.h"
|
||||
#include "game.h"
|
||||
#include "file_system.h"
|
||||
#include "network.h"
|
||||
#include "base64.h"
|
||||
|
||||
namespace ygo {
|
||||
|
||||
DeckManager deckManager;
|
||||
|
||||
void DeckManager::LoadLFListFromLineProvider(const std::function<bool(char*, size_t)>& getLine, bool insert) {
|
||||
std::vector<LFList> loadedLists;
|
||||
auto cur = loadedLists.rend();
|
||||
char linebuf[1024]{};
|
||||
wchar_t strBuffer[256]{};
|
||||
auto credit_hash = [](const char* s) -> uint32_t {
|
||||
uint32_t h = 2166136261u;
|
||||
for(const unsigned char* p = reinterpret_cast<const unsigned char*>(s); *p; ++p) {
|
||||
h ^= *p;
|
||||
h *= 16777619u;
|
||||
}
|
||||
return h;
|
||||
};
|
||||
auto credit_update_hash = [](uint32_t& h, uint32_t a, uint32_t b, uint32_t c) {
|
||||
h = h ^ ((a << 18) | (a >> 14)) ^ ((b << 9) | (b >> 23)) ^ ((c << 27) | (c >> 5));
|
||||
};
|
||||
if(true) {
|
||||
while(getLine(linebuf, sizeof linebuf)) {
|
||||
if(linebuf[0] == '#')
|
||||
continue;
|
||||
if(linebuf[0] == '!') {
|
||||
auto len = std::strcspn(linebuf, "\r\n");
|
||||
linebuf[len] = 0;
|
||||
BufferIO::DecodeUTF8(&linebuf[1], strBuffer);
|
||||
LFList newlist;
|
||||
newlist.listName = strBuffer;
|
||||
newlist.hash = 0x7dfcee6a;
|
||||
loadedLists.push_back(newlist);
|
||||
cur = loadedLists.rbegin();
|
||||
continue;
|
||||
}
|
||||
if(cur == loadedLists.rend())
|
||||
continue;
|
||||
if(linebuf[0] == '$') {
|
||||
char* keyPos = linebuf + 1;
|
||||
keyPos += std::strspn(keyPos, " \t");
|
||||
auto keyLen = std::strcspn(keyPos, " \t\r\n");
|
||||
if(!keyLen)
|
||||
continue;
|
||||
char keybuf[256];
|
||||
if(keyLen >= sizeof keybuf)
|
||||
keyLen = sizeof keybuf - 1;
|
||||
std::memcpy(keybuf, keyPos, keyLen);
|
||||
keybuf[keyLen] = 0;
|
||||
keyPos += keyLen;
|
||||
keyPos += std::strspn(keyPos, " \t");
|
||||
errno = 0;
|
||||
char* valuePos = keyPos;
|
||||
auto limitValue = std::strtoul(keyPos, &keyPos, 10);
|
||||
if(errno || valuePos == keyPos)
|
||||
continue;
|
||||
BufferIO::DecodeUTF8(keybuf, strBuffer);
|
||||
cur->credit_limits[strBuffer] = static_cast<uint32_t>(limitValue);
|
||||
credit_update_hash(cur->hash, credit_hash(keybuf), static_cast<uint32_t>(limitValue), 0x43524544u);
|
||||
continue;
|
||||
}
|
||||
char* pos = linebuf;
|
||||
errno = 0;
|
||||
char* codePos = pos;
|
||||
auto result = std::strtoul(pos, &pos, 10);
|
||||
if(errno || result > UINT32_MAX || codePos == pos)
|
||||
continue;
|
||||
if(*pos != ' ' && *pos != '\t')
|
||||
continue;
|
||||
pos += std::strspn(pos, " \t");
|
||||
uint32_t code = static_cast<uint32_t>(result);
|
||||
if(*pos == '$') {
|
||||
++pos;
|
||||
pos += std::strspn(pos, " \t");
|
||||
auto creditKeyLen = std::strcspn(pos, " \t\r\n");
|
||||
if(!creditKeyLen)
|
||||
continue;
|
||||
char keybuf[256];
|
||||
if(creditKeyLen >= sizeof keybuf)
|
||||
creditKeyLen = sizeof keybuf - 1;
|
||||
std::memcpy(keybuf, pos, creditKeyLen);
|
||||
keybuf[creditKeyLen] = 0;
|
||||
pos += creditKeyLen;
|
||||
pos += std::strspn(pos, " \t");
|
||||
errno = 0;
|
||||
char* creditValuePos = pos;
|
||||
auto creditValue = std::strtoul(pos, &pos, 10);
|
||||
if(errno || creditValuePos == pos)
|
||||
continue;
|
||||
BufferIO::DecodeUTF8(keybuf, strBuffer);
|
||||
cur->credits[code][strBuffer] = static_cast<uint32_t>(creditValue);
|
||||
credit_update_hash(cur->hash, code, credit_hash(keybuf), static_cast<uint32_t>(creditValue));
|
||||
continue;
|
||||
}
|
||||
errno = 0;
|
||||
char* countPos = pos;
|
||||
int count = std::strtol(pos, &pos, 10);
|
||||
if(errno || countPos == pos)
|
||||
continue;
|
||||
if(count < 0 || count > 2)
|
||||
continue;
|
||||
cur->content[code] = count;
|
||||
cur->hash = cur->hash ^ ((code << 18) | (code >> 14)) ^ ((code << (27 + count)) | (code >> (5 - count)));
|
||||
}
|
||||
}
|
||||
if(insert)
|
||||
_lfList.insert(_lfList.begin(), loadedLists.begin(), loadedLists.end());
|
||||
else
|
||||
_lfList.insert(_lfList.end(), loadedLists.begin(), loadedLists.end());
|
||||
}
|
||||
void DeckManager::LoadLFListSingle(const char* path, bool insert) {
|
||||
FILE* fp = myfopen(path, "r");
|
||||
if (!fp) return;
|
||||
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
|
||||
return std::fgets(buf, sz, fp) != nullptr;
|
||||
}, insert);
|
||||
std::fclose(fp);
|
||||
}
|
||||
void DeckManager::LoadLFListSingle(const wchar_t* path, bool insert) {
|
||||
FILE* fp = mywfopen(path, "r");
|
||||
if (!fp) return;
|
||||
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
|
||||
return std::fgets(buf, sz, fp) != nullptr;
|
||||
}, insert);
|
||||
std::fclose(fp);
|
||||
}
|
||||
#if defined(SERVER_ZIP_SUPPORT) || !defined(YGOPRO_SERVER_MODE)
|
||||
void DeckManager::LoadLFListSingle(irr::io::IReadFile* reader, bool insert) {
|
||||
std::string linebuf;
|
||||
char ch{};
|
||||
LoadLFListFromLineProvider([&](char* buf, size_t sz) {
|
||||
while (reader->read(&ch, 1)) {
|
||||
if (ch == '\0') break;
|
||||
linebuf.push_back(ch);
|
||||
if (ch == '\n' || linebuf.size() >= sz - 1) {
|
||||
std::strncpy(buf, linebuf.c_str(), sz - 1);
|
||||
buf[sz - 1] = '\0';
|
||||
linebuf.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}, insert);
|
||||
reader->drop();
|
||||
}
|
||||
#endif
|
||||
void DeckManager::LoadLFList() {
|
||||
#ifdef SERVER_PRO2_SUPPORT
|
||||
LoadLFListSingle("config/lflist.conf");
|
||||
#elif defined(SERVER_PRO3_SUPPORT)
|
||||
LoadLFListSingle("Data/lflist_merged.conf");
|
||||
#endif
|
||||
LoadLFListSingle("specials/lflist.conf");
|
||||
LoadLFListSingle("lflist.conf");
|
||||
LFList nolimit;
|
||||
nolimit.listName = L"N/A";
|
||||
nolimit.hash = 0;
|
||||
_lfList.push_back(nolimit);
|
||||
}
|
||||
#ifndef YGOPRO_SERVER_MODE
|
||||
const wchar_t* DeckManager::GetLFListName(unsigned int lfhash) {
|
||||
auto lit = std::find_if(_lfList.begin(), _lfList.end(), [lfhash](const ygo::LFList& list) {
|
||||
return list.hash == lfhash;
|
||||
});
|
||||
if(lit != _lfList.end())
|
||||
return lit->listName.c_str();
|
||||
return dataManager.unknown_string;
|
||||
}
|
||||
#endif
|
||||
const LFList* DeckManager::GetLFList(unsigned int lfhash) {
|
||||
auto lit = std::find_if(_lfList.begin(), _lfList.end(), [lfhash](const ygo::LFList& list) {
|
||||
return list.hash == lfhash;
|
||||
});
|
||||
if (lit != _lfList.end())
|
||||
return &(*lit);
|
||||
return nullptr;
|
||||
}
|
||||
static unsigned int checkAvail(unsigned int ot, unsigned int avail) {
|
||||
if(!!(ot & 0x4))
|
||||
return 0;
|
||||
if((ot & avail) == avail)
|
||||
return 0;
|
||||
if((ot & AVAIL_OCG) && (avail != AVAIL_OCG))
|
||||
return DECKERROR_OCGONLY;
|
||||
if((ot & AVAIL_TCG) && (avail != AVAIL_TCG))
|
||||
return DECKERROR_TCGONLY;
|
||||
return DECKERROR_NOTAVAIL;
|
||||
}
|
||||
uint32_t DeckManager::CheckDeck(const Deck& deck, unsigned int lfhash, size_t rule) {
|
||||
std::unordered_map<uint32_t, int> ccount;
|
||||
// rule
|
||||
if(deck.main.size() < DECK_MIN_SIZE || deck.main.size() > DECK_MAX_SIZE)
|
||||
return (DECKERROR_MAINCOUNT << 28) | (unsigned)deck.main.size();
|
||||
if(deck.extra.size() > EXTRA_MAX_SIZE)
|
||||
return (DECKERROR_EXTRACOUNT << 28) | (unsigned)deck.extra.size();
|
||||
if(deck.side.size() > SIDE_MAX_SIZE)
|
||||
return (DECKERROR_SIDECOUNT << 28) | (unsigned)deck.side.size();
|
||||
auto lflist = GetLFList(lfhash);
|
||||
if (!lflist)
|
||||
return 0;
|
||||
auto& list = lflist->content;
|
||||
std::unordered_map<std::wstring, uint32_t> credit_used;
|
||||
auto spend_credit = [&](uint32_t code) {
|
||||
auto code_credit_it = lflist->credits.find(code);
|
||||
if(code_credit_it == lflist->credits.end())
|
||||
return (uint32_t)0;
|
||||
auto code_credit = code_credit_it->second;
|
||||
for(auto& credit_it : code_credit) {
|
||||
auto key = credit_it.first;
|
||||
auto credit_limit_it = lflist->credit_limits.find(key);
|
||||
if(credit_limit_it == lflist->credit_limits.end())
|
||||
continue;
|
||||
auto credit_limit = credit_limit_it->second;
|
||||
if(credit_used.find(key) == credit_used.end())
|
||||
credit_used[key] = 0;
|
||||
auto credit_after = credit_used[key] + credit_it.second;
|
||||
if(credit_after > credit_limit)
|
||||
return (DECKERROR_LFLIST << 28) | code;
|
||||
credit_used[key] = credit_after;
|
||||
}
|
||||
return (uint32_t)0;
|
||||
};
|
||||
const unsigned int rule_map[6] = { AVAIL_OCG, AVAIL_TCG, AVAIL_SC, AVAIL_CUSTOM, AVAIL_OCGTCG, 0 };
|
||||
unsigned int avail = 0;
|
||||
if (rule < sizeof rule_map / sizeof rule_map[0])
|
||||
avail = rule_map[rule];
|
||||
for (auto& cit : deck.main) {
|
||||
auto gameruleDeckError = checkAvail(cit->ot, avail);
|
||||
if(gameruleDeckError)
|
||||
return (gameruleDeckError << 28) | cit->code;
|
||||
if (cit->type & (TYPES_EXTRA_DECK | TYPE_TOKEN))
|
||||
return (DECKERROR_MAINCOUNT << 28);
|
||||
auto code = cit->get_duel_code();
|
||||
ccount[code]++;
|
||||
int dc = ccount[code];
|
||||
if(dc > 3)
|
||||
return (DECKERROR_CARDCOUNT << 28) | cit->code;
|
||||
auto it = list.find(code);
|
||||
if(it != list.end() && dc > it->second)
|
||||
return (DECKERROR_LFLIST << 28) | cit->code;
|
||||
auto spend_credit_error = spend_credit(code);
|
||||
if(spend_credit_error)
|
||||
return spend_credit_error;
|
||||
}
|
||||
for (auto& cit : deck.extra) {
|
||||
auto gameruleDeckError = checkAvail(cit->ot, avail);
|
||||
if(gameruleDeckError)
|
||||
return (gameruleDeckError << 28) | cit->code;
|
||||
if (!(cit->type & TYPES_EXTRA_DECK) || cit->type & TYPE_TOKEN)
|
||||
return (DECKERROR_EXTRACOUNT << 28);
|
||||
auto code = cit->get_duel_code();
|
||||
ccount[code]++;
|
||||
int dc = ccount[code];
|
||||
if(dc > 3)
|
||||
return (DECKERROR_CARDCOUNT << 28) | cit->code;
|
||||
auto it = list.find(code);
|
||||
if(it != list.end() && dc > it->second)
|
||||
return (DECKERROR_LFLIST << 28) | cit->code;
|
||||
auto spend_credit_error = spend_credit(code);
|
||||
if(spend_credit_error)
|
||||
return spend_credit_error;
|
||||
}
|
||||
for (auto& cit : deck.side) {
|
||||
auto gameruleDeckError = checkAvail(cit->ot, avail);
|
||||
if(gameruleDeckError)
|
||||
return (gameruleDeckError << 28) | cit->code;
|
||||
if (cit->type & TYPE_TOKEN)
|
||||
return (DECKERROR_SIDECOUNT << 28);
|
||||
auto code = cit->get_duel_code();
|
||||
ccount[code]++;
|
||||
int dc = ccount[code];
|
||||
if(dc > 3)
|
||||
return (DECKERROR_CARDCOUNT << 28) | cit->code;
|
||||
auto it = list.find(code);
|
||||
if(it != list.end() && dc > it->second)
|
||||
return (DECKERROR_LFLIST << 28) | cit->code;
|
||||
auto spend_credit_error = spend_credit(code);
|
||||
if(spend_credit_error)
|
||||
return spend_credit_error;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
uint32_t DeckManager::LoadDeck(Deck& deck, uint32_t dbuf[], uint32_t mainc, uint32_t sidec, bool is_packlist) {
|
||||
deck.clear();
|
||||
uint32_t errorcode = 0;
|
||||
auto& _datas = dataManager.GetDataTable();
|
||||
for(uint32_t i = 0; i < mainc; ++i) {
|
||||
auto code = dbuf[i];
|
||||
auto it = _datas.find(code);
|
||||
if(it == _datas.end()) {
|
||||
errorcode = code;
|
||||
continue;
|
||||
}
|
||||
auto& cd = it->second;
|
||||
if (cd.type & TYPE_TOKEN) {
|
||||
errorcode = code;
|
||||
continue;
|
||||
}
|
||||
if(is_packlist) {
|
||||
deck.main.push_back(&cd);
|
||||
continue;
|
||||
}
|
||||
if (cd.type & TYPES_EXTRA_DECK) {
|
||||
if (deck.extra.size() < EXTRA_MAX_SIZE)
|
||||
deck.extra.push_back(&cd);
|
||||
}
|
||||
else {
|
||||
if (deck.main.size() < DECK_MAX_SIZE)
|
||||
deck.main.push_back(&cd);
|
||||
}
|
||||
}
|
||||
for(uint32_t i = 0; i < sidec; ++i) {
|
||||
auto code = dbuf[mainc + i];
|
||||
auto it = _datas.find(code);
|
||||
if(it == _datas.end()) {
|
||||
errorcode = code;
|
||||
continue;
|
||||
}
|
||||
auto& cd = it->second;
|
||||
if (cd.type & TYPE_TOKEN) {
|
||||
errorcode = code;
|
||||
continue;
|
||||
}
|
||||
if(deck.side.size() < SIDE_MAX_SIZE)
|
||||
deck.side.push_back(&cd);
|
||||
}
|
||||
return errorcode;
|
||||
}
|
||||
#ifndef YGOPRO_SERVER_MODE
|
||||
uint32_t DeckManager::LoadDeckFromStream(Deck& deck, std::istringstream& deckStream, bool is_packlist) {
|
||||
int ct = 0;
|
||||
int mainc = 0, sidec = 0;
|
||||
uint32_t cardlist[PACK_MAX_SIZE]{};
|
||||
bool is_side = false;
|
||||
std::string linebuf;
|
||||
while (std::getline(deckStream, linebuf) && ct < PACK_MAX_SIZE) {
|
||||
if (linebuf[0] == '!') {
|
||||
is_side = true;
|
||||
continue;
|
||||
}
|
||||
if (linebuf[0] < '0' || linebuf[0] > '9')
|
||||
continue;
|
||||
errno = 0;
|
||||
auto code = std::strtoul(linebuf.c_str(), nullptr, 10);
|
||||
if (errno || code > UINT32_MAX)
|
||||
continue;
|
||||
cardlist[ct++] = code;
|
||||
if (is_side)
|
||||
++sidec;
|
||||
else
|
||||
++mainc;
|
||||
}
|
||||
return LoadDeck(deck, cardlist, mainc, sidec, is_packlist);
|
||||
}
|
||||
#endif // YGOPRO_SERVER_MODE
|
||||
bool DeckManager::LoadSide(Deck& deck, uint32_t dbuf[], uint32_t mainc, uint32_t sidec) {
|
||||
std::unordered_map<uint32_t, int> pcount;
|
||||
std::unordered_map<uint32_t, int> ncount;
|
||||
for(auto card : deck.main)
|
||||
pcount[card->code]++;
|
||||
for(auto card : deck.extra)
|
||||
pcount[card->code]++;
|
||||
for(auto card : deck.side)
|
||||
pcount[card->code]++;
|
||||
Deck ndeck;
|
||||
LoadDeck(ndeck, dbuf, mainc, sidec);
|
||||
#ifndef YGOPRO_NO_SIDE_CHECK
|
||||
if(ndeck.main.size() != deck.main.size() || ndeck.extra.size() != deck.extra.size() || ndeck.side.size() != deck.side.size())
|
||||
return false;
|
||||
#endif
|
||||
for(auto card : ndeck.main)
|
||||
ncount[card->code]++;
|
||||
for(auto card : ndeck.extra)
|
||||
ncount[card->code]++;
|
||||
for(auto card : ndeck.side)
|
||||
ncount[card->code]++;
|
||||
#ifndef YGOPRO_NO_SIDE_CHECK
|
||||
for (auto& cdit : ncount)
|
||||
if (cdit.second != pcount[cdit.first])
|
||||
return false;
|
||||
#endif
|
||||
deck = ndeck;
|
||||
return true;
|
||||
}
|
||||
#ifndef YGOPRO_SERVER_MODE
|
||||
void DeckManager::GetCategoryPath(wchar_t* ret, int index, const wchar_t* text) {
|
||||
wchar_t catepath[256];
|
||||
switch(index) {
|
||||
case DECK_CATEGORY_PACK:
|
||||
myswprintf(catepath, L"./pack");
|
||||
break;
|
||||
case DECK_CATEGORY_BOT:
|
||||
BufferIO::CopyWideString(mainGame->gameConf.bot_deck_path, catepath);
|
||||
break;
|
||||
case -1:
|
||||
case DECK_CATEGORY_NONE:
|
||||
case DECK_CATEGORY_SEPARATOR:
|
||||
myswprintf(catepath, L"./deck");
|
||||
break;
|
||||
default:
|
||||
myswprintf(catepath, L"./deck/%ls", text);
|
||||
}
|
||||
BufferIO::CopyWStr(catepath, ret, 256);
|
||||
}
|
||||
void DeckManager::GetDeckFile(wchar_t* ret, int category_index, const wchar_t* category_name, const wchar_t* deckname) {
|
||||
wchar_t filepath[256];
|
||||
wchar_t catepath[256];
|
||||
if(deckname != nullptr) {
|
||||
GetCategoryPath(catepath, category_index, category_name);
|
||||
myswprintf(filepath, L"%ls/%ls.ydk", catepath, deckname);
|
||||
BufferIO::CopyWStr(filepath, ret, 256);
|
||||
}
|
||||
else {
|
||||
BufferIO::CopyWStr(L"", ret, 256);
|
||||
}
|
||||
}
|
||||
FILE* DeckManager::OpenDeckFile(const wchar_t* file, const char* mode) {
|
||||
FILE* fp = mywfopen(file, mode);
|
||||
return fp;
|
||||
}
|
||||
irr::io::IReadFile* DeckManager::OpenDeckReader(const wchar_t* file) {
|
||||
char file2[256];
|
||||
BufferIO::EncodeUTF8(file, file2);
|
||||
auto reader = dataManager.IrrFileSystem->createAndOpenFile(file2);
|
||||
return reader;
|
||||
}
|
||||
bool DeckManager::LoadCurrentDeck(std::istringstream& deckStream, bool is_packlist) {
|
||||
LoadDeckFromStream(current_deck, deckStream, is_packlist);
|
||||
return true; // the above LoadDeck has return value but we ignore it here for now
|
||||
}
|
||||
bool DeckManager::LoadCurrentDeck(const wchar_t* file, bool is_packlist) {
|
||||
current_deck.clear();
|
||||
if (!file[0])
|
||||
return false;
|
||||
char deckBuffer[MAX_YDK_SIZE]{};
|
||||
auto reader = OpenDeckReader(file);
|
||||
if(!reader) {
|
||||
wchar_t localfile[256];
|
||||
myswprintf(localfile, L"./deck/%ls.ydk", file);
|
||||
reader = OpenDeckReader(localfile);
|
||||
}
|
||||
if(!reader && !mywcsncasecmp(file, L"./pack", 6)) {
|
||||
wchar_t zipfile[256];
|
||||
myswprintf(zipfile, L"%ls", file + 2);
|
||||
reader = OpenDeckReader(zipfile);
|
||||
}
|
||||
if(!reader)
|
||||
return false;
|
||||
int size = reader->read(deckBuffer, sizeof deckBuffer);
|
||||
reader->drop();
|
||||
if (size >= (int)sizeof deckBuffer) {
|
||||
return false;
|
||||
}
|
||||
std::istringstream deckStream(deckBuffer);
|
||||
LoadDeckFromStream(current_deck, deckStream, is_packlist);
|
||||
return true; // the above function has return value but we ignore it here for now
|
||||
}
|
||||
bool DeckManager::LoadCurrentDeck(int category_index, const wchar_t* category_name, const wchar_t* deckname) {
|
||||
wchar_t filepath[256];
|
||||
GetDeckFile(filepath, category_index, category_name, deckname);
|
||||
bool is_packlist = (category_index == DECK_CATEGORY_PACK);
|
||||
if(!LoadCurrentDeck(filepath, is_packlist))
|
||||
return false;
|
||||
if (mainGame->is_building)
|
||||
mainGame->deckBuilder.RefreshPackListScroll();
|
||||
return true;
|
||||
}
|
||||
void DeckManager::SaveDeck(const Deck& deck, std::stringstream& deckStream) {
|
||||
deckStream << "#created by ..." << std::endl;
|
||||
deckStream << "#main" << std::endl;
|
||||
for(size_t i = 0; i < deck.main.size(); ++i)
|
||||
deckStream << deck.main[i]->code << std::endl;
|
||||
deckStream << "#extra" << std::endl;
|
||||
for(size_t i = 0; i < deck.extra.size(); ++i)
|
||||
deckStream << deck.extra[i]->code << std::endl;
|
||||
deckStream << "!side" << std::endl;
|
||||
for(size_t i = 0; i < deck.side.size(); ++i)
|
||||
deckStream << deck.side[i]->code << std::endl;
|
||||
}
|
||||
bool DeckManager::SaveDeck(const Deck& deck, const wchar_t* file) {
|
||||
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
|
||||
return false;
|
||||
FILE* fp = OpenDeckFile(file, "w");
|
||||
if(!fp)
|
||||
return false;
|
||||
std::stringstream deckStream;
|
||||
SaveDeck(deck, deckStream);
|
||||
std::fputs(deckStream.str().c_str(), fp);
|
||||
std::fclose(fp);
|
||||
return true;
|
||||
}
|
||||
bool DeckManager::DeleteDeck(const wchar_t* file) {
|
||||
return FileSystem::RemoveFile(file);
|
||||
}
|
||||
int DeckManager::TypeCount(std::vector<const CardDataC*> list, unsigned int ctype) {
|
||||
int res = 0;
|
||||
for(size_t i = 0; i < list.size(); ++i) {
|
||||
const CardDataC* cur = list[i];
|
||||
if(cur->type & ctype)
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
bool DeckManager::LoadDeckFromCode(Deck& deck, const unsigned char *code, int len) {
|
||||
unsigned char data[1024], *pdeck = data, *data_ = data;
|
||||
int decoded_len = Base64::DecodedLength(code, len);
|
||||
if(decoded_len > 1024 || decoded_len < 8 || !Base64::Decode(code, len, data_, decoded_len))
|
||||
return false;
|
||||
int mainc = BufferIO::Read<int32_t>(pdeck);
|
||||
int sidec = BufferIO::Read<int32_t>(pdeck);
|
||||
int errorcode = LoadDeck(deck, (uint32_t*)pdeck, mainc, sidec);
|
||||
return (errorcode == 0);
|
||||
}
|
||||
int DeckManager::SaveDeckToCode(Deck& deck, unsigned char* code) {
|
||||
unsigned char deckbuf[1024], *pdeck = deckbuf;
|
||||
BufferIO::Write<int32_t>(pdeck, deck.main.size() + deck.extra.size());
|
||||
BufferIO::Write<int32_t>(pdeck, deck.side.size());
|
||||
for(size_t i = 0; i < deck.main.size(); ++i)
|
||||
BufferIO::Write<int32_t>(pdeck, deck.main[i]->code);
|
||||
for(size_t i = 0; i < deck.extra.size(); ++i)
|
||||
BufferIO::Write<int32_t>(pdeck, deck.extra[i]->code);
|
||||
for(size_t i = 0; i < deck.side.size(); ++i)
|
||||
BufferIO::Write<int32_t>(pdeck, deck.side[i]->code);
|
||||
int len = pdeck - deckbuf;
|
||||
int encoded_len = Base64::EncodedLength(len);
|
||||
Base64::Encode(deckbuf, len, code, encoded_len+1);
|
||||
return encoded_len;
|
||||
}
|
||||
bool DeckManager::CreateCategory(const wchar_t* name) {
|
||||
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
|
||||
return false;
|
||||
if(name[0] == 0)
|
||||
return false;
|
||||
wchar_t localname[256];
|
||||
myswprintf(localname, L"./deck/%ls", name);
|
||||
return FileSystem::MakeDir(localname);
|
||||
}
|
||||
bool DeckManager::RenameCategory(const wchar_t* oldname, const wchar_t* newname) {
|
||||
if(!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
|
||||
return false;
|
||||
if(newname[0] == 0)
|
||||
return false;
|
||||
wchar_t oldlocalname[256];
|
||||
wchar_t newlocalname[256];
|
||||
myswprintf(oldlocalname, L"./deck/%ls", oldname);
|
||||
myswprintf(newlocalname, L"./deck/%ls", newname);
|
||||
return FileSystem::Rename(oldlocalname, newlocalname);
|
||||
}
|
||||
bool DeckManager::DeleteCategory(const wchar_t* name) {
|
||||
wchar_t localname[256];
|
||||
myswprintf(localname, L"./deck/%ls", name);
|
||||
if(!FileSystem::IsDirExists(localname))
|
||||
return false;
|
||||
return FileSystem::DeleteDir(localname);
|
||||
}
|
||||
bool DeckManager::SaveDeckArray(const DeckArray& deck, const wchar_t* name) {
|
||||
if (!FileSystem::IsDirExists(L"./deck") && !FileSystem::MakeDir(L"./deck"))
|
||||
return false;
|
||||
FILE* fp = OpenDeckFile(name, "w");
|
||||
if (!fp)
|
||||
return false;
|
||||
std::fprintf(fp, "#created by ...\n#main\n");
|
||||
for (const auto& code : deck.main)
|
||||
std::fprintf(fp, "%u\n", code);
|
||||
std::fprintf(fp, "#extra\n");
|
||||
for (const auto& code : deck.extra)
|
||||
std::fprintf(fp, "%u\n", code);
|
||||
std::fprintf(fp, "!side\n");
|
||||
for (const auto& code : deck.side)
|
||||
std::fprintf(fp, "%u\n", code);
|
||||
std::fclose(fp);
|
||||
return true;
|
||||
}
|
||||
#endif //YGOPRO_SERVER_MODE
|
||||
}
|
||||
Reference in new issue
Block a user