public GenericChildKeyStrategy(InformationHandlingFlags typeHandling, [NotNull] ITypeSerializerStrategy <TKeyType> keyTypeSerializerStrategy)
        {
            if (keyTypeSerializerStrategy == null)
            {
                throw new ArgumentNullException(nameof(keyTypeSerializerStrategy));
            }

            int i;

            if (!Enum.IsDefined(typeof(InformationHandlingFlags), typeHandling) && Int32.TryParse(typeHandling.ToString(), out i))
            {
                throw new ArgumentOutOfRangeException(nameof(typeHandling), "Value should be defined in the InformationHandlingFlags enum.");
            }

            /*int i;
             *
             * if (!Enum.IsDefined(typeof(InformationHandlingFlags), typeHandling) && Int32.TryParse(typeHandling.ToString(), out i))
             *      throw new InvalidEnumArgumentException(nameof(typeHandling), (int)typeHandling,
             *              typeof(InformationHandlingFlags));*/

            //Try to get the max value for this type
            DefaultKey = GenericMath.Convert <TKeyType, int>(GenericMath.Convert <int, TKeyType>(Int32.MaxValue));

            typeHandlingFlags         = typeHandling;
            KeyTypeSerializerStrategy = keyTypeSerializerStrategy;
        }
Ejemplo n.º 2
0
 /// <inheritdoc />
 protected override void Load(ContainerBuilder register)
 {
     if (typeof(TKeyType) == typeof(int))
     {
         //Because of active load scene, we have to iterate each scene
         foreach (var registerable in new SceneUiElementEnumerable())
         {
             //Registers the adapter with the specified Key and Service Type.
             register.RegisterInstance(registerable)
             .SingleInstance()
             .Keyed(registerable.RegisterationKey, registerable.UIServiceType);
         }
     }
     else
     {
         //Because of active load scene, we have to iterate each scene
         foreach (var registerable in new SceneUiElementEnumerable())
         {
             //Registers the adapter with the specified Key and Service Type.
             register.RegisterInstance(registerable)
             .SingleInstance()
             .Keyed(GenericMath.Convert <int, TKeyType>(registerable.RegisterationKey), registerable.UIServiceType);
         }
     }
 }
        /// <inheritdoc />
        public override Task WriteAsync(TEnumType value, IWireStreamWriterStrategyAsync dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException(nameof(dest));
            }

            return(serializerStrategy.WriteAsync(GenericMath.Convert <TEnumType, TBaseType>(value), dest));
        }
        /// <inheritdoc />
        public override void Write(TEnumType value, IWireStreamWriterStrategy dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException(nameof(dest));
            }

            serializerStrategy.Write(GenericMath.Convert <TEnumType, TBaseType>(value), dest);
        }
Ejemplo n.º 5
0
        public void ConvertInt32ToDouble()
        {
            int    from = 280;
            double d    = GenericMath.Convert <int, double>(from);
            int    i    = GenericMath.Convert <double, int>(d);

            Assert.AreEqual(i, from);
            Assert.AreEqual(d, (double)i);
        }
        /// <inheritdoc />
        public async Task <int> ReadAsync(IWireStreamReaderStrategyAsync source)
        {
            //Read the key from the stream.
            TKeyType key = await KeyTypeSerializerStrategy
                           .ReadAsync(typeHandlingFlags.HasFlag(InformationHandlingFlags.DontConsumeRead)?source.WithOnlyPeekingAsync() : source)
                           .ConfigureAwait(false);

            return(GenericMath.Convert <TKeyType, int>(key));
        }
