-
Notifications
You must be signed in to change notification settings - Fork 0
/
OBSControlPlugin.cs
232 lines (184 loc) · 7.39 KB
/
OBSControlPlugin.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using OsuRTDataProvider;
using Sync.Plugins;
using Sync.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OBSWebsocketDotNet;
using Newtonsoft.Json;
using OsuRTDataProvider.Listen;
using static OsuRTDataProvider.Listen.OsuListenerManager;
using System.IO;
using Sync.Tools.ConfigurationAttribute;
namespace OBSControl
{
[SyncRequirePlugin(typeof(OsuRTDataProviderPlugin))]
public class OBSControlPlugin : Plugin, IConfigurable
{
private Logger logger { get; } = new Logger<OBSControlPlugin>();
private PluginConfigurationManager config_manager;
public Dictionary<OsuStatus,ControlItem> StatusSwitchers = new Dictionary<OsuStatus, ControlItem>();
private OBSWebsocket obs_remote;
private string default_scene_name;
private string current_scene_name;
[Path(IsDirectory =false,RequireRestart = true)]
public ConfigurationElement SwitchTableFilePath { get; set; } = "./status_switch_table.json";
public ConfigurationElement OBSRemoteHostAndPort { get; set; } = "ws://localhost:4444";
public ConfigurationElement OBSRemotePassword { get; set; } = "JXU5RUJCJXU4MkIxJXU3MjVCJXU5MDNDJXU2MjExJXU1NzgzJXU1NzNF";
public ConfigurationElement DefaultStatus { get; set; } = OsuStatus.Idle.ToString();
public OBSControlPlugin() : base(nameof(OBSControlPlugin), "MikiraSora")
{
EventBus.BindEvent<PluginEvents.LoadCompleteEvent>(OnLoaded);
config_manager = new PluginConfigurationManager(this);
config_manager.AddItem(this);
}
private void OnLoaded(PluginEvents.LoadCompleteEvent @event)
{
if (!LoadConfig())
{
logger.LogError($"Load config files failed.");
return;
}
InitORTDP(@event);
InitOBSRemote();
logger.LogInfomation("Init done.");
}
private void InitOBSRemote()
{
try
{
logger.LogInfomation($"Initialize OBS websocket ....");
obs_remote = new OBSWebsocket();
var host_port = (string)OBSRemoteHostAndPort;
host_port = host_port.StartsWith("ws://") ? host_port : ("ws://" + host_port);
obs_remote.Connected += OnOBSConnected;
obs_remote.Disconnected += OnOBSDisconnected;
logger.LogInfomation($"OBS websocket is connecting....");
obs_remote.Connect(host_port, OBSRemotePassword);
logger.LogInfomation($"Initialize OBS websocket successfully.");
BuildCommandActions();
SetupDefaultScene();
}
catch (Exception e)
{
logger.LogError($"Initialize OBS websocket failed! "+e.Message);
obs_remote = null;
return;
}
}
private void BuildCommandActions()
{
var parser = new CommandParser(obs_remote);
foreach (var item in StatusSwitchers)
{
parser.ParseCommand(item.Value);
}
}
private void SetupDefaultScene()
{
if (!StatusSwitchers.Keys.Any(x=>x.ToString()==DefaultStatus))
{
logger.LogError($"option DefaultStatus is \"{DefaultStatus}\" but your config doesn't define its commands." +
$"default commands will be the first of status you define named \"{obs_remote.ListScenes().Select(x => x.Name).FirstOrDefault()??string.Empty}\".");
default_scene_name = StatusSwitchers.Keys.FirstOrDefault().ToString();
}
else
{
default_scene_name = DefaultStatus;
}
ExecuteCommands(StatusSwitchers[Enum.TryParse(default_scene_name, out OsuStatus v) ? v : OsuStatus.Idle]);
}
private void OnOBSDisconnected(object sender, EventArgs e)
{
logger.LogInfomation($"OBS websocket is disconnected.");
}
private void OnOBSConnected(object sender, EventArgs e)
{
logger.LogInfomation($"OBS websocket is connect.");
}
private bool LoadConfig()
{
try
{
if (!File.Exists(SwitchTableFilePath))
{
logger.LogInfomation("switch table file not found , try to create default.");
StatusSwitchers = new Dictionary<OsuStatus, ControlItem>();
StatusSwitchers.Add(OsuStatus.Idle, new ControlItem());
File.WriteAllText(SwitchTableFilePath, JsonConvert.SerializeObject(StatusSwitchers));
}
StatusSwitchers = JsonConvert.DeserializeObject<Dictionary<OsuStatus,ControlItem>>(File.ReadAllText(SwitchTableFilePath));
return true;
}
catch (Exception e)
{
logger.LogError(e.Message);
return false;
}
}
private void InitORTDP(PluginEvents.LoadCompleteEvent @event)
{
var ortdp_plugin = @event.Host.EnumPluings().OfType<OsuRTDataProviderPlugin>().FirstOrDefault();
if (ortdp_plugin == null)
{
logger.LogError("Can't find ortdp plugin.");
return;
}
ortdp_plugin.ListenerManager.OnStatusChanged += (prev,now) => {
if (prev == now)
return;
OnStatusChanged(now);
};
logger.LogInfomation("Initialize ORTDP event successfully.");
}
private void OnStatusChanged(OsuStatus now)
{
logger.LogInfomation($"Now osu status is {now.ToString()}");
if (StatusSwitchers.TryGetValue(now, out var command))
ExecuteCommands(command);
else if (Enum.TryParse(default_scene_name, out now))
if (StatusSwitchers.TryGetValue(now, out command))
ExecuteCommands(command);
}
private void ExecuteCommands(ControlItem command)
{
command.CachedExecutableAction?.Invoke();
}
private void ChangeScene(string scene_name)
{
if (string.IsNullOrWhiteSpace(scene_name))
{
logger.LogError($"Param scene_name is null/empty.");
return;
}
var scenes = obs_remote.ListScenes();
if (scenes.Count==0)
{
logger.LogError($"There is no any scene avaliable in OBS.");
return;
}
if (!obs_remote.ListScenes().Select(x=>x.Name).Any(x=>x==scene_name))
{
logger.LogError($"OBS scene { scene_name} not found.");
return;
}
if (current_scene_name == scene_name)
return;
current_scene_name = scene_name;
logger.LogInfomation($"Try to switch scene to { scene_name}.");
obs_remote.SetCurrentScene(scene_name);
logger.LogInfomation($"OBS scene is switched to {scene_name}.");
}
public void onConfigurationLoad()
{
}
public void onConfigurationReload()
{
}
public void onConfigurationSave()
{
}
}
}