public override string ToString()
    {
        string translatedString = ChatTranslation.TranslateString(translate, with?.Select((c) =>
        {
            return(c.ToString());
        })?.ToArray());

        return(translatedString + base.ToString());
    }
Exemplo n.º 2
0
    /// <summary>
    /// Gets an HTML-formatted TextMeshPro string for a <see cref="ChatComponent"/>
    /// </summary>
    /// <param name="component">The chat component to convert into a formatted string</param>
    /// <param name="parent">The parent chat Component of <paramref name="component"/>, used for inheriting styles</param>
    /// <returns></returns>
    public static string GetFormattedString(ChatComponent component, ChatComponent parent)
    {
        var formattedString = new StringBuilder();
        var closeTags       = new Stack <string>();

        // write formatting before the text.
        // if the style is not present in this component, check the parent's formatting
        if (component.Bold || (parent?.Bold ?? false))
        {
            writeTag("b");
        }
        if (component.Italic || (parent?.Italic ?? false))
        {
            writeTag("i");
        }
        if (component.Underlined || (parent?.Underlined ?? false))
        {
            writeTag("u");
        }
        if (component.Strikethrough || (parent?.Strikethrough ?? false))
        {
            writeTag("s");
        }
        if (component.Obfuscated || (parent?.Obfuscated ?? false))
        {
            writeTag("s");             // strikethrough placeholder for magic text todo: magic text?
        }
        if (!string.IsNullOrEmpty(component.Color) || !string.IsNullOrEmpty(parent?.Color))
        {
            writeColor(component.Color ?? parent.Color);
        }

        void writeTag(string tag)
        {
            formattedString.Append($"<{tag}>");
            closeTags.Push($"</{tag}>");
        }

        void writeColor(string color)
        {
            switch (color)
            {
            case "l":                       // bold
                writeTag("b");
                break;

            case "o":                       // italic
                writeTag("i");
                break;

            case "n":                       // underlined
                writeTag("u");
                break;

            case "m":                       // strikethrough
                writeTag("s");
                break;

            case "r":                       // reset - don't write any color at all
                return;

            case "k":                       // obfuscated or magic
                writeTag("s");              // placeholder for obfuscated text
                break;

            default:                        // "color" isn't a formatting code so it must be an actual color
                string htmlColorCode = ColorUtility.ToHtmlStringRGB(GetChatForegroundColor(color));
                formattedString.Append($"<color=#{htmlColorCode}>");
                closeTags.Push("</color>");
                break;
            }
        }

        // write the text content of the component
        switch (component)
        {
        case StringComponent stringComponent:
            formattedString.Append(stringComponent.text);
            break;

        case TranslateComponent translateComponent:
            var formattedWith = translateComponent.with.Select(w => GetFormattedString(w, component));                      // get formatted translation inserts
            formattedString.Append(ChatTranslation.TranslateString(translateComponent.translate, formattedWith.ToArray())); // translate string and append
            break;

        default:
            throw new NotImplementedException($"{component.GetType().ToString()} is not supported yet");
        }

        // append the close tags
        formattedString.Append(string.Join(string.Empty, closeTags));

        // add extra ChatComponents
        if (component.Extra != null)
        {
            foreach (var extraComp in component.Extra)
            {
                formattedString.Append(GetFormattedString(extraComp, component));
            }
        }

        return(formattedString.ToString());
    }