/// <summary> /// Start loading scripts and applying keys & scripts events. /// </summary> private static void Start() { LoadScripts(); GetGameMemory(_gameProcessName); CallPreScriptMethod(); Log.Print(NFSScriptLoader.INFO_TAG, $"Delaying the loader's thread for {(WaitBeforeLoad / 1000)} seconds before initializing."); // TODO FIXME: this is a very bad way to do this... Thread.Sleep(WaitBeforeLoad); Log.Print(NFSScriptLoader.INFO_TAG, "Delay is over, initializing."); // Step 1 if (NFS.IsGameRunning()) { CallInitScriptMethod(); WaitForGameLoad(); ApplyAndLoadIntPtrs(); Log.Print(NFSScriptLoader.INFO_TAG, "Game is fully loaded."); CallMainScriptMethod(); _timer = new Tick {Interval = UpdateTick}; _timer.Elapsed += Update_Elapsed; _timer.Start(); SetKeyboardHook(); } else Environment.Exit(0); }
/// <summary> /// Start loading scripts and applying keys & scripts events. /// </summary> private static void Start() { LoadScripts(); GetGameMemory(gameProcessName); CallPreScriptMethod(); Log.Print(NFSScriptLoader.INFO_TAG, string.Format("{0} {1} {2}", "Delaying the loader's thread for", (WAIT_BEFORE_LOAD / 1000), "seconds before initializing.")); Thread.Sleep(WAIT_BEFORE_LOAD); Log.Print(NFSScriptLoader.INFO_TAG, "Delay is over, initializing."); // Step 1 if (NFS.IsGameRunning()) { CallInitScriptMethod(); WaitForGameLoad(); ApplyAndLoadIntPtrs(); Log.Print(NFSScriptLoader.INFO_TAG, "Game is fully loaded."); CallMainScriptMethod(); timer = new Tick(); timer.Interval = UPDATE_TICK; timer.Elapsed += Update_Elapsed; timer.Start(); SetKeyboardHook(); } else { Environment.Exit(0); } }
/// <summary> /// Update elapsed event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void Update_Elapsed(object sender, ElapsedEventArgs e) { if (!NFS.IsGameRunning()) { Log.Print(NFSScriptLoader.INFO_TAG, "Game is not running."); Terminate(); } CallScriptsEvents(); for (var i = 0; i < _scripts.Count; i++) { if (_scripts[i].HasUpdate) _scripts[i].CallModFunction(ModScript.ModMethod.Update); } }
/// <summary> /// Get the hook callback. /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { IntPtr mainWindowHandle = gameMemory.GetMainProcess().MainWindowHandle; if ((currentNFSGame != NFSGame.World && !NFS.IsGameMinimized()) || (currentNFSGame == NFSGame.World && NFS.IsGameFocused())) { if (nCode >= 0 && wParam == (IntPtr)257) // OnKeyUp { int num = Marshal.ReadInt32(lParam); if (num == resetKey) { Restart(); } for (int i = 0; i < scripts.Count; i++) { if (scripts[i].HasOnKeyUp) { scripts[i].CallModFunction(ModScript.ModMethod.OnKeyUp, (NFSSKeys)num); } } } if (nCode >= 0 && wParam == (IntPtr)256) // OnKeyDown { int num = Marshal.ReadInt32(lParam); for (int i = 0; i < scripts.Count; i++) { if (scripts[i].HasOnKeyDown) { scripts[i].CallModFunction(ModScript.ModMethod.OnKeyDown, (NFSSKeys)num); } } } } return(NativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam)); }
/// <summary> /// Get the hook callback. /// </summary> /// <param name="nCode">classic EA param that does nothing</param> /// <param name="wParam">event param</param> /// <param name="lParam">key num param</param> /// <returns></returns> private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { var mainWindowHandle = GameMemory.GetMainProcess().MainWindowHandle; if ((_currentNFSGame == NFSGame.World || NFS.IsGameMinimized()) && (_currentNFSGame != NFSGame.World || !NFS.IsGameFocused())) return NativeMethods.CallNextHookEx(_hookId, nCode, wParam, lParam); if (nCode >= 0 && wParam == (IntPtr)257) // OnKeyUp { var num = Marshal.ReadInt32(lParam); if (num == ResetKey) Restart(); foreach (var script in _scripts) { if (script.HasOnKeyUp) { script.CallModFunction(ModScript.ModMethod.OnKeyUp, (NFSSKeys)num); } } } if (nCode < 0 || wParam != (IntPtr) 256) // OnKeyDown return NativeMethods.CallNextHookEx(_hookId, nCode, wParam, lParam); { var num = Marshal.ReadInt32(lParam); foreach (var script in _scripts) { if (script.HasOnKeyDown) { script.CallModFunction(ModScript.ModMethod.OnKeyDown, (NFSSKeys)num); } } } return NativeMethods.CallNextHookEx(_hookId, nCode, wParam, lParam); }
/// <summary> /// Wait till the game loads. /// </summary> private static void WaitForGameLoad() { Log.Print(NFSScriptLoader.INFO_TAG, "Waiting for the game to fully load. (Disabled in Most Wanted)"); switch (_currentNFSGame) { case NFSGame.Undetermined: break; case NFSGame.Underground: while (GameMemory.ReadByte((IntPtr)UGAddresses.GenericAddrs.STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; case NFSGame.Underground2: while (GameMemory.ReadByte((IntPtr)UG2Addresses.GenericAddrs.STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; case NFSGame.MW: break; case NFSGame.Carbon: while (GameMemory.ReadByte((IntPtr)CarbonAddresses.GenericAddrs.STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; case NFSGame.ProStreet: while(GameMemory.ReadByte((IntPtr)ProStreetAddresses.GenericAddrs.STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; case NFSGame.Undercover: while (GameMemory.ReadByte((IntPtr)UndercoverAddresses.GenericAddrs.STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; case NFSGame.World: while (GameMemory.ReadByte((IntPtr)GameMemory.getBaseAddress + WorldAddresses.GenericAddrs.NON_STATIC_IS_GAME_LOADED) != 0x01 && NFS.IsGameRunning()) { Thread.Sleep(100); } break; } }