-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.cs
157 lines (127 loc) · 4.77 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using BepInEx;
using BepInEx.Configuration;
using System;
using System.Threading;
using BepInEx.Logging;
using Steamworks;
using UnityEngine;
#if BEP6
using BepInEx.Unity.Mono;
using BepInEx.Unity.Mono.Configuration;
#endif
namespace NuclearVOIP
{
[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInProcess("NuclearOption.exe")]
public class Plugin : BaseUnityPlugin
{
private static Plugin? _Instance;
internal static Plugin Instance
{
get
{
if (_Instance == null)
throw new InvalidOperationException("Plugin not initialized");
return _Instance;
}
}
internal new static ManualLogSource Logger
{
get { return Instance._Logger; }
}
private ManualLogSource _Logger
{
get { return base.Logger; }
}
private OpusMultiStreamer? streamer;
internal readonly ConfigEntry<KeyboardShortcut> configTalkKey;
internal readonly ConfigEntry<KeyboardShortcut> configAllTalkKey;
internal readonly ConfigEntry<int> configVOIPPort;
internal readonly ConfigEntry<float> configInputGain;
internal readonly ConfigEntry<float> configOutputGain;
internal readonly bool NET_DEBUG = false;
Plugin()
{
if (Interlocked.CompareExchange(ref _Instance, this, null) != null) // I like being thread safe okay?
throw new InvalidOperationException($"Reinitialization of Plugin {MyPluginInfo.PLUGIN_GUID}");
configTalkKey = Config.Bind(
"General",
"Talk Key",
new KeyboardShortcut(KeyCode.V),
"Push to talk key"
);
configAllTalkKey = Config.Bind(
"General",
"All Talk Key",
new KeyboardShortcut(KeyCode.C),
"Push to talk to all key"
);
configVOIPPort = Config.Bind(
"General",
"VOIP Port",
5000,
"The port to discover and transmit voice chat on"
);
configInputGain = Config.Bind(
"General",
"Microphone Gain",
1.0f,
"A (linear) multiplier applied to microphone readings"
);
configOutputGain = Config.Bind(
"General",
"Output Gain",
1.0f,
"A (in dB) multiplier applied to incoming voice"
);
LoadingManager.GameLoaded += LateLoad;
}
~Plugin()
{
_Instance = null;
}
private void Awake()
{
Logger.LogInfo($"Loaded {MyPluginInfo.PLUGIN_GUID}");
}
private void LateLoad()
{
Logger.LogInfo($"LateLoading {MyPluginInfo.PLUGIN_GUID}");
if (!SteamManager.Initialized)
{
Logger.LogWarning("Disabling VOIP: steam is not initalized");
return;
}
LoadingManager.MissionUnloaded += MissionUnload;
LoadingManager.MissionLoaded += LoadingFinished;
SteamNetworking.AllowP2PPacketRelay(true);
if (SteamNetworkingSockets.GetAuthenticationStatus(out _) == ESteamNetworkingAvailability.k_ESteamNetworkingAvailability_NeverTried)
SteamNetworkingSockets.InitAuthentication();
if (SteamNetworkingUtils.GetRelayNetworkStatus(out _) == ESteamNetworkingAvailability.k_ESteamNetworkingAvailability_NeverTried)
SteamNetworkingUtils.InitRelayNetworkAccess();
Logger.LogInfo($"LateLoaded {MyPluginInfo.PLUGIN_GUID}");
}
private void OnDestroy()
{
Logger.LogInfo($"Unloaded {MyPluginInfo.PLUGIN_GUID}");
}
private void MissionUnload()
{
streamer = null;
}
private void LoadingFinished()
{
if (NET_DEBUG || GameManager.gameState != GameManager.GameState.Singleplayer)
{
GameObject host = GameManager.LocalPlayer.gameObject;
INetworkSystem networkSystem;
if (NET_DEBUG && GameManager.gameState == GameManager.GameState.Singleplayer)
networkSystem = host.AddComponent<DebugNetworkSystem>();
else
networkSystem = host.AddComponent<NetworkSystem>();
CommSystem comms = host.AddComponent<CommSystem>();
streamer = new(comms, networkSystem);
}
}
}
}