From 5263db61f57425a44f6ee62d8bb5fef3e198cea8 Mon Sep 17 00:00:00 2001 From: XFEstudio Date: Wed, 8 Nov 2023 01:09:45 +0800 Subject: [PATCH] =?UTF-8?q?XCC=E5=9B=BE=E7=89=87=E6=8E=A5=E6=94=B6?= =?UTF-8?q?=E5=99=A8=E6=9B=B4=E6=94=B9=E4=B8=BAXCC=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=8E=A5=E6=94=B6=E5=B8=AE=E5=8A=A9=E5=85=B6=EF=BC=8C=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E4=BD=BF=E7=94=A8XCC=E6=B6=88=E6=81=AF=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E5=99=A8=E5=AF=B9=E6=8E=A5XCC=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E9=80=9A=E8=AE=AF=E6=9B=B4=E5=8A=A0=E6=96=B9=E4=BE=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CyberComm.cs" | 242 +++++++++++++----- ...47\261\273\346\213\223\345\261\225.csproj" | 2 + 2 files changed, 185 insertions(+), 59 deletions(-) diff --git "a/XFE\345\220\204\347\261\273\346\213\223\345\261\225/CyberComm.cs" "b/XFE\345\220\204\347\261\273\346\213\223\345\261\225/CyberComm.cs" index ce962d0..9bbb56e 100644 --- "a/XFE\345\220\204\347\261\273\346\213\223\345\261\225/CyberComm.cs" +++ "b/XFE\345\220\204\347\261\273\346\213\223\345\261\225/CyberComm.cs" @@ -6,6 +6,7 @@ using System.Net; using System.Net.WebSockets; using System.Text; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using XFE各类拓展.ArrayExtension; @@ -1463,160 +1464,283 @@ internal XCCGroup(string groupId, string sender, XCCNetWorkBase xCCNetWorkBase) } } /// - /// XCC图片接收器 + /// XCC文件类型 /// - public class XCCImageReceiveHelper + public enum XCCFileType { - private Dictionary xCCImageDictionary = new Dictionary(); + /// + /// 图片 + /// + Image, + /// + /// 视频 + /// + Video, + /// + /// 音频 + /// + Audio + } + /// + /// XCC消息接收器 + /// + public class XCCMessageReceiveHelper + { + private readonly Dictionary xCCFileDictionary = new Dictionary(); + private readonly Dictionary xCCMessageDictionary = new Dictionary(); /// /// 自动保存到本地 /// - public bool AutoSaveInLocal { get; } + public bool AutoSaveInLocal { get; set; } /// /// 保存的根目录 /// public string SavePathRoot { get; set; } /// - /// 从设置的根目录加载所有图片 + /// 接收到文件事件 + /// + public event EventHandler FileReceived; + /// + /// 接收到文本事件 + /// + public event EventHandler TextReceived; + /// + /// 从设置的根目录加载 /// /// - public async Task LoadImage() + public async Task Load() { await Task.Run(() => { foreach (var groupId in Directory.EnumerateDirectories(SavePathRoot)) { - foreach (var image in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}")) + foreach (var file in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}")) { - var filePath = $"{SavePathRoot}/{groupId}/{image}"; - if (Path.GetExtension(filePath) == ".png") + var filePath = $"{SavePathRoot}/{groupId}/{file}"; + if (file == "XFEMessage.xfe") + { + xCCMessageDictionary.Add(groupId, new XFEMultiDictionary(File.ReadAllText(filePath))); + } + else { - var imageId = Path.GetFileNameWithoutExtension(filePath); - var imageBuffer = File.ReadAllBytes(filePath); - xCCImageDictionary.Add(imageId, new XCCImage(groupId, imageId, imageBuffer)); + LoadFile(groupId, filePath); } } } }); } /// - /// 添加图片 + /// 从设置的根目录的指定群组加载 + /// + /// 群组ID + /// + public async Task LoadGroup(string groupId) + { + await Task.Run(() => + { + foreach (var file in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}")) + { + var filePath = $"{SavePathRoot}/{groupId}/{file}"; + if (file == "XFEMessage.xfe") + { + xCCMessageDictionary.Add(groupId, new XFEMultiDictionary(File.ReadAllText(filePath))); + } + else + { + LoadFile(groupId, filePath); + } + } + }); + } + private void LoadFile(string groupId, string filePath) + { + var messageId = Path.GetFileNameWithoutExtension(filePath); + var fileBuffer = File.ReadAllBytes(filePath); + switch (Path.GetExtension(filePath)) + { + case ".png": + xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Image, fileBuffer)); + break; + case ".mp4": + xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Video, fileBuffer)); + break; + case ".mp3": + xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Audio, fileBuffer)); + break; + default: + break; + } + } + /// + /// 获取文件 + /// + /// 消息ID + /// + public XCCFile GetFile(string messageId) + { + return xCCFileDictionary.ContainsKey(messageId) ? xCCFileDictionary[messageId] : null; + } + /// + /// 添加文件 /// - /// XCC图片实例 - public void AddImage(XCCImage xCCImage) + /// XCC文件实例 + public void AddFile(XCCFile xCCFile) { - xCCImageDictionary.Add(xCCImage.ImageId, xCCImage); - if (AutoSaveInLocal && xCCImage.ImageBuffer != null) - Save(xCCImage); + xCCFileDictionary.Add(xCCFile.MessageId, xCCFile); + if (AutoSaveInLocal && xCCFile.FileBuffer != null) + Save(xCCFile); } /// /// 保存图片 /// - /// XCC图片实例 - public void Save(XCCImage xCCImage) + /// XCC图片实例 + public void Save(XCCFile xCCFile) { - if (!Directory.Exists($"{SavePathRoot}/{xCCImage.GroupId}")) + if (!Directory.Exists($"{SavePathRoot}/{xCCFile.GroupId}")) + { + Directory.CreateDirectory($"{SavePathRoot}/{xCCFile.GroupId}"); + } + switch (xCCFile.FileType) { - Directory.CreateDirectory($"{SavePathRoot}/{xCCImage.GroupId}"); + case XCCFileType.Image: + File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.png", xCCFile.FileBuffer); + break; + case XCCFileType.Video: + File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.mp4", xCCFile.FileBuffer); + break; + case XCCFileType.Audio: + File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.mp3", xCCFile.FileBuffer); + break; + default: + break; + } + } + private void ReceiveFilePlaceHolder(XCCTextMessageReceivedEventArgs e, XCCFileType fileType) + { + if (!xCCFileDictionary.ContainsKey(e.MessageId)) + { + xCCFileDictionary.Add(e.MessageId, new XCCFile(e.GroupId, e.MessageId, fileType)); } - File.WriteAllBytes($"{SavePathRoot}/{xCCImage.GroupId}/{xCCImage.ImageId}.png", xCCImage.ImageBuffer); } /// - /// 接收图片占位符 + /// 接收文本消息 /// + /// 发送者 /// 事件参数 - public void ReceiveImagePlaceHolder(XCCTextMessageReceivedEventArgs e) + public void ReceiveTextMessage(object sender, XCCTextMessageReceivedEventArgs e) { - if (e.MessageType == XCCTextMessageType.Image && !xCCImageDictionary.ContainsKey(e.MessageId)) + switch (e.MessageType) { - xCCImageDictionary.Add(e.MessageId, new XCCImage(e.GroupId, e.MessageId)); + case XCCTextMessageType.Text: + //xCCMessageDictionary.Add(e.GroupId, new XFEMultiDictionary(e.)); + break; + case XCCTextMessageType.Image: + ReceiveFilePlaceHolder(e, XCCFileType.Image); + break; + case XCCTextMessageType.Audio: + ReceiveFilePlaceHolder(e, XCCFileType.Audio); + break; + case XCCTextMessageType.Video: + ReceiveFilePlaceHolder(e, XCCFileType.Video); + break; + default: + break; } } /// - /// 接收图片 + /// 接收二进制消息 /// + /// 发送者 /// 事件参数 - public void ReceiveImage(XCCBinaryMessageReceivedEventArgs e) + public void ReceiveBinaryMessage(object sender, XCCBinaryMessageReceivedEventArgs e) { - if (e.MessageType == XCCBinaryMessageType.Image && xCCImageDictionary.ContainsKey(e.MessageId)) + if (xCCFileDictionary.ContainsKey(e.MessageId)) { - var xCCImage = xCCImageDictionary[e.MessageId]; - if (!xCCImage.Loaded) + var xCCFile = xCCFileDictionary[e.MessageId]; + if (!xCCFile.Loaded) { - xCCImageDictionary[e.MessageId].LoadImage(e.BinaryMessage); + xCCFileDictionary[e.MessageId].LoadFile(e.BinaryMessage); if (AutoSaveInLocal) - Save(xCCImage); + Save(xCCFile); } } } /// - /// XCC图片接收器 + /// XCC消息接收器 /// /// 保存根目录 /// 自动保存 - public XCCImageReceiveHelper(string savePathRoot, bool autoSaveInLocal) + public XCCMessageReceiveHelper(string savePathRoot, bool autoSaveInLocal = true) { AutoSaveInLocal = autoSaveInLocal; SavePathRoot = savePathRoot; } } /// - /// XCC图片 + /// XCC文件 /// - public class XCCImage + public class XCCFile { /// - /// 图片加载完成时触发 + /// 文件加载完成时触发 /// - public event EventHandler ImageLoaded; + public event EventHandler FileLoaded; /// /// 群组ID /// public string GroupId { get; } /// - /// 图片ID + /// 消息ID + /// + public string MessageId { get; } + /// + /// XCC文件类型 /// - public string ImageId { get; } + public XCCFileType FileType { get; } /// /// 是否已加载 /// public bool Loaded { get; private set; } /// - /// 图片文件流 + /// 文件流 /// - public byte[] ImageBuffer { get; set; } + public byte[] FileBuffer { get; set; } /// - /// 加载图片 + /// 加载文件 /// - /// 文件流 - public void LoadImage(byte[] imageBuffer) + /// 文件流 + public void LoadFile(byte[] fileBuffer) { - ImageBuffer = imageBuffer; + FileBuffer = fileBuffer; Loaded = true; - ImageLoaded?.Invoke(this, imageBuffer); + FileLoaded?.Invoke(this, fileBuffer); } /// - /// XCC图片 + /// XCC文件 /// /// 群组ID - /// 图片ID - /// 图片的Buffer - public XCCImage(string groupId, string imageId, byte[] imageBuffer) + /// 文件消息ID + /// 文件类型 + /// 文件的Buffer + public XCCFile(string groupId, string messageId, XCCFileType fileType, byte[] fileBuffer) { GroupId = groupId; - ImageId = imageId; - ImageBuffer = imageBuffer; + FileType = fileType; + MessageId = messageId; + FileBuffer = fileBuffer; Loaded = true; } /// - /// XCC图片 + /// XCC文件 /// /// 群组ID - /// 图片ID - public XCCImage(string groupId, string imageId) + /// 文件ID + /// 文件类型 + public XCCFile(string groupId, string messageId, XCCFileType fileType) { GroupId = groupId; - ImageId = imageId; + FileType = fileType; + MessageId = messageId; Loaded = false; } } diff --git "a/XFE\345\220\204\347\261\273\346\213\223\345\261\225/XFE\345\220\204\347\261\273\346\213\223\345\261\225.csproj" "b/XFE\345\220\204\347\261\273\346\213\223\345\261\225/XFE\345\220\204\347\261\273\346\213\223\345\261\225.csproj" index a516d59..3b171d4 100644 --- "a/XFE\345\220\204\347\261\273\346\213\223\345\261\225/XFE\345\220\204\347\261\273\346\213\223\345\261\225.csproj" +++ "b/XFE\345\220\204\347\261\273\346\213\223\345\261\225/XFE\345\220\204\347\261\273\346\213\223\345\261\225.csproj" @@ -33,6 +33,8 @@ MinimumRecommendedRules.ruleset true bin\Release\XFE各类拓展.xml + Auto + true true