1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2026-02-04 21:42:19 +08:00

work in progress for LuaScriptEngine, to support OBS websocket api

This commit is contained in:
2026-01-24 19:26:45 +08:00
parent 9787636d08
commit aa775bd05f
2 changed files with 54 additions and 7 deletions

View File

@@ -1,16 +1,23 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using BepInEx; using BepInEx;
using BepInEx.Logging; using BepInEx.Logging;
using HarmonyLib; using HarmonyLib;
using Newtonsoft.Json.Linq;
using NLua; using NLua;
using OBSWebsocketDotNet;
namespace LuaScriptEngine; namespace LuaScriptEngine;
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)] [BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class LuaScriptEngine : BaseUnityPlugin public class LuaScriptEngine : BaseUnityPlugin
{ {
private readonly OBSWebsocket _obs = new();
private readonly Dictionary<string, string> _scheduledText = [];
private class Timer(LuaFunction func, long startInterval, long repeatInterval = 0L) private class Timer(LuaFunction func, long startInterval, long repeatInterval = 0L)
{ {
public bool Check(long gameTick) public bool Check(long gameTick)
@@ -101,6 +108,49 @@ public class LuaScriptEngine : BaseUnityPlugin
{ {
Timers.Remove(timer); 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( var assemblyPath = System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!, System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!,
"scripts" "scripts"

View File

@@ -2,13 +2,14 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net472</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>LuaScriptEngine</AssemblyName> <AssemblyName>LuaScriptEngine</AssemblyName>
<BepInExPluginGuid>org.soardev.luascriptengine</BepInExPluginGuid> <BepInExPluginGuid>org.soardev.luascriptengine</BepInExPluginGuid>
<Description>DSP MOD - LuaScriptEngine</Description> <Description>DSP MOD - LuaScriptEngine</Description>
<Version>1.0.0</Version> <Version>1.0.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<RestoreAdditionalProjectSources>https://nuget.bepinex.dev/v3/index.json</RestoreAdditionalProjectSources> <RestoreAdditionalProjectSources>https://nuget.bepinex.dev/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup> </PropertyGroup>
@@ -16,8 +17,9 @@
<PackageReference Include="NLua" Version="1.*" /> <PackageReference Include="NLua" Version="1.*" />
<PackageReference Include="BepInEx.Core" Version="5.*" /> <PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" /> <PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
<!-- <PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" /> -->
<PackageReference Include="UnityEngine.Modules" Version="2022.3.53" IncludeAssets="compile" /> <PackageReference Include="UnityEngine.Modules" Version="2022.3.53" IncludeAssets="compile" />
<PackageReference Include="obs-websocket-dotnet" Version="5.*" />
<!-- <PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" /> -->
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -32,9 +34,4 @@
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'"> <ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" /> <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
</ItemGroup> </ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Configuration)' == 'Release'">
<Exec Command="del /F /Q package\$(ProjectName)-$(Version).zip
powershell Compress-Archive -Force -DestinationPath 'package/$(ProjectName)-$(Version).zip' -Path '$(TargetPath)', '$(TargetDir)/KeraLua.dll', '$(TargetDir)/lua54.dll', '$(TargetDir)/NLua.dll', package/icon.png, package/manifest.json, README.md" />
</Target>
</Project> </Project>