From 61496d4616c0eca815ec8380e6100b7bd00412e1 Mon Sep 17 00:00:00 2001 From: BobSong <605277374@qq.com> Date: Fri, 8 Aug 2025 09:13:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E9=A2=91=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NetworkProtocol/Outer/OuterMessage.proto | 7 ++- .../Outer/data/GlobalData.proto | 15 ++++++ Entity/Chat/ChatChannelCenterComponent.cs | 11 ++++ Entity/Chat/ChatChannelComponent.cs | 11 ++++ Entity/Generate/NetworkProtocol/GlobalData.cs | 43 ++++++++++++++++ .../Generate/NetworkProtocol/OuterMessage.cs | 8 ++- Hotfix/Chat/Helper/ChatChannelHelper.cs | 39 ++++++++++++++ .../ChatChannelCenterComponentSystem.cs | 51 +++++++++++++++++++ .../Inner/Chat2G_ChatMessageHandler.cs | 8 ++- Hotfix/OnSceneCreate_Init.cs | 1 + Server.sln.DotSettings.user | 2 +- 11 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 Entity/Chat/ChatChannelCenterComponent.cs create mode 100644 Entity/Chat/ChatChannelComponent.cs create mode 100644 Hotfix/Chat/Helper/ChatChannelHelper.cs create mode 100644 Hotfix/Chat/System/ChatChannelCenterComponentSystem.cs diff --git a/Config/NetworkProtocol/Outer/OuterMessage.proto b/Config/NetworkProtocol/Outer/OuterMessage.proto index 224e1dc..563d9dd 100644 --- a/Config/NetworkProtocol/Outer/OuterMessage.proto +++ b/Config/NetworkProtocol/Outer/OuterMessage.proto @@ -81,8 +81,11 @@ message Game2C_MailState // ICustomRouteMessage,GameRoute ///发送聊天 message C2Chat_SendMessageRequest // ICustomRouteRequest,Caht2C_SendMessageResponse,ChatRoute { - string Message = 1; + int32 Type = 1; //消息类型 + string Message = 2; + int64 Target = 3; //目标id,如果有 } + ///发送聊天响应 message Caht2C_SendMessageResponse // ICustomRouteResponse { @@ -92,5 +95,5 @@ message Caht2C_SendMessageResponse // ICustomRouteResponse ///推送消息 message Chat2C_Message // ICustomRouteMessage,ChatRoute { - string Message = 1; + ChatMessageInfo Message = 1; } \ No newline at end of file diff --git a/Config/NetworkProtocol/Outer/data/GlobalData.proto b/Config/NetworkProtocol/Outer/data/GlobalData.proto index e69de29..ebd2f3d 100644 --- a/Config/NetworkProtocol/Outer/data/GlobalData.proto +++ b/Config/NetworkProtocol/Outer/data/GlobalData.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; +package Fantasy.Network.Message; + +message ChatUserInfo +{ + int64 Id = 1;//用户id + int64 Name = 2;//用户名 +} + +message ChatMessageInfo +{ + int32 Type = 1; //消息类型 + ChatUserInfo Trigger = 2; //发送者 + repeated string Content = 3; //内容 +} \ No newline at end of file diff --git a/Entity/Chat/ChatChannelCenterComponent.cs b/Entity/Chat/ChatChannelCenterComponent.cs new file mode 100644 index 0000000..67650a9 --- /dev/null +++ b/Entity/Chat/ChatChannelCenterComponent.cs @@ -0,0 +1,11 @@ +using Fantasy.Entitas; + +namespace NB.Chat; + +/// +/// 聊天频道管理 +/// +public class ChatChannelCenterComponent : Entity +{ + public readonly Dictionary Channels = new Dictionary(); +} \ No newline at end of file diff --git a/Entity/Chat/ChatChannelComponent.cs b/Entity/Chat/ChatChannelComponent.cs new file mode 100644 index 0000000..3be133b --- /dev/null +++ b/Entity/Chat/ChatChannelComponent.cs @@ -0,0 +1,11 @@ +using Fantasy.Entitas; + +namespace NB.Chat; + +/// +/// 聊天频道实体 +/// +public class ChatChannelComponent : Entity +{ + public readonly HashSet Units = new HashSet(); +} \ No newline at end of file diff --git a/Entity/Generate/NetworkProtocol/GlobalData.cs b/Entity/Generate/NetworkProtocol/GlobalData.cs index c4f45da..02ceddc 100644 --- a/Entity/Generate/NetworkProtocol/GlobalData.cs +++ b/Entity/Generate/NetworkProtocol/GlobalData.cs @@ -17,4 +17,47 @@ using Fantasy.Serialize; namespace Fantasy { + [ProtoContract] + public partial class ChatUserInfo : AMessage, IProto + { + public static ChatUserInfo Create(Scene scene) + { + return scene.MessagePoolComponent.Rent(); + } + public override void Dispose() + { + Id = default; + Name = default; +#if FANTASY_NET || FANTASY_UNITY + GetScene().MessagePoolComponent.Return(this); +#endif + } + [ProtoMember(1)] + public long Id { get; set; } + [ProtoMember(2)] + public long Name { get; set; } + } + [ProtoContract] + public partial class ChatMessageInfo : AMessage, IProto + { + public static ChatMessageInfo Create(Scene scene) + { + return scene.MessagePoolComponent.Rent(); + } + public override void Dispose() + { + Type = default; + Trigger = default; + Content.Clear(); +#if FANTASY_NET || FANTASY_UNITY + GetScene().MessagePoolComponent.Return(this); +#endif + } + [ProtoMember(1)] + public int Type { get; set; } + [ProtoMember(2)] + public ChatUserInfo Trigger { get; set; } + [ProtoMember(3)] + public List Content = new List(); + } } diff --git a/Entity/Generate/NetworkProtocol/OuterMessage.cs b/Entity/Generate/NetworkProtocol/OuterMessage.cs index ef2aaf4..1682b43 100644 --- a/Entity/Generate/NetworkProtocol/OuterMessage.cs +++ b/Entity/Generate/NetworkProtocol/OuterMessage.cs @@ -299,7 +299,9 @@ namespace Fantasy } public override void Dispose() { + Type = default; Message = default; + Target = default; #if FANTASY_NET || FANTASY_UNITY GetScene().MessagePoolComponent.Return(this); #endif @@ -310,7 +312,11 @@ namespace Fantasy [ProtoIgnore] public int RouteType => Fantasy.RouteType.ChatRoute; [ProtoMember(1)] + public int Type { get; set; } + [ProtoMember(2)] public string Message { get; set; } + [ProtoMember(3)] + public long Target { get; set; } } /// /// 发送聊天响应 @@ -354,6 +360,6 @@ namespace Fantasy [ProtoIgnore] public int RouteType => Fantasy.RouteType.ChatRoute; [ProtoMember(1)] - public string Message { get; set; } + public ChatMessageInfo Message { get; set; } } } diff --git a/Hotfix/Chat/Helper/ChatChannelHelper.cs b/Hotfix/Chat/Helper/ChatChannelHelper.cs new file mode 100644 index 0000000..dd33966 --- /dev/null +++ b/Hotfix/Chat/Helper/ChatChannelHelper.cs @@ -0,0 +1,39 @@ +using Fantasy; + +namespace NB.Chat; + +public static class ChatChannelHelper +{ + /// + /// 申请一个频道 + /// + /// + /// + /// + public static ChatChannelComponent Apply(Scene scene, long channelId) + { + return scene.GetComponent().Apply(channelId); + } + + /// + /// 尝试获取一个频道 + /// + /// + /// + /// + /// + public static bool TryGet(Scene scene, long channelId, out ChatChannelComponent? channel) + { + return scene.GetComponent().TryGet(channelId, out channel); + } + + /// + /// 解散频道 + /// + /// + /// + public static void Disband(Scene scene, long channelId) + { + scene.GetComponent().Disband(channelId); + } +} \ No newline at end of file diff --git a/Hotfix/Chat/System/ChatChannelCenterComponentSystem.cs b/Hotfix/Chat/System/ChatChannelCenterComponentSystem.cs new file mode 100644 index 0000000..a51c84c --- /dev/null +++ b/Hotfix/Chat/System/ChatChannelCenterComponentSystem.cs @@ -0,0 +1,51 @@ +using Fantasy.Entitas; + +namespace NB.Chat; + +public static class ChatChannelCenterComponentSystem +{ + /// + /// 申请 + /// + /// + /// + /// + public static ChatChannelComponent Apply(this ChatChannelCenterComponent self, long channelId) + { + if (self.Channels.TryGetValue(channelId, out var channel)) + { + return channel; + } + + channel = Entity.Create(self.Scene, channelId, true, true); + self.Channels.Add(channelId, channel); + return channel; + } + + /// + /// 获取 + /// + /// + /// + /// + /// + public static bool TryGet(this ChatChannelCenterComponent self, long channelId, out ChatChannelComponent? channel) + { + return self.Channels.TryGetValue(channelId, out channel); + } + + /// + /// 解散 + /// + /// + /// + public static void Disband(this ChatChannelCenterComponent self, long channelId) + { + if (self.Channels.Remove(channelId, out var channel)) + { + return; + } + + channel?.Dispose(); + } +} \ No newline at end of file diff --git a/Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs b/Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs index a228fef..0cac019 100644 --- a/Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs +++ b/Hotfix/Gate/Handler/Inner/Chat2G_ChatMessageHandler.cs @@ -1,6 +1,9 @@ -using Fantasy; +using System.Diagnostics; +using Fantasy; using Fantasy.Async; +using Fantasy.Network; using Fantasy.Network.Interface; +using Fantasy.PacketParser; namespace NB.Gate; @@ -10,10 +13,11 @@ public class Chat2G_ChatMessageHandler : Route { var chatMessage = new Chat2C_Message() { - Message = message.Message, + Message = new ChatMessageInfo(), }; var gateUnitManage = scene.GetComponent(); + foreach (var session in gateUnitManage.ForEachUnitSession()) { session.Send(chatMessage); diff --git a/Hotfix/OnSceneCreate_Init.cs b/Hotfix/OnSceneCreate_Init.cs index fa70832..b0bfddf 100644 --- a/Hotfix/OnSceneCreate_Init.cs +++ b/Hotfix/OnSceneCreate_Init.cs @@ -42,6 +42,7 @@ public class OnSceneCreate_Init : AsyncEventSystem { //用于管理玩家的组件 scene.AddComponent(); + scene.AddComponent(); break; } } diff --git a/Server.sln.DotSettings.user b/Server.sln.DotSettings.user index c758943..ed77459 100644 --- a/Server.sln.DotSettings.user +++ b/Server.sln.DotSettings.user @@ -73,5 +73,5 @@ ForceIncluded ForceIncluded ForceIncluded - VISIBLE_FILES + \ No newline at end of file