Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripts dont work after loading save #50

Open
Freeeaky opened this issue May 22, 2015 · 8 comments
Open

Scripts dont work after loading save #50

Freeeaky opened this issue May 22, 2015 · 8 comments

Comments

@Freeeaky
Copy link
Owner

Reported by Baaa, IRC

@zh0ul
Copy link

zh0ul commented Dec 31, 2016

I've been asked about this innumerable times. Can ye provide some insight into this issue, or where to start digging?

It feels like GTALua simply loses its tick/hook, which likely means the game mem has shifted and somehow GTALua did not pick up on this fact and re-register with scripthook?

@Freeeaky
Copy link
Owner Author

Freeeaky commented Jan 1, 2017

What exactly happens when you load a save?

@zh0ul
Copy link

zh0ul commented Jan 2, 2017

Repro steps:

  • When using GTALua, vanilla or my own modified version...
  • When using any mod (I have tried many configurations, but the cleanest was enabling 1 of the example mods and only that mod).
  • Load GTA 5
  • ESC -> Game -> Replay Mission or Replay Strangers and Freaks
  • Pick/Load any mission.
  • A 'Save Game' is created before loading mission (by GTA's own engine)
  • At this point, we still have hook and menu is working fine.
  • You can fail the mission and 'Retry', menu still works.
  • Complete/Exit the mission (either successfully, or failed)
  • At this point, the Save Game that was ^^ saved by GTA is reloaded. "Loading Story Mode"
  • The menu itself stops working. (hotkeys, anything that was drawn on screen, etc)
  • However, the console itself is working.
  • When using console.cmd_show_all_addons() , main_thread is reported to be Active: true and Running: true
  • However, The tick functions are not running (i.e. my own mod also shows as Active/Running, but no tick calls are being made).

@zh0ul
Copy link

zh0ul commented Jan 3, 2017

  • I note that the GTALua::UpdateLoop() (from Main.cpp) contains the following:
void GTALua::UpdateLoop()
{
  while (m_bActive)
  {
    Update();
  }
}
  • However, comparing this to scripthookvdotnet, it appears what is lacking is 'What to do after m_bActive is no longer active'.
void ScriptMain()
{
        // Set up fibers
	sGameReloaded = true;
	sMainFib = GetCurrentFiber();

	if (sScriptFib == nullptr)
	{
		const auto callback = [](LPVOID)
		{
			while (ManagedInit())
			{
				sGameReloaded = false;

				// Run main loop
				while (!sGameReloaded && ManagedTick())
				{
					// Switch back to main script fiber used by Script Hook
					SwitchToFiber(sMainFib);
				}
			}
		};

		// Create our own fiber for the common language runtime once
		sScriptFib = CreateFiber(0, callback, nullptr);
	}

	while (true)
	{
		// Yield execution
		scriptWait(0);

		// Switch to our own fiber and wait for it to switch back
		SwitchToFiber(sScriptFib);
	}
}
  • Lastly, I'm not a real C++ coder, I just play one on T.V.

@zh0ul
Copy link

zh0ul commented Jan 3, 2017

  • The last major diff between scripthookdotnet and GTALua appears to be in scriptRegister/Unregister and GTALua's lack of Unregister framework.
  • Below is an excerpt from scripthookdotnet's DllMain.
BOOL WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpvReserved)
{
	switch (fdwReason)
	{
		case DLL_PROCESS_ATTACH:
			DisableThreadLibraryCalls(hModule);
			scriptRegister(hModule, &ScriptMain);
			keyboardHandlerRegister(&ScriptKeyboardMessage);
			break;
		case DLL_PROCESS_DETACH:
			DeleteFiber(sScriptFib);
			scriptUnregister(hModule);
			keyboardHandlerUnregister(&ScriptKeyboardMessage);
			break;
	}
  • And for GTALua, there appears to be scriptRegister for GTALua.asi, (found in LB_ScriptHookV.cpp) , but no steps defined for unregister/re-register

@Freeeaky
Copy link
Owner Author

Freeeaky commented Jan 3, 2017

I agree, it's probably related to not using scriptUnregister properly. Try finding out when m_bActive is false.

void GTALua::UpdateLoop() { while (m_bActive) { Update(); } MessageBox(0, "m_bActive is false", 0, 0); }

for example

@zh0ul
Copy link

zh0ul commented Jan 3, 2017

  • Since the console still works after such events, I went ahead and added a printf after the while loop.
void GTALua::UpdateLoop()
{
  while(m_bActive)
  {
    Update();
  }
  printf("m_bActive == false\n");
}
  • Interestingly; after loading save, no message is displayed.
  • Actually, after finally realizing that UpdateLoop() simply calls Update()..
  • And all Update() does, is ProcessConsoleInput();
  • And we know that the console still works after loading save.
  • Then its safe to assume that m_bActive and this update loop are never losing their thread.
  • In fact, m_bActive appears to only be set = false in the destructor class ~GTALua
  • Thus, this bit of code appears to work as expected.
  • At this point I'm looking into LB_MainScriptThread.cpp , and DLLMain.cpp

@Freeeaky
Copy link
Owner Author

Freeeaky commented Jan 3, 2017

That's interesting, i'll try looking into it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants