private void EnterRandomSequence(CancellationToken cts) { // Generate a sequence to input VirtualKeyCode[] sequence = StaticHelpers.GetRandomSequence(_Random); // Press the buttons and wait 30ms then press start (in case the cutscene plays) Log.Debug("Random Sequence: [" + string.Join(", ", sequence.Select(x => x.ToString())) + "]"); // Wait until we have focus if (!_Process.HasFocus()) { Log.Message("Waiting for MHW to have focus."); } while (!_Process.HasFocus() && !cts.IsCancellationRequested) { } ; for (int i = 0; i < sequence.Length; i++) { StaticHelpers.PressKey(_InputSimulator, sequence[i], ConfigurationReader.RandomInputDelay); Thread.Sleep(ConfigurationReader.RandomInputDelay); } StaticHelpers.PressKey(_InputSimulator, VirtualKeyCode.SPACE, ConfigurationReader.RandomInputDelay); Thread.Sleep(ConfigurationReader.RandomInputDelay); StaticHelpers.PressKey(_InputSimulator, ConfigurationReader.KeyCutsceneSkip, ConfigurationReader.RandomInputDelay); }
/// <summary> /// Method used to ensure synchronization with the steamworks process and the inputting of key codes. /// <para></para> /// Kudos to <a href="https://github.com/UNOWEN-OwO">UNOWEN-OwO</a> for his work on this /// </summary> /// <param name="cts">Cancellation token used to signal when to stop.</param> private void CheckSteamworksState(CancellationToken cts) { try { // Check the current Button Press Check Value byte currentButtonPressState = _SteamworksData.InputPressStateCheck; // While we are not in the input mode while (currentButtonPressState != (byte)ButtonPressedState.Beginning && !cts.IsCancellationRequested) { // Sleep so the animation plays a bit Thread.Sleep(50); // Then press skip cutscene with a minor delay StaticHelpers.PressKey(_InputSimulator, ConfigurationReader.KeyCutsceneSkip, ConfigurationReader.RandomInputDelay); // If the cutscene is over if (currentButtonPressState == (byte)ButtonPressedState.End) { // When the steam gauge has reset, it means we can press space to start again. if (_SteamworksData.SteamGuageValue == 0) { StaticHelpers.PressKey(_InputSimulator, VirtualKeyCode.SPACE, ConfigurationReader.RandomInputDelay); } } // Reread the current button press state currentButtonPressState = _SteamworksData.InputPressStateCheck; // Print if we dont have focus if (!_Process.HasFocus()) { Log.Message("Waiting for MHW to have focus."); } // Wait for focus while (!_Process.HasFocus() && !cts.IsCancellationRequested) { } ; } } catch (Exception e) { throw new Exception("Error in checking steamworks state", e); } }
/// <summary> /// Extracts from the process the correct sequence to input, then inputs it. /// </summary> /// <param name="cts">Cancellation token used to signal when to stop.</param> private void ExtractAndEnterSequence(CancellationToken cts) { try { // Generate a sequence to input VirtualKeyCode[] sequence; // If the version is supported, use the extracted sequence sequence = _SteamworksData.ExtractSequence(); if (sequence == null) { Log.Debug("Could not find a valid sequence. Are you sure you are in the game?"); Thread.Sleep(1000); return; } //Here we need to check the probability of us winning based on the rarity of the reward float probability = ConfigurationReader.CommonSuccessRate; if (_SteamworksData.RewardRarityValue == RewardRarity.Rare) { Log.Debug("Rare reward detected."); probability = ConfigurationReader.RareSuccessRate; } // If we fail the rng check, reverse the inputs if (_Random.NextDouble() > probability) { Log.Debug("Failed rng check. shifting sequence to guarantee incorrect input."); sequence = new VirtualKeyCode[] { sequence[1], sequence[2], sequence[0] }; // Sometimes the input being shifted doesnt change the input? Thread.Sleep(50); } // For each key in the sequence for (int i = 0; i < sequence.Length; i++) { // Record our pre-registered value for input byte beforeKeyPressValue = _SteamworksData.InputPressStateCheck; byte afterKeyPressValue = beforeKeyPressValue; // While our input has not been recognized and we haven't been signalled to quit while (afterKeyPressValue == beforeKeyPressValue && !cts.IsCancellationRequested) { // Wait until we have focus if (!_Process.HasFocus()) { Log.Message("Waiting for MHW to have focus."); } while (!_Process.HasFocus() && !cts.IsCancellationRequested) { } ; // Press the key StaticHelpers.PressKey(_InputSimulator, sequence[i]); afterKeyPressValue = _SteamworksData.InputPressStateCheck; } } } catch (Exception e) { throw new Exception("Error in extracting and entering sequence.", e); } }