com js过滤器 添加 BitConvert 支持

This commit is contained in:
梅浩东
2024-04-26 10:19:14 +08:00
parent c19353ddec
commit 11afaae1f7
5 changed files with 144 additions and 41 deletions
+31 -5
View File
@@ -7,9 +7,13 @@ using System.IO.Ports;
using Jint; using Jint;
using Jint.Runtime; using Jint.Runtime;
using System.Windows.Forms; using System.Windows.Forms;
using Jint.Native;
namespace BarrageGrab namespace BarrageGrab
{ {
/// <summary>
/// 串口服务
/// </summary>
public class ComPortServer : IDisposable public class ComPortServer : IDisposable
{ {
WsBarrageServer server; WsBarrageServer server;
@@ -22,7 +26,15 @@ namespace BarrageGrab
{ {
jsEngine = JsEngine.CreateNewEngine(); jsEngine = JsEngine.CreateNewEngine();
var jsFile = JsEngine.GetJsFile("comPortFilter.js"); var jsFile = JsEngine.GetJsFile("comPortFilter.js");
jsEngine.Execute(jsFile); try
{
jsEngine.Execute(jsFile);
}
catch (Exception ex)
{
Logger.LogError(ex, $"comPortFilter.js 执行错误: {ex.Message}");
throw;
}
} }
} }
@@ -50,6 +62,8 @@ namespace BarrageGrab
{ {
if (!sendPort.IsOpen) return; if (!sendPort.IsOpen) return;
if (e == null) return; if (e == null) return;
if (e.Message == null) return;
if (!AppSetting.Current.UseComPortFilter) if (!AppSetting.Current.UseComPortFilter)
{ {
var json = e.ToJson() + "\r\n"; var json = e.ToJson() + "\r\n";
@@ -57,7 +71,20 @@ namespace BarrageGrab
Send(jbuff); Send(jbuff);
return; return;
} }
var result = jsEngine.Invoke("onPackData", e.MsgType.GetHashCode(), e.Message);
var roomInfo = AppRuntime.RoomCaches.GetCachedWebRoomInfo(e.Message.RoomId.ToString());
JsValue result = null;
try
{
result = jsEngine.Invoke("onPackData", e.MsgType.GetHashCode(), e.Message, roomInfo);
}
catch (Exception ex)
{
Logger.LogError(ex, $"串口过滤器 onPackData 执行错误:{ex.Message}");
return;
}
if (result == null) return;
var nocanTypes = new Types[] var nocanTypes = new Types[]
{ {
Types.Null, Types.Null,
@@ -115,15 +142,14 @@ namespace BarrageGrab
sendPort.Open(); sendPort.Open();
} }
catch (Exception ex) catch (Exception ex)
{ {
sendPort.Close(); sendPort.Close();
sendPort.Dispose(); sendPort.Dispose();
sendPort = null; sendPort = null;
throw ex; throw ex;
} }
} }
public void Dispose() public void Dispose()
{ {
if (sendPort == null) return; if (sendPort == null) return;
+71 -5
View File
@@ -12,6 +12,10 @@ namespace BarrageGrab
{ {
public static class JsEngine public static class JsEngine
{ {
/// <summary>
/// 创建一个Js引擎
/// </summary>
/// <returns></returns>
public static Engine CreateNewEngine() public static Engine CreateNewEngine()
{ {
var jsonParse = new Func<object, string>((obj) => var jsonParse = new Func<object, string>((obj) =>
@@ -28,8 +32,15 @@ namespace BarrageGrab
}); });
var engine = new Engine(); var engine = new Engine();
engine.Execute(@"
byteToUint8Array = function(arr){
return new Uint8Array(arr);
}");
engine.SetValue("console", new JsConsole()); engine.SetValue("console", new JsConsole());
engine.SetValue("encoder", new JsEncoder(engine)); engine.SetValue("encoder", new JsEncoder(engine));
engine.SetValue("bitConvert", new JsBitConvert(engine));
return engine; return engine;
} }
@@ -72,11 +83,7 @@ namespace BarrageGrab
Engine eng; Engine eng;
public JsEncoder(Engine eng) public JsEncoder(Engine eng)
{ {
this.eng = eng; this.eng = eng;
eng.Execute(@"
byteToUint8Array = function(arr){
return new Uint8Array(arr);
}");
} }
private JsValue ByteConvert(byte[] buff) private JsValue ByteConvert(byte[] buff)
@@ -144,5 +151,64 @@ namespace BarrageGrab
return Encoding.ASCII.GetString(bytes); return Encoding.ASCII.GetString(bytes);
} }
} }
class JsBitConvert
{
Engine eng;
public JsBitConvert(Engine eng)
{
this.eng = eng;
}
private JsValue ByteConvert(byte[] buff)
{
return eng.Invoke("byteToUint8Array", buff);
}
public JsValue toNumber(byte[] buff,int startIndex = 0)
{
return BitConverter.ToDouble(buff, startIndex);
}
public bool toBoolean(byte[] buff, int startIndex = 0)
{
return BitConverter.ToBoolean(buff, startIndex);
}
public string toString(byte[] buff, int startIndex = 0)
{
return BitConverter.ToString(buff, startIndex);
}
public string toString(byte[] buff, int startIndex, int length)
{
return BitConverter.ToString(buff, startIndex, length);
}
public JsValue getBytes(JsValue value)
{
byte[] buff = new byte[0];
switch (value.Type)
{
case Jint.Runtime.Types.Empty:
case Jint.Runtime.Types.Undefined:
case Jint.Runtime.Types.Null:
break;
case Jint.Runtime.Types.Boolean:
buff = BitConverter.GetBytes(value.AsBoolean());
break;
case Jint.Runtime.Types.Number:
buff = BitConverter.GetBytes(value.AsNumber());
break;
case Jint.Runtime.Types.BigInt:
buff = BitConverter.GetBytes((long)value.AsNumber());
break;
default:
throw new Exception($"不支持该类型转换:" + value.Type);
}
return ByteConvert(buff);
}
}
} }
} }
+5 -4
View File
@@ -134,11 +134,12 @@ namespace BarrageGrab
return !createdNew; return !createdNew;
} }
} }
#if DEBUG
private static void JintTest() private static void JintTest()
{ {
var jsEng = JsEngine.CreateNewEngine(); var jsEng = JsEngine.CreateNewEngine();
var jsFile = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Scripts", "engine", "comPortFilter.js"), Encoding.UTF8); var jsFile = File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Scripts", "engine", "comPortFilter.js"), Encoding.UTF8);
//var jsFile = @"D:\develop\Project\dy-barrage-grab\BarrageGrab\Scripts\engine\comPortFilter.js";
jsEng.Execute(jsFile); jsEng.Execute(jsFile);
var result = jsEng.Invoke("onPackData", 1, new var result = jsEng.Invoke("onPackData", 1, new
{ {
@@ -149,9 +150,8 @@ namespace BarrageGrab
{ {
{ "key1","value1" } { "key1","value1" }
} }
}); },null);
Console.WriteLine($"返回类型:{ result.Type.ToString()},值:{result.ToString()}");
Console.WriteLine("返回类型:" + result.Type.ToString());
if (result.Type == Types.String) if (result.Type == Types.String)
{ {
Console.WriteLine("返回String: " + result.ToString()); Console.WriteLine("返回String: " + result.ToString());
@@ -169,5 +169,6 @@ namespace BarrageGrab
Console.WriteLine("返回Symbol: " + result.ToString()); Console.WriteLine("返回Symbol: " + result.ToString());
} }
} }
#endif
} }
} }
+28 -18
View File
@@ -1,28 +1,38 @@
/** /*
* 注解:串口数据过滤脚本,可用于自定义配置串口的发送报文 * 注解:串口数据过滤脚本,可用于自定义配置串口的发送报文
* Tip: 该脚本引擎为Jint,支持的语法请参考官方文档:https://github.com/sebastienros/jint * Tip: 该脚本引擎为Jint,支持的语法请参考官方文档:https://github.com/sebastienros/jint
* 工具环境: * 为了 工具环境:
* 1.支持 console.log, console.error, console.warn 这些日志将被输出到控制台, 其中error日志将被记录到日志文件 * 1.控制台: console: 控制台输出
* 2.编码工具 encoder.utf8ToBytes(str),encoder.utf8ToString 字符串 和 Uint8Array 互转 * console.log, console.error, console.warn 这些日志将被输出到控制台, 其中error日志将被记录到日志文件
* encoder.utf32ToBytes(str),encoder.utf32ToString *
* encoder.asciiToBytes(str),encoder.asciiToString * 2.编码器: encoder: 字符串 和 Uint8Array 互转
* encoder.utf8ToBytes(str):Unit8Array ,encoder.utf8ToString(buff):String
* encoder.utf32ToBytes(str):Unit8Array ,encoder.utf32ToString(buff):String
* encoder.asciiToBytes(str):Unit8Array ,encoder.asciiToString(buff):String
*
* 3.Bit转换: bitConvert: 支持 Number,Boolean 与 Uint8Array 互转
* bitConvert.toNumber(bytes, offset):Number
* bitConvert.toBoolean(bytes, offset):Boolean
* bitConvert.toString(bytes, offset, length):String
* bitConvert.toString(bytes, offset):String
* bitConvert.toString(bytes):String
* bitConvert.getBytes(value):Unit8Array 支持传入 Number,Boolean String请使用 encoder.utf8ToBytes
*/ */
/** /**
* 这个函数由程序JS引擎调用,不要删除 * 这个函数由程序JS引擎调用,不要删除
* @param {number} msgType 消息类型,如礼物,弹幕消息等,和程序内定义一致 * @param {number} msgType 消息类型,如礼物,弹幕消息等,和程序内定义一致 见 Modles\JsonEntity\BarrageMessages.cs PackMsgType 枚举
* @param {Object} msg 被WsBarrageServer简化后的消息对象,具体可参考Json实体文件 * @param {Object} msg 被WsBarrageServer简化后的消息对象,具体可参考Json实体文件 见 Modles\JsonEntity\BarrageMessages.cs
* @returns {Uint8Array|string} 返回一个Uint8Array类型的数据,或者返回一个字符串,如果返回 null 则不发送数据。返回的数据将会被发送到串口 * @param {Object} roomInfo 当前消息对应的房间信息 见 Modles\RoomInfo.cs 结构
* @returns {Uint8Array|string} 返回一个Uint8Array类型的数据,或者返回一个字符串(将自动进行utf8编码),如果返回 null 则不发送数据。返回的数据将会被发送到串口
*/ */
function onPackData(msgType, msg) { function onPackData(msgType, msg , roomInfo) {
let excludeMsgType = [3, 6, 0]; // 过滤掉进入直播间和统计信息 let json = JSON.stringify(msg);
if (excludeMsgType.includes(msgType)) return; return json+"\n";
var result = msg.Content + "\r\n"; // let excludeMsgType = [3, 6, 0]; // 过滤掉进入直播间和统计信息
return result; // if (excludeMsgType.includes(msgType)) return;
// var buff = encoder.utf8ToBytes(result); // var result = msg.Content + "\r\n";
// var newjs = encoder.utf8ToString(buff); // return result;
// console.log(newjs);
// return newjs;
} }
+9 -9
View File
@@ -284,7 +284,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = msg.Content, Content = msg.Content,
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
Type = msg.Type, Type = msg.Type,
User = GetUser(msg.User) User = GetUser(msg.User)
@@ -310,7 +310,7 @@ namespace BarrageGrab
TotalUserCount = msg.totalUser, TotalUserCount = msg.totalUser,
TotalUserCountStr = msg.totalPvForAnchor, TotalUserCountStr = msg.totalPvForAnchor,
OnlineUserCountStr = msg.onlineUserForAnchor, OnlineUserCountStr = msg.onlineUserForAnchor,
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
Content = $"当前直播间人数 {msg.onlineUserForAnchor},累计直播间人数 {msg.totalPvForAnchor}", Content = $"当前直播间人数 {msg.onlineUserForAnchor},累计直播间人数 {msg.totalPvForAnchor}",
User = null User = null
@@ -391,7 +391,7 @@ namespace BarrageGrab
var enty = new GiftMsg() var enty = new GiftMsg()
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
Content = $"{msg.User.Nickname} 送出 {msg.Gift.Name}{(msg.Gift.Combo ? "(可连击)" : "")} x {msg.repeatCount}个,增量{count}个", Content = $"{msg.User.Nickname} 送出 {msg.Gift.Name}{(msg.Gift.Combo ? "(可连击)" : "")} x {msg.repeatCount}个,增量{count}个",
DiamondCount = msg.Gift.diamondCount, DiamondCount = msg.Gift.diamondCount,
@@ -429,7 +429,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = $"{msg.User.Nickname} 关注了主播", Content = $"{msg.User.Nickname} 关注了主播",
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
User = GetUser(msg.User) User = GetUser(msg.User)
}; };
@@ -459,7 +459,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = $"{msg.User.Nickname} 分享了直播间到{type}", Content = $"{msg.User.Nickname} 分享了直播间到{type}",
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
ShareType = type, ShareType = type,
User = GetUser(msg.User) User = GetUser(msg.User)
@@ -486,7 +486,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = $"{msg.User.Nickname} 来了 直播间人数:{msg.memberCount}", Content = $"{msg.User.Nickname} 来了 直播间人数:{msg.memberCount}",
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
CurrentCount = msg.memberCount, CurrentCount = msg.memberCount,
User = GetUser(msg.User) User = GetUser(msg.User)
@@ -512,7 +512,7 @@ namespace BarrageGrab
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Count = msg.Count, Count = msg.Count,
Content = $"{msg.User.Nickname} 为主播点了{msg.Count}个赞,总点赞{msg.Total}", Content = $"{msg.User.Nickname} 为主播点了{msg.Count}个赞,总点赞{msg.Total}",
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
Total = msg.Total, Total = msg.Total,
User = GetUser(msg.User) User = GetUser(msg.User)
@@ -536,7 +536,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = msg.Content, Content = msg.Content,
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
User = GetUser(msg.User), User = GetUser(msg.User),
}; };
@@ -563,7 +563,7 @@ namespace BarrageGrab
{ {
MsgId = msg.Common.msgId, MsgId = msg.Common.msgId,
Content = "直播已结束", Content = "直播已结束",
RoomId = msg.Common.roomId, RoomId = msg.Common.roomId.ToString(),
WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()), WebRoomId = AppRuntime.RoomCaches.GetCachedWebRoomid(msg.Common.roomId.ToString()),
User = null User = null
}; };