Skip to content

Commit

Permalink
add function to manually trigger a forced recalc of all loaded scores
Browse files Browse the repository at this point in the history
  • Loading branch information
MinaciousGrace committed Jul 1, 2020
1 parent d16b368 commit 92fa46d
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ local translated_info = {
AssetSettings = THEME:GetString("TabProfile", "AssetSettingEntry"),
Success = THEME:GetString("TabProfile", "SaveSuccess"),
Failure = THEME:GetString("TabProfile", "SaveFail"),
ValidateAll = THEME:GetString("TabProfile", "ValidateAllScores")
ValidateAll = THEME:GetString("TabProfile", "ValidateAllScores"),
ForceRecalc = THEME:GetString("TabProfile", "ForceRecalcScores"),
}

local t =
Expand Down Expand Up @@ -743,7 +744,24 @@ local profilebuttons =
STATSMAN:UpdatePlayerRating()
end
end
}
},
LoadFont("Common Large") ..
{
InitCommand = function(self)
self:x(300):diffuse(getMainColor("positive")):settext(translated_info["ForceRecalc"]):zoom(0.3)
end
},
Def.Quad {
InitCommand = function(self)
self:x(300):zoomto(100, 20):diffusealpha(buttondiffuse)
end,
MouseLeftClickMessageCommand = function(self)
if ButtonActive(self) and rankingSkillset == 1 then
ms.ok("Recalculating Scores... this might be slow and may or may not crash")
profile:ForceRecalcScores()
end
end
}
}

t[#t + 1] = profilebuttons
Expand Down
1 change: 1 addition & 0 deletions Themes/Til Death/Languages/en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ SaveSuccess=Save Successful
SaveFail=Save Failed
AssetSettingEntry=Asset Settings
ValidateAllScores=Validate All
ForceRecalcScores=Recalc Scores
[TabFilter]
Title=Filters (WIP)
Expand Down
3 changes: 2 additions & 1 deletion Themes/Til Death/Languages/zh.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;Etterna Til Death主題繁體中文化檔案 Ver 0.66.1
;Etterna Til Death主題繁體中文化檔案 Ver 0.66.1
;譯者雨幕幕, 有問題或建議請加Q695312637或discord找我

[Common]
Expand Down Expand Up @@ -562,6 +562,7 @@ SaveSuccess=保存成功
SaveFail=保存失敗
AssetSettingEntry=個人偏好設置
ValidateAllScores=全部認證
ForceRecalcScores=重新計算分數
[TabFilter]
Title=歌曲篩選 (待完成)
Expand Down
7 changes: 7 additions & 0 deletions src/Etterna/Models/Misc/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,12 @@ class LunaProfile : public Luna<Profile>
return 1;
}

static int ForceRecalcScores(T* p, lua_State* L)
{
SCOREMAN->RecalculateSSRs(p->m_sProfileID);
return 0;
}

LunaProfile()
{
ADD_METHOD(AddScreenshot);
Expand Down Expand Up @@ -1815,6 +1821,7 @@ class LunaProfile : public Luna<Profile>
ADD_METHOD(SortByDiff);
ADD_METHOD(ToggleFilter);
ADD_METHOD(GetFilterMode);
ADD_METHOD(ForceRecalcScores);
}
};

Expand Down
115 changes: 114 additions & 1 deletion src/Etterna/Singletons/ScoreManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,120 @@ ScoreManager::RecalculateSSRs(LoadingWindow* ld, const string& profileID)
return;
}

