Ejemplo n.º 1
0
        /// <summary>
        /// Types don't need to be annotated with any special attributes for them to be serialized
        /// A type is serialized if:
        /// - it's a primitive, enum or string
        /// - or a UnityEngine.Object
        /// - or a Unity struct
        /// - or a single-dimensional array and the element type is serializable
        /// - or an interface
        /// - or included in the 'SupportedTypes' array
        /// - it's not included in the 'NotSupportedTypes' array
        /// - it's generic and all its generic type arguments are serializable as well
        /// </summary>
        public override bool IsSerializableType(Type type)
        {
            if (type.IsPrimitive || type.IsEnum || type == typeof(string) ||
                type.IsA <UnityObject>() ||
                UnityStructs.ContainsValue(type))
            {
                return(true);
            }

            if (type.IsArray)
            {
                return(type.GetArrayRank() == 1 && IsSerializableType(type.GetElementType()));
            }

            if (type.IsInterface)
            {
                return(true);
            }

            if (NotSupportedTypes.Any(type.IsA))
            {
                return(false);
            }

            if (SupportedTypes.Any(type.IsA))
            {
                return(true);
            }

            if (type.IsGenericType)
            {
                return(type.GetGenericArguments().All(IsSerializableType));
            }

            return(true);
        }
        public bool IsSerializable(Type type)
        {
            if (IsSimpleType(type) ||
                type.IsA <UnityObject>() ||
                UnityStructs.ContainsValue(type))
            {
                return(true);
            }

            if (type.IsArray)
            {
                return(type.GetArrayRank() == 1 && IsSerializable(type.GetElementType()));
            }

            if (type.IsInterface)
            {
                return(true);
            }

            if (NotSupportedTypes.Any(type.IsA))
            {
                return(false);
            }

            if (SupportedTypes.Any(type.IsA))
            {
                return(true);
            }

            if (type.IsGenericType)
            {
                return(type.GetGenericArguments().All(IsSerializable));
            }

            return(attributes.SerializableType.IsNullOrEmpty() || attributes.SerializableType.Any(type.IsDefined));
        }
Ejemplo n.º 3
0
        public static bool IsSerializableType(Type type)
        {
            if (type.IsPrimitive || type.IsEnum || type == typeof(string) ||
                typeof(UnityObject).IsAssignableFrom(type) ||
                IsUnityType(type))
            {
                return(true);
            }

            if (type.IsArray)
            {
                return(type.GetArrayRank() == 1 && IsSerializableType(type.GetElementType()));
            }

            if (type.IsInterface)
            {
                return(true);
            }

            if (NotSupportedTypes.Any(type.IsA))
            {
                return(false);
            }

            if (SupportedTypes.Any(type.IsA))
            {
                return(true);
            }

            if (type.IsGenericType)
            {
                return(type.GetGenericArguments().All(IsSerializableType));
            }

            return(true);
        }
