Files
DanMuRead/BarrageGrab/Program.cs
T

128 lines
4.2 KiB
C#
Raw Normal View History

2022-10-06 17:44:08 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2022-10-06 17:44:08 +08:00
using System.Linq;
2023-08-17 20:37:22 +08:00
using System.Net;
using System.Net.Sockets;
2022-10-06 17:44:08 +08:00
using System.Runtime.InteropServices;
using System.Text;
2023-06-19 21:44:21 +08:00
using System.Threading;
2024-03-29 17:34:14 +08:00
using System.Windows.Forms;
using Jint.Runtime;
2022-10-06 17:44:08 +08:00
namespace BarrageGrab
{
public class Program
{
2024-03-29 11:30:03 +08:00
static FormView mainForm = null;
static bool exited = false;
static bool formExited = false;
2024-03-29 17:34:14 +08:00
static WinApi.ControlCtrlDelegate controlCtr = ControlCtrlHandle;
2024-08-17 13:36:56 +08:00
static Mutex mutex = new Mutex(false, "DyBarrageGrab");
2022-10-06 17:44:08 +08:00
static void Main(string[] args)
2023-11-23 16:43:21 +08:00
{
2024-08-17 13:36:56 +08:00
if (!mutex.WaitOne(TimeSpan.Zero, true))
2022-10-06 17:44:08 +08:00
{
2024-08-17 13:36:56 +08:00
Console.WriteLine("另一个实例已在运行。");
2023-08-17 20:37:22 +08:00
Console.ReadKey();
return;
}
2024-08-17 13:36:56 +08:00
2024-03-29 11:30:03 +08:00
SetTitle("抖音弹幕监听推送");
2023-08-17 20:37:22 +08:00
2024-03-29 17:34:14 +08:00
try
{
Init();
}
catch (Exception ex)
{
2024-08-17 13:36:56 +08:00
Logger.LogError(ex, $"程序初始化错误,{ex.Message}");
MessageBox.Show(ex.Message, "程序初始化错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-03-29 17:34:14 +08:00
exited = true;
}
while (!exited)
{
2024-03-29 17:34:14 +08:00
Thread.Sleep(500);
}
if (!AppRuntime.WsServer.IsDisposed)
{
AppRuntime.WsServer.Dispose();
2024-03-29 17:34:14 +08:00
}
Logger.PrintColor("服务器已关闭...");
WinApi.SetConsoleCtrlHandler(controlCtr, false);//反注册捕获控制台关闭
}
private static void Init()
{
AppRuntime.Init();
2024-09-28 10:29:25 +08:00
LiveCompanHelper.SwitchSetup();
2024-03-29 17:34:14 +08:00
WinApi.SetConsoleCtrlHandler(controlCtr, true);//捕获控制台关闭
2023-12-17 23:56:10 +08:00
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
2024-03-29 17:46:24 +08:00
AppRuntime.DisplayConsole(!AppSetting.Current.HideConsole);//控制控制台可见
AppRuntime.WsServer.Grab.Proxy.SetUpstreamProxy(AppSetting.Current.UpstreamProxy);//设置上游代理
2024-03-29 17:34:14 +08:00
AppRuntime.WsServer.OnClose += (s, e) =>
{
exited = true;
};
2023-11-14 20:29:18 +08:00
2024-03-29 17:34:14 +08:00
//串口写入服务
2024-03-29 17:46:24 +08:00
if (!AppSetting.Current.ComPort.IsNullOrWhiteSpace())
2024-03-29 17:34:14 +08:00
{
AppRuntime.ComPortServer.OpenStart();
}
2024-03-29 11:30:03 +08:00
//显示窗体
2024-03-29 17:46:24 +08:00
if (AppSetting.Current.ShowWindow)
2023-11-23 16:43:21 +08:00
{
var uiThread = new Thread(new ThreadStart(() =>
{
mainForm = new FormView();
//开启消息循环
System.Windows.Forms.Application.Run(mainForm);
formExited = true;
2024-03-29 17:34:14 +08:00
AppRuntime.WsServer.Dispose();//Close后自动释放资源
2023-11-23 16:43:21 +08:00
}));
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.IsBackground = true;
uiThread.Start();
}
2023-11-16 09:26:40 +08:00
2024-03-29 17:34:14 +08:00
AppRuntime.WsServer.StartListen();//启动WS以及代理服务
2024-09-28 10:29:25 +08:00
Logger.PrintColor($"{AppRuntime.WsServer.ServerLocation} 弹幕服务已启动,其他端可通过此地址获取到弹幕流信息", ConsoleColor.Green);
2024-08-17 13:36:56 +08:00
Version version = System.Reflection.Assembly.GetAssembly(typeof(Program)).GetName().Version;
SetTitle($"抖音弹幕监听推送 v{version} [{AppRuntime.WsServer.ServerLocation}]");
2022-10-06 17:44:08 +08:00
}
2024-03-29 11:30:03 +08:00
//检测设置控制台标题
private static void SetTitle(string title)
{
if (WinApi.GetConsoleWindow() != IntPtr.Zero)
{
Console.Title = title;
}
}
//监听控制台消息事件
2024-03-29 17:34:14 +08:00
private static bool ControlCtrlHandle(int CtrlType)
2022-10-06 17:44:08 +08:00
{
switch (CtrlType)
{
case 0:
//Logger.PrintColor("0工具被强制关闭"); //Ctrl+C关闭
2022-10-06 17:44:08 +08:00
//server.Close();
break;
case 2:
Logger.PrintColor("2工具被强制关闭");//按控制台关闭按钮关闭
2024-03-29 17:34:14 +08:00
AppRuntime.WsServer.Dispose();
2022-10-06 17:44:08 +08:00
break;
}
return false;
2024-03-29 11:30:03 +08:00
}
2022-10-06 17:44:08 +08:00
}
}