File: SpawnArgs.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (379 lines) | stat: -rw-r--r-- 8,986 bytes parent folder | download | duplicates (3)
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
#include "SpawnArgs.h"

#include "ieclass.h"
#include "debugging/debugging.h"
#include "string/predicate.h"
#include <functional>

namespace entity
{

SpawnArgs::SpawnArgs(const IEntityClassPtr& eclass) :
	_eclass(eclass),
	_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), 
        std::function<void()>(), "EntityKeyValues"),
	_observerMutex(false),
	_isContainer(!eclass->isFixedSize()),
	_attachments(eclass->getDeclName())
{
    // Parse attachment keys
    parseAttachments();
}

SpawnArgs::SpawnArgs(const SpawnArgs& other) :
	Entity(other),
	_eclass(other.getEntityClass()),
	_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), 
        std::function<void()>(), "EntityKeyValues"),
	_observerMutex(false),
	_isContainer(other._isContainer),
	_attachments(other._attachments)
{
    // Copy keyvalue strings, not actual KeyValue pointers
    for (const KeyValuePair& p : other._keyValues)
    {
        insert(p.first, p.second->get());
    }
}

void SpawnArgs::parseAttachments()
{
    // Parse the keys
    forEachKeyValue(
        [this](const std::string& k, const std::string& v) {
            _attachments.parseDefAttachKeys(k, v);
        },
        true /* includeInherited */);

    // Post validation step
    _attachments.validateAttachments();
}

bool SpawnArgs::isModel() const
{
	std::string name = getKeyValue("name");
	std::string model = getKeyValue("model");
	std::string classname = getKeyValue("classname");

	return (classname == "func_static" && !name.empty() && name != model);
}

bool SpawnArgs::isOfType(const std::string& className)
{
	return _eclass->isOfType(className);
}

void SpawnArgs::importState(const KeyValues& keyValues)
{
	// Remove the entity key values, one by one
	while (_keyValues.size() > 0)
	{
		erase(_keyValues.begin());
	}

	for (const auto& pair : keyValues)
	{
		insert(pair.first, pair.second);
	}
}

void SpawnArgs::attachObserver(Observer* observer)
{
	ASSERT_MESSAGE(!_observerMutex, "observer cannot be attached during iteration");

	// Add the observer to the internal list
	_observers.insert(observer);

	// Now notify the observer about all the existing keys
	for(KeyValues::const_iterator i = _keyValues.begin(); i != _keyValues.end(); ++i)
    {
		observer->onKeyInsert(i->first, *i->second);
	}
}

void SpawnArgs::detachObserver(Observer* observer)
{
	ASSERT_MESSAGE(!_observerMutex, "observer cannot be detached during iteration");

	// Remove the observer from the list, if it can be found
	Observers::iterator found = _observers.find(observer);

	if (found == _observers.end())
	{
		// greebo: Observer was not found, no need to call onKeyErase()
		return;
	}

	// Remove the observer
	_observers.erase(found);

	// Call onKeyErase() for every spawnarg, so that the observer gets cleanly shut down
	for(KeyValues::const_iterator i = _keyValues.begin(); i != _keyValues.end(); ++i)
    {
		observer->onKeyErase(i->first, *i->second);
	}
}

void SpawnArgs::connectUndoSystem(IUndoSystem& undoSystem)
{
	for (const auto& keyValue : _keyValues)
	{
		keyValue.second->connectUndoSystem(undoSystem);
	}

    _undo.connectUndoSystem(undoSystem);
}

void SpawnArgs::disconnectUndoSystem(IUndoSystem& undoSystem)
{
	_undo.disconnectUndoSystem(undoSystem);

	for (const auto& keyValue : _keyValues)
	{
		keyValue.second->disconnectUndoSystem(undoSystem);
	}
}

IEntityClassPtr SpawnArgs::getEntityClass() const
{
	return _eclass;
}

void SpawnArgs::forEachKeyValue(KeyValueVisitFunc func,
                                bool includeInherited) const
{
    // Visit explicit spawnargs
    for (const KeyValuePair& pair : _keyValues)
	{
		func(pair.first, pair.second->get());
	}

    // If requested, visit inherited spawnargs from the entitydef
    if (includeInherited)
    {
        _eclass->forEachAttribute([&](const EntityClassAttribute& att, bool) {
            func(att.getName(), att.getValue());
        });
    }
}

void SpawnArgs::forEachEntityKeyValue(const EntityKeyValueVisitFunctor& func)
{
    for (const KeyValuePair& pair : _keyValues)
    {
        func(pair.first, *pair.second);
    }
}

void SpawnArgs::setKeyValue(const std::string& key, const std::string& value)
{
	if (value.empty())
	{
		// Empty value means: delete the key
		erase(key);
	}
	else
	{
		// Non-empty value, "insert" it (will overwrite existing keys - no duplicates)
		insert(key, value);
	}
}

