Chat.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. ** Command & Conquer Generals(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, 0, 0,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,0,0,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. RoomType roomType = StagingRoom;
  111. if (getCurrentGroupRoom())
  112. roomType = GroupRoom;
  113. PeerRequest req;
  114. req.text = message.str();
  115. message.trim();
  116. // Echo the user's input to the chat window
  117. if (!message.isEmpty())
  118. {
  119. if (!playerListbox)
  120. {
  121. // Public message
  122. req.message.isAction = isAction;
  123. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEROOM;
  124. TheGameSpyPeerMessageQueue->addRequest(req);
  125. return false;
  126. }
  127. // Get the selections (is this a private message?)
  128. Int maxSel = GadgetListBoxGetListLength(playerListbox);
  129. Int *selections;
  130. GadgetListBoxGetSelected(playerListbox, (Int *)&selections);
  131. if (selections[0] == -1)
  132. {
  133. // Public message
  134. req.message.isAction = isAction;
  135. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEROOM;
  136. TheGameSpyPeerMessageQueue->addRequest(req);
  137. return false;
  138. }
  139. else
  140. {
  141. // Private message
  142. // Construct a list
  143. AsciiString names = AsciiString::TheEmptyString;
  144. AsciiString tmp = AsciiString::TheEmptyString;
  145. AsciiString aStr; // AsciiString buf for translating Unicode entries
  146. names.format("%s", TheGameSpyInfo->getLocalName().str());
  147. for (int i=0; i<maxSel; i++)
  148. {
  149. if (selections[i] != -1)
  150. {
  151. aStr.translate(GadgetListBoxGetText(playerListbox, selections[i], GadgetListBoxGetNumColumns(playerListbox)-1));
  152. if (aStr.compareNoCase(TheGameSpyInfo->getLocalName()))
  153. {
  154. tmp.format(",%s", aStr.str());
  155. names.concat(tmp);
  156. }
  157. }
  158. else
  159. {
  160. break;
  161. }
  162. }
  163. if (!names.isEmpty())
  164. {
  165. req.nick = names.str();
  166. req.message.isAction = isAction;
  167. req.peerRequestType = PeerRequest::PEERREQUEST_MESSAGEPLAYER;
  168. TheGameSpyPeerMessageQueue->addRequest(req);
  169. }
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. void GameSpyInfo::addChat( AsciiString nick, Int profileID, UnicodeString msg, Bool isPublic, Bool isAction, GameWindow *win )
  176. {
  177. PlayerInfoMap::iterator it = getPlayerInfoMap()->find(nick);
  178. if (it != getPlayerInfoMap()->end())
  179. {
  180. addChat( it->second, msg, isPublic, isAction, win );
  181. }
  182. else
  183. {
  184. }
  185. }
  186. void GameSpyInfo::addChat( PlayerInfo p, UnicodeString msg, Bool isPublic, Bool isAction, GameWindow *win )
  187. {
  188. Int style;
  189. if(isSavedIgnored(p.m_profileID) || isIgnored(p.m_name))
  190. return;
  191. Bool isOwner = p.m_flags & PEER_FLAG_OP;
  192. Bool isBuddy = getBuddyMap()->find(p.m_profileID) != getBuddyMap()->end();
  193. Bool isMe = p.m_name.compare(TheGameSpyInfo->getLocalName()) == 0;
  194. if(!isMe)
  195. {
  196. if(m_disallowAsainText)
  197. {
  198. const WideChar *buff = msg.str();
  199. Int length = msg.getLength();
  200. for(Int i = 0; i < length; ++i)
  201. {
  202. if(buff[i] >= 256)
  203. return;
  204. }
  205. }
  206. else if(m_disallowNonAsianText)
  207. {
  208. const WideChar *buff = msg.str();
  209. Int length = msg.getLength();
  210. Bool hasUnicode = FALSE;
  211. for(Int i = 0; i < length; ++i)
  212. {
  213. if(buff[i] >= 256)
  214. {
  215. hasUnicode = TRUE;
  216. break;
  217. }
  218. }
  219. if(!hasUnicode)
  220. return;
  221. }
  222. if (!isPublic)
  223. {
  224. AudioEventRTS privMsgAudio("GUIMessageReceived");
  225. if( TheAudio )
  226. {
  227. TheAudio->addAudioEvent( &privMsgAudio );
  228. } // end if
  229. }
  230. }
  231. if (isBuddy)
  232. {
  233. style = GSCOLOR_CHAT_BUDDY;
  234. }
  235. else if (isPublic && isAction)
  236. {
  237. style = (isOwner)?GSCOLOR_CHAT_OWNER_EMOTE:GSCOLOR_CHAT_EMOTE;
  238. }
  239. else if (isPublic)
  240. {
  241. style = (isOwner)?GSCOLOR_CHAT_OWNER:GSCOLOR_CHAT_NORMAL;
  242. }
  243. else if (isAction)
  244. {
  245. style = (isOwner)?GSCOLOR_CHAT_PRIVATE_OWNER_EMOTE:GSCOLOR_CHAT_PRIVATE_EMOTE;
  246. }
  247. else
  248. {
  249. style = (isOwner)?GSCOLOR_CHAT_PRIVATE_OWNER:GSCOLOR_CHAT_PRIVATE;
  250. }
  251. UnicodeString name;
  252. name.translate(p.m_name);
  253. // filters language
  254. // if( TheGlobalData->m_languageFilterPref )
  255. // {
  256. TheLanguageFilter->filterLine(msg);
  257. // }
  258. UnicodeString fullMsg;
  259. if (isAction)
  260. {
  261. fullMsg.format( L"%ls %ls", name.str(), msg.str() );
  262. }
  263. else
  264. {
  265. fullMsg.format( L"[%ls] %ls", name.str(), msg.str() );
  266. }
  267. Int index = addText(fullMsg, GameSpyColor[style], win);
  268. if (index >= 0)
  269. {
  270. GadgetListBoxSetItemData(win, (void *)p.m_profileID, index);
  271. }
  272. }
  273. Int GameSpyInfo::addText( UnicodeString message, Color c, GameWindow *win )
  274. {
  275. if (TheGameSpyGame && TheGameSpyGame->isInGame() && TheGameSpyGame->isGameInProgress())
  276. {
  277. static AudioEventRTS messageFromChatSound("GUIMessageReceived");
  278. TheAudio->addAudioEvent(&messageFromChatSound);
  279. TheInGameUI->message(message);
  280. }
  281. if (!win)
  282. {
  283. // try to pick up a registered text window
  284. if (m_textWindows.empty())
  285. return -1;
  286. win = *(m_textWindows.begin());
  287. }
  288. Int index = GadgetListBoxAddEntryText(win, message, c, -1, -1);
  289. GadgetListBoxSetItemData(win, (void *)-1, index);
  290. return index;
  291. }
  292. void GameSpyInfo::registerTextWindow( GameWindow *win )
  293. {
  294. m_textWindows.insert(win);
  295. }
  296. void GameSpyInfo::unregisterTextWindow( GameWindow *win )
  297. {
  298. m_textWindows.erase(win);
  299. }