Skip to content

Commit 8f9dc70

Browse files
committed
热更新的MessageHandler处理
1 parent 5ce0982 commit 8f9dc70

17 files changed

+467
-39905
lines changed

Assembly-CSharp-Editor.csproj

Lines changed: 210 additions & 210 deletions
Large diffs are not rendered by default.

Assembly-CSharp.csproj

Lines changed: 188 additions & 188 deletions
Large diffs are not rendered by default.

Assets/Game/HotFix/HotFix.dll.bytes

512 Bytes
Binary file not shown.

Assets/Game/HotFix/HotFix.dll.bytes.meta

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game/HotFix/HotFix.pdb.bytes

2 KB
Binary file not shown.

HotFix/HotFix/Game/Network/C2S_TestInfo_Handle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace HotFix.Taurus
44
{
5-
[GT.MessageHandler(typeof(C2S_TestInfo))]
5+
[MessageHandler(typeof(C2S_TestInfo))]
66
public class C2S_TestInfo_Handle:MessageHandlerBase
77
{
88
public override void Handle(object message)
99
{
1010
C2S_TestInfo c2S_TestInfo = message as C2S_TestInfo;
1111

12-
HotFixMode.Network.SendMessage(new S2C_TestInfo(){ Message = "C2S_TestInfo_Handle -- S2C" },new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"),35120));
12+
HotFixMode.Network.SendMessage(new S2C_TestInfo(){RpcId=c2S_TestInfo.RpcId, Message = "C2S_TestInfo_Handle -- S2C" },new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"),35120));
1313
}
1414
}
1515
}

HotFix/HotFix/Game/Network/HotFixTestMessageHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace HotFix.Taurus
44
{
5-
[GT.MessageHandler(typeof(HotFixProtoTest))]
5+
[MessageHandler(typeof(HotFixProtoTest))]
66
public class HotFixTestMessageHandle: MessageHandlerBase
77
{
88
public override void Handle(object message)
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
using HotFix.Taurus;
2-
using GT=GameFramework.Taurus;
32

4-
[GT.Message(1000)]
3+
[Message(1000)]
54
public partial class HotFixProtoTest
65
{
76
}
87

9-
[GT.Message(1001)]
8+
[Message(1001)]
109
public partial class C2S_TestInfo : IRequest
1110
{
1211
}
1312

14-
[GT.Message(1002)]
13+
[Message(1002)]
1514
public partial class S2C_TestInfo : IResponse
1615
{
1716
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace HotFix.Taurus
4+
{
5+
public class MessageAttribute : Attribute
6+
{
7+
public ushort TypeCode { get; private set; }
8+
9+
public MessageAttribute(ushort typeCode)
10+
{
11+
TypeCode = typeCode;
12+
}
13+
}
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #消息处理类标记# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年8月7日 00点38分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
12+
namespace HotFix.Taurus
13+
{
14+
public class MessageHandlerAttribute : Attribute
15+
{
16+
public string TypeMessage { get; private set; }
17+
18+
public MessageHandlerAttribute(Type typeMessage)
19+
{
20+
TypeMessage = typeMessage.ToString();
21+
}
22+
}
23+
}

HotFix/HotFix/GameFramework/Network/NetworkManager.cs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
namespace HotFix.Taurus
1818
{
19-
public class NetworkManager:GameFrameworkModule
19+
public sealed class NetworkManager:GameFrameworkModule
2020
{
2121
#region 属性
22-
private readonly Dictionary<string, List<MessageHandlerBase>> _messageHandler = new Dictionary<string, List<MessageHandlerBase>>();
22+
private readonly Dictionary<Type, List<MessageHandlerBase>> _messageHandler = new Dictionary<Type, List<MessageHandlerBase>>();
2323
private readonly Dictionary<ushort, Type> _messageCodeType = new Dictionary<ushort, Type>();
2424
private readonly ProtobufPacker _protobufPacker;
2525
private int _rpcId = 0;
@@ -35,8 +35,9 @@ public NetworkManager()
3535
//加载所有的类型
3636
LoadMessageAttribute();
3737
}
38+
3839

39-
public void SetPort(int port)
40+
public void SetPort(int port)
4041
{
4142
GT.GameMode.Network.SetPort(port);
4243
}
@@ -45,12 +46,12 @@ public void SendMessage(object message, IPEndPoint endPoint)
4546
{
4647
byte[] messageData = _protobufPacker.ToBytes(message);
4748

48-
object[] attribute = message.GetType().GetCustomAttributes(typeof(GT.MessageAttribute), false);
49+
object[] attribute = message.GetType().GetCustomAttributes(typeof(MessageAttribute), false);
4950
if (attribute.Length <= 0)
5051
throw new GT.GamekException("class not found MessageAttribute");
51-
GT.MessageAttribute mgAttribute = (GT.MessageAttribute)attribute[0];
52-
53-
GT.GameMode.Network.SendMessage(mgAttribute.TypeCode, messageData, endPoint);
52+
MessageAttribute mgAttribute = attribute[0] as MessageAttribute;
53+
if(mgAttribute!=null)
54+
GT.GameMode.Network.SendMessage(mgAttribute.TypeCode, messageData, endPoint);
5455
}
5556

5657
public Task<T> Call<T>(IRequest message, IPEndPoint endPoint) where T : class, IResponse
@@ -72,21 +73,27 @@ private void LoadMessageAttribute()
7273
foreach (var item in types)
7374
{
7475
//get messagehandler
75-
object[] attribute = item.GetCustomAttributes(typeof(GT.MessageHandlerAttribute), false);
76+
object[] attribute = item.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
7677
if (attribute.Length > 0 && !item.IsAbstract)
7778
{
78-
GT.MessageHandlerAttribute msHanderAttibute = (GT.MessageHandlerAttribute)attribute[0];
79-
if (!_messageHandler.ContainsKey(msHanderAttibute.TypeMessage.FullName))
80-
_messageHandler[msHanderAttibute.TypeMessage.FullName] = new List<MessageHandlerBase>();
81-
_messageHandler[msHanderAttibute.TypeMessage.FullName].Add((MessageHandlerBase)Activator.CreateInstance(item));
79+
MessageHandlerAttribute msHanderAttibute = attribute[0] as MessageHandlerAttribute;
80+
if(msHanderAttibute!=null)
81+
{
82+
Type handType = Type.GetType(msHanderAttibute.TypeMessage);
83+
if (!_messageHandler.ContainsKey(handType))
84+
_messageHandler[handType] = new List<MessageHandlerBase>();
85+
_messageHandler[handType]
86+
.Add((MessageHandlerBase) Activator.CreateInstance(item));
87+
}
8288
}
8389

8490
//get message
85-
attribute = item.GetCustomAttributes(typeof(GT.MessageAttribute), false);
91+
attribute = item.GetCustomAttributes(typeof(MessageAttribute), false);
8692
if (attribute.Length > 0 && !item.IsAbstract)
8793
{
88-
GT.MessageAttribute msAttibute = (GT.MessageAttribute)attribute[0];
89-
_messageCodeType[msAttibute.TypeCode] = item;
94+
MessageAttribute msAttibute = attribute[0] as MessageAttribute;
95+
if(msAttibute!=null)
96+
_messageCodeType[msAttibute.TypeCode] = item;
9097
}
9198

9299
}
@@ -111,9 +118,9 @@ private void ReceiveMsgData(ushort typeCode, byte[] msgData)
111118
}
112119
}
113120
//消息处理类
114-
else if (_messageHandler.ContainsKey(type.FullName))
121+
else if (_messageHandler.ContainsKey(type))
115122
{
116-
foreach (var item in _messageHandler[type.FullName])
123+
foreach (var item in _messageHandler[type])
117124
item.Handle(message);
118125
}
119126
}

HotFix/HotFix/GameFramework/Network/TestMessageHandle.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

HotFix/HotFix/HotFix.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@
6868
<Compile Include="GameFramework\Event\GameEventArgs.cs" />
6969
<Compile Include="GameFramework\Network\IRequest.cs" />
7070
<Compile Include="GameFramework\Network\IResponse.cs" />
71+
<Compile Include="GameFramework\Network\MessageAttribute.cs" />
72+
<Compile Include="GameFramework\Network\MessageHandlerAttribute.cs" />
7173
<Compile Include="GameFramework\Network\MessageHandlerBase.cs" />
7274
<Compile Include="GameFramework\Network\NetworkManager.cs" />
7375
<Compile Include="GameFramework\Network\ProtobufPacker.cs" />
74-
<Compile Include="GameFramework\Network\TestMessageHandle.cs" />
7576
<Compile Include="GameFramework\State\GameState.cs" />
7677
<Compile Include="GameFramework\State\GameStateAttribute.cs" />
7778
<Compile Include="GameFramework\State\GameStateContext.cs" />

HotFix/HotFix/bin/Debug/HotFix.dll

512 Bytes
Binary file not shown.

HotFix/HotFix/bin/Debug/HotFix.pdb

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)