private void OnSaveLevel(LevelFile level) { level = level.Clone(); byte[] bytes = ByteSerializer.Serialize(level); string path = level.isOfficial ? officialLevelsDir_FullPath : customLevelsDir; path = Path.Combine(path, level.fileName) + (level.isOfficial ? ".txt" : ".lvl"); File.WriteAllBytes(path, bytes); if (level.isOfficial) { RegisterLevel(officialLevels, level); } else { RegisterLevel(customLevels, level); } EventManager.Raise(new LevelSavedEvent(level)); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif }
public void EmptyObjectsAndNulls() { var serializer = new ByteSerializer(); var model1 = new NoPropertiesModel(); var bytes1 = serializer.Serialize(model1); var model2 = serializer.Deserialize <NoPropertiesModel>(bytes1); Assert.IsNotNull(model2); var model3 = new NoPropertiesModel[] { new NoPropertiesModel(), new NoPropertiesModel() }; var bytes3 = serializer.Serialize(model3); var model4 = serializer.Deserialize <NoPropertiesModel[]>(bytes3); Assert.AreEqual(2, model4.Length); Assert.IsNotNull(model4[0]); Assert.IsNotNull(model4[1]); var model5 = new NoPropertiesModel[] { null, null }; var bytes5 = serializer.Serialize(model5); var model6 = serializer.Deserialize <NoPropertiesModel[]>(bytes5); Assert.AreEqual(2, model6.Length); Assert.IsNull(model6[0]); Assert.IsNull(model6[1]); var bytes7 = serializer.Serialize(null); var model7 = serializer.Deserialize <NoPropertiesModel>(bytes7); Assert.IsNull(model7); var bytes8 = serializer.Serialize(null); var model8 = serializer.Deserialize <NoPropertiesModel[]>(bytes8); Assert.IsNull(model8); var model9 = new NoPropertiesModel[0]; var bytes9 = serializer.Serialize(model9); var model10 = serializer.Deserialize <IEnumerable <NoPropertiesModel> >(bytes9); Assert.AreEqual(model9.GetType(), model10?.GetType()); var model11 = new ArrayChainModel[] { new ArrayChainModel() { ID = Guid.NewGuid(), Children = null } }; var bytes11 = serializer.Serialize(model11); var model12 = serializer.Deserialize <ArrayChainModel[]>(bytes11); Assert.AreEqual(model11[0].Children, model12[0].Children); }
public static NetOutgoingMessage CreateFoundGameMessage(NetPeer sender, FoundGame foundGame) { NetOutgoingMessage output = sender.CreateMessage(); output.Write(ByteSerializer.ObjectToByteArray(foundGame)); return(output); }
public string GetString() { var ms = new MemoryStream(NumberOfBytes()); var bw = new BinaryWriter(ms); bw.Write(Version); // Version 1 data bw.Write(name); bw.Write(inventory.Length); foreach (var item in inventory) { bw.Write(item.GetString()); } bw.Write(location.x); bw.Write(location.y); var saveData = new ByteSerializer(ms.GetBuffer()); bw.Close(); ms.Close(); return(saveData.GetAsString()); }
public void Deserialize(byte[] bytes, ref int offset) { this.username = ByteSerializer.DeserializeString(bytes, ref offset); this.position = ByteSerializer.DeserializeVector3(bytes, ref offset); this.pointerMode = (PointerMode)ByteSerializer.DeserializeInt(bytes, ref offset); this.pointerColor = ByteSerializer.DeserializeColor(bytes, ref offset); }
private static async Task SaveState() { await stateLock.WaitAsync(); try { var path = $"{GetAssemblyLocation()}\\relaystate.dat"; var infos = new List <ServiceInfo>(); foreach (var service in servicesByUrl.Values) { var providerTypes = servicesByProviderType.Where(x => x.Value.Any(y => y.Key == service.Url)).Select(x => x.Key).ToArray(); var info = new ServiceInfo() { Url = service.Url, ProviderTypes = providerTypes }; infos.Add(info); } var infoArray = infos.ToArray(); try { using (var file = File.Create(path)) { var serializer = new ByteSerializer(); await serializer.SerializeAsync(file, infoArray); } } catch { } } finally { stateLock.Release(); } }
/// <summary> /// 根据key获得值 /// </summary> /// <param name="key"></param> /// <returns></returns> public T Get <T>(string key) { //默认值 var value = default(T); if (_Database == null) { return(value); } try { //cache值 var cacheValue = _Database.StringGet(key); if (!cacheValue.IsNull) { byte[] bytes = (byte[])cacheValue; if (bytes != null && bytes.Length > 0) { value = ByteSerializer.Deserialize <T>(bytes); } if (value == null) { Remove(key); } return(value); } } catch (Exception ex) { //new ECFException(ex, "Cache-Redis"); } return(value); }
/// <summary> /// Serialisiert das Objekt /// </summary> /// <param name="value">Instanz, die serialisiert werden soll</param> /// <returns>Serialisierte Instanz in Form eines byte-Arrays</returns> public byte[] Serialize(TFor value) { var byteSerializer = new ByteSerializer(); Serialize(value, byteSerializer); return(byteSerializer.ToByteArray()); }
/// <summary> /// 接收到请求的处理 /// </summary> /// <param name="info"></param> private void client_OnMsgReceived(byte[] info) { SocketRequest requestMsg = ByteSerializer.DeserializeByte <SocketMsgBase>(info) as SocketRequest; if (requestMsg != null) { Console.WriteLine("子系统{0}请求执行{1}方法", requestMsg.SubSystem, requestMsg.RequestMethod); SocketResponse response = new SocketResponse(); string result = string.Empty; try { result = OperationFactory.Execute(requestMsg.Parameters, requestMsg.RequestMethodType); response.Result = result; } catch (Exception ex) { response.ErrorMsg = "执行遇到异常"; } response.MethodName = requestMsg.MethodName; response.Sender = requestMsg.Target; response.SubSystem = requestMsg.SubSystem; response.Target = requestMsg.Sender; response.MsgType = TcpMsgDataType.Relay; client.Send(ByteSerializer.ObjectToByte(response)); Console.WriteLine("执行成功,已返回结果"); } }
public void StringArrayOfArrayThing() { var serializer = new ByteSerializer(); var model1 = new string[][] { new string[] { "a", "b", "c" }, new string[] { "d", "e", "f" } }; var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <string[][]>(bytes); }
public object SetFromString(string textData) { var dataLoader = new ByteSerializer(textData); var data = dataLoader.GetAsBytes(); if (data == null || data.Length <= 1) { return(this); } var ms = new MemoryStream(data); var br = new BinaryReader(ms); var version = br.ReadInt32(); name = br.ReadString(); inventory = new Item[br.ReadInt32()]; for (var i = 0; i < inventory.Length; i++) { inventory[i] = (Item) new Item().SetFromString(br.ReadString()); } location = new Vector2Int(br.ReadInt32(), br.ReadInt32()); br.Close(); ms.Close(); return(this); }
public static async Task <Exception> DeserializeExceptionAsync(ContentType contentType, Stream stream) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(); var model = await serializer.DeserializeAsync <ExceptionModel>(stream); return(new Exception(model.Message)); } case ContentType.Json: { var model = await System.Text.Json.JsonSerializer.DeserializeAsync <ExceptionModel>(stream); return(new Exception(model.Message)); } case ContentType.JsonNameless: { using (MemoryStream ms = new MemoryStream()) { await stream.CopyToAsync(ms); var model = JsonSerializer.DeserializeNameless <ExceptionModel>(ms.ToArray()); return(new Exception(model.Message)); } } default: throw new NotImplementedException(); } }
public static async Task <T> DeserializeAsync <T>(ContentType contentType, Stream stream) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(true, false, true); return(await serializer.DeserializeAsync <T>(stream)); } case ContentType.Json: { return(await System.Text.Json.JsonSerializer.DeserializeAsync <T>(stream)); } case ContentType.JsonNameless: { using (MemoryStream ms = new MemoryStream()) { await stream.CopyToAsync(ms); return(JsonSerializer.DeserializeNameless <T>(ms.ToArray())); } } default: throw new NotImplementedException(); } }
public static async Task SerializeAsync(ContentType contentType, Stream stream, object obj) { switch (contentType) { case ContentType.Bytes: { var serializer = new ByteSerializer(true, false, true); await serializer.SerializeAsync(stream, obj); return; } case ContentType.Json: { await System.Text.Json.JsonSerializer.SerializeAsync(stream, obj); return; } case ContentType.JsonNameless: { var bytes = JsonSerializer.SerializeNamelessBytes(obj); #if NETSTANDARD2_0 await stream.WriteAsync(bytes, 0, bytes.Length); #else await stream.WriteAsync(bytes.AsMemory()); #endif return; } default: throw new NotImplementedException(); } }
public static NetOutgoingMessage CreateGridDataMessage(this Grid grid, NetPeer sender) { NetOutgoingMessage output = sender.CreateMessage(); output.Write(PacketType.GridData); output.Write(ByteSerializer.ObjectToByteArray(grid)); return(output); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); bs.Serialize (EmgData); return bs.GetBuffer (); }
public byte[] Serialize() { List <byte> list = new List <byte>(); list.AddRange(ByteSerializer.SerializeString(this.username)); list.AddRange(ByteSerializer.SerializeString(this.reason)); return(list.ToArray()); }
public static NetOutgoingMessage CreatePlayerDisconnectedMessage(this PlayerConnection client, NetPeer sender) { NetOutgoingMessage output = sender.CreateMessage(); output.Write(PacketType.PlayerDisconnected); output.Write(ByteSerializer.ObjectToByteArray(client.player)); return(output); }
public void Deserialize(byte[] bytes, ref int offset) { this.action = (actionType)ByteSerializer.DeserializeInt(bytes, ref offset); this.content = ByteSerializer.DeserializeByteArray(bytes, ref offset); this.username = ByteSerializer.DeserializeString(bytes, ref offset); this.metadata = ByteSerializer.DeserializeString(bytes, ref offset); this.playSound = ByteSerializer.DeserializeBool(bytes, ref offset); }
public void Deserialize(byte[] bytes, ref int offset) { this.action = (ConfigAction)ByteSerializer.DeserializeInt(bytes, ref offset); this.userCap = ByteSerializer.DeserializeInt(bytes, ref offset); this.acceptingConnections = ByteSerializer.DeserializeBool(bytes, ref offset); this.newPassword = ByteSerializer.DeserializeString(bytes, ref offset); this.lobbyMode = (LobbyMode)ByteSerializer.DeserializeInt(bytes, ref offset); }
public Bytes Serialize() { ByteSerializer bs = new ByteSerializer(); bs.Serialize(EmgData); return(bs.GetBuffer()); }
protected override void Serialize (ByteSerializer bs) { foreach (VibrationStep step in Steps) { bs.Serialize (step.Duration); bs.Serialize (step.Strength); } }
protected override void Serialize(ByteSerializer bs) { foreach (VibrationStep step in Steps) { bs.Serialize(step.Duration); bs.Serialize(step.Strength); } }
public void Boxing() { var serializer = new ByteSerializer(false, true); var model1 = Factory.GetBoxingModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestBoxingModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void ByPropertyName() { var serializer = new ByteSerializer(true); var model1 = Factory.GetAllTypesModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <AllTypesReversedModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void Types() { var serializer = new ByteSerializer(); var model1 = Factory.GetAllTypesModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <AllTypesModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void BySerializerIndex() { var serializer = new ByteSerializer(); var model1 = Factory.GetSerializerIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerIndexModel2>(bytes); Factory.AssertAreEqual(model1, model2); }
public void ByLargeSerializerIndex() { var serializer = new ByteSerializer(false, false, false, ByteSerializerIndexSize.UInt16); var model1 = Factory.GetSerializerLongIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerLongIndexModel>(bytes); Factory.AssertAreEqual(model1, model2); }
public void ByIgnoreSerailizerIndex() { var serializer = new ByteSerializer(false, false, true); var model1 = Factory.GetSerializerIndexModel(); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <TestSerializerIndexModel2>(bytes); Factory.AssertAreNotEqual(model1, model2); }
public void ExceptionObject() { var serializer = new ByteSerializer(); var model1 = new Exception("bad things happened"); var bytes = serializer.Serialize(model1); var model2 = serializer.Deserialize <Exception>(bytes); Assert.AreEqual(model1.Message, model2.Message); }
public byte[] Serialize() { List <byte> list = new List <byte>(); list.AddRange(ByteSerializer.SerializeString(this.message)); list.AddRange(ByteSerializer.SerializeString(this.username)); list.AddRange(ByteSerializer.SerializeColor(this.nameColor)); return(list.ToArray()); }
public void Deserialize(byte[] bytes, ref int offset) { this.userCount = ByteSerializer.DeserializeInt(bytes, ref offset); this.userCap = ByteSerializer.DeserializeInt(bytes, ref offset); this.acceptingConnections = ByteSerializer.DeserializeBool(bytes, ref offset); this.lobbyMode = (LobbyMode)ByteSerializer.DeserializeInt(bytes, ref offset); this.isFrozen = ByteSerializer.DeserializeBool(bytes, ref offset); this.playerNames = ByteSerializer.DeserializeStrings(bytes, ref offset); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); bs.Serialize (Event); bs.Serialize (TapDirection); bs.Serialize (TapCount); return bs.GetBuffer (); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); bs.Serialize (Command); bs.Serialize (PayloadSize); this.Serialize (bs); return bs.GetBuffer (); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); // ORDER IS MANDATORY bs.Serialize (Major); bs.Serialize (Minor); bs.Serialize (Patch); bs.Serialize (HardwareRevision); return bs.GetBuffer (); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); // ORDER IS MANDATORY bs.Serialize (OrientationW); bs.Serialize (OrientationX); bs.Serialize (OrientationY); bs.Serialize (OrientationZ); bs.Serialize (Accelerometer); bs.Serialize (Gyroscope); return bs.GetBuffer (); }
public Bytes Serialize () { ByteSerializer bs = new ByteSerializer (); // ORDER IS MANDATORY bs.Serialize (SerialNumber); bs.Serialize (UnlockPose); bs.Serialize (ActiveClassifierType); bs.Serialize (ActiveClassifierIndex); bs.Serialize (HasCustomClassifier); bs.Serialize (StreamIndicating); bs.Serialize (Sku); bs.Serialize (Reserved); return bs.GetBuffer (); }
protected override void Serialize (ByteSerializer bs) { bs.Serialize (EmgMode); bs.Serialize (ImuMode); bs.Serialize (ClassifierMode); }
protected override void Serialize (ByteSerializer bs) { bs.Serialize (SleepMode); }
protected override void Serialize (ByteSerializer bs) { // no payload }
protected abstract void Serialize (ByteSerializer bs);
protected override void Serialize (ByteSerializer bs) { bs.Serialize (UserAction); }
public void WriteClientCharacteristicConfiguration (Bytes uuid, BleCCCValue value) { BlePeripheralCharacteristic characteristic = this.PeripheralMap.FindCharacteristicByUUID (uuid); if (!characteristic.HasCCC) { throw new ArgumentException ($"Client characteristic {uuid} doesn't have a configuration attribute!"); } ByteSerializer bs = new ByteSerializer (); bs.Serialize (value); ushort attrHandle = characteristic.HandleCCC; this.WriteAttribute (attrHandle, bs.GetBuffer ()); }
protected override void Serialize (ByteSerializer bs) { bs.Serialize (Vibrate); }
protected override void Serialize (ByteSerializer bs) { bs.Serialize (Unlock); }