summaryrefslogtreecommitdiff
path: root/indra/llcharacter/llgesture.cpp
blob: 283a06e37ff77f30e339e26714b009339ae1731b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/** 
 * @file llgesture.cpp
 *
 * $LicenseInfo:firstyear=2002&license=viewergpl$
 * 
 * Copyright (c) 2002-2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "indra_constants.h"

#include "llgesture.h"
#include "llendianswizzle.h"
#include "message.h"
#include <boost/tokenizer.hpp>

// for allocating serialization buffers - these need to be updated when members change
const S32 LLGestureList::SERIAL_HEADER_SIZE = sizeof(S32);
const S32 LLGesture::MAX_SERIAL_SIZE = sizeof(KEY) + sizeof(MASK) + 16 + 26 + 41 + 41;

LLGesture::LLGesture()
:	mKey(KEY_NONE),
	mMask(MASK_NONE),
	mTrigger(),
	mTriggerLower(),
	mSoundItemID(),
	mAnimation(),
	mOutputString()
{ }

LLGesture::LLGesture(KEY key, MASK mask, const std::string &trigger,
					 const LLUUID &sound_item_id, 
					 const std::string &animation,
					 const std::string &output_string)
:
	mKey(key),
	mMask(mask),
	mTrigger(trigger),
	mTriggerLower(trigger),
	mSoundItemID(sound_item_id),
	mAnimation(animation),
	mOutputString(output_string)
{
	mTriggerLower = utf8str_tolower(mTriggerLower);
}

LLGesture::LLGesture(U8 **buffer, S32 max_size)
{
	*buffer = deserialize(*buffer, max_size);
}

LLGesture::LLGesture(const LLGesture &rhs)
{
	mKey			= rhs.mKey;
	mMask			= rhs.mMask;
	mTrigger		= rhs.mTrigger;
	mTriggerLower	= rhs.mTriggerLower;
	mSoundItemID	= rhs.mSoundItemID;
	mAnimation		= rhs.mAnimation;
	mOutputString	= rhs.mOutputString;
}

const LLGesture &LLGesture::operator =(const LLGesture &rhs)
{
	mKey			= rhs.mKey;
	mMask			= rhs.mMask;
	mTrigger		= rhs.mTrigger;
	mTriggerLower	= rhs.mTriggerLower;
	mSoundItemID	= rhs.mSoundItemID;
	mAnimation		= rhs.mAnimation;
	mOutputString	= rhs.mOutputString;
	return (*this);
}


BOOL LLGesture::trigger(KEY key, MASK mask)
{
	llwarns << "Parent class trigger called: you probably didn't mean this." << llendl;
	return FALSE;
}


BOOL LLGesture::trigger(const std::string& trigger_string)
{
	llwarns << "Parent class trigger called: you probably didn't mean this." << llendl;
	return FALSE;
}

// NOT endian-neutral
U8 *LLGesture::serialize(U8 *buffer) const
{
	htonmemcpy(buffer, &mKey, MVT_S8, 1);
	buffer += sizeof(mKey);
	htonmemcpy(buffer, &mMask, MVT_U32, 4);
	buffer += sizeof(mMask);
	htonmemcpy(buffer, mSoundItemID.mData, MVT_LLUUID, 16);
	buffer += 16;
	
	memcpy(buffer, mTrigger.c_str(), mTrigger.length() + 1);		/* Flawfinder: ignore */
	buffer += mTrigger.length() + 1;
	memcpy(buffer, mAnimation.c_str(), mAnimation.length() + 1);		/* Flawfinder: ignore */
	buffer += mAnimation.length() + 1;
	memcpy(buffer, mOutputString.c_str(), mOutputString.length() + 1);		/* Flawfinder: ignore */
	buffer += mOutputString.length() + 1;

	return buffer;
}

U8 *LLGesture::deserialize(U8 *buffer, S32 max_size)
{
	U8 *tmp = buffer;

	if (tmp + sizeof(mKey) + sizeof(mMask) + 16 > buffer + max_size)
	{
		llwarns << "Attempt to read past end of buffer, bad data!!!!" << llendl;
		return buffer;
	}

	htonmemcpy(&mKey, tmp, MVT_S8, 1);
	tmp += sizeof(mKey);
	htonmemcpy(&mMask, tmp, MVT_U32, 4);
	tmp += sizeof(mMask);
	htonmemcpy(mSoundItemID.mData, tmp, MVT_LLUUID, 16);
	tmp += 16;
	
	mTrigger.assign((char *)tmp);
	mTriggerLower = mTrigger;
	mTriggerLower = utf8str_tolower(mTriggerLower);
	tmp += mTrigger.length() + 1;
	mAnimation.assign((char *)tmp);
	//RN: force animation names to lower case
	// must do this for backwards compatibility
	mAnimation = utf8str_tolower(mAnimation);
	tmp += mAnimation.length() + 1;
	mOutputString.assign((char *)tmp);
	tmp += mOutputString.length() + 1;

	if (tmp > buffer + max_size)
	{
		llwarns << "Read past end of buffer, bad data!!!!" << llendl;
		return tmp;
	}

	return tmp;
}

S32 LLGesture::getMaxSerialSize()
{
	return MAX_SERIAL_SIZE;
}

