Example #1
0
        public static void HandleMessage
        (
            Entity parent,
            string text,
            string name,
            ushort hue,
            MessageType type,
            byte font,
            TextType textType,
            bool unicode = false,
            string lang  = null
        )
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            Profile currentProfile = ProfileManager.CurrentProfile;

            if (currentProfile != null && currentProfile.OverrideAllFonts)
            {
                font    = currentProfile.ChatFont;
                unicode = currentProfile.OverrideAllFontsIsUnicode;
            }

            switch (type)
            {
            case MessageType.Command:
            case MessageType.Encoded:
            case MessageType.System:
            case MessageType.Party:
                break;

            case MessageType.Guild:
                if (currentProfile.IgnoreGuildMessages)
                {
                    return;
                }
                break;

            case MessageType.Alliance:
                if (currentProfile.IgnoreAllianceMessages)
                {
                    return;
                }
                break;

            case MessageType.Spell:
            {
                //server hue color per default
                if (!string.IsNullOrEmpty(text) && SpellDefinition.WordToTargettype.TryGetValue(text, out SpellDefinition spell))
                {
                    if (currentProfile != null && currentProfile.EnabledSpellFormat && !string.IsNullOrWhiteSpace(currentProfile.SpellDisplayFormat))
                    {
                        ValueStringBuilder sb = new ValueStringBuilder(currentProfile.SpellDisplayFormat.AsSpan());
                        {
                            sb.Replace("{power}".AsSpan(), spell.PowerWords.AsSpan());
                            sb.Replace("{spell}".AsSpan(), spell.Name.AsSpan());

                            text = sb.ToString().Trim();
                        }
                        sb.Dispose();
                    }

                    //server hue color per default if not enabled
                    if (currentProfile != null && currentProfile.EnabledSpellHue)
                    {
                        if (spell.TargetType == TargetType.Beneficial)
                        {
                            hue = currentProfile.BeneficHue;
                        }
                        else if (spell.TargetType == TargetType.Harmful)
                        {
                            hue = currentProfile.HarmfulHue;
                        }
                        else
                        {
                            hue = currentProfile.NeutralHue;
                        }
                    }
                }

                goto case MessageType.Label;
            }

            default:
            case MessageType.Focus:
            case MessageType.Whisper:
            case MessageType.Yell:
            case MessageType.Regular:
            case MessageType.Label:
            case MessageType.Limit3Spell:

                if (parent == null)
                {
                    break;
                }

                // If person who send that message is in ignores list - but filter out Spell Text
                if (IgnoreManager.IgnoredCharsList.Contains(parent.Name) && type != MessageType.Spell)
                {
                    break;
                }

                TextObject msg = CreateMessage
                                 (
                    text,
                    hue,
                    font,
                    unicode,
                    type,
                    textType
                                 );

                msg.Owner = parent;

                if (parent is Item it && !it.OnGround)
                {
                    msg.X          = DelayedObjectClickManager.X;
                    msg.Y          = DelayedObjectClickManager.Y;
                    msg.IsTextGump = true;
                    bool found = false;

                    for (LinkedListNode <Gump> gump = UIManager.Gumps.Last; gump != null; gump = gump.Previous)
                    {
                        Control g = gump.Value;

                        if (!g.IsDisposed)
                        {
                            switch (g)
                            {
                            case PaperDollGump paperDoll when g.LocalSerial == it.Container:
                                paperDoll.AddText(msg);
                                found = true;

                                break;

                            case ContainerGump container when g.LocalSerial == it.Container:
                                container.AddText(msg);
                                found = true;

                                break;

                            case TradingGump trade when trade.ID1 == it.Container || trade.ID2 == it.Container:
                                trade.AddText(msg);
                                found = true;

                                break;
                            }
                        }

                        if (found)
                        {
                            break;
                        }
                    }
                }

                parent.AddMessage(msg);

                break;
            }

            MessageReceived.Raise
            (
                new MessageEventArgs
                (
                    parent,
                    text,
                    name,
                    hue,
                    type,
                    font,
                    textType,
                    unicode,
                    lang
                ),
                parent
            );
        }
        public void TestStringBuilders()
        {
            {
                Span <char> buf = stackalloc char[128];
                var         bdr = new FixedStringBuilder(buf);
                bdr.AppendLine("Hello World");
                bdr.Append("Hello");
                bdr.Append(' ');
                bdr.Append(12);
                bdr.Append(' ');
                bdr.AppendLine("Worlds");
                int numReplaced = bdr.Replace('e', 'u');
                Assert.AreEqual(2, numReplaced);

                var expected = "Hullo World" + Environment.NewLine + "Hullo 12 Worlds" + Environment.NewLine;
                Assert.AreEqual(expected.Length, bdr.Length);
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));

                numReplaced = bdr.Replace("Hullo", "Hi");
                Assert.AreEqual(2, numReplaced);
                expected = expected.Replace("Hullo", "Hi");
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));

                numReplaced = bdr.Replace("Hi", "");
                Assert.AreEqual(2, numReplaced);
                expected = expected.Replace("Hi", "");
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));

                bdr.Replace("World", "World");
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));
                bdr.Replace("Florka", "--");
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));
                bdr.Replace("Florka", "Florka");
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual(expected));

                bdr.Clear();
                Assert.AreEqual(0, bdr.Length);
                Assert.AreEqual("", bdr.ToString());
                bdr.Append("Koko");
                bdr.Append('s');
                Assert.AreEqual("Kokos".Length, bdr.Length);
                Assert.IsTrue(bdr.ReadOnlySpan.SequenceEqual("Kokos"));
            }

            {
                var sbdr = new ValueStringBuilder(8);
                sbdr.AppendLine("Hello World");
                sbdr.Append("Hello");
                sbdr.Append(' ');
                sbdr.Append(12u);
                sbdr.Append(' ');
                sbdr.AppendLine("Worlds");
                var numReplaced = sbdr.Replace('e', 'u');
                Assert.AreEqual(2, numReplaced);

                var expected = "Hullo World" + Environment.NewLine + "Hullo 12 Worlds" + Environment.NewLine;
                Assert.AreEqual(expected.Length, sbdr.Length);
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));

                numReplaced = sbdr.Replace("Hullo", "Hi");
                Assert.AreEqual(2, numReplaced);
                expected = expected.Replace("Hullo", "Hi");
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));

                numReplaced = sbdr.Replace("Hi", "");
                Assert.AreEqual(2, numReplaced);
                expected = expected.Replace("Hi", "");
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));

                sbdr.Replace("World", "World");
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));
                sbdr.Replace("Florka", "--");
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));
                sbdr.Replace("Florka", "Florka");
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual(expected));

                sbdr.Clear();
                Assert.AreEqual(0, sbdr.Length);
                Assert.AreEqual("", sbdr.ToString());
                sbdr.Append("Koko");
                sbdr.Append('s');
                Assert.AreEqual("Kokos".Length, sbdr.Length);
                Assert.IsTrue(sbdr.ReadOnlySpan.SequenceEqual("Kokos"));
            }
        }