diff --git a/BarrageGrab/App.config b/BarrageGrab/App.config index 5a5fae5..6a5a82d 100644 --- a/BarrageGrab/App.config +++ b/BarrageGrab/App.config @@ -1,38 +1,38 @@ - + - + - + - + - + - + - + - + - + - + - - - + + + - - + + - \ No newline at end of file + diff --git a/BarrageGrab/AppRuntime.cs b/BarrageGrab/AppRuntime.cs index b53ede1..7d51e9e 100644 --- a/BarrageGrab/AppRuntime.cs +++ b/BarrageGrab/AppRuntime.cs @@ -37,6 +37,30 @@ namespace BarrageGrab } + /// + /// 隐藏/显示 控制台 + /// + /// + public static void DisplayConsole(bool show) + { + //var hWnd = WinApi.FindWindow(null, Console.Title); + var hWnd = WinApi.GetConsoleWindow(); + if (hWnd != IntPtr.Zero) + { + WinApi.ShowWindow(hWnd, show? WinApi.CmdShow.SW_SHOW:WinApi.CmdShow.SW_HIDE); + } + } + + public static void DisplayConsoleByTitle(bool show) + { + var hWnd = WinApi.FindWindow(null, Console.Title); + //var hWnd = WinApi.GetConsoleWindow(); + if (hWnd != IntPtr.Zero) + { + WinApi.ShowWindow(hWnd, show ? WinApi.CmdShow.SW_SHOW : WinApi.CmdShow.SW_HIDE); + } + } + /// /// 房间缓存管理器 /// @@ -95,7 +119,8 @@ namespace BarrageGrab /// public long GetCachedWebRoomid(string roomid) { - if (RoomInfoCache.TryGetValue(roomid, out var value)) return long.Parse(value.WebRoomId??"-1"); + RoomInfo value; + if (RoomInfoCache.TryGetValue(roomid,out value)) return long.Parse(value.WebRoomId??"-1"); return -1; } @@ -106,7 +131,8 @@ namespace BarrageGrab /// public RoomInfo GetCachedWebRoomInfo(string roomid) { - if (RoomInfoCache.TryGetValue(roomid, out var value)) return value; + RoomInfo value; + if (RoomInfoCache.TryGetValue(roomid, out value)) return value; return null; } } diff --git a/BarrageGrab/Appsetting.cs b/BarrageGrab/Appsetting.cs index f414c69..9aaed9b 100644 --- a/BarrageGrab/Appsetting.cs +++ b/BarrageGrab/Appsetting.cs @@ -12,7 +12,7 @@ namespace BarrageGrab { private static readonly Appsetting ins = new Appsetting(); - public static Appsetting Current { get => ins; } + public static Appsetting Current { get { return ins; } } public Appsetting() { diff --git a/BarrageGrab/Modles/RoomInfo.cs b/BarrageGrab/Modles/RoomInfo.cs index b4301db..26a1006 100644 --- a/BarrageGrab/Modles/RoomInfo.cs +++ b/BarrageGrab/Modles/RoomInfo.cs @@ -510,12 +510,12 @@ namespace BarrageGrab.Modles /// /// /// - public static (int, string) TryParseRoomPageHtml(string html, out RoomInfo result) + public static Tuple TryParseRoomPageHtml(string html, out RoomInfo result) { result = null; if (html.IsNullOrWhiteSpace()) { - return (-1, "无效的页面数据"); + return Tuple.Create(-1, "无效的页面数据"); } //var roomDataReg = new Regex(@"(?<=self\.__pace_f\.push\(\[1,""a:)\[.+?\](?=\\n?""\]\)[\s\n\r]*?<\/script>)"); //var roomDataReg = new Regex(@"(?<=self\.__pace_f\.push\(\[1,\s*""0:)\[.+?\](?=\\n""?\]\)[\s\n\r]*?<\/script>)"); @@ -526,7 +526,7 @@ namespace BarrageGrab.Modles if (!match.Success) { //Logger.LogError("在通过正则匹配直播页房间信息时失败,可能是官方做了升级"); - return (1, "未能匹配到房间信息"); + return Tuple.Create(1, "未能匹配到房间信息"); } var matchData = Regex.Unescape(match.Value); @@ -537,7 +537,7 @@ namespace BarrageGrab.Modles } catch (Exception ex) { - return (2, "匹配到的房间数据格式有错误"); + return Tuple.Create(2, "匹配到的房间数据格式有错误"); } //var root = jsonObject["children"][3]; //var roomInfo = root["initialState"]["roomStore"]["roomInfo"]; @@ -552,7 +552,7 @@ namespace BarrageGrab.Modles if (room == null) { - return (3, "房间信息为空,获取失败"); + return Tuple.Create(3, "房间信息为空,获取失败"); } result = new RoomInfo(); @@ -579,7 +579,7 @@ namespace BarrageGrab.Modles FollowStatus = roomOwner["follow_info"]["follow_status"].Value() }; } - return (0, "succ"); + return Tuple.Create(0, "succ"); } } } diff --git a/BarrageGrab/ObjectExtension.cs b/BarrageGrab/ObjectExtension.cs index afcc6f3..f560390 100644 --- a/BarrageGrab/ObjectExtension.cs +++ b/BarrageGrab/ObjectExtension.cs @@ -59,7 +59,7 @@ namespace BarrageGrab { return dic[key]; } - return default; + return default(TValue); } /// diff --git a/BarrageGrab/Program.cs b/BarrageGrab/Program.cs index 8f77090..10c0a2b 100644 --- a/BarrageGrab/Program.cs +++ b/BarrageGrab/Program.cs @@ -23,18 +23,17 @@ namespace BarrageGrab WinApi.SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭 WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式 Console.Title = "抖音弹幕监听推送"; + AppRuntime.DisplayConsole(!Appsetting.Current.HideConsole); - if(Appsetting.Current.HideConsole) - { - var hWnd = WinApi.FindWindow(null, Console.Title); - if (hWnd != IntPtr.Zero) - { - WinApi.ShowWindow(hWnd,WinApi.CmdShow.SW_HIDE); - } - } - bool exited = false; AppRuntime.WssService.StartListen(); + + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"{AppRuntime.WssService.ServerLocation} 弹幕服务已启动..."); + Console.ForegroundColor = ConsoleColor.Gray; + Console.Title = $"抖音弹幕监听推送 [{AppRuntime.WssService.ServerLocation}]"; + + AppRuntime.WssService.OnClose += (s, e) => { //退出程序 @@ -71,9 +70,10 @@ namespace BarrageGrab //检测程序是否多开 private static bool CheckAlreadyRunning() { - const string mutexName = "DyBarrageGrab"; + const string mutexName = "DyBarrageGrab"; // Try to create a new named mutex. - using (Mutex mutex = new Mutex(true, mutexName, out bool createdNew)) + bool createdNew; + using (Mutex mutex = new Mutex(true, mutexName, out createdNew)) { return !createdNew; } diff --git a/BarrageGrab/Proxy/SystemProxy.cs b/BarrageGrab/Proxy/SystemProxy.cs index 394bd7c..c75eddc 100644 --- a/BarrageGrab/Proxy/SystemProxy.cs +++ b/BarrageGrab/Proxy/SystemProxy.cs @@ -27,7 +27,7 @@ namespace BarrageGrab.Proxy /// /// 代理端口 /// - public int ProxyPort { get => Appsetting.Current.ProxyPort; } + public int ProxyPort { get { return Appsetting.Current.ProxyPort; } } public abstract void Dispose(); diff --git a/BarrageGrab/Proxy/TitaniumProxy.cs b/BarrageGrab/Proxy/TitaniumProxy.cs index d56234c..9a7a791 100644 --- a/BarrageGrab/Proxy/TitaniumProxy.cs +++ b/BarrageGrab/Proxy/TitaniumProxy.cs @@ -173,8 +173,10 @@ namespace BarrageGrab.Proxy { //利用 HtmlAgilityPack 在尾部注入script 标签 var html = await e.GetResponseBodyAsString(); - - (int code, string msg) = RoomInfo.TryParseRoomPageHtml(html, out var roominfo); + RoomInfo roominfo; + var tup = RoomInfo.TryParseRoomPageHtml(html, out roominfo); + int code = tup.Item1; + string msg = tup.Item2; if (code == 0) { diff --git a/BarrageGrab/WinApi.cs b/BarrageGrab/WinApi.cs index 9b48913..927dad2 100644 --- a/BarrageGrab/WinApi.cs +++ b/BarrageGrab/WinApi.cs @@ -37,6 +37,9 @@ namespace BarrageGrab [DllImport("User32.dll", EntryPoint = "ShowWindow")] public static extern bool ShowWindow(IntPtr hWnd, CmdShow nCmdShow); + [DllImport("kernel32.dll")] + public static extern IntPtr GetConsoleWindow(); + /// diff --git a/BarrageGrab/WsBarrageService.cs b/BarrageGrab/WsBarrageService.cs index c86b8fe..aad6559 100644 --- a/BarrageGrab/WsBarrageService.cs +++ b/BarrageGrab/WsBarrageService.cs @@ -40,6 +40,11 @@ namespace BarrageGrab /// public event EventHandler OnClose; + /// + /// WS服务器启动地址 + /// + public string ServerLocation { get { return socketServer.Location; } } + public WsBarrageService() { #if DEBUG @@ -495,11 +500,7 @@ namespace BarrageGrab case CommandCode.DisplayConsole: { var display = (bool)cmdPack.Data; - var hand = WinApi.FindWindow(null, Console.Title); - if (hand != IntPtr.Zero) - { - WinApi.ShowWindow(hand, display ? WinApi.CmdShow.SW_SHOW : WinApi.CmdShow.SW_HIDE); - } + AppRuntime.DisplayConsole(display); break; } } @@ -550,9 +551,7 @@ namespace BarrageGrab public void StartListen() { this.grab.Start(); //启动代理 - this.socketServer.Start(Listen);//启动监听 - console.WriteLine($"{this.socketServer.Location} 弹幕服务已启动...", ConsoleColor.Green); - Console.Title = $"抖音弹幕监听推送 [{this.socketServer.Location}]"; + this.socketServer.Start(Listen);//启动监听 } /// diff --git a/BarrageGrab/WssBarrageGrab.cs b/BarrageGrab/WssBarrageGrab.cs index 7cdd773..f8d21ae 100644 --- a/BarrageGrab/WssBarrageGrab.cs +++ b/BarrageGrab/WssBarrageGrab.cs @@ -68,7 +68,7 @@ namespace BarrageGrab /// /// 代理 /// - public ISystemProxy Proxy { get => proxy; } + public ISystemProxy Proxy { get { return proxy; } } public WssBarrageGrab() { diff --git a/BarrageGrab/WssBarrageService.csproj b/BarrageGrab/WssBarrageService.csproj index 86299ed..48e575a 100644 --- a/BarrageGrab/WssBarrageService.csproj +++ b/BarrageGrab/WssBarrageService.csproj @@ -10,7 +10,7 @@ Properties BarrageGrab WssBarrageService - v4.8 + v4.6.2 512 true publish\ @@ -28,6 +28,7 @@ false false true + true