-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mod.cpp
330 lines (275 loc) · 8.09 KB
/
Mod.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "Configuration.h"
struct resultData
{
Results resultType; // The chosen results.
double clearLength; // How long act clear lasts for (-1 for only clear).
float crossFade; // How long the cross fade to results lasts for.
bool hasERank; // If the chosen results has a E-Rank variant.
bool hasBoss; // If the chosen results has a boss clear variant.
};
const char* resultsChar[] =
{
"Custom",
"Sonic1",
"SCDJP",
"SCDUS",
"S3K",
"SonicR",
"Adventure",
"Adventure2",
"Shadow",
"Sonic06",
"Sonic06Town",
"SecretRings",
"Unleashed",
"SmashBros",
"BlackKnight",
"S4E1",
"Colors",
"Frontiers",
"S4E2",
"LostWorld",
"Mania",
"Forces",
"SRB2",
"Persona",
"Persona2IS",
"Persona2EP",
"Persona3",
"Persona4",
"Persona5",
"Custom2",
"Rush",
"RushAdventure",
"ColorsSim",
"FrontiersQuest",
"FrontiersHack",
"DIVAAC",
"DIVAACB",
"DIVAFT",
"Persona3R"
};
resultData modernResults;
resultData classicResults;
resultData currentResults;
char clearString[32];
char bossString[32];
char result0String[32];
char result1String[32];
char result2String[32];
// This prepares the result data.
resultData PrepareResults(bool isModern)
{
Results resultType = Results::Generations;
double clearLength = 6.099999904632568; // Default Generations length. (0x17046C0)
float crossFade = 0.0f;
bool hasERank = false;
bool hasBoss = false;
if (isModern)
resultType = Configuration::ResultOptionModern;
else
resultType = Configuration::ResultOptionClassic;
switch (resultType)
{
case Sonic06:
case Sonic06Town:
{
clearLength = 7.381;
break;
}
case Unleashed:
{
clearLength = 6.021;
crossFade = 0.8f;
hasERank = true;
hasBoss = true;
break;
}
case Colors:
case ColorsSim:
{
clearLength = 8.01;
crossFade = 0.2f;
if (resultType != Results::ColorsSim)
hasBoss = true;
break;
}
case LostWorld:
{
clearLength = 8.182;
break;
}
case BlackKnight:
{
clearLength = 10.0;
hasERank = true;
break;
}
case Frontiers:
{
clearLength = 0.0;
break;
}
case DIVAAC:
case DIVAACB:
case DIVAFT:
{
clearLength = 0.0;
hasERank = true;
break;
}
case SmashBros:
{
clearLength = 7.631;
break;
}
case Custom:
{
if (Configuration::CustomOnlyRC)
clearLength = -1.0;
else
clearLength = (double)Configuration::CustomDuration;
crossFade = Configuration::CustomCrossfade;
hasERank = Configuration::CustomERank;
hasBoss = Configuration::CustomBoss;
break;
}
case Custom2:
{
if (Configuration::Custom2OnlyRC)
clearLength = -1.0;
else
clearLength = (double)Configuration::Custom2Duration;
crossFade = Configuration::Custom2Crossfade;
hasERank = Configuration::Custom2ERank;
hasBoss = Configuration::Custom2Boss;
break;
}
default:
{
if (resultType != Results::Generations)
clearLength = -1;
break;
}
}
if (hasERank && !isModern)
hasERank = false;
return { resultType, clearLength, crossFade, hasERank, hasBoss };
}
// This prepares the strings that will get used.
void PrepareStrings(resultData resultData)
{
int resultType = (int)resultData.resultType;
const char* resultsName = resultType < 0 ? "" : resultsChar[resultType];
strcpy(clearString, resultType < 0 ? "Result" : std::format("RC_{}", resultsName).c_str());
strcpy(bossString, resultType < 0 ? "Result" : std::format("RC_{}Boss", resultsName).c_str());
strcpy(result0String, resultType < 0 ? "Result1" : std::format("R0_{}", resultsName).c_str());
strcpy(result1String, resultType < 0 ? "Result1" : std::format("R1_{}", resultsName).c_str());
strcpy(result2String, resultType < 0 ? "Result2" : std::format("R2_{}", resultsName).c_str());
// S-Rank Type configuration
if (Configuration::SRankType == Always)
strcpy(result2String, resultType < 0 ? "Result1" : std::format("R1_{}", resultsName).c_str());
else if (Configuration::SRankType == Never)
strcpy(result1String, resultType < 0 ? "Result2" : std::format("R2_{}", resultsName).c_str());
}
/////////////////
// HOOKS BELOW //
/////////////////
// This fades out the previous track instead of making it stop.
int PlayMusicFadeOutPrevious(int a1, const Hedgehog::Base::CSharedString& name, float fadeInTime)
{
int result = 0;
uint32_t func = 0xD62C90;
__asm
{
push fadeInTime
push name
mov eax, a1
call func
//add esp, 8
mov result, eax
}
return result;
}
// This stops the previous track instead of making it fade out.
// FUNCTION_PTR(uint32_t, __stdcall, PlayResultMusic, 0xD62440, int a1, const Hedgehog::Base::CSharedString& name, float fadeInTime);
HOOK(void, __fastcall, _PlayActClear, 0xCFD2D0, int This)
{
original_PlayActClear(This);
uint32_t gameplayFlowStageAct = *reinterpret_cast<uint32_t*>(This + 8);
uint32_t sender = *reinterpret_cast<uint32_t*>(gameplayFlowStageAct + 96);
// There's a chance that this will have false positives. Find an alternative if this is the case.
bool isModern = Sonic::Player::CSonicClassicContext::GetInstance() == nullptr;
if (isModern)
currentResults = modernResults;
else
currentResults = classicResults;
PrepareStrings(currentResults);
bool isBoss = currentResults.hasBoss && (Helpers::CheckCurrentStage("bms") || Helpers::CheckCurrentStage("bde") || Helpers::CheckCurrentStage("bsd") || Helpers::CheckCurrentStage("bpc") || Helpers::CheckCurrentStage("bsl") || Helpers::CheckCurrentStage("bne") || Helpers::CheckCurrentStage("blb"));
PlayMusicFadeOutPrevious(sender, isBoss ? bossString : clearString, 0.0f);
}
// This function sets the time once, we don't need to nop it or anything.
HOOK(void*, __fastcall, _CalcScoreAndSetTime, 0xCFD550, int This)
{
void* result = original_CalcScoreAndSetTime(This);
*(double*)(This + 0x2C) = currentResults.clearLength - 0.25;
return result;
}
HOOK(void, __fastcall, _PlayResults, 0xCFD410, int This)
{
if (Configuration::OnlyRoundClear || currentResults.clearLength == -1)
return;
// Get AppDocument this way w/o dealing with "CHolderBase"
auto appDocument = Sonic::CApplicationDocument::GetInstance();
auto docMember = appDocument->m_pMember;
// Awful pointer arithmetic to get the data we need.
#pragma region Constants
const uint32_t gameplayFlowStageAct = *reinterpret_cast<uint32_t*>(This + 8);
const uint32_t sender = *reinterpret_cast<uint32_t*>(gameplayFlowStageAct + 96);
const uint32_t gameParameter = *reinterpret_cast<uint32_t*>(reinterpret_cast<uint32_t>(docMember) + 0x1B4);
const float deltaTime = *reinterpret_cast<float*>(*reinterpret_cast<uint32_t*>(This + 0x0C) + 0x18);
const unsigned __int8 compareFlag = (unsigned __int8)**reinterpret_cast<uint32_t**>(gameParameter + 0x80);
// Signed, because it can be negative with Score Generations's E-Rank.
const int32_t rank = *reinterpret_cast<int32_t*>(gameplayFlowStageAct + 0x174);
#pragma endregion
// Pointer data that we're going to modify.
double* pSongTime = reinterpret_cast<double*>(This + 0x2C);
uint8_t* pScoreType = reinterpret_cast<uint8_t*>(This + 0x28);
///////////////////////
// Logic starts here //
///////////////////////
if (!(compareFlag <= 0x1A && !*pScoreType))
return;
*pSongTime -= (double)deltaTime;
if (*pSongTime >= 0.0)
return;
bool isSRank = rank == 4;
bool isERank = rank < 0; // For Score Generations support.
*pScoreType = isSRank ? 1 : 2;
char resultMusic[32];
if (isERank && currentResults.hasERank)
strcpy(resultMusic, result0String);
else
strcpy(resultMusic, isSRank ? result1String : result2String);
// Simply do what Generations does for now.
PlayMusicFadeOutPrevious(sender, resultMusic, currentResults.crossFade);
}
extern "C" __declspec(dllexport) void Init()
{
// Load the configuration file.
if (!Configuration::load("CRM.ini"))
{
MessageBox(nullptr, TEXT("Failed to load CRM.ini!\nPlease configure the mod via HedgeModManager's Configure Mod option."),
TEXT("Customizable Results Music"), MB_ICONERROR);
exit(-1);
}
// Prepare the results data.
modernResults = PrepareResults(true);
classicResults = PrepareResults(false);
// Install the hooks.
WRITE_JUMP(0xCFD3C8, 0xCFD3D6);
WRITE_JUMP(0xCFD3DD, 0xCFD400);
INSTALL_HOOK(_PlayActClear);
INSTALL_HOOK(_CalcScoreAndSetTime);
INSTALL_HOOK(_PlayResults);
}