| 
									
										
										
										
											2023-08-30 12:26:51 +00:00
										 |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.IO; | 
					
						
							|  |  |  |  | using ProtoBuf.Meta; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-26 03:03:04 +00:00
										 |  |  |  | namespace DofLibrary | 
					
						
							| 
									
										
										
										
											2023-08-30 12:26:51 +00:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     public interface ISupportInitialize | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         void BeginInit(); | 
					
						
							|  |  |  |  |         void EndInit(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public interface IDisposable | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         void Dispose(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public static class ProtobufHelper | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  | 	    public static void Init() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public static object FromBytes(Type type, byte[] bytes, int index, int count) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  | 	        using (MemoryStream stream = new MemoryStream(bytes, index, count)) | 
					
						
							|  |  |  |  | 	        { | 
					
						
							|  |  |  |  | 		        object o = RuntimeTypeModel.Default.Deserialize(stream, null, type); | 
					
						
							|  |  |  |  | 		        if (o is ISupportInitialize supportInitialize) | 
					
						
							|  |  |  |  | 		        { | 
					
						
							|  |  |  |  | 			        supportInitialize.EndInit(); | 
					
						
							|  |  |  |  | 		        } | 
					
						
							|  |  |  |  | 		        return o; | 
					
						
							|  |  |  |  | 	        } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public static byte[] ToBytes(object message) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  | 	        using (MemoryStream stream = new MemoryStream()) | 
					
						
							|  |  |  |  | 	        { | 
					
						
							|  |  |  |  | 		        ProtoBuf.Serializer.Serialize(stream, message); | 
					
						
							|  |  |  |  | 		        return stream.ToArray(); | 
					
						
							|  |  |  |  | 	        } | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public static void ToStream(object message, MemoryStream stream) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             ProtoBuf.Serializer.Serialize(stream, message); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         public static object FromStream(Type type, MemoryStream stream) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  | 	        object o = RuntimeTypeModel.Default.Deserialize(stream, null, type); | 
					
						
							|  |  |  |  | 	        if (o is ISupportInitialize supportInitialize) | 
					
						
							|  |  |  |  | 	        { | 
					
						
							|  |  |  |  | 		        supportInitialize.EndInit(); | 
					
						
							|  |  |  |  | 	        } | 
					
						
							|  |  |  |  | 	        return o; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |