Repository navigation
Expand file tree
/
Copy pathPlugin.cs
More file actions
115 lines (95 loc) · 3.69 KB
/
Copy pathPlugin.cs
File metadata and controls
115 lines (95 loc) · 3.69 KB
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
using BepInEx;
using BepInEx.Configuration;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using ZeepSDK.Scripting.ZUA;
using ZeepSDK.Scripting;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ZtreamerBot
{
[BepInPlugin(pluginGUID, pluginName, pluginVersion)]
public class Plugin : BaseUnityPlugin
{
public const string pluginGUID = "com.metalted.zeepkist.Ztreamerbot";
public const string pluginName = "Ztreamerbot";
public const string pluginVersion = "1.0";
public static Plugin Instance;
public Action<string> StreamerBotUDPAction;
public ConfigEntry<int> udpPort;
private void Awake()
{
udpPort = Config.Bind("Settings", "UDP Port", 12345, "The port to listen on. Should match the port defined in the UDP Broadcast Sub-Action from Streamerbot. Restart required to apply.");
Thread udpListenerThread = new Thread(() => StartUdpListener(udpPort.Value));
udpListenerThread.IsBackground = true;
udpListenerThread.Start();
Instance = this;
ScriptingApi.RegisterEvent<OnStreamerbotUDPEvent>();
ScriptingApi.RegisterType<Dictionary<string, object>>();
Logger.LogInfo($"Plugin {pluginGUID} is loaded!");
Logger.LogInfo($"UDP listener thread started on port {udpPort}.");
}
private void StartUdpListener(int port)
{
using (UdpClient udpClient = new UdpClient(AddressFamily.InterNetwork))
{
IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
try
{
udpClient.Client.Bind(remoteEndpoint);
udpClient.EnableBroadcast = true;
while (true)
{
if (udpClient.Available > 0)
{
byte[] receivedBytes = udpClient.Receive(ref remoteEndpoint);
string receivedMessage = Encoding.UTF8.GetString(receivedBytes);
StreamerBotUDPAction?.Invoke(receivedMessage);
}
Thread.Sleep(10);
}
}
catch (SocketException ex)
{
Logger.LogError($"SocketException: {ex.Message}");
}
catch (Exception ex)
{
Logger.LogError($"Exception: {ex.Message}");
}
}
}
public Dictionary<string, object> ParseUDPPayload(string json)
{
return JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
}
}
public class OnStreamerbotUDPEvent : ILuaEvent
{
public string Name => "Streamerbot_OnUDP";
private Action<string> _streambotUDPAction;
public void Subscribe()
{
_streambotUDPAction = jsonPayload =>
{
// Parse the JSON payload
Dictionary<string, object> data = Plugin.Instance.ParseUDPPayload(jsonPayload);
// Call Lua function with the Lua table
ScriptingApi.CallFunction(Name, data);
};
// Assume you have a custom event to trigger this
Plugin.Instance.StreamerBotUDPAction += _streambotUDPAction;
}
public void Unsubscribe()
{
if (_streambotUDPAction != null)
{
Plugin.Instance.StreamerBotUDPAction -= _streambotUDPAction;
_streambotUDPAction = null;
}
}
}
}