forked from Shop-kins/SubnauticaTwitchInteractionMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPatcher.cs
80 lines (67 loc) · 2.99 KB
/
MainPatcher.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
using QModManager.API.ModLoading;
using System.Reflection;
using TwitchInteraction.CrowdControl;
using TwitchLib.Unity;
using TwitchLib.PubSub;
using System;
using HarmonyLib;
using System.Threading;
using TwitchInteraction.Player_Events;
namespace TwitchInteraction
{
[QModCore]
public class MainPatcher
{
public static TwitchChatClient otherclient;
public static TwitchPubSubClient otherpubsub;
public static Channel TextChannel;
public static Channel PubSubChannel;
public static System.Threading.CancellationToken cts;
public static System.Threading.CancellationToken cts2;
public static Api api;
public static Secrets secrets;
internal static Assembly myAssembly = Assembly.GetExecutingAssembly();
[QModPatch]
public static void Patch()
{
secrets = new Secrets();
// Customize event configuration
EventLookup.ConfigureEventCost(secrets.eventConfigList);
if (secrets.client == "crowdcontrol")
{
Console.WriteLine("CrowdControl client active");
StartCrowdControlServer();
} else {
Console.WriteLine("Twitch client active");
//StartTwitchChatClient(); Turned off cause the ping pong doesnt work and when it disconnects it crashes the game
StartTwitchPubSubClient();
}
Harmony.CreateAndPatchAll(myAssembly, "subnautica.mod.twitchinteraction");
}
private static async void StartTwitchChatClient()
{
cts = new System.Threading.CancellationToken();
otherclient = new TwitchChatClient();
await otherclient.ConnectAsync("oauth:" + secrets.access_token, secrets.botname, cts);
TextChannel = await otherclient.JoinChannelAsync(secrets.username, cts);
TextChannel.MessageReceived += TwitchEventManager.ChatMessageReceived;
}
private static async void StartTwitchPubSubClient()
{
cts2 = new System.Threading.CancellationToken();
otherpubsub = new TwitchPubSubClient();
await otherpubsub.ConnectAsync(secrets.api_token, secrets.nick_id, cts2);
PubSubChannel = await otherpubsub.JoinChannelAsync(secrets.username, cts);
PubSubChannel.MessageReceived += TwitchEventManager.PubSubMessageReceived;
}
private static void StartCrowdControlServer()
{
var client = new CrowdControlClient();
// Setup handlers
client.Connected += new ConnectedHandler(CrowdControlEventManager.ClientConnected);
client.MessageReceived += new ClientMessageReceivedHandler(CrowdControlEventManager.ClientMessageReceived);
client.MessageSubmitted += new ClientMessageSubmittedHandler(CrowdControlEventManager.ClientMessageSent);
client.StartClient();
}
}
}