Chat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Chat.cpp //////////////////////////////////////////////////////
  24. // Generals GameSpy chat-related code
  25. // Author: Matthew D. Campbell, July 2002
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include "Common/AudioEventRTS.h"
  28. #include "Common/INI.h"
  29. #include "GameClient/GameText.h"
  30. #include "GameClient/GadgetListBox.h"
  31. #include "GameClient/LanguageFilter.h"
  32. #include "GameClient/GameWindowManager.h"
  33. #include "GameNetwork/GameSpy/PeerDefsImplementation.h"
  34. #include "GameNetwork/GameSpy/PeerThread.h"
  35. #include "GameClient/InGameUI.h"
  36. #ifdef _INTERNAL
  37. // for occasional debugging...
  38. //#pragma optimize("", off)
  39. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  40. #endif
  41. #define OFFSET(x) (sizeof(Int) * (x))
  42. static const FieldParse GameSpyColorFieldParse[] =
  43. {
  44. { "Default", INI::parseColorInt, NULL, OFFSET(GSCOLOR_DEFAULT) },
  45. { "CurrentRoom", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CURRENTROOM) },
  46. { "ChatRoom", INI::parseColorInt, NULL, OFFSET(GSCOLOR_ROOM) },
  47. { "Game", INI::parseColorInt, NULL, OFFSET(GSCOLOR_GAME) },
  48. { "GameFull", INI::parseColorInt, NULL, OFFSET(GSCOLOR_GAME_FULL) },
  49. { "GameCRCMismatch", INI::parseColorInt, NULL, OFFSET(GSCOLOR_GAME_CRCMISMATCH) },
  50. { "PlayerNormal", INI::parseColorInt, NULL, OFFSET(GSCOLOR_PLAYER_NORMAL) },
  51. { "PlayerOwner", INI::parseColorInt, NULL, OFFSET(GSCOLOR_PLAYER_OWNER) },
  52. { "PlayerBuddy", INI::parseColorInt, NULL, OFFSET(GSCOLOR_PLAYER_BUDDY) },
  53. { "PlayerSelf", INI::parseColorInt, NULL, OFFSET(GSCOLOR_PLAYER_SELF) },
  54. { "PlayerIgnored", INI::parseColorInt, NULL, OFFSET(GSCOLOR_PLAYER_IGNORED) },
  55. { "ChatNormal", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_NORMAL) },
  56. { "ChatEmote", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_EMOTE) },
  57. { "ChatOwner", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_OWNER) },
  58. { "ChatOwnerEmote", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_OWNER_EMOTE) },
  59. { "ChatPriv", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_PRIVATE) },
  60. { "ChatPrivEmote", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_PRIVATE_EMOTE) },
  61. { "ChatPrivOwner", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_PRIVATE_OWNER) },
  62. { "ChatPrivOwnerEmote", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_PRIVATE_OWNER_EMOTE) },
  63. { "ChatBuddy", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_BUDDY) },
  64. { "ChatSelf", INI::parseColorInt, NULL, OFFSET(GSCOLOR_CHAT_SELF) },
  65. { "AcceptTrue", INI::parseColorInt, NULL, OFFSET(GSCOLOR_ACCEPT_TRUE) },
  66. { "AcceptFalse", INI::parseColorInt, NULL, OFFSET(GSCOLOR_ACCEPT_FALSE) },
  67. { "MapSelected", INI::parseColorInt, NULL, OFFSET(GSCOLOR_MAP_SELECTED) },
  68. { "MapUnselected", INI::parseColorInt, NULL, OFFSET(GSCOLOR_MAP_UNSELECTED) },
  69. { "MOTD", INI::parseColorInt, NULL, OFFSET(GSCOLOR_MOTD) },
  70. { "MOTDHeading", INI::parseColorInt, NULL, OFFSET(GSCOLOR_MOTD_HEADING) },
  71. { NULL, NULL, NULL, 0 } // keep this last
  72. };
  73. void INI::parseOnlineChatColorDefinition( INI* ini )
  74. {
  75. // parse the ini definition
  76. ini->initFromINI( GameSpyColor, GameSpyColorFieldParse );
  77. }
  78. Color GameSpyColor[GSCOLOR_MAX] =
  79. {
  80. GameMakeColor(255,255,255,255), // GSCOLOR_DEFAULT
  81. GameMakeColor(255,255, 0,255), // GSCOLOR_CURRENTROOM
  82. GameMakeColor(255,255,255,255), // GSCOLOR_ROOM
  83. GameMakeColor(128,128,0,255), // GSCOLOR_GAME
  84. GameMakeColor(128,128,128,255), // GSCOLOR_GAME_FULL
  85. GameMakeColor(128,128,128,255), // GSCOLOR_GAME_CRCMISMATCH
  86. GameMakeColor(255,255,255,255), // GSCOLOR_PLAYER_NORMAL
  87. GameMakeColor(255, 0,255,255), // GSCOLOR_PLAYER_OWNER
  88. GameMakeColor(255, 0,128,255), // GSCOLOR_PLAYER_BUDDY
  89. GameMakeColor(255, 0, 0,255), // GSCOLOR_PLAYER_SELF
  90. GameMakeColor(128,128,128,255), // GSCOLOR_PLAYER_IGNORED
  91. GameMakeColor(255,255,255,255), // GSCOLOR_CHAT_NORMAL
  92. GameMakeColor(255,128,0,255), // GSCOLOR_CHAT_EMOTE,
  93. GameMakeColor(255,255,0,255), // GSCOLOR_CHAT_OWNER,
  94. GameMakeColor(128,255,0,255), // GSCOLOR_CHAT_OWNER_EMOTE,
  95. GameMakeColor(0,0,255,255), // GSCOLOR_CHAT_PRIVATE,
  96. GameMakeColor(0,255,255,255), // GSCOLOR_CHAT_PRIVATE_EMOTE,
  97. GameMakeColor(255,0,255,255), // GSCOLOR_CHAT_PRIVATE_OWNER,
  98. GameMakeColor(255,128,255,255), // GSCOLOR_CHAT_PRIVATE_OWNER_EMOTE,
  99. GameMakeColor(255, 0,255,255), // GSCOLOR_CHAT_BUDDY,
  100. GameMakeColor(255, 0,128,255), // GSCOLOR_CHAT_SELF,
  101. GameMakeColor( 0,255, 0,255), // GSCOLOR_ACCEPT_TRUE,
  102. GameMakeColor(255, 0, 0,255), // GSCOLOR_ACCEPT_FALSE,
  103. GameMakeColor(255,255, 0,255), // GSCOLOR_MAP_SELECTED,
  104. GameMakeColor(255,255,255,255), // GSCOLOR_MAP_UNSELECTED,
  105. GameMakeColor(255,255,255,255), // GSCOLOR_MOTD,
  106. GameMakeColor(255,255, 0,255), // GSCOLOR_MOTD_HEADING,
  107. };
  108. Bool GameSpyInfo::sendChat( UnicodeString message, Bool isAction, GameWindow *playerListbox )
  109. {
  110. static UnicodeString s_prevMsg = UnicodeString::TheEmptyString; //stop spam before it happens
  111. RoomType roomType = StagingRoom;
  112. if (getCurrentGroupRoom())
  113. roomType = GroupRoom;
  114. PeerRequest req;
  115. req.text = message.str();
  116. message.trim();
  117. // Echo the user's input to the chat window
  118. if (!message.isEmpty())
  119. {
  120. if (!playerListbox)
  121. { // Public message
  122. if( isAction || message.compare(s_prevMsg) != 0 ) //don't send duplicate messages
  123. {
  124. req.message.isAction = isAction;
  125. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEROOM;
  126. TheGameSpyPeerMessageQueue->addRequest(req);
  127. s_prevMsg = message;
  128. }
  129. return false;
  130. }
  131. // Get the selections (is this a private message?)
  132. Int maxSel = GadgetListBoxGetListLength(playerListbox);
  133. Int *selections;
  134. GadgetListBoxGetSelected(playerListbox, (Int *)&selections);
  135. if (selections[0] == -1)
  136. { // Public message
  137. if( isAction || message.compare(s_prevMsg) != 0 ) //don't send duplicate messages
  138. {
  139. req.message.isAction = isAction;
  140. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEROOM;
  141. TheGameSpyPeerMessageQueue->addRequest(req);
  142. s_prevMsg = message;
  143. }
  144. return false;
  145. }
  146. else
  147. {
  148. // Private message
  149. // Construct a list
  150. AsciiString names = AsciiString::TheEmptyString;
  151. AsciiString tmp = AsciiString::TheEmptyString;
  152. AsciiString aStr; // AsciiString buf for translating Unicode entries
  153. names.format("%s", TheGameSpyInfo->getLocalName().str());
  154. for (int i=0; i<maxSel; i++)
  155. {
  156. if (selections[i] != -1)
  157. {
  158. aStr.translate(GadgetListBoxGetText(playerListbox, selections[i], GadgetListBoxGetNumColumns(playerListbox)-1));
  159. if (aStr.compareNoCase(TheGameSpyInfo->getLocalName()))
  160. {
  161. tmp.format(",%s", aStr.str());
  162. names.concat(tmp);
  163. }
  164. }
  165. else
  166. {
  167. break;
  168. }
  169. }
  170. if (!names.isEmpty())
  171. {
  172. req.nick = names.str();
  173. req.message.isAction = isAction;
  174. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEPLAYER;
  175. TheGameSpyPeerMessageQueue->addRequest(req);
  176. }
  177. s_prevMsg = message;
  178. return true;
  179. }
  180. }
  181. s_prevMsg = message;
  182. return false;
  183. }
  184. void GameSpyInfo::addChat( AsciiString nick, Int profileID, UnicodeString msg, Bool isPublic, Bool isAction, GameWindow *win )
  185. {
  186. PlayerInfoMap::iterator it = getPlayerInfoMap()->find(nick);
  187. if (it != getPlayerInfoMap()->end())
  188. {
  189. addChat( it->second, msg, isPublic, isAction, win );
  190. }
  191. else
  192. {
  193. }
  194. }
  195. void GameSpyInfo::addChat( PlayerInfo p, UnicodeString msg, Bool isPublic, Bool isAction, GameWindow *win )
  196. {
  197. Int style;
  198. if(isSavedIgnored(p.m_profileID) || isIgnored(p.m_name))
  199. return;
  200. Bool isOwner = p.m_flags & PEER_FLAG_OP;
  201. Bool isBuddy = getBuddyMap()->find(p.m_profileID) != getBuddyMap()->end();
  202. Bool isMe = p.m_name.compare(TheGameSpyInfo->getLocalName()) == 0;
  203. if(!isMe)
  204. {
  205. if(m_disallowAsainText)
  206. {
  207. const WideChar *buff = msg.str();
  208. Int length = msg.getLength();
  209. for(Int i = 0; i < length; ++i)
  210. {
  211. if(buff[i] >= 256)
  212. return;
  213. }
  214. }
  215. else if(m_disallowNonAsianText)
  216. {
  217. const WideChar *buff = msg.str();
  218. Int length = msg.getLength();
  219. Bool hasUnicode = FALSE;
  220. for(Int i = 0; i < length; ++i)
  221. {
  222. if(buff[i] >= 256)
  223. {
  224. hasUnicode = TRUE;
  225. break;
  226. }
  227. }
  228. if(!hasUnicode)
  229. return;
  230. }
  231. if (!isPublic)
  232. {
  233. AudioEventRTS privMsgAudio("GUIMessageReceived");
  234. if( TheAudio )
  235. {
  236. TheAudio->addAudioEvent( &privMsgAudio );
  237. } // end if
  238. }
  239. }
  240. if (isBuddy)
  241. {
  242. style = GSCOLOR_CHAT_BUDDY;
  243. }
  244. else if (isPublic && isAction)
  245. {
  246. style = (isOwner)?GSCOLOR_CHAT_OWNER_EMOTE:GSCOLOR_CHAT_EMOTE;
  247. }
  248. else if (isPublic)
  249. {
  250. style = (isOwner)?GSCOLOR_CHAT_OWNER:GSCOLOR_CHAT_NORMAL;
  251. }
  252. else if (isAction)
  253. {
  254. style = (isOwner)?GSCOLOR_CHAT_PRIVATE_OWNER_EMOTE:GSCOLOR_CHAT_PRIVATE_EMOTE;
  255. }
  256. else
  257. {
  258. style = (isOwner)?GSCOLOR_CHAT_PRIVATE_OWNER:GSCOLOR_CHAT_PRIVATE;
  259. }
  260. UnicodeString name;
  261. name.translate(p.m_name);
  262. // filters language
  263. // if( TheGlobalData->m_languageFilterPref )
  264. // {
  265. TheLanguageFilter->filterLine(msg);
  266. // }
  267. UnicodeString fullMsg;
  268. if (isAction)
  269. {
  270. fullMsg.format( L"%ls %ls", name.str(), msg.str() );
  271. }
  272. else
  273. {
  274. fullMsg.format( L"[%ls] %ls", name.str(), msg.str() );
  275. }
  276. Int index = addText(fullMsg, GameSpyColor[style], win);
  277. if (index >= 0)
  278. {
  279. GadgetListBoxSetItemData(win, (void *)p.m_profileID, index);
  280. }
  281. }
  282. Int GameSpyInfo::addText( UnicodeString message, Color c, GameWindow *win )
  283. {
  284. if (TheGameSpyGame && TheGameSpyGame->isInGame() && TheGameSpyGame->isGameInProgress())
  285. {
  286. static AudioEventRTS messageFromChatSound("GUIMessageReceived");
  287. TheAudio->addAudioEvent(&messageFromChatSound);
  288. TheInGameUI->message(message);
  289. }
  290. if (!win)
  291. {
  292. // try to pick up a registered text window
  293. if (m_textWindows.empty())
  294. return -1;
  295. win = *(m_textWindows.begin());
  296. }
  297. Int index = GadgetListBoxAddEntryText(win, message, c, -1, -1);
  298. GadgetListBoxSetItemData(win, (void *)-1, index);
  299. return index;
  300. }
  301. void GameSpyInfo::registerTextWindow( GameWindow *win )
  302. {
  303. m_textWindows.insert(win);
  304. }
  305. void GameSpyInfo::unregisterTextWindow( GameWindow *win )
  306. {
  307. m_textWindows.erase(win);
  308. }