/// <summary> /// Инициализация "Преобразователя" данных. /// </summary> public ConverterBase(string inputData, IConversionFactory <T> cf) { this._parser = cf.CreateParser(); this._parser.InputData = inputData; this._mapper = cf.CreateMapper(); }
public Deserializer(ILogger logger, IReflectionCache reflectionCache, IConversionFactory conversionFactory) { _logger = logger; _packetReader = new PacketReader(); _reflectionCache = reflectionCache; _conversionFactory = conversionFactory; }
private static void AddInitialData <T, K>(ObservableCollection <K> slave, IEnumerable <T> master, IConversionFactory <T, K> conversionFactory) { foreach (var item in master) { slave.Add(conversionFactory.Build(item)); } }
protected override Guid ToObject(string value, Type type, IConversionFactory factory) { if (!Guid.TryParse(value, out Guid id)) { throw new ConversionException(value, type); } return(id); }
protected override byte?ToObject(string value, Type type, IConversionFactory factory) { if (value == "-1" || value == "-") { return(null); } return((byte)factory.ToObject(value, typeof(byte))); }
protected override int ToObject(string value, Type type, IConversionFactory factory) { if (!int.TryParse(value, out int parsed)) { throw new ConversionException(value, type); } return(parsed); }
protected override short?ToObject(string value, Type type, IConversionFactory factory) { if (value == "-1") { return(null); } return((short)factory.ToObject(value, typeof(short))); }
protected override Enum ToObject(string value, Type type, IConversionFactory factory) { try { return((Enum)Enum.Parse(type, value)); } catch (Exception) { throw new ConversionException(value, type); } }
protected override string ToString(IList value, Type type, IConversionFactory factory) { Type valueType = value.GetType().GenericTypeArguments.FirstOrDefault(); var sb = new StringBuilder(); foreach (object entry in value) { sb.Append(factory.ToString(entry, valueType)).Append(" "); } return(sb.ToString().TrimEnd()); }
protected override IList ToObject(string value, Type type, IConversionFactory factory) { Type generic = type.GenericTypeArguments.FirstOrDefault(); Type listType = typeof(List <>).MakeGenericType(type); var list = (IList)Activator.CreateInstance(listType); string[] values = value.Split(' '); foreach (string entry in values) { list.Add(factory.ToObject(entry, generic)); } return(list); }
protected override RaidListPacket ToObject(string value, Type type, IConversionFactory factory) { string[] split = value.Split(' '); int minimumLevel = Convert.ToInt32(split[0]); int maximumLevel = Convert.ToInt32(split[1]); int raidId = Convert.ToInt32(split[2]); var playerData = new List <RaidPlayerData>(); int start = 3; if (int.TryParse(split[start], out int temp)) { start++; raidId = temp; } for (int i = start; i < split.Length; i++) { string player = split[i]; string[] splittedPlayer = player.Split('.'); int level = Convert.ToInt32(splittedPlayer[0]); var classType = (ClassType)Convert.ToInt32(splittedPlayer[2]); string name = splittedPlayer[4]; int championLevel = Convert.ToInt32(splittedPlayer[7]); int id = Convert.ToInt32(splittedPlayer[6]); playerData.Add(new RaidPlayerData { Level = level, ChampionLevel = championLevel, Id = id, Name = name, Class = classType }); } return(new RaidListPacket { Data = playerData, MaximumLevel = maximumLevel, MinimumLevel = minimumLevel, RaidId = raidId }); }
protected override RaidPacket ToObject(string value, Type type, IConversionFactory factory) { string[] splitted = value.Split(' '); var packet = new RaidPacket { Type = RaidPacketType.Unknown }; if (splitted[0] == "1") { if (splitted[1] == "0") { packet.Type = RaidPacketType.Start; } if (splitted[1] == "1") { packet.Type = RaidPacketType.Initialization; } } if (splitted[0] == "2") { if (splitted[1] == "-1") { packet.Type = RaidPacketType.Left; } else { packet.Type = RaidPacketType.RaidLeader; packet.LeaderId = (long)factory.ToObject(splitted[1], typeof(long)); } } if (splitted[0] == "3") { packet.Type = RaidPacketType.PlayerHealths; } return(packet); }
protected override InvPacket ToObject(string value, Type type, IConversionFactory factory) { string[] split = value.Split(' '); var subs = new List <IvnSubPacket>(); var bagType = (BagType)Convert.ToInt32(split[0]); for (int i = 1; i < split.Length; i++) { string[] itemData = split[i].Split('.'); int slot = Convert.ToInt32(itemData[0]); int vnum = Convert.ToInt32(itemData[1]); var sub = new IvnSubPacket { Slot = slot, VNum = vnum }; if (bagType == BagType.EQUIPMENT) { short rarity = Convert.ToInt16(itemData[2]); byte upgrade = Convert.ToByte(itemData[3]); sub.RareAmount = rarity; sub.UpgradeDesign = upgrade; } else { int amount = Convert.ToInt32(itemData[2]); sub.RareAmount = amount; } subs.Add(sub); } return(new InvPacket { BagType = bagType, SubPackets = subs }); }
protected override InPacket ToObject(string value, Type type, IConversionFactory factory) { string[] split = value.Split(' '); var entityType = (EntityType)Convert.ToInt32(split[0]); int startIndex = entityType == EntityType.PLAYER ? 3 : 2; var packet = new InPacket { EntityType = entityType, Name = entityType == EntityType.PLAYER ? split[1] : string.Empty, Vnum = entityType != EntityType.PLAYER ? Convert.ToInt32(split[1]) : 0, EntityId = Convert.ToInt32(split[startIndex]), PositionX = Convert.ToInt16(split[startIndex + 1]), PositionY = Convert.ToInt16(split[startIndex + 2]), Direction = entityType != EntityType.GROUND_ITEM ? Convert.ToByte(split[startIndex + 3]) : (byte)0 }; string content = string.Join(" ", split.Skip(startIndex + (entityType == EntityType.GROUND_ITEM ? 3 : 4))); switch (entityType) { case EntityType.MONSTER: case EntityType.NPC: packet.NpcSubPacket = (InNpcSubPacket)factory.ToObject(content, typeof(InNpcSubPacket)); break; case EntityType.PLAYER: packet.PlayerSubPacket = (InPlayerSubPacket)factory.ToObject(content, typeof(InPlayerSubPacket)); packet.PlayerSubPacket.Level = byte.Parse(split.Skip(27).SkipWhile(x => x.Any(char.IsLetter)).Skip(5).First()); break; case EntityType.GROUND_ITEM: packet.DropSubPacket = (InDropSubPacket)factory.ToObject(content, typeof(InDropSubPacket)); break; default: throw new InvalidOperationException(); } return(packet); }
protected override IList ToObject(string value, Type type, IConversionFactory factory) { Type generic = type.GenericTypeArguments.FirstOrDefault(); Type listType = typeof(List <>).MakeGenericType(type); var list = (IList)Activator.CreateInstance(listType); char separator = ' '; if (typeof(IPacket).IsAssignableFrom(generic)) { separator = '^'; } string[] values = value.Split(separator); foreach (string entry in values) { list.Add(factory.ToObject(entry, generic)); } return(list); }
protected override SkiPacket ToObject(string value, Type type, IConversionFactory factory) { var skills = new List <int>(); string[] split = value.Split(' '); foreach (string entry in split) { string[] skillId = entry.Split('|'); if (skillId.Length > 0) { skills.Add(Convert.ToInt32(skillId[0])); continue; } skills.Add(Convert.ToInt32(entry)); } return(new SkiPacket { Skills = skills }); }
protected override IvnPacket ToObject(string value, Type type, IConversionFactory factory) { string[] values = value.Split(' '); string[] itemData = values[1].Split('.'); var bagType = (BagType)Convert.ToInt32(values[0]); int slot = Convert.ToInt32(itemData[0]); int vnum = Convert.ToInt32(itemData[1]); var sub = new IvnSubPacket { Slot = slot, VNum = vnum }; if (bagType == BagType.EQUIPMENT) { short rarity = Convert.ToInt16(itemData[2]); byte upgrade = Convert.ToByte(itemData[3]); sub.RareAmount = rarity; sub.UpgradeDesign = upgrade; } else { int amount = Convert.ToInt32(itemData[2]); sub.RareAmount = amount; } return(new IvnPacket { BagType = bagType, SubPacket = sub }); }
protected override Qnamli2Packet ToObject(string value, Type type, IConversionFactory factory) { var packet = new Qnamli2Packet(); string[] splitted = value.Split(' '); packet.Command = (string)factory.ToObject(splitted[1], typeof(string)); packet.Type = (Game18NConstString)factory.ToObject(splitted[2], typeof(Game18NConstString)); packet.ParametersCount = (int)factory.ToObject(splitted[3], typeof(int)); packet.Parameters = new string[packet.ParametersCount]; for (int i = 0; i < packet.ParametersCount; i++) { if (i + 4 >= splitted.Length) { break; } packet.Parameters[i] = splitted[i + 4]; } return(packet); }
protected override string ToString(int value, Type type, IConversionFactory factory) => value.ToString();
object IConverter.ToObject(string value, Type type, IConversionFactory factory) => ToObject(value, type, factory);
protected abstract T ToObject(string value, Type type, IConversionFactory factory);
protected abstract string ToString(T value, Type type, IConversionFactory factory);
protected override string ToString(bool value, Type type, IConversionFactory factory) => value ? "1" : "0";
public static void SlaveTo <T, K>(this ObservableCollection <K> slave, ObservableCollection <T> master, IConversionFactory <T, K> conversionFactory) { AddInitialData(slave, master, conversionFactory); master.CollectionChanged += (sender, args) => { if (args.Action == NotifyCollectionChangedAction.Add) { AddItems(slave, args, conversionFactory); } else if (args.Action == NotifyCollectionChangedAction.Remove) { RemoveItems(slave, args); } else if (args.Action == NotifyCollectionChangedAction.Reset) { slave.Clear(); } }; }
protected override string ToString(IvnPacket value, Type type, IConversionFactory factory) => throw new NotImplementedException();
public Serializer(ILogger logger, IConversionFactory conversionFactory) { _conversionFactory = conversionFactory; _logger = logger; }
protected override string ToString(short?value, Type type, IConversionFactory factory) => value == null ? "-1" : factory.ToString(value, typeof(short));
protected override bool ToObject(string value, Type type, IConversionFactory factory) => value == "1";
protected override string ToObject(string value, Type type, IConversionFactory factory) => value;
private static void AddItems <T, K>(ObservableCollection <K> slave, NotifyCollectionChangedEventArgs args, IConversionFactory <T, K> conversionFactory) { foreach (var newItem in args.NewItems) { slave.Add(conversionFactory.Build((T)newItem)); } }