/messages/UserMessagesManager.cpp

http://cssmatch-plugin.googlecode.com/ · C++ · 243 lines · 152 code · 49 blank · 42 comment · 11 complexity · 93dcd0dfe84c0a35536422d45e7b94c7 MD5 · raw file

  1. /*
  2. * Copyright 2008-2013 Nicolas Maingot
  3. *
  4. * This file is part of CSSMatch.
  5. *
  6. * CSSMatch is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * CSSMatch is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with CSSMatch; if not, see <http://www.gnu.org/licenses>.
  18. *
  19. * Additional permission under GNU GPL version 3 section 7
  20. *
  21. * If you modify CSSMatch, or any covered work, by linking or combining
  22. * it with "Source SDK" (or a modified version of that SDK), containing
  23. * parts covered by the terms of Source SDK licence, the licensors of
  24. * CSSMatch grant you additional permission to convey the resulting work.
  25. */
  26. #include "UserMessagesManager.h"
  27. #include "RecipientFilter.h"
  28. #include "../plugin/ServerPlugin.h"
  29. #include "bitbuf.h"
  30. #include "IEngineSound.h"
  31. #include "soundflags.h" // CHAN_STATIC
  32. #include <algorithm>
  33. #include <sstream>
  34. using namespace cssmatch;
  35. using std::map;
  36. using std::string;
  37. using std::ostringstream;
  38. using std::vector;
  39. int UserMessagesManager::findMessageType(const std::string & typeName)
  40. {
  41. int id = CSSMATCH_INVALID_MSG_TYPE;
  42. // Does this message type already known?
  43. map<string, int>::iterator itTypeId = messageTypes.find(typeName);
  44. if (itTypeId == messageTypes.end())
  45. {
  46. // No, search/cache it
  47. ServerPlugin * plugin = ServerPlugin::getInstance();
  48. ValveInterfaces * interfaces = plugin->getInterfaces();
  49. char foundName[20];
  50. int foundNameSize = 0;
  51. int i = 0;
  52. int nbTypes = plugin->getConVar("cssmatch_usermessages")->GetInt();
  53. while(i < nbTypes)
  54. {
  55. if (interfaces->serverGameDll->GetUserMessageInfo(i, foundName, sizeof(foundName),
  56. foundNameSize))
  57. {
  58. if (typeName == foundName)
  59. {
  60. messageTypes[typeName] = i;
  61. id = i;
  62. break;
  63. }
  64. }
  65. i++;
  66. }
  67. if (id == CSSMATCH_INVALID_MSG_TYPE)
  68. CSSMATCH_PRINT("Unknown message type " + typeName)
  69. }
  70. else
  71. {
  72. // Yes
  73. id = itTypeId->second;
  74. }
  75. return id;
  76. }
  77. UserMessagesManager::UserMessagesManager()
  78. {
  79. engine = ServerPlugin::getInstance()->getInterfaces()->engine;
  80. }
  81. UserMessagesManager::~UserMessagesManager()
  82. {}
  83. void UserMessagesManager::chatSay(RecipientFilter & recipients, const string & message,
  84. int playerIndex)
  85. {
  86. ostringstream output;
  87. output << "\004[" << CSSMATCH_NAME << "]\001 " << message << "\n";
  88. bf_write * pBitBuf = engine->UserMessageBegin(&recipients, findMessageType("SayText"));
  89. pBitBuf->WriteByte(playerIndex);
  90. pBitBuf->WriteString(output.str().c_str());
  91. pBitBuf->WriteByte(1); // DOCUMENT ME
  92. engine->MessageEnd();
  93. }
  94. void UserMessagesManager::chatWarning(RecipientFilter & recipients, const string & message)
  95. {
  96. ostringstream output;
  97. output << "\004[" << CSSMATCH_NAME << "]\003 " << message << "\n";
  98. bf_write * pBitBuf = engine->UserMessageBegin(&recipients, findMessageType("SayText"));
  99. pBitBuf->WriteByte(0x02); // \003 => team color
  100. pBitBuf->WriteString(output.str().c_str());
  101. pBitBuf->WriteByte(0x01); // \003 => team color
  102. pBitBuf->WriteByte(1); // DOCUMENT ME
  103. engine->MessageEnd();
  104. }
  105. void UserMessagesManager::popupSay( RecipientFilter & recipients,
  106. const string & message,
  107. int lifeTime,
  108. int flags)
  109. {
  110. // Only CSSMATCH_MAX_MSG_SIZE bytes can be sent in one user message
  111. // So, as the popup menus are generally large, they are split in n messages of
  112. // CSSMATCH_MAX_MSG_SIZE bytes
  113. int iBegin = 0;
  114. int popupSize = message.size();
  115. bool moreToSend = false;
  116. do
  117. {
  118. string toSend = message.substr(iBegin, CSSMATCH_MAX_MSG_SIZE);
  119. iBegin += CSSMATCH_MAX_MSG_SIZE;
  120. bf_write * pBuffer = engine->UserMessageBegin(&recipients, findMessageType("ShowMenu"));
  121. pBuffer->WriteShort(flags); // set the flags
  122. pBuffer->WriteChar(lifeTime); // set the lifetime
  123. moreToSend = iBegin < popupSize;
  124. pBuffer->WriteByte(moreToSend); // Is the message completed?
  125. pBuffer->WriteString(toSend.c_str()); // set the text
  126. engine->MessageEnd();
  127. }
  128. while(moreToSend);
  129. }
  130. void UserMessagesManager::hintSay(RecipientFilter & recipients, const string & message)
  131. {
  132. ServerPlugin * plugin = ServerPlugin::getInstance();
  133. ValveInterfaces * interfaces = plugin->getInterfaces();
  134. bf_write * pWrite = engine->UserMessageBegin(&recipients, findMessageType("HintText"));
  135. pWrite->WriteString(message.c_str());
  136. engine->MessageEnd();
  137. // Stop the HintText annoying sound
  138. const vector<int> * recipientlist = recipients.getVector();
  139. vector<int>::const_iterator itIndex;
  140. for (itIndex = recipientlist->begin(); itIndex != recipientlist->end(); itIndex++)
  141. {
  142. interfaces->sounds->StopSound(*itIndex, CHAN_STATIC, "UI/hint.wav");
  143. }
  144. }
  145. void UserMessagesManager::motdSay(RecipientFilter recipients, MotdType type,
  146. const string & title,
  147. const string & message)
  148. {
  149. bf_write * pWrite = engine->UserMessageBegin(&recipients, findMessageType("VGUIMenu"));
  150. pWrite->WriteString("info"); // Let give some info about this message
  151. pWrite->WriteByte(1); // 1=Show this message, 0=otherwise
  152. pWrite->WriteByte(3); // "title", "type" and "msg"
  153. pWrite->WriteString("title");
  154. pWrite->WriteString(title.c_str());
  155. pWrite->WriteString("type");
  156. pWrite->WriteString(toString(type).c_str());
  157. pWrite->WriteString("msg");
  158. pWrite->WriteString(message.c_str());
  159. engine->MessageEnd();
  160. }
  161. void UserMessagesManager::showPanel(RecipientFilter recipients, const std::string & panelName,
  162. bool show)
  163. {
  164. bf_write * pWrite = engine->UserMessageBegin(&recipients, findMessageType("VGUIMenu"));
  165. pWrite->WriteString(panelName.c_str());
  166. pWrite->WriteByte((int)show);
  167. pWrite->WriteByte(0); // No more data
  168. engine->MessageEnd();
  169. }
  170. void UserMessagesManager::centerSay(RecipientFilter & recipients, const string & message)
  171. {
  172. bf_write * pWrite = engine->UserMessageBegin(&recipients, findMessageType("TextMsg"));
  173. pWrite->WriteByte(4); // DOCUMENT ME
  174. pWrite->WriteString(message.c_str());
  175. pWrite->WriteByte(0); // DOCUMENT ME
  176. engine->MessageEnd();
  177. }
  178. void UserMessagesManager::consoleSay(RecipientFilter & recipients, const string & message)
  179. {
  180. bf_write * pWrite = engine->UserMessageBegin(&recipients, findMessageType("TextMsg"));
  181. pWrite->WriteByte(HUD_PRINTNOTIFY);
  182. pWrite->WriteString(message.c_str());
  183. engine->MessageEnd();
  184. }
  185. /*void UserMessagesManager::consoleTell(int index, const string & message)
  186. {
  187. edict_t * pEntity = engine->PEntityOfEntIndex(index);
  188. if (isValidEntity(pEntity))
  189. {
  190. engine->ClientPrintf(pEntity,message.c_str()); // Crash if pEntity is a fake client or SourceTv
  191. //engine->ClientCommand(pEntity,("echo " + message + "\n").c_str());
  192. }
  193. }*/