From aa775bd05fcfc9c739406e920d43dd346356c30f Mon Sep 17 00:00:00 2001 From: Soar Qin Date: Sat, 24 Jan 2026 19:26:45 +0800 Subject: [PATCH] work in progress for LuaScriptEngine, to support OBS websocket api --- LuaScriptEngine/LuaScriptEngine.cs | 50 ++++++++++++++++++++++++++ LuaScriptEngine/LuaScriptEngine.csproj | 11 +++--- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/LuaScriptEngine/LuaScriptEngine.cs b/LuaScriptEngine/LuaScriptEngine.cs index 545c3fa..50f2293 100644 --- a/LuaScriptEngine/LuaScriptEngine.cs +++ b/LuaScriptEngine/LuaScriptEngine.cs @@ -1,16 +1,23 @@ using System; using System.Collections.Generic; using System.Text; + using BepInEx; using BepInEx.Logging; using HarmonyLib; + +using Newtonsoft.Json.Linq; using NLua; +using OBSWebsocketDotNet; namespace LuaScriptEngine; [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] public class LuaScriptEngine : BaseUnityPlugin { + private readonly OBSWebsocket _obs = new(); + private readonly Dictionary _scheduledText = []; + private class Timer(LuaFunction func, long startInterval, long repeatInterval = 0L) { public bool Check(long gameTick) @@ -101,6 +108,49 @@ public class LuaScriptEngine : BaseUnityPlugin { Timers.Remove(timer); }; + LuaState["obs_connect"] = void (string server, string password) => + { + _obs.Connected += (sender, e) => + { + Logger.LogDebug("Connected to OBS"); + foreach (var (sourceName, text) in _scheduledText) + { + _obs.SetInputSettings(sourceName, + new JObject { + {"text", text} + }); + } + _scheduledText.Clear(); + }; + _obs.Disconnected += (sender, e) => + { + Logger.LogDebug("Disconnected from OBS"); + _obs.ConnectAsync(server, password); + }; + _obs.ConnectAsync(server, password); + }; + LuaState["obs_set_source_text"] = void (string sourceName, string text) => + { + if (_obs.IsConnected) + { + try + { + _obs.SetInputSettings(sourceName, new JObject { + {"text", text} + }); + } + catch (Exception e) + { + Logger.LogError($"Error setting source text: {e}"); + _obs.Disconnect(); + _scheduledText[sourceName] = text; + } + } + else + { + _scheduledText[sourceName] = text; + } + }; var assemblyPath = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!, "scripts" diff --git a/LuaScriptEngine/LuaScriptEngine.csproj b/LuaScriptEngine/LuaScriptEngine.csproj index b495cf7..59c4737 100644 --- a/LuaScriptEngine/LuaScriptEngine.csproj +++ b/LuaScriptEngine/LuaScriptEngine.csproj @@ -2,13 +2,14 @@ - net472 + netstandard2.1 LuaScriptEngine org.soardev.luascriptengine DSP MOD - LuaScriptEngine 1.0.0 true latest + PackageReference https://nuget.bepinex.dev/v3/index.json @@ -16,8 +17,9 @@ - + + @@ -32,9 +34,4 @@ - - - -