Path: blob/master/RSDKv5/RSDK/User/Core/UserLeaderboards.cpp
1163 views
#include "RSDK/Core/RetroEngine.hpp"1#include <string>23// ====================4// API Cores5// ====================67namespace RSDK8{9namespace SKU10{11// Dummy API12#if RETRO_USERCORE_DUMMY13#include "RSDK/User/Dummy/DummyLeaderboards.cpp"14#endif1516// Steam API17#if RETRO_USERCORE_STEAM18#include "RSDK/User/Steam/SteamLeaderboards.cpp"19#endif2021// Epic Games API22#if RETRO_USERCORE_EOS23#include "RSDK/User/EOS/EOSLeaderboards.cpp"24#endif2526// Switch API27#if RETRO_USERCORE_NX28#include "RSDK/User/NX/NXLeaderboards.cpp"29#endif3031} // namespace SKU32} // namespace RSDK3334using namespace RSDK;3536#if RETRO_REV0237SKU::UserLeaderboards *RSDK::SKU::leaderboards = NULL;3839RSDK::SKU::LeaderboardLoadInfo *RSDK::SKU::LeaderboardLoadList::InitLoadInfo()40{41LeaderboardLoadInfo *info = NULL;42AllocateStorage((void **)&info, sizeof(LeaderboardLoadInfo), DATASET_TMP, true);43memset(info, 0, sizeof(LeaderboardLoadInfo));44info->parent = this;45return info;46}4748void RSDK::SKU::LeaderboardLoadList::Clear()49{50while (true) {51LeaderboardLoadInfo *current = this->next;52if (!current)53break;5455LeaderboardLoadInfo *next = current->next;56if (next)57next->prev = NULL;58this->next = next;5960if (current == this->prev)61this->prev = NULL;6263--this->count;6465RemoveStorageEntry((void **)¤t);66}6768this->prev = NULL;69this->next = NULL;70this->last = NULL;71this->count = 0;72this->unused = 0;73}7475void RSDK::SKU::LeaderboardLoadList::AddLoadInfoPrev(LeaderboardLoadInfo *info)76{77LeaderboardLoadInfo *next = this->next;78if (next) {79if (this->count != 10) {80next->prev = info;81info->next = next;82++this->count;83this->next = info;84}85}86else {87Clear();8889this->count = 1;90this->prev = info;91this->next = info;92this->last = info;93if (info->avail.start == 1)94info->disableLoadPrev = true;95}96}9798void RSDK::SKU::LeaderboardLoadList::AddLoadInfoNext(LeaderboardLoadInfo *info)99{100LeaderboardLoadInfo *last = this->last;101if (last) {102if (this->count != 10) {103last->next = info;104info->prev = last;105++this->count;106this->last = info;107}108}109else {110Clear();111112this->count = 1;113this->prev = info;114this->next = info;115this->last = info;116117if (info->avail.start == 1)118info->disableLoadPrev = true;119}120}121122void RSDK::SKU::LeaderboardLoadList::RemoveLoadInfoPrev()123{124LeaderboardLoadInfo *last = this->last;125126if (this->last) {127LeaderboardLoadInfo *current = last->prev;128if (current)129current->next = NULL;130131this->last = current;132133if (last == this->prev)134this->prev = NULL;135136--this->count;137}138}139void RSDK::SKU::LeaderboardLoadList::RemoveLoadInfoNext()140{141LeaderboardLoadInfo *next = this->next;142143if (next) {144LeaderboardLoadInfo *current = next->next;145if (current)146current->prev = NULL;147this->next = current;148149if (next == this->prev)150this->prev = NULL;151152--this->count;153}154}155156void RSDK::SKU::LeaderboardEntryInfo::Setup()157{158this->viewSize.start = 0;159this->viewSize.length = 0;160memset(this->entries, 0, sizeof(this->entries));161162LeaderboardLoadInfo *info = this->loadList->next;163int32 count = 0;164if (info) {165this->viewSize.start = info->avail.start;166167while (info) {168LeaderboardEntry **entryList = &entries[count];169LeaderboardEntry *entry = info->entries;170171for (int32 e = 0; e < info->avail.length; ++e) {172*entryList++ = &entry[e];173++count;174}175176info = info->next;177}178}179180this->viewSize.length = count;181}182183void RSDK::SKU::LeaderboardEntryInfo::HandleTimers()184{185UserLeaderboards *leaderboards = this->parent;186if (leaderboards->currentLeaderboard && this->loadType) {187LeaderboardLoadInfo *next = this->loadList->next;188LeaderboardLoadInfo *last = this->loadList->last;189190if (next && last) {191switch (this->loadType) {192default:193case LEADERBOARD_LOAD_INIT:194case LEADERBOARD_LOAD_NEXT:195if ((this->loadSize.start + this->loadSize.length) != (last->avail.start + last->avail.length) && !next->disableLoadNext196&& last->status == STATUS_OK) {197if (this->loadList->count == 10) {198this->loadList->RemoveLoadInfoNext();199RemoveStorageEntry((void **)&next);200}201LeaderboardLoadInfo *info = this->loadList->InitLoadInfo();202info->avail.start = last->avail.length + last->avail.start;203info->avail.length = 20;204this->loadList->AddLoadInfoNext(info);205leaderboards->LoadLeaderboards(info);206Setup();207}208break;209210case LEADERBOARD_LOAD_PREV:211if (next->avail.start != this->loadSize.start && !next->disableLoadPrev && next->status == STATUS_OK) {212if (this->loadList->count == 10) {213this->loadList->RemoveLoadInfoPrev();214RemoveStorageEntry((void **)&last);215}216LeaderboardLoadInfo *info = this->loadList->InitLoadInfo();217218info->avail.start = next->avail.start;219if (info->avail.start > 20)220info->avail.start -= 20;221else222info->avail.start = 1;223224info->avail.length = next->avail.start - info->avail.start;225226if (info->avail.start == 1)227info->disableLoadPrev = true;228229this->loadList->AddLoadInfoPrev(info);230leaderboards->LoadLeaderboards(info);231Setup();232}233break;234}235}236}237}238239void RSDK::SKU::LeaderboardEntryInfo::LoadLeaderboardEntries(int32 start, uint32 length, int32 type)240{241switch (type) {242default:243case LEADERBOARD_LOAD_INIT:244loadType = (LeaderboardLoadTypes)type;245246loadSize.start = start;247loadSize.length = length;248break;249250case LEADERBOARD_LOAD_PREV:251loadType = (LeaderboardLoadTypes)type;252253loadSize.start = MAX(start, 1);254loadSize.length = MIN(length, 200);255break;256257case LEADERBOARD_LOAD_NEXT: {258loadType = (LeaderboardLoadTypes)type;259260int32 startPos = start;261int32 endPos = start + length;262if (length - start < 200)263startPos = endPos - 200;264265loadSize.start = startPos;266loadSize.length = endPos - startPos;267break;268}269}270}271272void RSDK::SKU::ResetLeaderboardInfo()273{274leaderboards->status = STATUS_NONE;275leaderboards->currentLeaderboard = NULL;276277leaderboards->loadList.Clear();278leaderboards->entryInfo.Setup();279}280281SKU::LeaderboardEntry *RSDK::SKU::ReadLeaderboardEntry(int32 entryID)282{283if (entryID >= leaderboards->entryInfo.viewSize.start284&& entryID < leaderboards->entryInfo.viewSize.start + leaderboards->entryInfo.viewSize.length)285return leaderboards->entryInfo.entries[entryID - leaderboards->entryInfo.viewSize.start];286287return NULL;288}289#endif290291292