XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

XCC图片接收器更改为XCC消息接收帮助其,现在使用XCC消息接收器对接XCC网络通讯更加方便

5263db6
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +185 -59
Modified XFE各类拓展/CyberComm.cs +183 -59
@@ -6,6 +6,7 @@ using System.Linq;
6 6 using System.Net;
7 7 using System.Net.WebSockets;
8 8 using System.Text;
9 using System.Text.RegularExpressions;
9 10 using System.Threading;
10 11 using System.Threading.Tasks;
11 12 using XFE各类拓展.ArrayExtension;
@@ -1463,160 +1464,283 @@ namespace XFE各类拓展.CyberComm
1463 1464 }
1464 1465 }
1465 1466 /// <summary>
1466 /// XCC图片接收器
1467 /// XCC文件类型
1467 1468 /// </summary>
1468 public class XCCImageReceiveHelper
1469 public enum XCCFileType
1469 1470 {
1470 private Dictionary<string, XCCImage> xCCImageDictionary = new Dictionary<string, XCCImage>();
1471 /// <summary>
1472 /// 图片
1473 /// </summary>
1474 Image,
1475 /// <summary>
1476 /// 视频
1477 /// </summary>
1478 Video,
1479 /// <summary>
1480 /// 音频
1481 /// </summary>
1482 Audio
1483 }
1484 /// <summary>
1485 /// XCC消息接收器
1486 /// </summary>
1487 public class XCCMessageReceiveHelper
1488 {
1489 private readonly Dictionary<string, XCCFile> xCCFileDictionary = new Dictionary<string, XCCFile>();
1490 private readonly Dictionary<string, XFEMultiDictionary> xCCMessageDictionary = new Dictionary<string, XFEMultiDictionary>();
1471 1491 /// <summary>
1472 1492 /// 自动保存到本地
1473 1493 /// </summary>
1474 public bool AutoSaveInLocal { get; }
1494 public bool AutoSaveInLocal { get; set; }
1475 1495 /// <summary>
1476 1496 /// 保存的根目录
1477 1497 /// </summary>
1478 1498 public string SavePathRoot { get; set; }
1479 1499 /// <summary>
1480 /// 从设置的根目录加载所有图片
1500 /// 接收到文件事件
1501 /// </summary>
1502 public event EventHandler<XCCFile> FileReceived;
1503 /// <summary>
1504 /// 接收到文本事件
1505 /// </summary>
1506 public event EventHandler<string> TextReceived;
1507 /// <summary>
1508 /// 从设置的根目录加载
1481 1509 /// </summary>
1482 1510 /// <returns></returns>
1483 public async Task LoadImage()
1511 public async Task Load()
1484 1512 {
1485 1513 await Task.Run(() =>
1486 1514 {
1487 1515 foreach (var groupId in Directory.EnumerateDirectories(SavePathRoot))
1488 1516 {
1489 foreach (var image in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}"))
1517 foreach (var file in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}"))
1490 1518 {
1491 var filePath = $"{SavePathRoot}/{groupId}/{image}";
1492 if (Path.GetExtension(filePath) == ".png")
1519 var filePath = $"{SavePathRoot}/{groupId}/{file}";
1520 if (file == "XFEMessage.xfe")
1521 {
1522 xCCMessageDictionary.Add(groupId, new XFEMultiDictionary(File.ReadAllText(filePath)));
1523 }
1524 else
1493 1525 {
1494 var imageId = Path.GetFileNameWithoutExtension(filePath);
1495 var imageBuffer = File.ReadAllBytes(filePath);
1496 xCCImageDictionary.Add(imageId, new XCCImage(groupId, imageId, imageBuffer));
1526 LoadFile(groupId, filePath);
1497 1527 }
1498 1528 }
1499 1529 }
1500 1530 });
1501 1531 }
1502 1532 /// <summary>
1503 /// 添加图片
1533 /// 从设置的根目录的指定群组加载
1534 /// </summary>
1535 /// <param name="groupId">群组ID</param>
1536 /// <returns></returns>
1537 public async Task LoadGroup(string groupId)
1538 {
1539 await Task.Run(() =>
1540 {
1541 foreach (var file in Directory.EnumerateFiles($"{SavePathRoot}/{groupId}"))
1542 {
1543 var filePath = $"{SavePathRoot}/{groupId}/{file}";
1544 if (file == "XFEMessage.xfe")
1545 {
1546 xCCMessageDictionary.Add(groupId, new XFEMultiDictionary(File.ReadAllText(filePath)));
1547 }
1548 else
1549 {
1550 LoadFile(groupId, filePath);
1551 }
1552 }
1553 });
1554 }
1555 private void LoadFile(string groupId, string filePath)
1556 {
1557 var messageId = Path.GetFileNameWithoutExtension(filePath);
1558 var fileBuffer = File.ReadAllBytes(filePath);
1559 switch (Path.GetExtension(filePath))
1560 {
1561 case ".png":
1562 xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Image, fileBuffer));
1563 break;
1564 case ".mp4":
1565 xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Video, fileBuffer));
1566 break;
1567 case ".mp3":
1568 xCCFileDictionary.Add(messageId, new XCCFile(groupId, messageId, XCCFileType.Audio, fileBuffer));
1569 break;
1570 default:
1571 break;
1572 }
1573 }
1574 /// <summary>
1575 /// 获取文件
1576 /// </summary>
1577 /// <param name="messageId">消息ID</param>
1578 /// <returns></returns>
1579 public XCCFile GetFile(string messageId)
1580 {
1581 return xCCFileDictionary.ContainsKey(messageId) ? xCCFileDictionary[messageId] : null;
1582 }
1583 /// <summary>
1584 /// 添加文件
1504 1585 /// </summary>
1505 /// <param name="xCCImage">XCC图片实例</param>
1506 public void AddImage(XCCImage xCCImage)
1586 /// <param name="xCCFile">XCC文件实例</param>
1587 public void AddFile(XCCFile xCCFile)
1507 1588 {
1508 xCCImageDictionary.Add(xCCImage.ImageId, xCCImage);
1509 if (AutoSaveInLocal && xCCImage.ImageBuffer != null)
1510 Save(xCCImage);
1589 xCCFileDictionary.Add(xCCFile.MessageId, xCCFile);
1590 if (AutoSaveInLocal && xCCFile.FileBuffer != null)
1591 Save(xCCFile);
1511 1592 }
1512 1593 /// <summary>
1513 1594 /// 保存图片
1514 1595 /// </summary>
1515 /// <param name="xCCImage">XCC图片实例</param>
1516 public void Save(XCCImage xCCImage)
1596 /// <param name="xCCFile">XCC图片实例</param>
1597 public void Save(XCCFile xCCFile)
1517 1598 {
1518 if (!Directory.Exists($"{SavePathRoot}/{xCCImage.GroupId}"))
1599 if (!Directory.Exists($"{SavePathRoot}/{xCCFile.GroupId}"))
1600 {
1601 Directory.CreateDirectory($"{SavePathRoot}/{xCCFile.GroupId}");
1602 }
1603 switch (xCCFile.FileType)
1519 1604 {
1520 Directory.CreateDirectory($"{SavePathRoot}/{xCCImage.GroupId}");
1605 case XCCFileType.Image:
1606 File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.png", xCCFile.FileBuffer);
1607 break;
1608 case XCCFileType.Video:
1609 File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.mp4", xCCFile.FileBuffer);
1610 break;
1611 case XCCFileType.Audio:
1612 File.WriteAllBytes($"{SavePathRoot}/{xCCFile.GroupId}/{xCCFile.MessageId}.mp3", xCCFile.FileBuffer);
1613 break;
1614 default:
1615 break;
1616 }
1617 }
1618 private void ReceiveFilePlaceHolder(XCCTextMessageReceivedEventArgs e, XCCFileType fileType)
1619 {
1620 if (!xCCFileDictionary.ContainsKey(e.MessageId))
1621 {
1622 xCCFileDictionary.Add(e.MessageId, new XCCFile(e.GroupId, e.MessageId, fileType));
1521 1623 }
1522 File.WriteAllBytes($"{SavePathRoot}/{xCCImage.GroupId}/{xCCImage.ImageId}.png", xCCImage.ImageBuffer);
1523 1624 }
1524 1625 /// <summary>
1525 /// 接收图片占位符
1626 /// 接收文本消息
1526 1627 /// </summary>
1628 /// <param name="sender">发送者</param>
1527 1629 /// <param name="e">事件参数</param>
1528 public void ReceiveImagePlaceHolder(XCCTextMessageReceivedEventArgs e)
1630 public void ReceiveTextMessage(object sender, XCCTextMessageReceivedEventArgs e)
1529 1631 {
1530 if (e.MessageType == XCCTextMessageType.Image && !xCCImageDictionary.ContainsKey(e.MessageId))
1632 switch (e.MessageType)
1531 1633 {
1532 xCCImageDictionary.Add(e.MessageId, new XCCImage(e.GroupId, e.MessageId));
1634 case XCCTextMessageType.Text:
1635 //xCCMessageDictionary.Add(e.GroupId, new XFEMultiDictionary(e.));
1636 break;
1637 case XCCTextMessageType.Image:
1638 ReceiveFilePlaceHolder(e, XCCFileType.Image);
1639 break;
1640 case XCCTextMessageType.Audio:
1641 ReceiveFilePlaceHolder(e, XCCFileType.Audio);
1642 break;
1643 case XCCTextMessageType.Video:
1644 ReceiveFilePlaceHolder(e, XCCFileType.Video);
1645 break;
1646 default:
1647 break;
1533 1648 }
1534 1649 }
1535 1650 /// <summary>
1536 /// 接收图片
1651 /// 接收二进制消息
1537 1652 /// </summary>
1653 /// <param name="sender">发送者</param>
1538 1654 /// <param name="e">事件参数</param>
1539 public void ReceiveImage(XCCBinaryMessageReceivedEventArgs e)
1655 public void ReceiveBinaryMessage(object sender, XCCBinaryMessageReceivedEventArgs e)
1540 1656 {
1541 if (e.MessageType == XCCBinaryMessageType.Image && xCCImageDictionary.ContainsKey(e.MessageId))
1657 if (xCCFileDictionary.ContainsKey(e.MessageId))
1542 1658 {
1543 var xCCImage = xCCImageDictionary[e.MessageId];
1544 if (!xCCImage.Loaded)
1659 var xCCFile = xCCFileDictionary[e.MessageId];
1660 if (!xCCFile.Loaded)
1545 1661 {
1546 xCCImageDictionary[e.MessageId].LoadImage(e.BinaryMessage);
1662 xCCFileDictionary[e.MessageId].LoadFile(e.BinaryMessage);
1547 1663 if (AutoSaveInLocal)
1548 Save(xCCImage);
1664 Save(xCCFile);
1549 1665 }
1550 1666 }
1551 1667 }
1552 1668 /// <summary>
1553 /// XCC图片接收器
1669 /// XCC消息接收器
1554 1670 /// </summary>
1555 1671 /// <param name="savePathRoot">保存根目录</param>
1556 1672 /// <param name="autoSaveInLocal">自动保存</param>
1557 public XCCImageReceiveHelper(string savePathRoot, bool autoSaveInLocal)
1673 public XCCMessageReceiveHelper(string savePathRoot, bool autoSaveInLocal = true)
1558 1674 {
1559 1675 AutoSaveInLocal = autoSaveInLocal;
1560 1676 SavePathRoot = savePathRoot;
1561 1677 }
1562 1678 }
1563 1679 /// <summary>
1564 /// XCC图片
1680 /// XCC文件
1565 1681 /// </summary>
1566 public class XCCImage
1682 public class XCCFile
1567 1683 {
1568 1684 /// <summary>
1569 /// 图片加载完成时触发
1685 /// 文件加载完成时触发
1570 1686 /// </summary>
1571 public event EventHandler<byte[]> ImageLoaded;
1687 public event EventHandler<byte[]> FileLoaded;
1572 1688 /// <summary>
1573 1689 /// 群组ID
1574 1690 /// </summary>
1575 1691 public string GroupId { get; }
1576 1692 /// <summary>
1577 /// 图片ID
1693 /// 消息ID
1694 /// </summary>
1695 public string MessageId { get; }
1696 /// <summary>
1697 /// XCC文件类型
1578 1698 /// </summary>
1579 public string ImageId { get; }
1699 public XCCFileType FileType { get; }
1580 1700 /// <summary>
1581 1701 /// 是否已加载
1582 1702 /// </summary>
1583 1703 public bool Loaded { get; private set; }
1584 1704 /// <summary>
1585 /// 图片文件流
1705 /// 文件流
1586 1706 /// </summary>
1587 public byte[] ImageBuffer { get; set; }
1707 public byte[] FileBuffer { get; set; }
1588 1708 /// <summary>
1589 /// 加载图片
1709 /// 加载文件
1590 1710 /// </summary>
1591 /// <param name="imageBuffer">文件流</param>
1592 public void LoadImage(byte[] imageBuffer)
1711 /// <param name="fileBuffer">文件流</param>
1712 public void LoadFile(byte[] fileBuffer)
1593 1713 {
1594 ImageBuffer = imageBuffer;
1714 FileBuffer = fileBuffer;
1595 1715 Loaded = true;
1596 ImageLoaded?.Invoke(this, imageBuffer);
1716 FileLoaded?.Invoke(this, fileBuffer);
1597 1717 }
1598 1718 /// <summary>
1599 /// XCC图片
1719 /// XCC文件
1600 1720 /// </summary>
1601 1721 /// <param name="groupId">群组ID</param>
1602 /// <param name="imageId">图片ID</param>
1603 /// <param name="imageBuffer">图片的Buffer</param>
1604 public XCCImage(string groupId, string imageId, byte[] imageBuffer)
1722 /// <param name="messageId">文件消息ID</param>
1723 /// <param name="fileType">文件类型</param>
1724 /// <param name="fileBuffer">文件的Buffer</param>
1725 public XCCFile(string groupId, string messageId, XCCFileType fileType, byte[] fileBuffer)
1605 1726 {
1606 1727 GroupId = groupId;
1607 ImageId = imageId;
1608 ImageBuffer = imageBuffer;
1728 FileType = fileType;
1729 MessageId = messageId;
1730 FileBuffer = fileBuffer;
1609 1731 Loaded = true;
1610 1732 }
1611 1733 /// <summary>
1612 /// XCC图片
1734 /// XCC文件
1613 1735 /// </summary>
1614 1736 /// <param name="groupId">群组ID</param>
1615 /// <param name="imageId">图片ID</param>
1616 public XCCImage(string groupId, string imageId)
1737 /// <param name="messageId">文件ID</param>
1738 /// <param name="fileType">文件类型</param>
1739 public XCCFile(string groupId, string messageId, XCCFileType fileType)
1617 1740 {
1618 1741 GroupId = groupId;
1619 ImageId = imageId;
1742 FileType = fileType;
1743 MessageId = messageId;
1620 1744 Loaded = false;
1621 1745 }
1622 1746 }
Modified XFE各类拓展/XFE各类拓展.csproj +2 -0
@@ -33,6 +33,8 @@
33 33 <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
34 34 <CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
35 35 <DocumentationFile>bin\Release\XFE各类拓展.xml</DocumentationFile>
36 <GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
37 <DebugSymbols>true</DebugSymbols>
36 38 </PropertyGroup>
37 39 <PropertyGroup>
38 40 <SignAssembly>true</SignAssembly>