Ejemplo n.º 4
0
        public virtual ParseResult Parse(ExtractResult extResult)
        {
            // Check if the parser is configured to support specific types
            if (SupportedTypes != null && !SupportedTypes.Any(t => extResult.Type.Equals(t)))
            {
                return(null);
            }

            ParseResult ret = null;

            if (!(extResult.Data is string extra))
            {
                extra = LongFormatRegex.Match(extResult.Text).Success ? Constants.NUMBER_SUFFIX : Config.LangMarker;
            }

            // Resolve symbol prefix
            var isNegative    = false;
            var matchNegative = Config.NegativeNumberSignRegex.Match(extResult.Text);

            if (matchNegative.Success)
            {
                isNegative     = true;
                extResult.Text = extResult.Text.Substring(matchNegative.Groups[1].Length);
            }

            // Assign resolution value
            if (extResult.Data is List <ExtractResult> ers)
            {
                var innerPrs  = ers.Select(Parse).ToList();
                var mergedPrs = new List <ParseResult>();

                double val   = 0;
                var    count = 0;

                for (var idx = 0; idx < innerPrs.Count; idx++)
                {
                    val += (double)innerPrs[idx].Value;

                    if (idx + 1 >= innerPrs.Count || !IsMergeable((double)innerPrs[idx].Value, (double)innerPrs[idx + 1].Value))
                    {
                        var start  = (int)ers[idx - count].Start;
                        var length = (int)(ers[idx].Start + ers[idx].Length - start);
                        mergedPrs.Add(new ParseResult
                        {
                            Start  = start,
                            Length = length,
                            Text   = extResult.Text.Substring((int)(start - extResult.Start), length),
                            Type   = extResult.Type,
                            Value  = val,
                            Data   = null,
                        });

                        val   = 0;
                        count = 0;
                    }
                    else
                    {
                        count++;
                    }
                }

                ret = new ParseResult(extResult)
                {
                    Value = val, Data = mergedPrs
                };
            }
            else if (extra.Contains(Constants.NUMBER_SUFFIX))
            {
                ret = DigitNumberParse(extResult);
            }
            else if (extra.Contains($"{Constants.FRACTION_PREFIX}{Config.LangMarker}"))
            {
                // Such fractions are special cases, parse via another method
                ret = FracLikeNumberParse(extResult);
            }
            else if (extra.Contains(Config.LangMarker))
            {
                ret = TextNumberParse(extResult);
            }
            else if (extra.Contains(Constants.POWER_SUFFIX))
            {
                ret = PowerNumberParse(extResult);
            }

            if (ret?.Data is List <ParseResult> prs)
            {
                foreach (var parseResult in prs)
                {
                    parseResult.ResolutionStr = GetResolutionStr(parseResult.Value);
                }
            }
            else if (ret?.Value != null)
            {
                if (isNegative)
                {
                    // Recover the original extracted Text
                    ret.Text  = matchNegative.Groups[1].Value + extResult.Text;
                    ret.Value = -(double)ret.Value;
                }

                ret.ResolutionStr = GetResolutionStr(ret.Value);
            }

            // Add "offset" and "relativeTo" for ordinal
            if (!string.IsNullOrEmpty(ret.Type) && ret.Type.Contains(Constants.MODEL_ORDINAL))
            {
                if (Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
                    Config.RelativeReferenceRelativeToMap.ContainsKey(extResult.Text))
                {
                    ret.Metadata.Offset     = Config.RelativeReferenceOffsetMap[extResult.Text];
                    ret.Metadata.RelativeTo = Config.RelativeReferenceRelativeToMap[extResult.Text];
                }
                else
                {
                    ret.Metadata.Offset = ret.ResolutionStr;

                    // Every ordinal number is relative to the start
                    ret.Metadata.RelativeTo = Constants.RELATIVE_START;
                }
            }

            if (ret != null)
            {
                ret.Type = DetermineType(extResult);
                ret.Text = ret.Text.ToLowerInvariant();
            }

            return(ret);
        }
        public override ParseResult Parse(ExtractResult extResult)
        {
            // Check if the parser is configured to support specific types
            if (SupportedTypes != null && !SupportedTypes.Any(t => extResult.Type.Equals(t, StringComparison.Ordinal)))
            {
                return(null);
            }

            string      extra = null;
            ParseResult ret   = null;

            extra = extResult.Data as string;

            var getExtResult = new ExtractResult()
            {
                Start    = extResult.Start,
                Length   = extResult.Length,
                Data     = extResult.Data,
                Text     = extResult.Text,
                Type     = extResult.Type,
                Metadata = extResult.Metadata,
            };

            if (Config.CultureInfo.Name == "zh-CN")
            {
                getExtResult.Text = ReplaceTraWithSim(getExtResult.Text);
            }

            if (extra == null)
            {
                return(null);
            }

            if (extra.Contains("Per"))
            {
                ret = ParsePercentage(getExtResult);
            }
            else if (extra.Contains("Num"))
            {
                getExtResult.Text = NormalizeCharWidth(getExtResult.Text);
                ret = DigitNumberParse(getExtResult);
                if (Config.NegativeNumberSignRegex.IsMatch(getExtResult.Text) && (double)ret.Value > 0)
                {
                    ret.Value = -(double)ret.Value;
                }

                ret.ResolutionStr = ((double)ret.Value).ToString("G15", CultureInfo.InvariantCulture);
            }
            else if (extra.Contains("Pow"))
            {
                getExtResult.Text = NormalizeCharWidth(getExtResult.Text);
                ret = PowerNumberParse(getExtResult);
                ret.ResolutionStr = ((double)ret.Value).ToString("G15", CultureInfo.InvariantCulture);
            }
            else if (extra.Contains("Frac"))
            {
                ret = ParseFraction(getExtResult);
            }
            else if (extra.Contains("Dou"))
            {
                ret = ParseDouble(getExtResult);
            }
            else if (extra.Contains("Integer"))
            {
                ret = ParseInteger(getExtResult);
            }
            else if (extra.Contains("Ordinal"))
            {
                ret = ParseOrdinal(getExtResult);
            }

            if (ret != null)
            {
                ret.Text = extResult.Text.ToLowerInvariant();
            }

            // Add "offset" and "relativeTo" for ordinal
            if (!string.IsNullOrEmpty(ret.Type) && ret.Type.Contains(Constants.MODEL_ORDINAL))
            {
                if (Config.RelativeReferenceOffsetMap.ContainsKey(extResult.Text) &&
                    Config.RelativeReferenceRelativeToMap.ContainsKey(extResult.Text))
                {
                    ret.Metadata.Offset     = Config.RelativeReferenceOffsetMap[extResult.Text];
                    ret.Metadata.RelativeTo = Config.RelativeReferenceRelativeToMap[extResult.Text];
                    ret.Type = Constants.MODEL_ORDINAL_RELATIVE;
                }
                else
                {
                    ret.Metadata.Offset = ret.ResolutionStr;

                    // Every ordinal number is relative to the start
                    ret.Metadata.RelativeTo = Constants.RELATIVE_START;
                    ret.Type = Constants.MODEL_ORDINAL;
                }
            }

            // TODO: @Refactor this check to determine the subtype for JA and KO
            if ((Config.CultureInfo.Name == "ja-JP" || Config.CultureInfo.Name == "ko-KR") && ret != null)
            {
                ret.Type = DetermineType(extResult);
                ret.Text = ret.Text.ToLowerInvariant();
            }

            return(ret);
        }
        public override ParseResult Parse(ExtractResult extResult)
        {
            // Check if the parser is configured to support specific types
            if (SupportedTypes != null && !SupportedTypes.Any(t => extResult.Type.Equals(t)))
            {
                return(null);
            }

            string      extra = null;
            ParseResult ret   = null;

            extra = extResult.Data as string;

            var getExtResult = new ExtractResult()
            {
                Start  = extResult.Start,
                Length = extResult.Length,
                Data   = extResult.Data,
                Text   = extResult.Text,
                Type   = extResult.Type,
            };

            if (Config.CultureInfo.Name == "zh-CN")
            {
                getExtResult.Text = ReplaceTraWithSim(getExtResult.Text);
            }

            if (extra == null)
            {
                return(null);
            }

            if (extra.Contains("Per"))
            {
                ret = ParsePercentage(getExtResult);
            }
            else if (extra.Contains("Num"))
            {
                getExtResult.Text = NormalizeCharWidth(getExtResult.Text);
                ret = DigitNumberParse(getExtResult);
                if (Config.NegativeNumberSignRegex.IsMatch(getExtResult.Text) && (double)ret.Value > 0)
                {
                    ret.Value = -(double)ret.Value;
                }

                ret.ResolutionStr = ret.Value.ToString();
            }
            else if (extra.Contains("Pow"))
            {
                getExtResult.Text = NormalizeCharWidth(getExtResult.Text);
                ret = PowerNumberParse(getExtResult);
                ret.ResolutionStr = ret.Value.ToString();
            }
            else if (extra.Contains("Frac"))
            {
                ret = ParseFraction(getExtResult);
            }
            else if (extra.Contains("Dou"))
            {
                ret = ParseDouble(getExtResult);
            }
            else if (extra.Contains("Integer"))
            {
                ret = ParseInteger(getExtResult);
            }
            else if (extra.Contains("Ordinal"))
            {
                ret = ParseOrdinal(getExtResult);
            }

            if (ret != null)
            {
                ret.Text = extResult.Text.ToLowerInvariant();
            }

            return(ret);
        }
Ejemplo n.º 7
0
 public static bool IsSupported(string emailType)
 {
     return(!string.IsNullOrEmpty(emailType) && SupportedTypes.Any(t => t.Name.ToLower() == emailType.ToLower()));
 }