std::string SpawnArgs::getKeyValue(const std::string& key) const
{
	// Lookup the key in the map
	KeyValues::const_iterator i = find(key);

	// If key is found, return it, otherwise lookup the default value on
	// the entity class
	if (i != _keyValues.end())
	{
		return i->second->get();
	}
	else
	{
		return _eclass->getAttributeValue(key);
	}
}

bool SpawnArgs::isInherited(const std::string& key) const
{
	// Check if we have the key in the local keyvalue map
	bool definedLocally = (find(key) != _keyValues.end());

	// The value is inherited, if it doesn't exist locally and the inherited one is not empty
	return (!definedLocally && !_eclass->getAttributeValue(key).empty());
}

void SpawnArgs::forEachAttachment(AttachmentFunc func) const
{
    _attachments.forEachAttachment(func);
}

EntityKeyValuePtr SpawnArgs::getEntityKeyValue(const std::string& key)
{
	KeyValues::const_iterator found = find(key);

	return (found != _keyValues.end()) ? found->second : EntityKeyValuePtr();
}

bool SpawnArgs::isWorldspawn() const
{
	return getKeyValue("classname") == "worldspawn";
}

bool SpawnArgs::isContainer() const
{
	return _isContainer;
}

void SpawnArgs::setIsContainer(bool isContainer)
{
	_isContainer = isContainer;
}

void SpawnArgs::notifyInsert(const std::string& key, KeyValue& value)
{
	// Block the addition/removal of new Observers during this process
	_observerMutex = true;

	// Notify all the Observers about the new keyvalue
	for (Observers::iterator i = _observers.begin(); i != _observers.end(); ++i)
	{
		(*i)->onKeyInsert(key, value);
	}

	_observerMutex = false;
}

void SpawnArgs::notifyChange(const std::string& k, const std::string& v)
{
    _observerMutex = true;

    for (Observers::iterator i = _observers.begin();
         i != _observers.end();
         ++i)
    {
		(*i)->onKeyChange(k, v);
	}

    _observerMutex = false;
}

void SpawnArgs::notifyErase(const std::string& key, KeyValue& value)
{
	// Block the addition/removal of new Observers during this process
	_observerMutex = true;

	for(Observers::iterator i = _observers.begin(); i != _observers.end(); ++i)
	{
		(*i)->onKeyErase(key, value);
	}

	_observerMutex = false;
}

void SpawnArgs::insert(const std::string& key, const KeyValuePtr& keyValue)
{
	// Insert the new key at the end of the list
	auto& pair = _keyValues.emplace_back(key, keyValue);

	// Dereference the iterator to get a KeyValue& reference and notify the observers
	notifyInsert(key, *pair.second);

	if (_undo.isConnected())
	{
        pair.second->connectUndoSystem(_undo.getUndoSystem());
	}
}

void SpawnArgs::insert(const std::string& key, const std::string& value)
{
	// Try to lookup the key in the map
    auto i = find(key);

    if (i != _keyValues.end())
    {
        // Key has been found, assign the value
        i->second->assign(value);
        // Observer notification happens through the lambda callback we passed 
        // to the KeyValue constructor
    }
	else
	{
		// No key with that name found, create a new one
		_undo.save();

		// Allocate a new KeyValue object and insert it into the map
        // Capture the key by value in the lambda
		insert(key, std::make_shared<KeyValue>(value, _eclass->getAttributeValue(key),
            [key, this](const std::string& value) { notifyChange(key, value); }));
	}
}

void SpawnArgs::erase(const KeyValues::iterator& i)
{
	if (_undo.isConnected())
	{
		i->second->disconnectUndoSystem(_undo.getUndoSystem());
	}

	// Retrieve the key and value from the vector before deletion
	std::string key(i->first);
	KeyValuePtr value(i->second);

	// Actually delete the object from the list
	_keyValues.erase(i);

	// Notify about the deletion
	notifyErase(key, *value);

	// Scope ends here, the KeyValue object will be deleted automatically
	// as the std::shared_ptr useCount will reach zero.
}

void SpawnArgs::erase(const std::string& key)
{
	// Try to lookup the key
	KeyValues::iterator i = find(key);

	if (i != _keyValues.end())
	{
		_undo.save();
		erase(i);
	}
}

SpawnArgs::KeyValues::const_iterator SpawnArgs::find(const std::string& key) const
{
	for (KeyValues::const_iterator i = _keyValues.begin();
		 i != _keyValues.end();
		 ++i)
	{
		if (string::iequals(i->first, key))
		{
			return i;
		}
	}

	// Not found
	return _keyValues.end();
}

SpawnArgs::KeyValues::iterator SpawnArgs::find(const std::string& key)
{
	for (KeyValues::iterator i = _keyValues.begin();
		 i != _keyValues.end();
		 ++i)
	{
		if (string::iequals(i->first, key))
		{
			return i;
		}
	}

	// Not found
	return _keyValues.end();
}

} // namespace entity