From 75a6977afbe75d1cd9d09eeed97f3fac3cd76545 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Fri, 19 May 2023 00:06:43 +0800 Subject: [PATCH] [+] ProtobufUtility [+] ProtobufUtility --- .../GameLogic/Network/ProtobufUtility.cs | 143 ++++++++++++++++++ .../GameLogic/Network/ProtobufUtility.cs.meta | 3 + 2 files changed, 146 insertions(+) create mode 100644 Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs create mode 100644 Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs.meta diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs new file mode 100644 index 00000000..76825c17 --- /dev/null +++ b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs @@ -0,0 +1,143 @@ +using System; +using System.ComponentModel; +using System.IO; +using Google.Protobuf; + + +/// +/// ProtoBuf工具 +/// +public partial class ProtobufUtility +{ + /// + /// 消息压入内存流。 + /// + /// + /// + public static void ToStream(object message, MemoryStream stream) + { + ((IMessage)message).WriteTo(stream); + } + + /// + /// 比特流解析。 + /// + /// + /// + /// + /// + /// + public static object FromBytes(Type type, byte[] bytes, int index, int count) + { + object message = Activator.CreateInstance(type); + ((IMessage)message).MergeFrom(bytes, index, count); + ISupportInitialize iSupportInitialize = message as ISupportInitialize; + if (iSupportInitialize == null) + { + return message; + } + + iSupportInitialize.EndInit(); + return message; + } + + /// + /// 比特流解析。 + /// + /// + /// + /// + /// + /// + public static object FromBytes(object instance, byte[] bytes, int index, int count) + { + object message = instance; + ((IMessage)message).MergeFrom(bytes, index, count); + ISupportInitialize iSupportInitialize = message as ISupportInitialize; + if (iSupportInitialize == null) + { + return message; + } + + iSupportInitialize.EndInit(); + return message; + } + + /// + /// 从内存流取出。 + /// + /// + /// + /// + public static object FromStream(Type type, MemoryStream stream) + { + object message = Activator.CreateInstance(type); + ((IMessage)message).MergeFrom(stream.GetBuffer(), (int)stream.Position, (int)stream.Length); + ISupportInitialize iSupportInitialize = message as ISupportInitialize; + if (iSupportInitialize == null) + { + return message; + } + + iSupportInitialize.EndInit(); + return message; + } + + /// + /// 从内存流取出。 + /// + /// + /// + /// + public static object FromStream(object message, MemoryStream stream) + { + // TODO 这个message最好从池中获取,减少gc + ((IMessage)message).MergeFrom(stream.GetBuffer(), (int)stream.Position, (int)stream.Length); + ISupportInitialize iSupportInitialize = message as ISupportInitialize; + if (iSupportInitialize == null) + { + return message; + } + + iSupportInitialize.EndInit(); + return message; + } + + public static byte[] ToBytes(object message) + { + return ((IMessage)message).ToByteArray(); + } + + /// + /// 序列化protobuf + /// + /// + /// + public static byte[] Serialize(object message) + { + return ((IMessage)message).ToByteArray(); + } + + /// + /// 反序列化protobuf + /// + /// + /// + /// + public static T Deserialize(byte[] dataBytes) where T : IMessage, new() + { + T msg = new T(); + msg = (T)msg.Descriptor.Parser.ParseFrom(dataBytes); + return msg; + } + + public static int GetHighOrder(int cmdMerge) + { + return cmdMerge >> 16; + } + + public static int GetLowOrder(int cmdMerge) + { + return cmdMerge & 65535; + } +} \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs.meta b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs.meta new file mode 100644 index 00000000..400e157c --- /dev/null +++ b/Assets/GameScripts/HotFix/GameLogic/Network/ProtobufUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 972ca4545003463d8710de956f0fde66 +timeCreated: 1682047511 \ No newline at end of file