Skip to content
Permalink
Browse files
Fixed #10 crash after loading savegame (finally)
  • Loading branch information
crosire committed May 14, 2015
1 parent 82da02f commit bf12d5c800cc78ac9475f97d274b5f79e6504a82
Showing 1 changed file with 39 additions and 5 deletions.
@@ -64,19 +64,52 @@ void ManagedKeyboardMessage(int key, bool status, bool statusCtrl, bool statusSh
}

#pragma unmanaged
#pragma warning(disable: 4793)

#include <Windows.h>

void ScriptMainLoop()
bool sGameReloaded = false;
PVOID sMainFib = nullptr;
PVOID sScriptFib = nullptr;

void ScriptYield()
{
// Switch back to main script fiber used by Script Hook
SwitchToFiber(sMainFib);
}
void CALLBACK ScriptMainLoop()
{
while (ManagedInit())
{
while (ManagedTick())
sGameReloaded = false;

// Run main loop
while (!sGameReloaded && ManagedTick())
{
scriptWait(0);
ScriptYield();
}
}
}
void ScriptMainSetup()
{
sGameReloaded = true;
sMainFib = GetCurrentFiber();

if (sScriptFib == nullptr)
{
// Create our own fiber for the common language runtime once
sScriptFib = CreateFiber(0, reinterpret_cast<LPFIBER_START_ROUTINE>(&ScriptMainLoop), nullptr);
}

while (true)
{
// Switch to our fiber and wait for it to switch back
SwitchToFiber(sScriptFib);

// Yield execution and switch fiber back to Script Hook
scriptWait(0);
}
}
void ScriptKeyboardMessage(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore, BOOL isUpNow)
{
ManagedKeyboardMessage(static_cast<int>(key), isUpNow == FALSE, (GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0, (GetAsyncKeyState(VK_MENU) & 0x8000) != 0, isWithAlt != FALSE);
@@ -88,11 +121,12 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpvReserved)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hModule);
scriptRegister(hModule, &ScriptMainLoop);
scriptRegister(hModule, &ScriptMainSetup);
keyboardHandlerRegister(&ScriptKeyboardMessage);
break;
case DLL_PROCESS_DETACH:
scriptUnregister(&ScriptMainLoop);
DeleteFiber(sScriptFib);
scriptUnregister(&ScriptMainSetup);
keyboardHandlerUnregister(&ScriptKeyboardMessage);
break;
}

1 comment on commit bf12d5c

@Nacorpio
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job on fixing this issue, finally.

Please sign in to comment.