Example #1
0
        private IUnrealTypeReference GenerateUnrealTypeReference(FieldDefinitionRaw fieldDefinition)
        {
            IUnrealTypeReference unrealTypeReference;

            if (fieldDefinition.IsMap())
            {
                var containedKey   = GenerateUnrealTypeReference(fieldDefinition.mapType.keyType);
                var containedValue = GenerateUnrealTypeReference(fieldDefinition.mapType.valueType);
                unrealTypeReference = new UnrealMapTypeReference(containedKey, containedValue);
            }
            else if (fieldDefinition.IsOption())
            {
                var containedType = GenerateUnrealTypeReference(fieldDefinition.optionType.valueType);
                unrealTypeReference = new UnrealOptionTypeReference(containedType);
            }
            else if (fieldDefinition.IsList())
            {
                var containedType = GenerateUnrealTypeReference(fieldDefinition.listType.valueType);
                unrealTypeReference = new UnrealListTypeReference(containedType);
            }
            else
            {
                unrealTypeReference = GenerateUnrealTypeReference(fieldDefinition.singularType);
            }

            return(unrealTypeReference);
        }
        public UnityFieldDetails(FieldDefinitionRaw rawFieldDefinition)
        {
            PascalCaseName = Formatting.SnakeCaseToCapitalisedCamelCase(rawFieldDefinition.name);
            CamelCaseName  = Formatting.SnakeCaseToCamelCase(rawFieldDefinition.name);
            var packagePrefix = UnityTypeMappings.PackagePrefix;

            RawFieldDefinition = rawFieldDefinition;

            if (rawFieldDefinition.IsOption())
            {
                var containedType = GetTypeFromTypeReference(rawFieldDefinition.optionType.valueType, packagePrefix);
                Type = string.Format("global::System.Nullable<{0}>", containedType);
            }
            else if (rawFieldDefinition.IsList())
            {
                var containedType = GetTypeFromTypeReference(rawFieldDefinition.listType.valueType, packagePrefix);
                Type = string.Format("global::System.Collections.Generic.List<{0}>", containedType);
            }
            else if (rawFieldDefinition.IsMap())
            {
                var containedKeyType   = GetTypeFromTypeReference(rawFieldDefinition.mapType.keyType, packagePrefix);
                var containedValueType = GetTypeFromTypeReference(rawFieldDefinition.mapType.valueType, packagePrefix);
                Type = string.Format("global::System.Collections.Generic.Dictionary<{0}, {1}>", containedKeyType,
                                     containedValueType);
            }
            else
            {
                Type = GetTypeFromTypeReference(rawFieldDefinition.singularType, packagePrefix);
            }
        }
        public UnityFieldDetails(FieldDefinitionRaw rawFieldDefinition, bool isBlittable, HashSet <string> enumSet)
        {
            this.rawFieldDefinition = rawFieldDefinition;
            IsBlittable             = isBlittable;

            if (rawFieldDefinition.IsOption())
            {
                fieldType  = new OptionFieldType(rawFieldDefinition.optionType, enumSet);
                CanBeEmpty = true;
            }
            else if (rawFieldDefinition.IsList())
            {
                fieldType  = new ListFieldType(rawFieldDefinition.listType, enumSet);
                CanBeEmpty = true;
            }
            else if (rawFieldDefinition.IsMap())
            {
                fieldType  = new MapFieldType(rawFieldDefinition.mapType, enumSet);
                CanBeEmpty = true;
            }
            else
            {
                fieldType = new SingularFieldType(rawFieldDefinition.singularType, enumSet);
            }
        }
