llgesture.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * @file llgesture.cpp
  3. *
  4. * $LicenseInfo:firstyear=2002&license=viewergpl$
  5. *
  6. * Copyright (c) 2002-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include "boost/tokenizer.hpp"
  33. #include "indra_constants.h"
  34. #include "llgesture.h"
  35. #include "llstl.h"
  36. #include "llmessage.h"
  37. constexpr S32 MAX_GESTURES = 4096;
  38. LLGesture::LLGesture()
  39. : mKey(KEY_NONE),
  40. mMask(MASK_NONE),
  41. mTrigger(),
  42. mTriggerLower(),
  43. mSoundItemID(),
  44. mAnimation(),
  45. mOutputString()
  46. {
  47. }
  48. LLGesture::LLGesture(KEY key, MASK mask, const std::string& trigger,
  49. const LLUUID& sound_item_id,
  50. const std::string& animation,
  51. const std::string& output_string)
  52. : mKey(key),
  53. mMask(mask),
  54. mTrigger(trigger),
  55. mTriggerLower(trigger),
  56. mSoundItemID(sound_item_id),
  57. mAnimation(animation),
  58. mOutputString(output_string)
  59. {
  60. mTriggerLower = utf8str_tolower(mTriggerLower);
  61. }
  62. LLGesture::LLGesture(U8** buffer, S32 max_size)
  63. {
  64. *buffer = deserialize(*buffer, max_size);
  65. }
  66. LLGesture::LLGesture(const LLGesture& rhs)
  67. {
  68. mKey = rhs.mKey;
  69. mMask = rhs.mMask;
  70. mTrigger = rhs.mTrigger;
  71. mTriggerLower = rhs.mTriggerLower;
  72. mSoundItemID = rhs.mSoundItemID;
  73. mAnimation = rhs.mAnimation;
  74. mOutputString = rhs.mOutputString;
  75. }
  76. const LLGesture& LLGesture::operator=(const LLGesture& rhs)
  77. {
  78. mKey = rhs.mKey;
  79. mMask = rhs.mMask;
  80. mTrigger = rhs.mTrigger;
  81. mTriggerLower = rhs.mTriggerLower;
  82. mSoundItemID = rhs.mSoundItemID;
  83. mAnimation = rhs.mAnimation;
  84. mOutputString = rhs.mOutputString;
  85. return (*this);
  86. }
  87. bool LLGesture::trigger(KEY key, MASK mask)
  88. {
  89. llwarns << "Parent class trigger called: you probably didn't mean this."
  90. << llendl;
  91. return false;
  92. }
  93. bool LLGesture::trigger(const std::string& trigger_string)
  94. {
  95. llwarns << "Parent class trigger called: you probably didn't mean this."
  96. << llendl;
  97. return false;
  98. }
  99. // NOT endian-neutral
  100. U8* LLGesture::serialize(U8* buffer) const
  101. {
  102. htonmemcpy(buffer, &mKey, MVT_S8, 1);
  103. buffer += sizeof(mKey);
  104. htonmemcpy(buffer, &mMask, MVT_U32, 4);
  105. buffer += sizeof(mMask);
  106. htonmemcpy(buffer, mSoundItemID.mData, MVT_LLUUID, 16);
  107. buffer += 16;
  108. memcpy(buffer, mTrigger.c_str(), mTrigger.length() + 1);
  109. buffer += mTrigger.length() + 1;
  110. memcpy(buffer, mAnimation.c_str(), mAnimation.length() + 1);
  111. buffer += mAnimation.length() + 1;
  112. memcpy(buffer, mOutputString.c_str(), mOutputString.length() + 1);
  113. buffer += mOutputString.length() + 1;
  114. return buffer;
  115. }
  116. U8* LLGesture::deserialize(U8* buffer, S32 max_size)
  117. {
  118. U8* tmp = buffer;
  119. if (tmp + sizeof(mKey) + sizeof(mMask) + 16 > buffer + max_size)
  120. {
  121. llwarns << "Attempt to read past end of buffer, bad data !" << llendl;
  122. return buffer;
  123. }
  124. htonmemcpy(&mKey, tmp, MVT_S8, 1);
  125. tmp += sizeof(mKey);
  126. htonmemcpy(&mMask, tmp, MVT_U32, 4);
  127. tmp += sizeof(mMask);
  128. htonmemcpy(mSoundItemID.mData, tmp, MVT_LLUUID, 16);
  129. tmp += 16;
  130. mTrigger.assign((char *)tmp);
  131. mTriggerLower = mTrigger;
  132. mTriggerLower = utf8str_tolower(mTriggerLower);
  133. tmp += mTrigger.length() + 1;
  134. mAnimation.assign((char *)tmp);
  135. //RN: force animation names to lower case
  136. // must do this for backwards compatibility
  137. mAnimation = utf8str_tolower(mAnimation);
  138. tmp += mAnimation.length() + 1;
  139. mOutputString.assign((char *)tmp);
  140. tmp += mOutputString.length() + 1;
  141. if (tmp > buffer + max_size)
  142. {
  143. llwarns << "Read past end of buffer, bad data !" << llendl;
  144. return tmp;
  145. }
  146. return tmp;
  147. }
  148. S32 LLGesture::getMaxSerialSize()
  149. {
  150. return MAX_SERIAL_SIZE;
  151. }
  152. //---------------------------------------------------------------------
  153. // LLGestureList
  154. //---------------------------------------------------------------------
  155. LLGestureList::LLGestureList()
  156. : mList(0)
  157. {
  158. }
  159. LLGestureList::~LLGestureList()
  160. {
  161. deleteAll();
  162. }
  163. void LLGestureList::deleteAll()
  164. {
  165. delete_and_clear(mList);
  166. }
  167. // Iterates through space delimited tokens in string, triggering any gestures
  168. // found. Generates a revised string that has the found tokens replaced by
  169. // their replacement strings and (as a minor side effect) has multiple spaces
  170. // in a row replaced by single spaces.
  171. bool LLGestureList::triggerAndReviseString(const std::string& string,
  172. std::string* revised_string)
  173. {
  174. std::string tokenized = string;
  175. bool found_gestures = false;
  176. bool first_token = true;
  177. typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
  178. boost::char_separator<char> sep(" ");
  179. tokenizer tokens(string, sep);
  180. tokenizer::iterator token_iter;
  181. for (token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
  182. {
  183. LLGesture* gesture = NULL;
  184. if (!found_gestures) // Only pay attention to the first gesture in the string.
  185. {
  186. std::string cur_token_lower = *token_iter;
  187. LLStringUtil::toLower(cur_token_lower);
  188. for (S32 i = 0, count = mList.size(); i < count; ++i)
  189. {
  190. gesture = mList[i];
  191. if (gesture && gesture->trigger(cur_token_lower))
  192. {
  193. if (!gesture->getOutputString().empty())
  194. {
  195. if (!first_token)
  196. {
  197. revised_string->append(" ");
  198. }
  199. // Do not muck with the user's capitalization if we
  200. // do not have to.
  201. const std::string& output = gesture->getOutputString();
  202. std::string output_lower = std::string(output.c_str());
  203. LLStringUtil::toLower(output_lower);
  204. if (cur_token_lower == output_lower)
  205. {
  206. revised_string->append(*token_iter);
  207. }
  208. else
  209. {
  210. revised_string->append(output);
  211. }
  212. }
  213. found_gestures = true;
  214. break;
  215. }
  216. gesture = NULL;
  217. }
  218. }
  219. if (!gesture)
  220. {
  221. if (!first_token)
  222. {
  223. revised_string->append(" ");
  224. }
  225. revised_string->append(*token_iter);
  226. }
  227. first_token = false;
  228. }
  229. return found_gestures;
  230. }
  231. bool LLGestureList::trigger(KEY key, MASK mask)
  232. {
  233. for (S32 i = 0, count = mList.size(); i < count; ++i)
  234. {
  235. LLGesture* gesture = mList[i];
  236. if (gesture)
  237. {
  238. if (gesture->trigger(key, mask))
  239. {
  240. return true;
  241. }
  242. }
  243. else
  244. {
  245. llwarns << "NULL gesture in gesture list (" << i << ")" << llendl;
  246. }
  247. }
  248. return false;
  249. }
  250. // NOT endian-neutral
  251. U8* LLGestureList::serialize(U8* buffer) const
  252. {
  253. // a single S32 serves as the header that tells us how many to read
  254. S32 count = mList.size();
  255. htonmemcpy(buffer, &count, MVT_S32, 4);
  256. buffer += sizeof(count);
  257. for (S32 i = 0; i < count; i++)
  258. {
  259. buffer = mList[i]->serialize(buffer);
  260. }
  261. return buffer;
  262. }
  263. U8* LLGestureList::deserialize(U8* buffer, S32 max_size)
  264. {
  265. deleteAll();
  266. S32 count;
  267. U8* tmp = buffer;
  268. if (tmp + sizeof(count) > buffer + max_size)
  269. {
  270. llwarns << "Invalid max_size" << llendl;
  271. return buffer;
  272. }
  273. htonmemcpy(&count, tmp, MVT_S32, 4);
  274. if (count > MAX_GESTURES)
  275. {
  276. llwarns << "Unreasonably large gesture list count in deserialize: "
  277. << count << llendl;
  278. return tmp;
  279. }
  280. tmp += sizeof(count);
  281. mList.resize(count);
  282. for (S32 i = 0; i < count; i++)
  283. {
  284. mList[i] = create_gesture(&tmp, max_size - (S32)(tmp - buffer));
  285. if (tmp - buffer > max_size)
  286. {
  287. llwarns << "Deserialization read past end of buffer, bad data !"
  288. << llendl;
  289. return tmp;
  290. }
  291. }
  292. return tmp;
  293. }
  294. // This is a helper for deserialize: it gets overridden by LLViewerGestureList
  295. // to create LLViewerGestures overridden by child class to use local LLGesture
  296. // implementation
  297. LLGesture* LLGestureList::create_gesture(U8** buffer, S32 max_size)
  298. {
  299. return new LLGesture(buffer, max_size);
  300. }
  301. S32 LLGestureList::getMaxSerialSize()
  302. {
  303. return SERIAL_HEADER_SIZE + count() * LLGesture::getMaxSerialSize();
  304. }