Ejemplo n.º 7
0
        public static TValueType GetEnumFieldValue <TValueType>(this IReadonlyEntityDataFieldContainer container, GameObjectField index)
            where TValueType : Enum
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            return(GenericMath.Convert <int, TValueType>(container.GetFieldValue <int>((int)index)));
        }
        /// <inheritdoc />
        public override TEnumType Read(IWireStreamReaderStrategy source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            //TODO: Should be handle exceptions?
            return(GenericMath.Convert <TBaseType, TEnumType>(serializerStrategy.Read(source)));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Helper extension for setting entity data in <see cref="IEntityDataFieldContainer"/>
        /// based on the int value of a specified Enum value <see cref="index"/>.
        /// </summary>
        /// <typeparam name="TEnumType"></typeparam>
        /// <param name="container"></param>
        /// <param name="index"></param>
        /// <param name="guid"></param>
        public static void SetFieldValue <TEnumType>(this IEntityDataFieldContainer container, TEnumType index, NetworkEntityGuid guid)
            where TEnumType : Enum
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.SetFieldValue(GenericMath.Convert <TEnumType, int>(index), guid.RawGuidValue);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Determines the size of the collection from the stream.
        /// </summary>
        public int Size(IWireStreamReaderStrategy reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            //Read the byte size from the stream.
            //Readd the addedsize so that we know how many items are really there.
            return(GenericMath.Convert <TSizeType, int>(SizeTypeSerializerStrategy.Read(reader)) + AddedSize);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Helper extension for setting entity data in <see cref="IEntityDataFieldContainer"/>
        /// based on the int value of a specified Enum value <see cref="index"/>.
        /// </summary>
        /// <typeparam name="TEnumType"></typeparam>
        /// <typeparam name="TValueType"></typeparam>
        /// <param name="container"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public static void SetFieldValue <TEnumType, TValueType>(this IEntityDataFieldContainer container, TEnumType index, TValueType value)
            where TValueType : struct
            where TEnumType : Enum
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            container.SetFieldValue(GenericMath.Convert <TEnumType, int>(index), value);
        }
        /// <inheritdoc />
        public override async Task <TEnumType> ReadAsync(IWireStreamReaderStrategyAsync source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            //TODO: Should be handle exceptions?
            return(GenericMath.Convert <TBaseType, TEnumType>(await serializerStrategy.ReadAsync(source)
                                                              .ConfigureAwait(false)));
        }
Ejemplo n.º 13
0
        private static bool HasOpCodeAttribute <TWireLinkBaseAttributeType, TOpcodeType>(Type t, TOpcodeType opcode)
            where TWireLinkBaseAttributeType : WireDataContractBaseLinkAttribute
        {
            TWireLinkBaseAttributeType attri = t.GetCustomAttribute <TWireLinkBaseAttributeType>();

            if (attri == null)
            {
                return(false);
            }

            return(attri.Index == GenericMath.Convert <TOpcodeType, int>(opcode));
        }
        /// <inheritdoc />
        public int Size(IWireStreamReaderStrategy reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            TSizeType size = sizeSerializer.Read(reader);

            //Using JonSkeets MiscUtils we can convert objects efficently
            return(GenericMath <TSizeType, int> .Convert(size) + AddedSize);
        }
        private void WriteCollectionSizeToField(TContainingType obj)
        {
            //We must first access the collection to get the size and then write it into the field
            //before we try to truly serialize it
            ICollection enumerable = CollectionGetter.Getter(obj) as ICollection;

            if (enumerable == null)
            {
                throw new InvalidOperationException($"Tried to read the size of collection in Type: {typeof(TContainingType).Name} but did not implement {nameof(ICollection)}.");
            }

            MemberSetter.Setter(obj, GenericMath.Convert <int, TSizeType>(enumerable.Count));
        }
        /// <inheritdoc />
        public int Read(IWireStreamReaderStrategy source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            //Read the key from the stream.
            TKeyType key = KeyTypeSerializerStrategy
                           .Read(typeHandlingFlags.HasFlag(InformationHandlingFlags.DontConsumeRead) ? source.WithOnlyPeeking() : source);

            return(GenericMath.Convert <TKeyType, int>(key));
        }
        /// <inheritdoc />
        public async Task <int> SizeAsync(IWireStreamReaderStrategyAsync reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            TSizeType size = await sizeSerializer.ReadAsync(reader)
                             .ConfigureAwait(false);

            //Using JonSkeets MiscUtils we can convert objects efficently
            return(GenericMath <TSizeType, int> .Convert(size) + AddedSize);
        }
Ejemplo n.º 18
0
        /// <inheritdoc />
        public async Task <int> SizeAsync(IWireStreamReaderStrategyAsync reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            //Read the byte size from the stream.
            TSizeType result = await SizeTypeSerializerStrategy.ReadAsync(reader)
                               .ConfigureAwait(false);

            //Readd the addedsize so that we know how many items are really there.
            return(GenericMath.Convert <TSizeType, int>(result) + AddedSize);
        }
        /// <inheritdoc />
        public void Write(int value, IWireStreamWriterStrategy dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException(nameof(dest));
            }

            //If the key shouldn't be written then we avoid writing it
            //It may be that the data is needed to be left in the stream to indicate
            //something about the type later down the line.
            if (!typeHandlingFlags.HasFlag(InformationHandlingFlags.DontWrite))
            {
                KeyTypeSerializerStrategy.Write(GenericMath.Convert <int, TKeyType>(value), dest);
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <exception cref="InconclusiveException">Throws if the key cannot be constructed.</exception>
        /// <returns></returns>
        private static TKeyType TryBuildKey(int key)
        {
            //TODO: Is there a better way to do this?
            TKeyType keyValue = default(TKeyType);

            try
            {
                keyValue = GenericMath.Convert <int, TKeyType>(key);
            }
            catch (Exception)
            {
                Assert.Inconclusive($"Unable to convert Key: {key} to Type: {typeof(TKeyType).Name} so skipping.");
            }

            return(keyValue);
        }
        /// <inheritdoc />
        public int Size(string stringValue, IWireStreamWriterStrategy writer)
        {
            if (stringValue == null)
            {
                throw new ArgumentNullException(nameof(stringValue));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            int size = stringValue.Length - AddedSize;

            //add one for null terminator
            //Using JonSkeets MiscUtils we can convert objects efficently
            sizeSerializer.Write(GenericMath <int, TSizeType> .Convert(size + (includeNullTerminatorInSizeCalculation ? 1 : 0)), writer);

            return(size);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// The size to consider the collection.
        /// </summary>
        public int Size <TCollectionType, TElementType>(TCollectionType collection, IWireStreamWriterStrategy writer)
            where TCollectionType : IEnumerable, IEnumerable <TElementType>
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            //When writing N items we want to write N - X. The otherside will X so it will still understand
            //This was added to suppose very rare scenarios where the encoded size was off from the actual size.
            int size = collection.Count() - AddedSize;

            //Since the size is unknown it's critical that we write the size to the stream.
            SizeTypeSerializerStrategy.Write(GenericMath.Convert <int, TSizeType>(size), writer);

            //We don't know the size so just provide the size of the collection
            return(size);
        }
Ejemplo n.º 23
0
        /// <inheritdoc />
        public async Task <int> SizeAsync <TCollectionType, TElementType>(TCollectionType collection, IWireStreamWriterStrategyAsync writer)
            where TCollectionType : IEnumerable, IEnumerable <TElementType>
        {
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            //When writing N items we want to write N - X. The otherside will X so it will still understand
            //This was added to suppose very rare scenarios where the encoded size was off from the actual size.
            int size = collection.Count() - AddedSize;

            //yield until we write (shouldn't take long and maybe syncronous is more efficient and performant but async API should be fully async)
            await SizeTypeSerializerStrategy.WriteAsync(GenericMath.Convert <int, TSizeType>(size), writer)
            .ConfigureAwait(false);

            //We don't know the size so just provide the size of the collection
            return(size);
        }
 /// <summary>
 /// Method should convert the <paramref name="statType"/> to a key-value; implementation is
 /// deferred to implementing concrete (non-generic) types.
 /// </summary>
 /// <param name="statType">The stat type to convert to int.</param>
 /// <returns>A key value for the given <paramref name="statType"/>.</returns>
 protected int ConvertStatToKey(TStatType statType)
 {
     return(GenericMath.Convert <TStatType, int>(statType));
 }
Ejemplo n.º 25
0
 /// <inheritdoc />
 public void SetFieldValue <TValueType>(TFieldType index, TValueType value)
     where TValueType : struct
 {
     SetFieldValue <TValueType>(GenericMath.Convert <TFieldType, int>(index), value);
 }
Ejemplo n.º 26
0
        //TFieldType

        public TValueType GetFieldValue <TValueType>(TFieldType index)
            where TValueType : struct
        {
            return(GetFieldValue <TValueType>(GenericMath.Convert <TFieldType, int>(index)));
        }
Ejemplo n.º 27
0
 public static bool HasAnyFlags <T>(this T enumValue, T flags)
     where T : Enum
 {
     return((GenericMath.Convert <T, int>(enumValue) & GenericMath.Convert <T, int>(flags)) != 0);
 }
Ejemplo n.º 28
0
 private static short ConvertPayloadOperationCode <TBasePayloadType, TOperationType>(TBasePayloadType payload)
     where TBasePayloadType : IOperationCodeable <TOperationType>, ITypeSerializerReadingStrategy <TBasePayloadType>
     where TOperationType : Enum
 {
     return(GenericMath.Convert <TOperationType, short>(payload.OperationCode));
 }
Ejemplo n.º 29
0
        public static string BuildPacketDocumentation <TOutgoingPayloadAttributeType, TIncomingPayloadAttributeType, TOpcodeType>([NotNull] IEnumerable <Type> packets, string packetType)
            where TOpcodeType : struct
            where TOutgoingPayloadAttributeType : WireDataContractBaseLinkAttribute
            where TIncomingPayloadAttributeType : WireDataContractBaseLinkAttribute
        {
            if (packets == null)
            {
                throw new ArgumentNullException(nameof(packets));
            }
            packets = packets.ToList();

            StringBuilder builder = new StringBuilder();

            //Generate the patch packet documentation
            builder.Append(BuildHeader(1, $"{packetType} Packets"));
            InsertLineBreak(builder);
            InsertLineBreak(builder);

            builder.Append(BuildPacketHeaderRow());
            InsertLineBreak(builder);

            foreach (TOpcodeType opcode in Enum.GetValues(typeof(TOpcodeType)))
            {
                Type serverPayloadType = packets.FirstOrDefault(p => HasOpCodeAttribute <TIncomingPayloadAttributeType, TOpcodeType>(p, opcode));
                Type clientPayloadType = packets.FirstOrDefault(p => HasOpCodeAttribute <TOutgoingPayloadAttributeType, TOpcodeType>(p, opcode));

                //Skip if there are no payloads for this OpCode
                if (serverPayloadType == null && clientPayloadType == null)
                {
                    continue;
                }

                string row = BuildPacketInformationRow(opcode.ToString(), $"0x{String.Format("{0:X4}", GenericMath.Convert<TOpcodeType, int>(opcode))}",
                                                       serverPayloadType, clientPayloadType, packetType);

                builder.Append(row);
                InsertLineBreak(builder);
            }

            InsertLineBreak(builder);
            InsertLineBreak(builder);
            builder.Append("This documentation was automatically generated using the documentation tools.");

            return(builder.ToString());
        }
Ejemplo n.º 30
0
 /// <inheritdoc />
 public void OnEntityStateChanged(byte value) => OnEntityStateChanged(GenericMath <byte, TNetworkStateType> .Convert(value));