Example #4
0
        internal UnityFieldDefinition(FieldDefinitionRaw rawFieldDefinition)
        {
            this.RawFieldDefinition = rawFieldDefinition;
            Name   = rawFieldDefinition.name;
            Number = rawFieldDefinition.Number;
            if (rawFieldDefinition.IsOption())
            {
                RawValueType = rawFieldDefinition.optionType.valueType;
                IsOption     = true;
            }
            else if (rawFieldDefinition.IsList())
            {
                RawValueType = rawFieldDefinition.listType.valueType;
                IsList       = true;
            }
            else if (rawFieldDefinition.IsMap())
            {
                RawKeyType   = rawFieldDefinition.mapType.keyType;
                RawValueType = rawFieldDefinition.mapType.valueType;
                IsMap        = true;
            }
            else
            {
                RawValueType = rawFieldDefinition.singularType;
            }

            if (RawKeyType != null && RawKeyType.IsBuiltInType)
            {
                KeyType = new UnityTypeReference(RawKeyType.TypeName, null, null);
            }

            if (RawValueType != null && RawValueType.IsBuiltInType)
            {
                ValueType = new UnityTypeReference(RawValueType.TypeName, null, null);
            }
        }
        public static string GetNativeTypeMethod(FieldDefinitionRaw rawFieldType, string spatialOSTypeObject,
                                                 HashSet <string> enumSet)
        {
            if (rawFieldType.IsList())
            {
                var containedType = rawFieldType.listType.valueType;

                var containedTypeAndConversion =
                    GetNativeConversionAndNativeType(containedType.TypeName, "internalObject", enumSet);
                var containedTypeConversion = containedTypeAndConversion.Conversion;

                if (SchemaTypesThatRequireNoConversion.Contains(containedType.TypeName))
                {
                    return(string.Format("{0}", spatialOSTypeObject));
                }

                return(string.Format("{0}.Select(internalObject => {1}).ToList()", spatialOSTypeObject,
                                     containedTypeConversion));
            }

            if (rawFieldType.IsOption())
            {
                var containedType = rawFieldType.optionType.valueType;

                var containedTypeAndConversion =
                    GetNativeConversionAndNativeType(containedType.TypeName, spatialOSTypeObject + ".Value", enumSet);
                var containedTypeQualifiedName = containedTypeAndConversion.TypeName;
                var containedTypeConversion    = containedTypeAndConversion.Conversion;

                return(string.Format(
                           "{0}.HasValue ? new global::System.Nullable<{1}>({2}) : new global::System.Nullable<{1}>()",
                           spatialOSTypeObject,
                           containedTypeQualifiedName, containedTypeConversion));
            }

            if (rawFieldType.IsMap())
            {
                var keyTypeAndConversion =
                    GetNativeConversionAndNativeType(rawFieldType.mapType.keyType.TypeName, "entry.Key", enumSet);
                var keyFullQualifiedName = keyTypeAndConversion.TypeName;
                var keyTypeConversion    = keyTypeAndConversion.Conversion;

                var valueTypeAndConversion =
                    GetNativeConversionAndNativeType(rawFieldType.mapType.valueType.TypeName, "entry.Value", enumSet);
                var valueFullQualifiedName = valueTypeAndConversion.TypeName;
                var valueTypeConversion    = valueTypeAndConversion.Conversion;

                return(string.Format("{0}.ToDictionary(entry => {1}, entry => {2})", spatialOSTypeObject,
                                     string.Format(keyTypeConversion, keyFullQualifiedName),
                                     string.Format(valueTypeConversion, valueFullQualifiedName)));
            }

            if (rawFieldType.singularType.TypeName == BuiltInTypeConstants.builtInEntityId)
            {
                return(string.Format("{0}.Id", spatialOSTypeObject));
            }

            if (enumSet.Contains(rawFieldType.singularType.TypeName))
            {
                return(GetEnumConversion(GetFullyQualifiedGeneratedTypeName(rawFieldType.singularType.TypeName),
                                         spatialOSTypeObject));
            }

            if (SchemaTypesThatRequireNoConversion.Contains(rawFieldType.singularType.TypeName))
            {
                return(string.Format("{0}", spatialOSTypeObject));
            }

            return(string.Format("{0}.ToNative({1})",
                                 GetFullyQualifiedGeneratedTypeName(rawFieldType.singularType.TypeName), spatialOSTypeObject));
        }
        public static string GetSpatialTypeMethod(FieldDefinitionRaw rawFieldType, string nativeTypeObject,
                                                  HashSet <string> enumSet)
        {
            if (rawFieldType.IsList())
            {
                var containedType = rawFieldType.listType.valueType;

                var containedTypeAndConversion =
                    GetSpatialConversionAndSpatialType(containedType.TypeName, "nativeInternalObject", enumSet);
                var containedTypeName       = containedTypeAndConversion.TypeName;
                var containedTypeConversion = containedTypeAndConversion.Conversion;

                if (SchemaTypesThatRequireNoConversion.Contains(containedType.TypeName))
                {
                    return(string.Format("new global::Improbable.Collections.List<{0}>({1})", containedTypeName,
                                         nativeTypeObject));
                }

                return(string.Format(
                           "new global::Improbable.Collections.List<{0}>({1}.Select(nativeInternalObject => {2}))",
                           containedTypeName, nativeTypeObject, containedTypeConversion));
            }

            if (rawFieldType.IsOption())
            {
                var containedTypeAndConversion =
                    GetSpatialConversionAndSpatialType(rawFieldType.optionType.valueType.TypeName,
                                                       nativeTypeObject + ".Value",
                                                       enumSet);
                var containedTypeName       = containedTypeAndConversion.TypeName;
                var containedTypeConversion = containedTypeAndConversion.Conversion;

                return(string.Format(
                           "{0}.HasValue ? new global::Improbable.Collections.Option<{1}>({2}) : new global::Improbable.Collections.Option<{1}>()",
                           nativeTypeObject, containedTypeName,
                           containedTypeConversion));
            }

            if (rawFieldType.IsMap())
            {
                var keyTypeAndConversion =
                    GetSpatialConversionAndSpatialType(rawFieldType.mapType.keyType.TypeName, "entry.Key", enumSet);
                var keyFullQualifiedName = keyTypeAndConversion.TypeName;
                var keyTypeConversion    = keyTypeAndConversion.Conversion;

                var valueTypeAndConversion =
                    GetSpatialConversionAndSpatialType(rawFieldType.mapType.valueType.TypeName, "entry.Value", enumSet);
                var valueFullQualifiedName = valueTypeAndConversion.TypeName;
                var valueTypeConversion    = valueTypeAndConversion.Conversion;

                return(string.Format(
                           "new global::Improbable.Collections.Map<{0},{1}>({2}.ToDictionary(entry => {3}, entry => {4}))",
                           keyFullQualifiedName, valueFullQualifiedName, nativeTypeObject,
                           keyTypeConversion,
                           valueTypeConversion));
            }

            if (enumSet.Contains(rawFieldType.singularType.TypeName))
            {
                return(GetEnumConversion(GetFullyQualifiedSpatialTypeName(rawFieldType.singularType.TypeName),
                                         nativeTypeObject));
            }

            if (rawFieldType.singularType.TypeName == BuiltInTypeConstants.builtInEntityId)
            {
                return(string.Format("new global::Improbable.EntityId({0})", nativeTypeObject));
            }

            if (SchemaTypesThatRequireNoConversion.Contains(rawFieldType.singularType.TypeName))
            {
                return(string.Format("{0}", nativeTypeObject));
            }

            return(string.Format("{0}.ToSpatial({1})",
                                 GetFullyQualifiedGeneratedTypeName(rawFieldType.singularType.TypeName), nativeTypeObject));
        }
