/// <summary> /// Parses the resource into a strongly-typed object. /// </summary> /// <typeparam name="TData">The type of the data object to deserialise the ProtoContract file into.</typeparam> /// <param name="fileName">Name of the file to parse.</param> /// <returns>An instance of <see cref="TData" />, populated with deserialised data from the ProtoContract file.</returns> public static TData ParseBinaryResourceAs <TData>(string fileName) where TData : class { var assembly = typeof(TData).Assembly; var stream = GetResourceStream(assembly, fileName); using var reader = new BinaryReader(stream); return(ProtoEx.Deserialise <TData>(reader.ReadBytes((int)stream.Length))); }
// Use this for initialization void Start() { TestProtoClass test = new TestProtoClass(); test.dependenies = new List <string>() { "123", "asd", "zxc", "dfg" }; var bytes = ProtoEx.Serialize(test); var testFromProto = ProtoEx.DeSerialize <TestProtoClass>(bytes); Debug.Log(testFromProto.dependenies.ConverToString()); }
/// <summary> /// 包创建工厂 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="value"></param> /// <returns></returns> public static Package MakePakage <T>(int id, T value) { Package pkg = new Package(); pkg.eventId = id; pkg.data = ProtoEx.Serialize <T>(value); pkg.dataLength = pkg.data.Length; pkg.totalLength = pkg.dataLength + idLength + headLength; byte[] lengthData = BitConverter.GetBytes(pkg.totalLength); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthData); } byte[] eventIdData = BitConverter.GetBytes(pkg.eventId); pkg.totalData = lengthData.Concat(eventIdData).Concat(pkg.data); return(pkg); }
/// <summary> /// 包创建工厂 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <param name="value"></param> /// <returns></returns> public static Package MakePakage <T>(int id, int channelId, T value, SendType sendType) { //防止Id溢出 _currentRequestId = (_currentRequestId + 1) % int.MaxValue; Package pkg = new Package(); pkg.eventId = id; pkg.channelId = channelId; pkg.requestId = _currentRequestId; if (value != null) { pkg.data = ProtoEx.Serialize <T>(value); } else { pkg.data = new byte[0]; } pkg.dataLength = pkg.data.Length; pkg.totalLength = pkg.dataLength + idLength + headLength + channelIdLength + requestIdLength; byte[] lengthData = BitConverter.GetBytes(pkg.totalLength); if (BitConverter.IsLittleEndian && sendType == SendType.TCP) { Array.Reverse(lengthData); } byte[] eventIdData = BitConverter.GetBytes(pkg.eventId); byte[] channelIdData = BitConverter.GetBytes(pkg.channelId); byte[] requestIdData = BitConverter.GetBytes(pkg.requestId); pkg.totalData = lengthData.Concat(eventIdData).Concat(channelIdData).Concat(requestIdData).Concat(pkg.data); return(pkg); }
public void ReadProto <T>(byte[] bytes) { TestProtoClass test = ProtoEx.Read <TestProtoClass>(bytes); }
public byte[] Json2Proto <T>(string json) { var obj = JsonMapper.ToObject <T>(json); return(ProtoEx.Serialize(obj)); }
//获取值 public T GetValue <T>() { return(ProtoEx.DeSerialize <T>(data)); }
/// <summary> /// Deserialises the specified file as a strongly-typed object. /// </summary> /// <typeparam name="TModel">The type of object to deserialise into.</typeparam> public TModel ParseAsProtoObject <TModel>() where TModel : class, new() { var bytes = _fileOnDisk.Exists ? File.ReadAllBytes(_fileOnDisk.FullName) : new byte[] { }; return(ProtoEx.Deserialise <TModel>(bytes)); }
/// <summary> /// Serialises the specified instance, and saves the resulting ProtoContract to file. /// </summary> public void SaveAsProtoContract <TModel>(TModel instance) where TModel : class, new() { SaveBinaryToDisk(ProtoEx.Serialise(instance)); }