//---------------------------------------------------------------------
// LLGestureList
//---------------------------------------------------------------------

LLGestureList::LLGestureList()
:	mList(0)
{
	// add some gestures for debugging
//	LLGesture *gesture = NULL;
/*
	gesture = new LLGesture(KEY_F2, MASK_NONE, ":-)", 
		SND_CHIRP, "dance2", ":-)" );
	mList.put(gesture);

	gesture = new LLGesture(KEY_F3, MASK_NONE, "/dance", 
		SND_OBJECT_CREATE, "dance3", "(dances)" );
	mList.put(gesture);

	gesture = new LLGesture(KEY_F4, MASK_NONE, "/boogie", 
		LLUUID::null, "dance4", LLString::null );
	mList.put(gesture);

	gesture = new LLGesture(KEY_F5, MASK_SHIFT, "/tongue", 
		LLUUID::null, "Express_Tongue_Out", LLString::null );
	mList.put(gesture);
	*/
}

LLGestureList::~LLGestureList()
{
	deleteAll();
}


void LLGestureList::deleteAll()
{
	S32 count = mList.count();
	for (S32 i = 0; i < count; i++)
	{
		delete mList.get(i);
	}
	mList.reset();
}

// Iterates through space delimited tokens in string, triggering any gestures found.
// Generates a revised string that has the found tokens replaced by their replacement strings
// and (as a minor side effect) has multiple spaces in a row replaced by single spaces.
BOOL LLGestureList::triggerAndReviseString(const LLString &string, LLString* revised_string)
{
	LLString tokenized = string;

	BOOL found_gestures = FALSE;
	BOOL first_token = TRUE;

	typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
	boost::char_separator<char> sep(" ");
	tokenizer tokens(string, sep);
	tokenizer::iterator token_iter;

	for( token_iter = tokens.begin(); token_iter != tokens.end(); ++token_iter)
	{
		LLGesture* gesture = NULL;

		if( !found_gestures ) // Only pay attention to the first gesture in the string.
		{
			LLString cur_token_lower = *token_iter;
			LLString::toLower(cur_token_lower);

			for (S32 i = 0; i < mList.count(); i++)
			{
				gesture = mList.get(i);
				if (gesture->trigger(cur_token_lower))
				{
					if( !gesture->getOutputString().empty() )
					{
						if( !first_token )
						{
							revised_string->append( " " );
						}

						// Don't muck with the user's capitalization if we don't have to.
						const std::string& output = gesture->getOutputString();
						LLString output_lower = LLString(output.c_str());
						LLString::toLower(output_lower);
						if( cur_token_lower == output_lower )
						{
							revised_string->append(*token_iter);
						}
						else
						{
							revised_string->append(output.c_str());
						}

					}
					found_gestures = TRUE;
					break;
				}
				gesture = NULL;
			}
		}

		if( !gesture )
		{
			if( !first_token )
			{
				revised_string->append( " " );
			}
			revised_string->append( *token_iter );
		}

		first_token = FALSE;
	}
	return found_gestures;
}



BOOL LLGestureList::trigger(KEY key, MASK mask)
{
	for (S32 i = 0; i < mList.count(); i++)
	{
		LLGesture* gesture = mList.get(i);
		if( gesture )
		{
			if (gesture->trigger(key, mask))
			{
				return TRUE;
			}
		}
		else
		{
			llwarns << "NULL gesture in gesture list (" << i << ")" << llendl
		}
	}
	return FALSE;
}

// NOT endian-neutral
U8 *LLGestureList::serialize(U8 *buffer) const
{
	// a single S32 serves as the header that tells us how many to read
	S32 count = mList.count();
	htonmemcpy(buffer, &count, MVT_S32, 4);
	buffer += sizeof(count);

	for (S32 i = 0; i < count; i++)
	{
		buffer = mList[i]->serialize(buffer);
	}

	return buffer;
}

const S32 MAX_GESTURES = 4096;

U8 *LLGestureList::deserialize(U8 *buffer, S32 max_size)
{
	deleteAll();

	S32 count;
	U8 *tmp = buffer;

	if (tmp + sizeof(count) > buffer + max_size)
	{
		llwarns << "Invalid max_size" << llendl;
		return buffer;
	}

	htonmemcpy(&count, tmp, MVT_S32, 4);

	if (count > MAX_GESTURES)
	{
		llwarns << "Unreasonably large gesture list count in deserialize: " << count << llendl;
		return tmp;
	}

	tmp += sizeof(count);

	mList.reserve_block(count);

	for (S32 i = 0; i < count; i++)
	{
		mList[i] = create_gesture(&tmp, max_size - (S32)(tmp - buffer));
		if (tmp - buffer > max_size)
		{
			llwarns << "Deserialization read past end of buffer, bad data!!!!" << llendl;
			return tmp;
		}
	}

	return tmp;
}

// this is a helper for deserialize
// it gets overridden by LLViewerGestureList to create LLViewerGestures
// overridden by child class to use local LLGesture implementation
LLGesture *LLGestureList::create_gesture(U8 **buffer, S32 max_size)
{
	return new LLGesture(buffer, max_size);
}

S32 LLGestureList::getMaxSerialSize()
{
	return SERIAL_HEADER_SIZE + (count() * LLGesture::getMaxSerialSize());
}