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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
This file is part of the KDE games library
Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
Copyright (C) 2001 Martin Heni (martin@heni-online.de)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/*
$Id$
*/
#include "kgamepropertyhandler.h"
#include "kgameproperty.h"
#include "kgamemessage.h"
#include <tqmap.h>
#include <tqptrqueue.h>
#include <klocale.h>
#include <typeinfo>
#define KPLAYERHANDLER_LOAD_COOKIE 6239
//---------------------- KGamePropertyHandler -----------------------------------
class KGamePropertyHandlerPrivate
{
public:
KGamePropertyHandlerPrivate()
{
}
TQMap<int, TQString> mNameMap;
TQIntDict<KGamePropertyBase> mIdDict;
int mUniqueId;
int mId;
KGamePropertyBase::PropertyPolicy mDefaultPolicy;
bool mDefaultUserspace;
int mIndirectEmit;
TQPtrQueue<KGamePropertyBase> mSignalQueue;
};
KGamePropertyHandler::KGamePropertyHandler(int id, const TQObject* receiver, const char * sendf, const char *emitf, TQObject* parent) : TQObject(parent)
{
init();
registerHandler(id,receiver,sendf,emitf);
}
KGamePropertyHandler::KGamePropertyHandler(TQObject* parent) : TQObject(parent)
{
init();
}
KGamePropertyHandler::~KGamePropertyHandler()
{
clear();
delete d;
}
void KGamePropertyHandler::init()
{
kdDebug(11001) << k_funcinfo << ": this=" << this << endl;
d = new KGamePropertyHandlerPrivate; // for future use - is BC important to us?
d->mId = 0;
d->mUniqueId=KGamePropertyBase::IdAutomatic;
d->mDefaultPolicy=KGamePropertyBase::PolicyLocal;
d->mDefaultUserspace=true;
d->mIndirectEmit=0;
}
int KGamePropertyHandler::id() const
{
return d->mId;
}
void KGamePropertyHandler::setId(int id)
{
d->mId = id;
}
void KGamePropertyHandler::registerHandler(int id,const TQObject * receiver, const char * sendf, const char *emitf)
{
setId(id);
if (receiver && sendf) {
kdDebug(11001) << "Connecting TQT_SLOT " << sendf << endl;
connect(this, TQT_SIGNAL(signalSendMessage(int, TQDataStream &, bool*)), receiver, sendf);
}
if (receiver && emitf) {
kdDebug(11001) << "Connecting TQT_SLOT " << emitf << endl;
connect(this, TQT_SIGNAL(signalPropertyChanged(KGamePropertyBase *)), receiver, emitf);
}
}
bool KGamePropertyHandler::processMessage(TQDataStream &stream, int id, bool isSender)
{
// kdDebug(11001) << k_funcinfo << ": id=" << id << " mId=" << d->mId << endl;
if (id != d->mId) {
return false; // Is the message meant for us?
}
KGamePropertyBase* p;
int propertyId;
KGameMessage::extractPropertyHeader(stream, propertyId);
// kdDebug(11001) << k_funcinfo << ": Got property " << propertyId << endl;
if (propertyId==KGamePropertyBase::IdCommand) {
int cmd;
KGameMessage::extractPropertyCommand(stream, propertyId, cmd);
//kdDebug(11001) << k_funcinfo << ": Got COMMAND for id= "<<propertyId <<endl;
p = d->mIdDict.find(propertyId);
if (p) {
if (!isSender || p->policy()==KGamePropertyBase::PolicyClean) {
p->command(stream, cmd, isSender);
}
} else {
kdError(11001) << k_funcinfo << ": (cmd): property " << propertyId << " not found" << endl;
}
return true;
}
p = d->mIdDict.find(propertyId);
if (p) {
//kdDebug(11001) << k_funcinfo << ": Loading " << propertyId << endl;
if (!isSender || p->policy()==KGamePropertyBase::PolicyClean) {
p->load(stream);
}
} else {
kdError(11001) << k_funcinfo << ": property " << propertyId << " not found" << endl;
}
return true;
}
bool KGamePropertyHandler::removeProperty(KGamePropertyBase* data)
{
if (!data) {
return false;
}
d->mNameMap.erase(data->id());
return d->mIdDict.remove(data->id());
}
bool KGamePropertyHandler::addProperty(KGamePropertyBase* data, TQString name)
{
//kdDebug(11001) << k_funcinfo << ": " << data->id() << endl;
if (d->mIdDict.find(data->id())) {
// this id already exists
kdError(11001) << " -> cannot add property " << data->id() << endl;
return false;
} else {
d->mIdDict.insert(data->id(), data);
// if here is a check for "is_debug" or so we can add the strings only in debug mode
// and save memory!!
if (!name.isNull()) {
d->mNameMap[data->id()] = name;
//kdDebug(11001) << k_funcinfo << ": nid="<< (data->id()) << " inserted in Map name=" << d->mNameMap[data->id()] <<endl;
//kdDebug(11001) << "Typeid=" << typeid(data).name() << endl;
//kdDebug(11001) << "Typeid call=" << data->typeinfo()->name() << endl;
}
}
return true;
}
TQString KGamePropertyHandler::propertyName(int id) const
{
TQString s;
if (d->mIdDict.find(id)) {
if (d->mNameMap.contains(id)) {
s = i18n("%1 (%2)").tqarg(d->mNameMap[id]).tqarg(id);
} else {
s = i18n("Unnamed - ID: %1").tqarg(id);
}
} else {
// Should _never_ happen
s = i18n("%1 unregistered").tqarg(id);
}
return s;
}
bool KGamePropertyHandler::load(TQDataStream &stream)
{
// Prevent direct emmiting until all is loaded
lockDirectEmit();
uint count,i;
stream >> count;
kdDebug(11001) << k_funcinfo << ": " << count << " KGameProperty objects " << endl;
for (i = 0; i < count; i++) {
processMessage(stream, id(),false);
}
TQ_INT16 cookie;
stream >> cookie;
if (cookie == KPLAYERHANDLER_LOAD_COOKIE) {
kdDebug(11001) << " KGamePropertyHandler loaded propertly"<<endl;
} else {
kdError(11001) << "KGamePropertyHandler loading error. probably format error"<<endl;
}
// Allow direct emmiting (if no other lock still holds)
unlockDirectEmit();
return true;
}
bool KGamePropertyHandler::save(TQDataStream &stream)
{
kdDebug(11001) << k_funcinfo << ": " << d->mIdDict.count() << " KGameProperty objects " << endl;
stream << (uint)d->mIdDict.count();
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
KGamePropertyBase *base=it.current();
if (base) {
KGameMessage::createPropertyHeader(stream, base->id());
base->save(stream);
}
++it;
}
stream << (TQ_INT16)KPLAYERHANDLER_LOAD_COOKIE;
return true;
}
KGamePropertyBase::PropertyPolicy KGamePropertyHandler::policy()
{
// kdDebug(11001) << k_funcinfo << ": " << d->mDefaultPolicy << endl;
return d->mDefaultPolicy;
}
void KGamePropertyHandler::setPolicy(KGamePropertyBase::PropertyPolicy p,bool userspace)
{
// kdDebug(11001) << k_funcinfo << ": " << p << endl;
d->mDefaultPolicy=p;
d->mDefaultUserspace=userspace;
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
if (!userspace || it.current()->id()>=KGamePropertyBase::IdUser) {
it.current()->setPolicy((KGamePropertyBase::PropertyPolicy)p);
}
++it;
}
}
void KGamePropertyHandler::unlockProperties()
{
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
it.current()->unlock();
++it;
}
}
void KGamePropertyHandler::lockProperties()
{
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
it.current()->lock();
++it;
}
}
int KGamePropertyHandler::uniquePropertyId()
{
return d->mUniqueId++;
}
void KGamePropertyHandler::flush()
{
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
if (it.current()->isDirty()) {
it.current()->sendProperty();
}
++it;
}
}
/* Fire all property signal changed which are collected in
* the queque
**/
void KGamePropertyHandler::lockDirectEmit()
{
d->mIndirectEmit++;
}
void KGamePropertyHandler::unlockDirectEmit()
{
// If the flag is <=0 we emit the queued signals
d->mIndirectEmit--;
if (d->mIndirectEmit<=0)
{
KGamePropertyBase *prop;
while((prop=d->mSignalQueue.dequeue()) != 0)
{
// kdDebug(11001) << "emmiting signal for " << prop->id() << endl;
emit signalPropertyChanged(prop);
}
}
}
void KGamePropertyHandler::emitSignal(KGamePropertyBase *prop)
{
// If the indirect flag is set (load and network transmit)
// we cannot emit the signals directly as it can happend that
// a sigal causes an access to a property which is e.g. not
// yet loaded or received
if (d->mIndirectEmit>0)
{
// Queque the signal
d->mSignalQueue.enqueue(prop);
}
else
{
// directly emit
emit signalPropertyChanged(prop);
}
}
bool KGamePropertyHandler::sendProperty(TQDataStream &s)
{
bool sent = false;
emit signalSendMessage(id(), s, &sent);
return sent;
}
KGamePropertyBase *KGamePropertyHandler::find(int id)
{
return d->mIdDict.find(id);
}
void KGamePropertyHandler::clear()
{
kdDebug(11001) << k_funcinfo << id() << endl;
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.toFirst()) {
KGamePropertyBase* p = it.toFirst();
p->unregisterData();
if (d->mIdDict.find(p->id())) {
// shouldn't happen - but if mOwner in KGamePropertyBase is NULL
// this might be possible
removeProperty(p);
}
}
}
TQIntDict<KGamePropertyBase>& KGamePropertyHandler::dict() const
{
return d->mIdDict;
}
TQString KGamePropertyHandler::propertyValue(KGamePropertyBase* prop)
{
if (!prop) {
return i18n("NULL pointer");
}
int id = prop->id();
TQString name = propertyName(id);
TQString value;
const type_info* t = prop->typeinfo();
if (*t == typeid(int)) {
value = TQString::number(((KGamePropertyInt*)prop)->value());
} else if (*t == typeid(unsigned int)) {
value = TQString::number(((KGamePropertyUInt *)prop)->value());
} else if (*t == typeid(long int)) {
value = TQString::number(((KGameProperty<long int> *)prop)->value());
} else if (*t == typeid(unsigned long int)) {
value = TQString::number(((KGameProperty<unsigned long int> *)prop)->value());
} else if (*t == typeid(TQString)) {
value = ((KGamePropertyTQString*)prop)->value();
} else if (*t == typeid(TQ_INT8)) {
value = ((KGamePropertyBool*)prop)->value() ? i18n("True") : i18n("False");
} else {
emit signalRequestValue(prop, value);
}
if (value.isNull()) {
value = i18n("Unknown");
}
return value;
}
void KGamePropertyHandler::Debug()
{
kdDebug(11001) << "-----------------------------------------------------------" << endl;
kdDebug(11001) << "KGamePropertyHandler:: Debug this=" << this << endl;
kdDebug(11001) << " Registered properties: (Policy,Lock,Emit,Optimized, Dirty)" << endl;
TQIntDictIterator<KGamePropertyBase> it(d->mIdDict);
while (it.current()) {
KGamePropertyBase *p=it.current();
kdDebug(11001) << " "<< p->id() << ": p=" << p->policy()
<< " l="<<p->isLocked()
<< " e="<<p->isEmittingSignal()
<< " o=" << p->isOptimized()
<< " d="<<p->isDirty()
<< endl;
++it;
}
kdDebug(11001) << "-----------------------------------------------------------" << endl;
}
#include "kgamepropertyhandler.moc"
|