using System;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using TMPro;
namespace PerfectWorld.Scripts.Common
{
///
/// Utility class for formatting text with special characters and color codes
/// Handles Perfect World's custom text formatting system
///
public static class EC_TextFormatter
{
///
/// Format text for TextMeshPro components with rich text support
///
/// Raw text with formatting codes
/// Formatted text for TextMeshPro
public static string FormatForTextMeshPro(string text)
{
if (string.IsNullOrEmpty(text))
return string.Empty;
StringBuilder result = new StringBuilder(text);
// Handle line breaks (\r)
result.Replace("\\r", "\n");
// Handle color codes (^RRGGBB format)
string processedText = ProcessColorCodes(result);
return processedText;
}
///
/// Format text for legacy Text components (limited rich text support)
///
/// Raw text with formatting codes
/// Formatted text for legacy Text
public static string FormatForLegacyText(string text)
{
if (string.IsNullOrEmpty(text))
return string.Empty;
StringBuilder result = new StringBuilder(text);
// Handle line breaks (\r)
result.Replace("\\r", "\n");
// Handle color codes (^RRGGBB format) - convert to Unity's rich text format
string processedText = ProcessColorCodesForLegacy(result);
return processedText;
}
///
/// Process color codes for TextMeshPro (supports hex colors directly)
///
private static string ProcessColorCodes(StringBuilder text)
{
// Pattern to match color codes: ^ followed by 6 hex characters
string pattern = @"\^([0-9A-Fa-f]{6})";
return Regex.Replace(text.ToString(), pattern, match =>
{
string hexColor = match.Groups[1].Value;
return $"";
}, RegexOptions.None);
}
///
/// Process color codes for legacy Text (convert to Unity rich text format)
///
private static string ProcessColorCodesForLegacy(StringBuilder text)
{
// Pattern to match color codes: ^ followed by 6 hex characters
string pattern = @"\^([0-9A-Fa-f]{6})";
return Regex.Replace(text.ToString(), pattern, match =>
{
string hexColor = match.Groups[1].Value;
// Convert hex to Unity color format
Color color = HexToColor(hexColor);
return $"";
}, RegexOptions.None);
}
///
/// Convert hex color string to Unity Color
///
/// Hex color string (e.g., "ffcb4a")
/// Unity Color object
private static Color HexToColor(string hex)
{
if (hex.Length != 6)
return Color.white;
try
{
int r = Convert.ToInt32(hex.Substring(0, 2), 16);
int g = Convert.ToInt32(hex.Substring(2, 2), 16);
int b = Convert.ToInt32(hex.Substring(4, 2), 16);
return new Color(r / 255f, g / 255f, b / 255f, 1f);
}
catch
{
return Color.white;
}
}
///
/// Apply formatted text to a TextMeshPro component
///
/// TextMeshPro component
/// Raw text with formatting codes
public static void SetFormattedText(TMPro.TextMeshProUGUI textComponent, string rawText)
{
if (textComponent == null)
return;
textComponent.text = FormatForTextMeshPro(rawText);
}
///
/// Apply formatted text to a legacy Text component
///
/// Legacy Text component
/// Raw text with formatting codes
public static void SetFormattedText(UnityEngine.UI.Text textComponent, string rawText)
{
if (textComponent == null)
return;
textComponent.text = FormatForLegacyText(rawText);
}
///
/// Get formatted text for display (auto-detects best format)
///
/// Raw text with formatting codes
/// Whether to prefer TextMeshPro formatting
/// Formatted text
public static string GetFormattedText(string rawText, bool preferTextMeshPro = true)
{
if (preferTextMeshPro)
return FormatForTextMeshPro(rawText);
else
return FormatForLegacyText(rawText);
}
///
/// Test method to demonstrate formatting with the provided example
///
public static void TestFormatting()
{
string testText = @"^ffcb4aTương truyền mang chiếc chuông này, muốn tới nơi nào, chỉ cần lắc nhẹ là sẽ tới.\r^5998FFĐể vật phẩm này trong túi đồ, khi tìm Di chuyển tiến hành di chuyển, chỉ cần kích vào điểm di chuyển trên bản đồ\rsẽ đến thẳng được địa điểm đó. Phí trung chuyển sẽ bị khấu trừ ít.\r^00ffffCó thời hạn trong một tuần, sau một tuần sẽ biến mất.";
Debug.Log("=== Text Formatting Test ===");
Debug.Log("Original Text:");
Debug.Log(testText);
Debug.Log("\nFormatted for TextMeshPro:");
Debug.Log(FormatForTextMeshPro(testText));
Debug.Log("\nFormatted for Legacy Text:");
Debug.Log(FormatForLegacyText(testText));
}
}
}