// should deal with this misnomer - mina
void
ScoreManager::RecalculateSSRs(const string& profileID)
{
auto& scores = SCOREMAN->GetAllScores();

mutex songVectorPtrMutex;
vector<std::uintptr_t> currentlyLockedSongs;
// This is meant to ensure mutual exclusion for a song
class SongLock
{
public:
mutex& songVectorPtrMutex; // This mutex guards the vector
vector<std::uintptr_t>&
currentlyLockedSongs; // Vector of currently locked songs
std::uintptr_t song; // The song for this lock
SongLock(vector<std::uintptr_t>& vec, mutex& mut, std::uintptr_t k)
: currentlyLockedSongs(vec)
, songVectorPtrMutex(mut)
, song(k)
{
bool active = true;
{
lock_guard<mutex> lk(songVectorPtrMutex);
active = find(currentlyLockedSongs.begin(),
currentlyLockedSongs.end(),
song) != currentlyLockedSongs.end();
if (!active)
currentlyLockedSongs.emplace_back(song);
}
while (active) {
// TODO: Try to make this wake up from the destructor (CondVar's
// maybe)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
{
lock_guard<mutex> lk(songVectorPtrMutex);
active = find(currentlyLockedSongs.begin(),
currentlyLockedSongs.end(),
song) != currentlyLockedSongs.end();
if (!active)
currentlyLockedSongs.emplace_back(song);
}
}
}
~SongLock()
{
lock_guard<mutex> lk(songVectorPtrMutex);
currentlyLockedSongs.erase(find(
currentlyLockedSongs.begin(), currentlyLockedSongs.end(), song));
}
};
function<void(std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>>,
ThreadData*)>
callback =
[&songVectorPtrMutex, &currentlyLockedSongs](
std::pair<vectorIt<HighScore*>, vectorIt<HighScore*>> workload,
ThreadData* data) {
std::unique_ptr<Calc> per_thread_calc = std::make_unique<Calc>();

int scoreIndex = 0;
for (auto it = workload.first; it != workload.second; it++) {
auto hs = *it;
++scoreIndex;

const string& ck = hs->GetChartKey();
Steps* steps = SONGMAN->GetStepsByChartkey(ck);

// check for unloaded steps, only allow 4k
if (steps == nullptr ||
steps->m_StepsType != StepsType_dance_single)
continue;

float ssrpercent = hs->GetSSRNormPercent();

// don't waste time on <= 0%s
if (ssrpercent <= 0.f || !steps->IsRecalcValid()) {
hs->ResetSkillsets();
continue;
}
SongLock lk(currentlyLockedSongs,
songVectorPtrMutex,
reinterpret_cast<std::uintptr_t>(steps->m_pSong));

float musicrate = hs->GetMusicRate();

TimingData* td = steps->GetTimingData();
NoteData nd;
steps->GetNoteData(nd);

const auto& serializednd = nd.SerializeNoteData2(td);
vector<float> dakine;

if (steps->m_StepsType == StepsType_dance_single) {
dakine = MinaSDCalc(serializednd,
musicrate,
ssrpercent,
per_thread_calc.get());
}

auto ssrVals = dakine;
FOREACH_ENUM(Skillset, ss)
hs->SetSkillsetSSR(ss, ssrVals[ss]);
hs->SetSSRCalcVersion(GetCalcVersion());

td->UnsetEtaner();
nd.UnsetNerv();
nd.UnsetSerializedNoteData();
steps->Compress();
}
};

parallelExecution<HighScore*>(scores, callback);
return;
}

void
ScoreManager::UnInvalidateAllScores()
{
Expand Down
1 change: 1 addition & 0 deletions src/Etterna/Singletons/ScoreManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class ScoreManager
Skillset ss,
const string& profileID = PROFILEMAN->GetProfile(PLAYER_1)->m_sProfileID);
void RecalculateSSRs(LoadingWindow* ld, const string& profileID);
void RecalculateSSRs(const string& profileID);
void UnInvalidateAllScores();
void CalcPlayerRating(float& prating,
float* pskillsets,
Expand Down
28 changes: 28 additions & 0 deletions src/RageUtil/Misc/RageThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ parallelExecution(vector<T> vec,
parallelExecution(vec, update, exec, nullptr);
}

template<typename T>
void
parallelExecution(vector<T> vec,
function<void(vectorRange<T>, ThreadData*)> exec)
{
const int THREADS = PREFSMAN->ThreadsToUse <= 0
? std::thread::hardware_concurrency()
: min((int)PREFSMAN->ThreadsToUse,
(int)std::thread::hardware_concurrency());
std::vector<vectorRange<T>> workloads =
splitWorkLoad(vec, static_cast<size_t>(vec.size() / THREADS));
ThreadData data;
auto threadCallback = [&data, &exec](vectorRange<T> workload) {
exec(workload, &data);
data._threadsFinished++;
data.setUpdated(true);
};
vector<thread> threadpool;
for (auto& workload : workloads)
threadpool.emplace_back(thread(threadCallback, workload));
while (data._threadsFinished < (int)workloads.size()) {
data.waitForUpdate();
data.setUpdated(false);
}
for (auto& thread : threadpool)
thread.join();
}

struct ThreadSlot;
class RageTimer;

Expand Down

0 comments on commit 92fa46d

Please sign in to comment.