Skip to content

Commit

Permalink
added TalkState/SleeperWake
Browse files Browse the repository at this point in the history
Now, if enabled in the config, sleepers can wake up from players talking.

This is a host-only feature meaning the host has to be running the mod for it to work, clients are optional.
  • Loading branch information
WWYDF committed Oct 9, 2024
1 parent 40553e6 commit 489a4cc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Dissonance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HarmonyLib;
using GTFO.API;
using SNetwork;
using ProximityChat.TalkState;

namespace ProximityChat.Dissonance
{
Expand All @@ -16,6 +17,7 @@ public class DissonanceUtils
private SlotManager slotManager;
private SteamLink steamLink;
private PlayerHandler.PlayerManager playerManager;
private SleeperWake sleeperWake;
public bool isInLevel = false; // Initialize and store isInLevel check.

// Giver Instance Loader.
Expand All @@ -39,6 +41,7 @@ public void Init() // Called once on plugin load.
slotManager = SlotManager.Instance;
steamLink = SteamLink.Instance;
playerManager = PlayerHandler.PlayerManager.Instance;
sleeperWake = SleeperWake.Instance;

LevelAPI.OnEnterLevel += onEnterLevel;
LevelAPI.OnLevelCleanup += OnExitLevel;
Expand Down Expand Up @@ -92,6 +95,9 @@ public void SetupIncomingAudio(string targetDUID, int pSlot)
MainPlugin.SendLog.LogInfo("[SetupAudio] Successfully enabled audio spatialization.");

}

if (sleeperWake.sleepWakeEnabled)
sleeperWake.enableSleeperWake(cAgent, voicePlaybackComponent);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using ProximityChat.Dissonance;
using UnityEngine;
using BepInEx.Configuration;
using ProximityChat.TalkState;
using GTFO.API;
using GameData;

namespace ProximityChat
{
Expand All @@ -27,6 +30,7 @@ public class MainPlugin : BasePlugin
private PlayerHandler.SlotManager? slotManagerInstance;
private SteamComms.SteamLink? steamLink;
private PlayerHandler.PlayerManager? playerManager;
private TalkState.SleeperWake? sleeperWake;

// Giver Instance Loader
private static MainPlugin _instance;
Expand Down Expand Up @@ -61,10 +65,14 @@ public override void Load() // Runs once when plugin is loaded.
slotManagerInstance = SlotManager.Instance;
steamLink = SteamLink.Instance;
playerManager = PlayerHandler.PlayerManager.Instance;
sleeperWake = SleeperWake.Instance;

// Assign BepInEx logger to static field
SendLog = Log;

// Subscribe to events
LevelAPI.OnBuildStart += sleeperWake.Init;

// Initialize other classes.
dissonanceInstance.Init();
steamLink.Init();
Expand Down
14 changes: 12 additions & 2 deletions src/SteamComms.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Dissonance.Integrations.SteamworksP2P;
using Dissonance;
using Dissonance.Integrations.SteamworksP2P;
using Dissonance.Networking;
using Player;
using ProximityChat.TalkState;
using SNetwork;
using Steamworks;

Expand All @@ -12,6 +15,7 @@ public class SteamLink
private Dissonance.DissonanceUtils dissonanceInstance;
private PlayerHandler.SlotManager slotManager;
private PlayerHandler.PlayerManager playerManager;
private SleeperWake sleeperWake;

// Giver Instance Loader
private static SteamLink _instance;
Expand All @@ -34,6 +38,7 @@ public void Init()
dissonanceInstance = Dissonance.DissonanceUtils.Instance;
slotManager = PlayerHandler.SlotManager.Instance;
playerManager = PlayerHandler.PlayerManager.Instance;
sleeperWake = SleeperWake.Instance;
}

public void HostSteamP2P()
Expand Down Expand Up @@ -76,7 +81,12 @@ public void HostSteamP2P()

}
MainPlugin.SendLog.LogInfo("[SteamP2PHost] Player is Host! Using different linking method..");
dissonanceInstance.LinkPositionUpdater(slotManager.GetPlayerAgentBySlot(SNet.LocalPlayer.CharacterIndex), DSObj.gameObject);

PlayerAgent localAgent = slotManager.GetPlayerAgentBySlot(SNet.LocalPlayer.CharacterIndex);
dissonanceInstance.LinkPositionUpdater(localAgent, DSObj.gameObject);

if (sleeperWake.sleepWakeEnabled)
sleeperWake.enableSleeperWake(localAgent, null, DSObj.gameObject.GetComponent<DissonanceComms>().Players[0]);
}

public void ClientSteamP2P() // this is the new one that needs different values to work properly.
Expand Down
91 changes: 91 additions & 0 deletions src/TalkState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Agents;
using Dissonance;
using Dissonance.Audio.Playback;
using Player;
using PlayFab.DataModels;
using ProximityChat.Dissonance;
using ProximityChat.PlayerHandler;
using ProximityChat.SteamComms;
using SNetwork;
using UnityEngine;

namespace ProximityChat.TalkState
{
public class SleeperWake
{
// Setup instance linking.
private MainPlugin rootInstance;
private DissonanceUtils dissonanceInstance;
private SteamLink steamLink;
private SlotManager slotManager;

// Giver Instance Loader.
private static SleeperWake _instance;
public static SleeperWake Instance
{
get
{
if (_instance == null)
_instance = new SleeperWake();
return _instance;
}
}

public bool sleepWakeEnabled = false;

// Proceed.
public void Init()
{
if (!ProximityConfig.enableSleeperWake.Value)
return;

MainPlugin.SendLog.LogInfo("[SleeperWake] Config is true, checking host...");
if (!SNet.LocalPlayer.IsMaster)
{
MainPlugin.SendLog.LogError("[SleeperWake] SleeperWake is enabled, but user is not the host. Disabling SleeperWake...");
return;
}
MainPlugin.SendLog.LogInfo("[SleeperWake] User is indeed the host, enabling SleeperWake functionality!");

sleepWakeEnabled = true; // Tells LinkPositionUpdater to pass Player Variables to us.

}

public async void enableSleeperWake(PlayerAgent sourceAgent, VoicePlayback? sourceObject = null, VoicePlayerState? selfObject = null)
{
// Save local copies to prevent nulling mfw
var pAgent = sourceAgent;
VoicePlayback dissonanceObject = sourceObject;

MainPlugin.SendLog.LogInfo($"[SleeperWake] Starting listener for {pAgent.PlayerName}!");

while (pAgent != null)
{
if (GameStateManager.CurrentStateName.ToString() != "InLevel")
{
MainPlugin.SendLog.LogInfo($"[SleeperWake] Supposedly exited level, shutting down!");
break;
}

await Task.Delay(12);

if (sourceObject == null && selfObject != null) // If local player
{
while (selfObject.IsSpeaking)
{
pAgent.Noise = Agent.NoiseType.Walk;
await Task.Delay(12);
}
continue;
}

while (dissonanceObject.IsSpeaking)
{
pAgent.Noise = Agent.NoiseType.Walk;
await Task.Delay(12);
}
}
MainPlugin.SendLog.LogError("[SleeperWake] Connection to player unexpectedly severed!");
}
}
}

0 comments on commit 489a4cc

Please sign in to comment.