Skip to content

Commit

Permalink
Bug fixes + cleanup
Browse files Browse the repository at this point in the history
Fixed bug that would cause menu items to render an invalid layout when
the menuIndex exceeded a certain range. Also fixed a bug that caused the
managed version of PauseMenu::ValueChanged to fire multiple times
because of PauseMenuItem::valueChanged being (mistakenly) declared
static.
  • Loading branch information
CamxxCore committed Apr 14, 2017
1 parent 8054ce2 commit 0f970fd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
52 changes: 31 additions & 21 deletions PauseMenuHelper/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include <mutex>
#include <map>

#ifdef _DEBUG
#define DEBUGLOG(str) OutputDebugStringA(str);
#else
#define DEBUGLOG(str)
#endif

GetHashKey_t g_getHashKey;

std::map<unsigned int, std::string> g_textEntries;
Expand Down Expand Up @@ -41,13 +47,15 @@ bool bIgnoreNextMenuEvent = false;

void callFunctionOnMovie_Stub(void * sfMovie, const char * functionName, CScaleformParameter * arg, CScaleformParameter * arg1, CScaleformParameter * arg2, CScaleformParameter * arg3)
{
int menuState = (int)(*reinterpret_cast<double*>(arg)) - 1000;
int menuState = static_cast<int>(arg->m_value.dvalue) - 1000;
// we need to explicity override the menu state here so the game loads
// a valid layout for custom sub menus.
// This is necessary or the game won't set any layout at all.
if (menuState > g_origMenuCount)
int idx = menuState - 148;

if (idx > 0 && idx < g_customPrefs.size())
{
*reinterpret_cast<double*>(arg) = 1136.0f;
arg->m_value.dvalue = 1136.0;
}

return g_callInteractionResponseFn->fn(sfMovie, functionName, arg, arg1, arg2, arg3);
Expand Down Expand Up @@ -86,20 +94,28 @@ void SetPauseMenuPreference_Stub(long long settingIndex, int value, unsigned int
{
if (settingIndex >= 175)
{
DEBUGLOG("SetPauseMenuPreference_Stub(): Found a custom pref.. doing lookup.");

auto it = g_customPrefs.find((int)settingIndex);

if (it != g_customPrefs.end())
{
DEBUGLOG("SetPauseMenuPreference_Stub(): Found custom pref instance.");

it->second.m_value = value;

if (!bIgnoreNextMenuEvent)
{
if (it->second.m_callback)
{
DEBUGLOG("SetPauseMenuPreference_Stub(): Found callback..");

auto cmenu = lookupMenuForIndex(it->second.m_menuId);

if (cmenu)
{
DEBUGLOG("SetPauseMenuPreference_Stub(): Invoking callback..");

it->second.m_callback(cmenu, it->second.m_itemIndex, value);
}
}
Expand Down Expand Up @@ -129,7 +145,10 @@ int getMenuPreference(int settingIndex)

void setMenuPreference(int settingIndex, int value, bool ignoreCallback)
{
DEBUGLOG("setMenuPreference(): setting menu pref..");

bIgnoreNextMenuEvent = ignoreCallback;

SetPauseMenuPreference_Stub(settingIndex, value, 3u);
}

Expand Down Expand Up @@ -202,15 +221,6 @@ int getFreeSettingIndex()
return settingIndex;
}

void callMenuMovieFunction(const char * functionName,
CScaleformParameter * arg,
CScaleformParameter * arg1,
CScaleformParameter * arg2,
CScaleformParameter * arg3)
{
return callFunctionOnMovie_Stub((void*)0x141F4B3EC, functionName, arg, arg1, arg2, arg3);
}

CPauseMenuInstance * addMenuInstance(int menuId)
{
CPauseMenuInstance menu;
Expand Down Expand Up @@ -321,7 +331,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_getHashKey");
DEBUGLOG("initializeGame(): Failed to find g_getHashKey");
return;
}

Expand All @@ -334,7 +344,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_getGxtEntryFn");
DEBUGLOG("initializeGame(): Failed to find g_getGxtEntryFn");
return;
}

Expand All @@ -349,7 +359,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_activeMenuArray");
DEBUGLOG("initializeGame(): Failed to find g_activeMenuArray");
return;
}

Expand All @@ -364,7 +374,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find SetMenuSlot #1");
DEBUGLOG("initializeGame(): Failed to find SetMenuSlot #1");
return;
}

Expand All @@ -377,7 +387,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find SetMenuSlot #2");
DEBUGLOG("initializeGame(): Failed to find SetMenuSlot #2");
return;
}

Expand All @@ -390,7 +400,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_globalTextManager #2");
DEBUGLOG("initializeGame(): Failed to find g_globalTextManager #2");
return;
}

Expand All @@ -405,7 +415,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_setPauseMenuPreferenceFn");
DEBUGLOG("initializeGame(): Failed to find g_setPauseMenuPreferenceFn");
return;
}

Expand All @@ -419,7 +429,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find toggle prefs patch");
DEBUGLOG("initializeGame(): Failed to find toggle prefs patch");
return;
}

Expand All @@ -434,7 +444,7 @@ void initializeGame()

else
{
OutputDebugStringA("initializeGame(): Failed to find g_callInteractionResponseFn");
DEBUGLOG("initializeGame(): Failed to find g_callInteractionResponseFn");
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions PauseMenuHelper/PauseMenuItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ void PauseMenuItem::Initialize(CPauseMenuInstance * parent, CPauseMenuItem * ite

m_index = CMenuFunctions::GetItemIndex(parent, item);

m_nativeCallback = gcnew NativeMenuValueChangedEvent(&valueChanged);

if (item->settingId > 0)
{
m_nativeCallback = gcnew NativeMenuValueChangedEvent(this, &PauseMenuItem::valueChanged);

registerMenuPref(item->settingId, parent->menuId, m_index, CMenuPreferenceCallback(
Marshal::GetFunctionPointerForDelegate(m_nativeCallback).ToPointer()));
}
Expand Down
4 changes: 2 additions & 2 deletions PauseMenuHelper/PauseMenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public ref class PauseMenuItem
{
internal:
NativeMenuValueChangedEvent ^ m_nativeCallback;
static MenuValueChangedEvent ^ m_managedCallback;
MenuValueChangedEvent ^ m_managedCallback;
void Initialize(CPauseMenuInstance * parent, CPauseMenuItem * item);
~PauseMenuItem();
!PauseMenuItem();
Expand Down Expand Up @@ -76,7 +76,7 @@ public ref class PauseMenuItem
}
}

static void valueChanged(CPauseMenuInstance * parent, int itemIndex, int newValue) {
void valueChanged(CPauseMenuInstance * parent, int itemIndex, int newValue) {
if (m_managedCallback != nullptr)
{
m_managedCallback(parent->menuId, itemIndex, newValue);
Expand Down
2 changes: 1 addition & 1 deletion PauseMenuHelper/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ union CScaleformParameterValue
unsigned int unsignedInt;
unsigned int hash;
const char * string;
float value;
float fvalue;
double dvalue;
};

Expand Down

0 comments on commit 0f970fd

Please sign in to comment.