ScoreSaver: reduce startup delays, hopefully reduce crashing on exit

This commit is contained in:
somewhatlurker
2019-10-04 21:51:44 +10:00
parent 6274f23e5b
commit 23fdf9b09d
2 changed files with 23 additions and 13 deletions
@@ -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()
@@ -3,6 +3,7 @@
#include "../Constants.h"
#include <windows.h>
#include <vector>
#include <thread>
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<uint8_t> 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;
};
}