Example #1
0
        public override DbTypeInfo GetDbTypeInfo(EntityMemberInfo member, SystemLog log)
        {
            var ti = base.GetDbTypeInfo(member, log);

            if (ti == null)
            {
                return(null);
            }
            var type = member.DataType;

            if (type.IsNullableValueType())
            {
                type = type.GetUnderlyingType();
            }
            if (type.IsInt())
            {
                ti.DbType = GetDbTypeForInt(type);
                // Assign converter for the specific target type
                var conv = new IntValueConverter()
                {
                    TargetType = type
                };
                ti.ColumnToPropertyConverter = conv.ConvertValue;
                ti.PropertyToColumnConverter = DbValueConverter.NoConvertFunc;
            }
            return(ti);
        }
Example #2
0
        /// <summary>
        /// Replaces BBCode style tags with unity rich text syntax and parses embedded views.
        /// </summary>
        public string ParseText(string text)
        {
            if (text == null)
            {
                return(String.Empty);
            }

            if (!TextComponent.supportRichText)
            {
                return(text);
            }

            string formattedText   = string.Empty;
            string separatorString = "&sp;";

            // search for tokens and apply formatting and embedded views
            List <TextToken> tokens = new List <TextToken>();

            formattedText = _tagRegex.Replace(text, x =>
            {
                string tag     = x.Groups["tag"].Value.Trim();
                string tagNoWs = tag.RemoveWhitespace();

                // check if tag matches default tokens
                if (String.Equals("B", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // bold
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.BoldStart
                    });
                    return(separatorString);
                }
                else if (String.Equals("/B", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // bold end
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.BoldEnd
                    });
                    return(separatorString);
                }
                else if (String.Equals("I", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // italic
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.ItalicStart
                    });
                    return(separatorString);
                }
                else if (String.Equals("/I", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // italic end
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.ItalicEnd
                    });
                    return(separatorString);
                }
                else if (tagNoWs.StartsWith("SIZE=", StringComparison.OrdinalIgnoreCase))
                {
                    // parse size value
                    var vc            = new IntValueConverter();
                    var convertResult = vc.Convert(tagNoWs.Substring(5), ValueConverterContext.Empty);
                    if (!convertResult.Success)
                    {
                        // unable to parse token
                        Utils.LogError("[MarkLight] {0}: Unable to parse text embedded size tag \"[{1}]\". {2}", GameObjectName, tag, convertResult.ErrorMessage);
                        return(String.Format("[{0}]", tag));
                    }

                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.SizeStart, FontSize = (int)convertResult.ConvertedValue
                    });
                    return(separatorString);
                }
                else if (String.Equals("/SIZE", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // size end
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.SizeEnd
                    });
                    return(separatorString);
                }
                else if (tagNoWs.StartsWith("COLOR=", StringComparison.OrdinalIgnoreCase))
                {
                    // parse color value
                    var vc            = new ColorValueConverter();
                    var convertResult = vc.Convert(tagNoWs.Substring(6), ValueConverterContext.Empty);
                    if (!convertResult.Success)
                    {
                        // unable to parse token
                        Utils.LogError("[MarkLight] {0}: Unable to parse text embedded color tag \"[{1}]\". {2}", GameObjectName, tag, convertResult.ErrorMessage);
                        return(String.Format("[{0}]", tag));
                    }

                    Color color = (Color)convertResult.ConvertedValue;
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.ColorStart, FontColor = color
                    });
                    return(separatorString);
                }
                else if (String.Equals("/COLOR", tagNoWs, StringComparison.OrdinalIgnoreCase))
                {
                    // color end
                    tokens.Add(new TextToken {
                        TextTokenType = TextTokenType.ColorEnd
                    });
                    return(separatorString);
                }

                return(String.Format("[{0}]", tag));
            });

            // replace newline in string
            formattedText = formattedText.Replace("\\n", Environment.NewLine);

            // split the string up on each line
            StringBuilder result          = new StringBuilder();
            var           splitString     = formattedText.Split(new string[] { separatorString }, StringSplitOptions.None);
            int           splitIndex      = 0;
            int           fontBoldCount   = 0;
            int           fontItalicCount = 0;
            Stack <int>   fontSizeStack   = new Stack <int>();

            // loop through each split string and apply tokens (embedded views & font styles)
            foreach (var str in splitString)
            {
                int tokenIndex = splitIndex - 1;
                var token      = tokenIndex >= 0 && tokenIndex < tokens.Count ? tokens[tokenIndex] : null;
                ++splitIndex;

                // do we have a token?
                if (token != null)
                {
                    // yes. parse token type
                    switch (token.TextTokenType)
                    {
                    case TextTokenType.BoldStart:
                        result.Append("<b>");
                        ++fontBoldCount;
                        break;

                    case TextTokenType.BoldEnd:
                        result.Append("</b>");
                        --fontBoldCount;
                        break;

                    case TextTokenType.ItalicStart:
                        result.Append("<i>");
                        ++fontItalicCount;
                        break;

                    case TextTokenType.ItalicEnd:
                        result.Append("</i>");
                        --fontItalicCount;
                        break;

                    case TextTokenType.SizeStart:
                        result.Append(String.Format("<size={0}>", token.FontSize));
                        fontSizeStack.Push(token.FontSize);
                        break;

                    case TextTokenType.SizeEnd:
                        result.Append("</size>");
                        fontSizeStack.Pop();
                        break;

                    case TextTokenType.ColorStart:
                        int r = (int)(token.FontColor.r * 255f);
                        int g = (int)(token.FontColor.g * 255f);
                        int b = (int)(token.FontColor.b * 255f);
                        int a = (int)(token.FontColor.a * 255f);
                        result.Append(String.Format("<color=#{0}{1}{2}{3}>", r.ToString("X2"), g.ToString("X2"), b.ToString("X2"), a.ToString("X2")));
                        break;

                    case TextTokenType.ColorEnd:
                        result.Append("</color>");
                        break;

                    case TextTokenType.Unknown:
                    default:
                        break;
                    }
                }

                result.Append(str);
            }

            return(result.ToString());
        }