From 23fdf9b09dae171dbd1401fe505107165e704579 Mon Sep 17 00:00:00 2001 From: somewhatlurker <52014015+somewhatlurker@users.noreply.github.com> Date: Fri, 4 Oct 2019 21:51:44 +1000 Subject: [PATCH] ScoreSaver: reduce startup delays, hopefully reduce crashing on exit --- .../plugins/TLAC/Components/ScoreSaver.cpp | 33 +++++++++++-------- .../plugins/TLAC/Components/ScoreSaver.h | 3 ++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/source-code/source/plugins/TLAC/Components/ScoreSaver.cpp b/source-code/source/plugins/TLAC/Components/ScoreSaver.cpp index ce7eb29..c684e44 100644 --- a/source-code/source/plugins/TLAC/Components/ScoreSaver.cpp +++ b/source-code/source/plugins/TLAC/Components/ScoreSaver.cpp @@ -1,4 +1,5 @@ #include "ScoreSaver.h" +#include "GameState.h" #include "../Constants.h" #include "../framework.h" #include "../Utilities/Operations.h" @@ -22,11 +23,6 @@ namespace TLAC::Components ScoreSaver::~ScoreSaver() { - for (int diff = 0; diff < 4; diff++) - { - *(DivaScore**)(PLAYER_DATA_ADDRESS + diff * 0x18 + 0x5d0) = 0; - *(DivaScore**)(PLAYER_DATA_ADDRESS + diff * 0x18 + 0x5d8) = 0; - } } const char* ScoreSaver::GetDisplayName() @@ -34,14 +30,8 @@ namespace TLAC::Components return "score_saver"; } - bool(__stdcall* ScoreSaver::divaInitResults)(void* cls) = (bool(__stdcall*)(void* cls))RESULTS_INIT_ADDRESS; - void ScoreSaver::Initialize(ComponentsManager*) + void ScoreSaver::initCache() { - DetourTransactionBegin(); - DetourUpdateThread(GetCurrentThread()); - DetourAttach(&(PVOID&)ScoreSaver::divaInitResults, (PVOID)(ScoreSaver::hookedInitResults)); - DetourTransactionCommit(); - // build the score cache UpdateScoreCache(); UpdateClearCounts(); @@ -54,6 +44,18 @@ namespace TLAC::Components } } + bool(__stdcall* ScoreSaver::divaInitResults)(void* cls) = (bool(__stdcall*)(void* cls))RESULTS_INIT_ADDRESS; + std::thread ScoreSaver::initThread; + void ScoreSaver::Initialize(ComponentsManager*) + { + DetourTransactionBegin(); + DetourUpdateThread(GetCurrentThread()); + DetourAttach(&(PVOID&)ScoreSaver::divaInitResults, (PVOID)(ScoreSaver::hookedInitResults)); + DetourTransactionCommit(); + + initThread = std::thread(initCache); + } + bool ScoreSaver::checkExistingScoreValid(int pv, int difficulty, int isEx) { WCHAR keyBase[32]; // needs to be big enough to store pv.999.diff.3.ex @@ -313,7 +315,12 @@ namespace TLAC::Components void ScoreSaver::Update() { - return; + if (*(GameState*)CURRENT_GAME_STATE_ADDRESS == GS_GAME && *(SubGameState*)CURRENT_GAME_SUB_STATE_ADDRESS == SUB_SELECTOR && initThread.joinable()) + { + // it's actually fine to let the init happen in the background after reaching game state, but this is probably safer + printf("[ScoreSaver] Waiting for initialisation..."); + initThread.join(); + } } void ScoreSaver::UpdateInput() diff --git a/source-code/source/plugins/TLAC/Components/ScoreSaver.h b/source-code/source/plugins/TLAC/Components/ScoreSaver.h index 8a7dc89..89236c5 100644 --- a/source-code/source/plugins/TLAC/Components/ScoreSaver.h +++ b/source-code/source/plugins/TLAC/Components/ScoreSaver.h @@ -3,6 +3,7 @@ #include "../Constants.h" #include #include +#include namespace TLAC::Components { @@ -109,10 +110,12 @@ namespace TLAC::Components static bool(__stdcall* divaInitResults)(void* cls); static bool hookedInitResults(void* cls); static void InjectCode(void* address, const std::vector data); + static void initCache(); static bool checkExistingScoreValid(int pv, int difficulty, int isEx); static int calculateCheck(int score, int cntCools, int cntFines, int percent, int combo, int clearRank, int allTimeRank, int allTimeModifiers, int allTimePercent); static WCHAR configPath[256]; static WCHAR rival_configPath[256]; + static std::thread initThread; }; }