winstxnhdw / lc-hax

A powerful, feature-rich and highly performant internal cheat for the co-op indie horror video game, Lethal Company.
77 stars 25 forks source link

[FEATURE REQUEST]: F3 Disconnect Then Reconnect / Reconnect #342

Closed D1GQ closed 4 months ago

D1GQ commented 5 months ago

F3 Disconnect Then Reconnect / Reconnect

Added a simple option instead of just disconnecting with Shift F4, You can disconnect then reconnect with Shift F3, also if you somehow get disconnected from the game and it doesn't attempt to reconnect you can just press Shift F3 to reconnect to the last game you were in.

Code

DisconnectMod.cs:

using Hax;
using UnityEngine;
using Steamworks;
using System.Collections; // Added

public sealed class DisconnectMod : MonoBehaviour {
    bool IsShiftHeld { get; set; } = false;
    bool HasAnnouncedGameJoin { get; set; } = false;

    void OnEnable() {
        InputListener.onShiftButtonHold += this.HoldShift;
        InputListener.onF4Press += this.TryToDisconnect;
        InputListener.onF3Press += this.Rejoin; // Added
    }

    void OnDisable() {
        InputListener.onShiftButtonHold -= this.HoldShift;
        InputListener.onF4Press -= this.TryToDisconnect;
        InputListener.onF3Press -= this.Rejoin; // Added
    }

    void HoldShift(bool isHeld) => this.IsShiftHeld = isHeld;

    void TryToDisconnect() {
        if (!this.IsShiftHeld) return;

        GameNetworkManager.Instance.Disconnect();
        Setting.DisconnectedVoluntarily = true;
    }

    // Added
    void Rejoin() {
        if (!this.IsShiftHeld) return;
        GameNetworkManager.Instance.Disconnect();
        Setting.DisconnectedVoluntarily = true;
        _ = this.StartCoroutine(this.JoinLastGame());
    }

    // Added
    IEnumerator JoinLastGame() {
        yield return new WaitForSeconds(0.2f);
        this.HasAnnouncedGameJoin = false;
        if (Setting.ConnectedLobbyId is SteamId lobbyId) {
            GameNetworkManager.Instance.StartClient(lobbyId);
        }
    }
}

InputListener.cs:

using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class InputListener : MonoBehaviour {
    public static event Action? onF3Press; // Added

    (Func<bool>, Action)[] InputActions { get; } = [
        (() => Keyboard.current[Key.F3].wasPressedThisFrame, () => InputListener.onF3Press?.Invoke()), // Added
    ];
}
winstxnhdw commented 5 months ago

What’s up with using coroutines here?

D1GQ commented 5 months ago

IEnumerator JoinLastGame() { yield return new WaitForSeconds(0.2f);

Because it wasn't trying to reconnect without adding a small delay, and it's like the easiest way to add a delay without adding too much code that I know of, if you have a better way you can change it lol.

D1GQ commented 5 months ago

This seems to also make it where if they host closes the game if you keep trying you can eventually rejoin if they made a new game, I don't know if this works all the time or not.

D1GQ commented 5 months ago

@winstxnhdw Is there a way to forcibly stop trying to connect to a game and go back to the main menu to prevent a "an error occurred" message?

winstxnhdw commented 5 months ago

Just don't press the ok button.

Totoqoe commented 5 months ago

This seems to also make it where if they host closes the game if you keep trying you can eventually rejoin if they made a new game, I don't know if this works all the time or not.

Can confirm, your code even allows you to join the host if they rehost a private lobby.

joep26020 commented 5 months ago

This seems to also make it where if they host closes the game if you keep trying you can eventually rejoin if they made a new game, I don't know if this works all the time or not.

Can confirm, your code even allows you to join the host if they rehost a private lobby.

how do you get it to keep retrying?

D1GQ commented 5 months ago

how do you get it to keep retrying? Take a look at https://github.com/winstxnhdw/lc-hax/pull/343

winstxnhdw commented 4 months ago

Added in #346