Files
DanMuRead/BarrageGrab/JsEngine.cs
T

245 lines
7.1 KiB
C#
Raw Normal View History

2024-03-29 17:34:14 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2024-03-29 17:34:14 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jint;
using Jint.Native;
using Newtonsoft.Json;
2024-03-29 17:34:14 +08:00
namespace BarrageGrab
{
public static class JsEngine
{
2024-04-26 10:19:14 +08:00
/// <summary>
/// 创建一个Js引擎
/// </summary>
/// <returns></returns>
2024-03-29 17:34:14 +08:00
public static Engine CreateNewEngine()
{
var jsonParse = new Func<object, string>((obj) =>
{
try
{
return JsonConvert.SerializeObject(JsonConvert.SerializeObject(obj));
}
catch (Exception ex)
{
Logger.LogError("JS:" + ex.Message);
return null;
}
});
var engine = new Engine();
2024-04-26 10:19:14 +08:00
engine.Execute(@"
byteToUint8Array = function(arr){
return new Uint8Array(arr);
}");
2024-03-29 17:34:14 +08:00
engine.SetValue("console", new JsConsole());
engine.SetValue("encoder", new JsEncoder(engine));
2024-04-26 10:19:14 +08:00
engine.SetValue("bitConvert", new JsBitConvert(engine));
2024-03-29 17:34:14 +08:00
return engine;
}
/// <summary>
/// 从scripts 目录获取js 文件
2024-03-29 17:34:14 +08:00
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string GetJsFile(string fileName)
{
var checkDirs = new string[]
{
Path.Combine(AppContext.BaseDirectory, "Scripts"),
Path.Combine(AppContext.BaseDirectory, "scripts"),
};
foreach (var dir in checkDirs)
{
if (Directory.Exists(dir))
{
var path = Path.Combine(dir, fileName);
if (File.Exists(path))
{
return File.ReadAllText(path, Encoding.UTF8);
}
}
}
return null;
}
/// <summary>
/// 在scripts目录下创建js 文件
/// </summary>
/// <param name="fileName"></param>
/// <param name="content"></param>
public static void CreateJsFile(string fileName,string content)
{
var dir = Path.Combine(AppContext.BaseDirectory, "scripts");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(Path.Combine(dir, fileName), content, Encoding.UTF8);
2024-03-29 17:34:14 +08:00
}
class JsConsole
{
public void log(object obj)
{
if (obj == null) return;
Logger.PrintColor("JS: " + obj.ToString());
}
public void error(object obj)
{
if (obj == null) return;
Logger.LogError("JS: " + obj.ToString());
}
public void warn(object obj)
{
if (obj == null) return;
Logger.LogWarn("JS: " + obj.ToString());
}
}
class JsEncoder
{
Engine eng;
public JsEncoder(Engine eng)
{
this.eng = eng;
2024-03-29 17:34:14 +08:00
}
private JsValue ByteConvert(byte[] buff)
{
return eng.Invoke("byteToUint8Array", buff);
}
public JsValue utf8ToBytes(string str)
{
byte[] res = new byte[0];
if (str.IsNullOrWhiteSpace())
{
res = new byte[0];
}
else
{
res = Encoding.UTF8.GetBytes(str);
}
return ByteConvert(res);
}
public JsValue utf32ToBytes(string str)
{
byte[] res = new byte[0];
if (str.IsNullOrWhiteSpace())
{
res = new byte[0];
}
else
{
res = Encoding.UTF32.GetBytes(str);
}
return ByteConvert(res);
}
public JsValue asciiToBytes(string str)
{
byte[] res = new byte[0];
if (str.IsNullOrWhiteSpace())
{
res = new byte[0];
}
else
{
res = Encoding.ASCII.GetBytes(str);
}
return ByteConvert(res);
}
public string utf8ToString(byte[] bytes)
{
if (bytes == null) return null;
return Encoding.UTF8.GetString(bytes);
}
public string utf32ToString(byte[] bytes)
{
if (bytes == null) return null;
return Encoding.UTF32.GetString(bytes);
}
public string asciiToString(byte[] bytes)
{
if (bytes == null) return null;
return Encoding.ASCII.GetString(bytes);
}
}
2024-04-26 10:19:14 +08:00
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)
2024-04-26 10:19:14 +08:00
{
return BitConverter.ToDouble(buff, startIndex);
}
2024-04-26 10:19:14 +08:00
public bool toBoolean(byte[] buff, int startIndex = 0)
{
return BitConverter.ToBoolean(buff, startIndex);
2024-04-26 10:19:14 +08:00
}
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);
2024-04-26 10:19:14 +08:00
}
public JsValue getBytes(JsValue value)
{
byte[] buff = new byte[0];
switch (value.Type)
{
case Jint.Runtime.Types.Empty:
2024-04-26 10:19:14 +08:00
case Jint.Runtime.Types.Undefined:
case Jint.Runtime.Types.Null:
break;
case Jint.Runtime.Types.Boolean:
buff = BitConverter.GetBytes(value.AsBoolean());
break;
2024-04-26 10:19:14 +08:00
case Jint.Runtime.Types.Number:
buff = BitConverter.GetBytes(value.AsNumber());
break;
2024-04-26 10:19:14 +08:00
case Jint.Runtime.Types.BigInt:
buff = BitConverter.GetBytes((long)value.AsNumber());
break;
2024-04-26 10:19:14 +08:00
default:
throw new Exception($"不支持该类型转换:" + value.Type);
2024-04-26 10:19:14 +08:00
}
return ByteConvert(buff);
}
}
2024-03-29 17:34:14 +08:00
}
}