Skip to content

Commit cdeca4b

Browse files
committed
fix: crash conflict with fast/slow mod, better totalnotes fix
1 parent 3681207 commit cdeca4b

2 files changed

Lines changed: 36 additions & 27 deletions

File tree

src/LR2HackBox/Features/BattleFixes.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,28 @@ int BattleFixes::OnAddDrawingBuffer_PlayArea(LR2::DrawingBuf* drb, LR2::SRCstruc
9696
}
9797
}
9898

99-
int BattleFixes::OnProcSinglenote(LR2::game* g, int lane, int keypress, int timing, int player) {
99+
void BattleFixes::OnBeginProcNote(SafetyHookContext& regs) {
100+
BattleFixes& battleFixes = *(BattleFixes*)(LR2HackBox::Get().mBattleFixes.get());
101+
LR2::game& game = *LR2HackBox::Get().GetGame();
102+
if (!battleFixes.mIsEnabled || game.config.play.battle != 1) return;
103+
battleFixes.autoadjust_lastPlayer = *(int*)(regs.esp + 0x14);
104+
battleFixes.autoadjust_lastMidCount = game.gameplay.autojudge_midcount;
105+
battleFixes.autoadjust_lastMidSum = game.gameplay.autojudge_midsum;
106+
}
107+
108+
void BattleFixes::OnEndProcNote(SafetyHookContext& regs) {
100109
BattleFixes& battleFixes = *(BattleFixes*)(LR2HackBox::Get().mBattleFixes.get());
101110
GameOptions& options = *(GameOptions*)(LR2HackBox::Get().mGameOptions.get());
102-
if (!battleFixes.mIsEnabled) return battleFixes.oProcSinglenote.ccall<int>(g, lane, keypress, timing, player);
103-
int oldMidCount = g->gameplay.autojudge_midcount;
104-
int oldMidSum = g->gameplay.autojudge_midsum;
105-
int retVal = battleFixes.oProcSinglenote.ccall<int>(g, lane, keypress, timing, player);
106-
if (player == 1) {
107-
if (oldMidCount != g->gameplay.autojudge_midcount) {
111+
LR2::game& game = *LR2HackBox::Get().GetGame();
112+
if (!battleFixes.mIsEnabled || game.config.play.battle != 1) return;
113+
if (battleFixes.autoadjust_lastPlayer == 1) {
114+
if (battleFixes.autoadjust_lastMidCount != game.gameplay.autojudge_midcount) {
108115
battleFixes.autoadjust_midcountP2++;
109-
battleFixes.autoadjust_midsumP2 += g->gameplay.autojudge_midsum - oldMidSum;
110-
g->gameplay.autojudge_midcount = oldMidCount;
111-
g->gameplay.autojudge_midsum = oldMidSum;
116+
battleFixes.autoadjust_midsumP2 += game.gameplay.autojudge_midsum - battleFixes.autoadjust_lastMidSum;
117+
game.gameplay.autojudge_midcount = battleFixes.autoadjust_lastMidCount;
118+
game.gameplay.autojudge_midsum = battleFixes.autoadjust_lastMidSum;
112119
}
113120
}
114-
return retVal;
115121
}
116122

117123
void BattleFixes::OnCheckAutoadjustCondition(SafetyHookContext& regs) {
@@ -152,18 +158,12 @@ void BattleFixes::OnCheckMousewheelLanecover(SafetyHookContext& regs) {
152158
}
153159
}
154160

155-
void BattleFixes::OnGameStart(SafetyHookContext& regs) {
161+
void BattleFixes::OnShuffleNotesLoop(SafetyHookContext& regs) {
162+
BattleFixes& battleFixes = *(BattleFixes*)(LR2HackBox::Get().mBattleFixes.get());
156163
LR2::game& game = *LR2HackBox::Get().GetGame();
157-
158-
int totalnotes[2];
159-
memset(totalnotes, 0, sizeof(totalnotes));
160-
for (int lane = 0; lane < 20; lane++) {
161-
int laneCount = game.gameplay.bmsobj_note[lane].count;
162-
int player = lane < 10 ? 0 : 1;
163-
totalnotes[player] += laneCount;
164-
}
165-
game.gameplay.player[0].totalnotes = totalnotes[0];
166-
game.gameplay.player[1].totalnotes = totalnotes[1];
164+
if (!battleFixes.mIsEnabled) return;
165+
unsigned int& isBattle = *(unsigned int*)(regs.esp + 0x48);
166+
isBattle = 0;
167167
}
168168

169169
void BattleFixes::SceneInit() {
@@ -172,6 +172,9 @@ void BattleFixes::SceneInit() {
172172
lastLineBmstime = -1.;
173173
autoadjust_midcountP2 = 0;
174174
autoadjust_midsumP2 = 0;
175+
autoadjust_lastPlayer = 0;
176+
autoadjust_lastMidCount = 0;
177+
autoadjust_lastMidSum = 0;
175178
}
176179

177180
static void ApplyP2ShutterFix(bool enable) {
@@ -214,12 +217,14 @@ bool BattleFixes::Init(uintptr_t moduleBase) {
214217
mMidHooks.push_back(safetyhook::create_mid(0x42BC62, OnSetBpmChangedBmstime));
215218
oAddDrawingBuffer_PlayArea = safetyhook::create_inline(0x49D630, OnAddDrawingBuffer_PlayArea);
216219

217-
oProcSinglenote = safetyhook::create_inline(moduleBase + 0x018850, OnProcSinglenote);
220+
mMidHooks.push_back(safetyhook::create_mid(0x419320, OnBeginProcNote));
221+
mMidHooks.push_back(safetyhook::create_mid(0x419410, OnEndProcNote));
222+
mMidHooks.push_back(safetyhook::create_mid(0x419432, OnEndProcNote));
218223
mMidHooks.push_back(safetyhook::create_mid(0x42C832, OnCheckAutoadjustCondition));
219224

220225
mMidHooks.push_back(safetyhook::create_mid(0x427779, OnCheckMousewheelLanecover));
221226

222-
mMidHooks.push_back(safetyhook::create_mid(0x42CD2C, OnGameStart));
227+
mMidHooks.push_back(safetyhook::create_mid(0x4B4A30, OnShuffleNotesLoop));
223228

224229
ApplyP2ShutterFix(mIsEnabled);
225230

src/LR2HackBox/Features/BattleFixes.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ class BattleFixes : public ModFeature {
1818
static void OnDrawKeyLoop(SafetyHookContext& regs);
1919
static void OnSetBpmChangedBmstime(SafetyHookContext& regs);
2020
static int OnAddDrawingBuffer_PlayArea(LR2::DrawingBuf* drb, LR2::SRCstruct* src, LR2::DSTstruct* dst, LR2::Timer* T, float shiftX, float shiftY, int alpha, float sizeX, float sizeY, char flag);
21-
static int OnProcSinglenote(LR2::game* g, int lane, int keypress, int timing, int player);
21+
static void OnBeginProcNote(SafetyHookContext& regs);
22+
static void OnEndProcNote(SafetyHookContext& regs);
2223
static void OnCheckAutoadjustCondition(SafetyHookContext& regs);
2324
static void OnCheckMousewheelLanecover(SafetyHookContext& regs);
24-
static void OnGameStart(SafetyHookContext& regs);
25+
static void OnShuffleNotesLoop(SafetyHookContext& regs);
2526
SafetyHookInline oAddDrawingBuffer_PlayArea;
26-
SafetyHookInline oProcSinglenote;
27+
SafetyHookInline oProcNote;
2728
std::vector<SafetyHookMid> mMidHooks;
2829

2930
bool mIsEnabled = false;
@@ -34,4 +35,7 @@ class BattleFixes : public ModFeature {
3435
int bpmChangedBmstimeP2 = -1;
3536
int autoadjust_midcountP2 = 0;
3637
int autoadjust_midsumP2 = 0;
38+
int autoadjust_lastPlayer = 0;
39+
int autoadjust_lastMidCount = 0;
40+
int autoadjust_lastMidSum = 0;
3741
};

0 commit comments

Comments
 (0)