using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using BarrageGrab.Modles; namespace BarrageGrab { /// /// 程序运行时信息 /// public static class AppRuntime { /// /// Ws弹幕服务示例 /// public static WsBarrageService WssService { get; private set; } = new WsBarrageService(); /// /// 房间缓存信息 /// public static RoomCacheManager RoomCaches { get; } = new RoomCacheManager(); /// /// 程序进程信息 /// public static Process CurrentProcess { get; private set; } = System.Diagnostics.Process.GetCurrentProcess(); static AppRuntime() { } /// /// 隐藏/显示 控制台 /// /// 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); } } /// /// 房间缓存管理器 /// public class RoomCacheManager { /// /// 房间ID - 房间信息映射缓存 /// public ConcurrentDictionary RoomInfoCache { get; } = new ConcurrentDictionary(); /// /// 添加房间缓存时触发 /// public event EventHandler OnCache; /// /// 添加WebRoomid-房间ID 映射 /// /// /// public void SetRoomCache(string roomid, string webrid) { if (RoomInfoCache.ContainsKey(roomid)) { RoomInfoCache[roomid].WebRoomId = webrid; } else { var info = new RoomInfo() { RoomId = roomid, WebRoomId = webrid, Title = "房间" + webrid }; var succ = RoomInfoCache.TryAdd(roomid,info); if (succ) { OnCache?.Invoke(this, new RoomCacheEventArgs() { Model = 0, RoomInfo = info }); } } } /// /// 添加房间缓存信息 /// /// /// public void AddRoomInfoCache(RoomInfo roomInfo) { if (roomInfo == null) return; var roomid = roomInfo.RoomId; if (RoomInfoCache.ContainsKey(roomid)) { RoomInfoCache[roomid] = roomInfo; OnCache?.Invoke(this, new RoomCacheEventArgs() { Model = 1, RoomInfo = roomInfo }); } else { var succ = RoomInfoCache.TryAdd(roomid, roomInfo); if (succ) { OnCache?.Invoke(this,new RoomCacheEventArgs() { Model = 0, RoomInfo = roomInfo }); } } } /// /// 根据roomid从缓存获取webRoomid /// /// /// public long GetCachedWebRoomid(string roomid) { RoomInfo value; if (RoomInfoCache.TryGetValue(roomid,out value)) return long.Parse(value.WebRoomId??"-1"); return -1; } /// /// 根据roomid从缓存获取房间信息 /// /// /// public RoomInfo GetCachedWebRoomInfo(string roomid) { RoomInfo value; if (RoomInfoCache.TryGetValue(roomid, out value)) return value; return null; } /// /// 根据webRoomid从缓存获取房间信息 /// /// /// public RoomInfo GetByWebRoomid(string webrid) { var find = RoomInfoCache.FirstOrDefault(p => p.Value.WebRoomId == webrid); return find.Value; } public class RoomCacheEventArgs : EventArgs { public RoomInfo RoomInfo { get; set; } /// /// 0:添加,1更新,2删除 /// public int Model { get; set; } } } } }