Example #7
0
        public UnityFieldDetails(FieldDefinitionRaw rawFieldDefinition, bool isBlittable, HashSet <string> enumSet)
        {
            PascalCaseName = Formatting.SnakeCaseToCapitalisedCamelCase(rawFieldDefinition.name);
            CamelCaseName  = Formatting.SnakeCaseToCamelCase(rawFieldDefinition.name);
            var packagePrefix = UnityTypeMappings.PackagePrefix;

            RawFieldDefinition = rawFieldDefinition;
            FieldNumber        = rawFieldDefinition.Number;
            IsBlittable        = isBlittable;

            if (rawFieldDefinition.IsOption())
            {
                var valueType        = rawFieldDefinition.optionType.valueType;
                var containedType    = GetTypeFromTypeReference(valueType, packagePrefix);
                var isBuiltInRefType = valueType.TypeName == BuiltInTypeConstants.builtInBytes ||
                                       valueType.TypeName == BuiltInTypeConstants.builtInString;
                Type = isBuiltInRefType
                    ? string.Format("global::Improbable.Gdk.Core.Option<{0}>", containedType)
                    : string.Format("global::System.Nullable<{0}>", containedType);
                SchemaTypeInfo = new FieldTypeInfo
                {
                    OptionTypeInfo = new TypeInfo
                    {
                        IsPrimitive =
                            SchemaFunctionMappings.BuiltInTypeToAddSchemaFunction.ContainsKey(valueType.TypeName),
                        IsEnum = enumSet.Contains(valueType.TypeName),
                        Type   = containedType,
                        SerializationFunctions = new SerializationFunctionStrings
                        {
                            Serialize = GetSerializationFunctionFromType(valueType,
                                                                         packagePrefix),
                            Deserialize =
                                GetDeserializationFunctionFromType(valueType,
                                                                   packagePrefix),
                            GetCount         = GetSchemaCountFunctionFromType(valueType),
                            DeserializeIndex = GetDeserializeIndexFunctionFromType(valueType)
                        }
                    }
                };
            }
            else if (rawFieldDefinition.IsList())
            {
                var valueType     = rawFieldDefinition.listType.valueType;
                var containedType = GetTypeFromTypeReference(valueType, packagePrefix);
                Type           = string.Format("global::System.Collections.Generic.List<{0}>", containedType);
                SchemaTypeInfo = new FieldTypeInfo
                {
                    ListTypeInfo = new TypeInfo
                    {
                        IsPrimitive =
                            SchemaFunctionMappings.BuiltInTypeToAddSchemaFunction.ContainsKey(valueType.TypeName),
                        IsEnum = enumSet.Contains(valueType.TypeName),
                        Type   = containedType,
                        SerializationFunctions = new SerializationFunctionStrings
                        {
                            Serialize = GetSerializationFunctionFromType(valueType,
                                                                         packagePrefix),
                            Deserialize =
                                GetDeserializationFunctionFromType(valueType,
                                                                   packagePrefix),
                            GetCount         = GetSchemaCountFunctionFromType(valueType),
                            DeserializeIndex = GetDeserializeIndexFunctionFromType(valueType)
                        }
                    }
                };
            }
            else if (rawFieldDefinition.IsMap())
            {
                var keyType   = rawFieldDefinition.mapType.keyType;
                var valueType = rawFieldDefinition.mapType.valueType;

                var containedKeyType   = GetTypeFromTypeReference(keyType, packagePrefix);
                var containedValueType = GetTypeFromTypeReference(valueType, packagePrefix);
                Type = string.Format("global::System.Collections.Generic.Dictionary<{0}, {1}>", containedKeyType,
                                     containedValueType);
                SchemaTypeInfo = new FieldTypeInfo
                {
                    MapTypeInfo = new KeyValuePair <TypeInfo, TypeInfo>(
                        new TypeInfo
                    {
                        IsPrimitive =
                            SchemaFunctionMappings.BuiltInTypeToAddSchemaFunction.ContainsKey(keyType.TypeName),
                        IsEnum = enumSet.Contains(keyType.TypeName),
                        Type   = containedKeyType,
                        SerializationFunctions = new SerializationFunctionStrings
                        {
                            Serialize = GetSerializationFunctionFromType(keyType,
                                                                         packagePrefix),
                            Deserialize =
                                GetDeserializationFunctionFromType(keyType,
                                                                   packagePrefix),
                            GetCount         = GetSchemaCountFunctionFromType(keyType),
                            DeserializeIndex = GetDeserializeIndexFunctionFromType(keyType)
                        }
                    },
                        new TypeInfo
                    {
                        IsPrimitive =
                            SchemaFunctionMappings.BuiltInTypeToAddSchemaFunction.ContainsKey(valueType.TypeName),
                        IsEnum = enumSet.Contains(valueType.TypeName),
                        Type   = containedValueType,
                        SerializationFunctions = new SerializationFunctionStrings
                        {
                            Serialize = GetSerializationFunctionFromType(valueType,
                                                                         packagePrefix),
                            Deserialize =
                                GetDeserializationFunctionFromType(valueType,
                                                                   packagePrefix),
                            GetCount         = GetSchemaCountFunctionFromType(valueType),
                            DeserializeIndex = GetDeserializeIndexFunctionFromType(valueType)
                        }
                    }
                        )
                };
            }
            else
            {
                Type           = GetTypeFromTypeReference(rawFieldDefinition.singularType, packagePrefix);
                SchemaTypeInfo = new FieldTypeInfo
                {
                    SingularTypeInfo = new TypeInfo
                    {
                        IsPrimitive =
                            SchemaFunctionMappings.BuiltInTypeToAddSchemaFunction.ContainsKey(rawFieldDefinition
                                                                                              .singularType.TypeName),
                        IsEnum = enumSet.Contains(rawFieldDefinition.singularType.TypeName),
                        Type   = Type,
                        SerializationFunctions = new SerializationFunctionStrings
                        {
                            Serialize =
                                GetSerializationFunctionFromType(rawFieldDefinition.singularType, packagePrefix),
                            Deserialize =
                                GetDeserializationFunctionFromType(rawFieldDefinition.singularType, packagePrefix),
                            GetCount         = GetSchemaCountFunctionFromType(rawFieldDefinition.singularType),
                            DeserializeIndex = GetDeserializeIndexFunctionFromType(rawFieldDefinition.singularType)
                        }
                    }
                };
            }
        }