Пример #1
0
        public static bool TryConsumeObject(out dynamic value, Type type, HyperlinkInfo info)
        {
            value = default;

            switch (type.Name)
            {
            case nameof(AchievementRecord):
            {
                HyperlinkDataTokenizer t = new(info.Data);
                if (!t.TryConsumeTo(out uint achievementId))
                {
                    return(false);
                }

                value = CliDB.AchievementStorage.LookupByKey(achievementId);
                return(true);
            }

            case nameof(CurrencyTypesRecord):
            {
                HyperlinkDataTokenizer t = new(info.Data);
                if (!t.TryConsumeTo(out uint currencyId))
                {
                    return(false);
                }

                value = CliDB.CurrencyTypesStorage.LookupByKey(currencyId);
                return(true);
            }

            case nameof(GameTele):
            {
                HyperlinkDataTokenizer t = new(info.Data);
                if (!t.TryConsumeTo(out uint teleId))
                {
                    return(false);
                }

                value = Global.ObjectMgr.GetGameTele(teleId);
                return(true);
            }

            case nameof(SpellInfo):
            {
                HyperlinkDataTokenizer t = new(info.Data);
                if (!t.TryConsumeTo(out uint spellId))
                {
                    return(false);
                }

                value = Global.SpellMgr.GetSpellInfo(spellId, Difficulty.None);

                return(true);
            }
            }

            return(false);
        }
Пример #2
0
        public static bool TryConsume(out dynamic value, Type type, StringArguments args)
        {
            value = default;

            HyperlinkInfo info = ParseHyperlink(args.GetString());

            // invalid hyperlinks cannot be consumed
            if (info == null)
            {
                return(false);
            }

            // store value
            switch (Type.GetTypeCode(type))
            {
            case TypeCode.UInt32:
            {
                if (!uint.TryParse(info.Data, out uint tempValue))
                {
                    return(false);
                }

                value = tempValue;
                return(true);
            }

            case TypeCode.UInt64:
            {
                if (!ulong.TryParse(info.Data, out ulong tempValue))
                {
                    return(false);
                }

                value = tempValue;
                return(true);
            }

            case TypeCode.String:
            {
                value = info.Data;
                return(true);
            }

            case TypeCode.Object:
                return(TryConsumeObject(out value, type, info));
            }

            return(false);
        }
Пример #3
0
        public static bool CheckAllLinks(string str)
        {
            // Step 1: Disallow all control sequences except ||, |H, |h, |c and |r
            {
                int pos = 0;
                while ((pos = str.IndexOf('|', pos)) != -1)
                {
                    char next = str[pos + 1];
                    if (next == 'H' || next == 'h' || next == 'c' || next == 'r' || next == '|')
                    {
                        pos += 2;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            // Step 2: Parse all link sequences
            // They look like this: |c<color>|H<linktag>:<linkdata>|h[<linktext>]|h|r
            // - <color> is 8 hex characters AARRGGBB
            // - <linktag> is arbitrary length [a-z_]
            // - <linkdata> is arbitrary length, no | contained
            // - <linktext> is printable
            {
                int pos = 0;
                while ((pos = str.IndexOf('|', pos)) != -1)
                {
                    if (str[pos + 1] == '|') // this is an escaped pipe character (||)
                    {
                        pos += 2;
                        continue;
                    }

                    HyperlinkInfo info = ParseHyperlink(str.Substring(pos));
                    if (info == null)// todo fix me || !ValidateLinkInfo(info))
                    {
                        return(false);
                    }

                    // tag is fine, find the next one
                    pos = str.Length - info.next.Length;
                }
            }

            // all tags are valid
            return(true);
        }