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;
using ColorConsole;
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();
///
/// 添加WebRoomid-房间ID 映射
///
///
///
public void SetRoomCache(string roomid, string webrid)
{
if (RoomInfoCache.ContainsKey(roomid))
{
RoomInfoCache[roomid].WebRoomId = webrid;
}
else
{
RoomInfoCache.TryAdd(roomid, new RoomInfo()
{
RoomId = roomid,
WebRoomId = webrid,
Title = "房间" + webrid
});
}
}
///
/// 添加房间缓存信息
///
///
///
public void AddRoomInfoCache(RoomInfo roomInfo)
{
if (roomInfo == null) return;
var roomid = roomInfo.RoomId;
if (RoomInfoCache.ContainsKey(roomid))
{
RoomInfoCache[roomid] = roomInfo;
}
else
{
RoomInfoCache.TryAdd(roomid, 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;
}
}
}
}