Merge pull request 'feature/chat_01' (#295) from feature/chat_01 into develop

Reviewed-on: https://git.pthub.vn/Unity/perfect-world-unity/pulls/295
This commit is contained in:
cuongnv
2026-04-06 10:51:34 +00:00
5 changed files with 19 additions and 72 deletions
@@ -321,52 +321,6 @@ namespace CSNetwork
return sb.ToString();
}
/// <summary>
/// Chuyển đổi định dạng printf (C-style: %s, %d) sang string.Format (C#-style: {0}, {1})
/// </summary>
public static string ConvertPrintfToCSharpFormat(string format)
{
if (string.IsNullOrEmpty(format)) return "";
StringBuilder sb = new StringBuilder();
int argIndex = 0;
for (int i = 0; i < format.Length; i++)
{
if (format[i] == '%' && i + 1 < format.Length)
{
char next = format[i + 1];
if (next == '%') // Trường hợp %% -> %
{
sb.Append('%');
i++;
}
else
{
sb.Append('{').Append(argIndex++).Append('}');
i++;
// Nhảy qua các ký tự định dạng (ví dụ: %02d, %ls, %f)
while (i < format.Length && (char.IsDigit(format[i]) || format[i] == '.' || format[i] == 'l' || format[i] == 'u' || format[i] == 'd' || format[i] == 's' || format[i] == 'f' || format[i] == 'x'))
{
// Nếu gặp ký tự kết thúc định dạng (s, d, f, ...) thì dừng lại sau ký tự đó
char c = format[i];
if (c == 's' || c == 'd' || c == 'f' || c == 'u' || c == 'x' || c == 'g')
{
// i++; // Đã ở đúng vị trí để vòng lặp cha thực hiện i++ tiếp theo
break;
}
i++;
}
}
}
else
{
sb.Append(format[i]);
}
}
return sb.ToString();
}
/// <summary>
/// [Port] CECGameUIMan::FilterInvalidTags (EC_GameUIMan.cpp:6424)
/// Lọc bỏ các tag đặc biệt không hợp lệ trong nội dung chat nhận từ server.
@@ -1962,7 +1962,8 @@ namespace CSNetwork
string szName = pHost.GetName();
char[] szText = new char[80];
AUICommon.AUI_ConvertChatString(ref szName, ref szText, false);
string fmt = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT));
Debug.Log($"[Cuong] {szText}");
string fmt = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT));
string str;
try {
str = string.Format(fmt, szName, szMsg);
@@ -2057,8 +2058,8 @@ namespace CSNetwork
string strMsg = Encoding.Unicode.GetString(p.Msg.ToArray());
string strSrcName = Encoding.Unicode.GetString(p.Name.ToArray());
string fmt = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT));
Debug.Log($"[Cuong] {strMsg}");
string fmt = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT));
string formatted;
try {
formatted = string.Format(fmt, strSrcName, strMsg);
@@ -2208,13 +2209,14 @@ namespace CSNetwork
{
char[] szText = new char[80];
AUICommon.AUI_ConvertChatString(ref szName,ref szText, false);
string fmt = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT));
Debug.Log($"[Cuong] {szText}");
string fmt = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT), szText);
string str = string.Format(
fmt,
szName,
szMsg
);
Debug.Log($"[Cuong] {str} {fmt}");
// [Port] Gọi AddChatMessage để hiển thị lên UI Chat Box.
// AddChatMessage bên trong đã tự publish ChatMessageEvent (cho ChatPanelUI)
// VÀ EventChatMessageOnTopPlayer (cho Head Bubble) nếu channel thuộc nhóm
@@ -2231,8 +2233,7 @@ namespace CSNetwork
if (pNPC != null)
{
string str;
string template = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT2));
string template = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_CHAT2));
string message = string.Format(
template,
@@ -2275,7 +2276,7 @@ namespace CSNetwork
if (p.Channel == 0 /* CHANNEL_NORMAL */ || p.Channel == 1 /* CHANNEL_NORMALRE */)
{
// Format: "[Name] whispers to [You]: [Message]"
string fmt = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_PRIVATECHAT1));
string fmt = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_PRIVATECHAT1));
BMLogger.Log($"[Cuong] OnPrtcPrivateChat {fmt}");
string formatted;
try {
+1 -1
View File
@@ -649,7 +649,7 @@ namespace BrewMonster
cmd_pickup_money pCmd = GPDataTypeHelper.FromBytes<cmd_pickup_money>(data);
AddMoneyAmount(pCmd.amount);
CECGameRun pGameRun = EC_Game.GetGameRun();
pGameRun.AddFixedMessage((int)FixedMsg.FIXMSG_PICKUPMONEY);
pGameRun.AddFixedMessage((int)FixedMsg.FIXMSG_PICKUPMONEY, pCmd.amount);
BubbleText((int)BubbleTextType.BUBBLE_MONEY, (uint)pCmd.amount);
}
+1 -1
View File
@@ -457,7 +457,7 @@ namespace BrewMonster.Scripts.ChatUI
// Tránh truyền "&target&" vì kết quả sẽ là "&&target&&" → regex chỉ match "target"
// nhưng để lại & dư ở ngoài → UI hiển thị "&target&" thay vì link sạch.
CECStringTab pStrTab = EC_Game.GetFixedMsgs();
string fmt = AUICommon.ConvertPrintfToCSharpFormat(pStrTab.GetWideString((int)FixedMsg.FIXMSG_PRIVATECHAT2));
string fmt = AUIDialog.FormatPrintf(pStrTab.GetWideString((int)FixedMsg.FIXMSG_PRIVATECHAT2));
string localMsg;
try
{
+8 -16
View File
@@ -773,23 +773,19 @@ public partial class CECGameRun : ITickable
return;
}
// Format the message with provided arguments
// fixed_msg.txt uses printf-style (%d, %s); not C# {0} placeholders
string szFormattedMsg;
try
{
if (args != null && args.Length > 0)
{
szFormattedMsg = string.Format(szFixMsg, args);
}
szFormattedMsg = AUIDialog.FormatPrintf(szFixMsg, args);
else
{
szFormattedMsg = szFixMsg;
}
}
catch (System.FormatException ex)
catch (Exception ex)
{
Debug.LogError($"[AddFixedMessage] Format error for message {iMsg}: {ex.Message}");
szFormattedMsg = szFixMsg; // Use unformatted message as fallback
szFormattedMsg = szFixMsg;
}
// Try to add to in-game UI chat
@@ -828,23 +824,19 @@ public partial class CECGameRun : ITickable
return;
}
// Format the message with provided arguments
// fixed_msg.txt uses printf-style (%d, %s); not C# {0} placeholders
string szFormattedMsg;
try
{
if (args != null && args.Length > 0)
{
szFormattedMsg = string.Format(szFixMsg, args);
}
szFormattedMsg = AUIDialog.FormatPrintf(szFixMsg, args);
else
{
szFormattedMsg = szFixMsg;
}
}
catch (System.FormatException ex)
catch (Exception ex)
{
Debug.LogError($"[AddFixedChannelMsg] Format error for message {iMsg}: {ex.Message}");
szFormattedMsg = szFixMsg; // Use unformatted message as fallback
szFormattedMsg = szFixMsg;
}
// Try to add to in-game UI chat with specific channel