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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
#include "api.h"
#include "PaletteManager.h"
#include <api/skin/skinparse.h>
#define COLOR_WHITE (0xffffff)
#define COLOR_BLACK (0x000000)
#define COLOR_ERROR (0xff00ff)
static ARGB32 parseColor(const wchar_t *color, int *error)
{
if (color == NULL || *color == '\0') { if (error) *error = 1; return COLOR_ERROR; }
if (!WCSICMP(color, L"white")) return COLOR_WHITE;
if (!WCSICMP(color, L"black")) return COLOR_BLACK;
if (wcschr(color, ','))
{
int r = 0, g = 0, b = 0;
if (swscanf(color, L"%d,%d,%d", &r, &g, &b) != 3) return COLOR_ERROR;
return RGB(r, g, b); // our colors are reversed internally
}
if (*color == '#')
{
int r = 0, g = 0, b = 0;
if (swscanf(color, L"#%02x%02x%02x", &r, &g, &b) != 3) return COLOR_ERROR;
return RGB(r, g, b);
}
if (error) *error = 1;
return COLOR_ERROR;
}
#pragma warning(push)
#pragma warning(disable : 4355)
PaletteManager::PaletteManager() : paletteGC(this)
{
skinpart_iterator = 1;
genericcounter = 0;
WASABI_API_SYSCB->syscb_registerCallback(&paletteGC);
WASABI_API_SVC->service_register(&gammaFilterFactory);
}
#pragma warning(pop)
PaletteManager::~PaletteManager()
{
WASABI_API_SVC->service_deregister(&gammaFilterFactory);
WASABI_API_SYSCB->syscb_deregisterCallback(&paletteGC);
}
void PaletteManager::StartTransaction()
{
skinColorList.setAutoSort(FALSE);
skinAliasList.setAutoSort(FALSE);
skinCursorList.setAutoSort(FALSE);
skinBitmapList.setAutoSort(FALSE);
}
void PaletteManager::EndTransaction()
{
skinAliasList.sort();
skinColorList.sort();
skinCursorList.sort();
skinBitmapList.sort();
}
const int *PaletteManager::getSkinPartIteratorPtr()
{
return &skinpart_iterator;
}
int PaletteManager::newSkinPart()
{
return ++skinpart_iterator;
}
int PaletteManager::getSkinPartIterator()
{
return skinpart_iterator;
}
void PaletteManager::AddAlias(const wchar_t *id, const wchar_t *target)
{
// TODO: benski, in transaction, set some transaction_iterator = skinpart_iterator?
skinAliasList.addItem(new SkinElementAlias(id, target, skinpart_iterator, genericcounter++));
}
void PaletteManager::Reset()
{
skinAliasList.deleteAll();
skinColorList.deleteAll();
skinCursorList.deleteAll();
skinBitmapList.deleteAll();
skinAliasList.setAutoSort(FALSE);
skinColorList.setAutoSort(FALSE);
skinCursorList.setAutoSort(FALSE);
skinBitmapList.setAutoSort(FALSE);
}
const wchar_t *PaletteManager::getElementAlias(const wchar_t *alias)
{
if (alias == NULL) return NULL;
int pos;
SkinElementAlias *sea = skinAliasList.findLastItem(alias, &pos);
if (sea == NULL) return NULL;
return skinAliasList.enumItem(pos)->getTargetId();
}
void PaletteManager::UnloadElements(int skinpart)
{
for (int i = 0;i < skinAliasList.getNumItems();i++)
{
if (skinAliasList.enumItem(i)->getSkinPartId() == skinpart)
{
delete skinAliasList.enumItem(i);
skinAliasList.removeByPos(i);
i--;
}
}
for (int i = 0;i < skinColorList.getNumItems();i++)
{
if (skinColorList.enumItem(i)->getSkinPartId() == skinpart)
{
delete skinColorList.enumItem(i);
skinColorList.removeByPos(i);
i--;
}
}
for (int i = 0;i < skinCursorList.getNumItems();i++)
{
if (skinCursorList.enumItem(i)->getScriptId() == skinpart)
{
delete skinCursorList.enumItem(i);
skinCursorList.removeByPos(i);
i--;
}
}
for (int i = 0;i < skinBitmapList.getNumItems();i++)
{
if (skinBitmapList.enumItem(i)->getSkinPartId() == skinpart)
{
delete skinBitmapList.enumItem(i);
skinBitmapList.removeByPos(i);
i--;
}
}
}
SkinItem *PaletteManager::getAliasAncestor(SkinItem *item)
{
SkinElementAlias *elem = static_cast<SkinElementAlias *>(item);
int pos = skinAliasList.searchItem(elem);
if (pos <= 0) return NULL;
const wchar_t *it = elem->getAliasName();
pos--;
SkinElementAlias *aelem = skinAliasList.enumItem(pos);
if (aelem == NULL) return NULL;
const wchar_t *ait = aelem->getAliasName();
if (!WCSICMP(it, ait)) return aelem;
return NULL;
}
void PaletteManager::AddColor(const wchar_t *id, ARGB32 value, const wchar_t *colorgroup, const wchar_t *path, ifc_xmlreaderparams *params)
{
skinColorList.addItem(new SkinColorElement(id, value, skinpart_iterator, genericcounter++, colorgroup, path, params));
}
int PaletteManager::getNumColorElements()
{
return skinColorList.getNumItems();
}
const wchar_t *PaletteManager::enumColorElement(int n)
{
return skinColorList.enumItem(n)->getId();
}
ARGB32 *PaletteManager::getColorElementRef(const wchar_t *type, const wchar_t **grp)
{
const wchar_t *alias = getElementAlias(type);
if (alias != NULL)
type = alias;
if (grp != NULL) *grp = NULL;
SkinColorElement *sce = skinColorList.findLastItem(type);
if (sce == NULL) return NULL;
if (grp != NULL) *grp = sce->getColorGroup();
return (unsigned long *)(sce->getColorRef());
}
SkinItem *PaletteManager::getColorAncestor(SkinItem *item)
{
SkinColorElement *elem = static_cast<SkinColorElement *>(item);
int pos = skinColorList.searchItem(elem);
if (pos <= 0) return NULL;
const wchar_t *it = elem->getId();
pos--;
SkinColorElement *aelem = skinColorList.enumItem(pos);
if (aelem == NULL) return NULL;
const wchar_t *ait = aelem->getId();
if (!WCSICMP(it, ait)) return aelem;
return NULL;
}
ARGB32 PaletteManager::getColorElement(const wchar_t *type, const wchar_t **grp)
{
const wchar_t *alias = getElementAlias(type);
if (alias != NULL)
type = alias;
ARGB32 *v = getColorElementRef(type, grp);
if (!v)
{
int err = 0;
ARGB32 r = parseColor(type, &err);
if (!err) return r;
}
return v ? *v : RGB(255, 0, 255);
}
void PaletteManager::AddCursor(const wchar_t *id, const wchar_t *bitmapid, int x, int y, const wchar_t *path, ifc_xmlreaderparams *params)
{
skinCursorList.addItem(new SkinCursorElement(id, bitmapid, x, y, skinpart_iterator, genericcounter++, path, params));
}
OSCURSOR PaletteManager::getCursor(const wchar_t *id)
{
int pos = getCursorElement(id);
if (pos >= 0)
{
SkinCursorElement *sce = enumCursorElement(pos);
return sce->getCursor();
}
return INVALIDOSCURSORHANDLE;
}
int PaletteManager::getCursorElement(const wchar_t *id)
{
const wchar_t *alias = getElementAlias(id);
if (alias != NULL)
id = alias;
int pos;
SkinCursorElement *sce = skinCursorList.findLastItem(id, &pos);
if (sce == NULL) return 0;
return pos;
}
SkinCursorElement *PaletteManager::enumCursorElement(int n)
{
return skinCursorList.enumItem(n);
}
int PaletteManager::getNumSkinCursorElements()
{
return skinCursorList.getNumItems();
}
SkinItem *PaletteManager::getCursorAncestor(SkinItem *item)
{
SkinCursorElement *elem = static_cast<SkinCursorElement *>(item);
int pos = skinCursorList.searchItem(elem);
if (pos <= 0) return NULL;
const wchar_t *it = elem->getId();
pos--;
SkinCursorElement *aelem = skinCursorList.enumItem(pos);
if (aelem == NULL) return NULL;
const wchar_t *ait = aelem->getId();
if (!WCSICMP(it, ait)) return aelem;
return NULL;
}
const wchar_t *PaletteManager::getSkinCursorBitmapId(const wchar_t *cursor)
{
int pos = getCursorElement(cursor);
if (pos < 0) return NULL;
SkinCursorElement *sce = enumCursorElement(pos);
return sce->getBitmapId();
}
void PaletteManager::AddBitmap(const wchar_t *id, const wchar_t *filename, const wchar_t *path, int x, int y, int w, int h, ifc_xmlreaderparams *params, const wchar_t *colorgroup)
{
skinBitmapList.addItem(new SkinBitmapElement(id, filename, path, x, y, w, h, params, skinpart_iterator, genericcounter++, colorgroup));
}
int PaletteManager::getBitmapElement(const wchar_t *type)
{
if (type == NULL)
return -1;
const wchar_t *alias = getElementAlias(type);
if (alias != NULL)
{
int pos;
SkinBitmapElement *sbe = skinBitmapList.findLastItem(alias, &pos);
if (sbe == NULL) return -1;
return pos;
}
else
{
int pos;
SkinBitmapElement *sbe = skinBitmapList.findLastItem(type, &pos);
if (sbe == NULL) return -1;
return pos;
}
}
RegionServer *PaletteManager::requestSkinRegion(const wchar_t *id)
{
int n = getBitmapElement(id);
if (n == -1) return NULL;
SkinBitmapElement *el = skinBitmapList.enumItem(n);
// if (el->region != NULL) el->region->getRegion()->debug();
return el->getRegionServer();
}
void PaletteManager::cacheSkinRegion(const wchar_t *id, api_region *r)
{
int n = getBitmapElement(id);
if (n == -1) return ;
SkinBitmapElement *el = skinBitmapList.enumItem(n);
ASSERT(el != NULL);
if (el->getRegionServer() != NULL)
{
DebugString("Trying to cache a region but cache is already set!\n");
return ;
}
el->setRegionServer(new ElementRegionServer(r));
//el->region->getRegion()->debug();
}
SkinItem *PaletteManager::getBitmapAncestor(SkinItem *item)
{
SkinBitmapElement *elem = static_cast<SkinBitmapElement *>(item);
int pos = skinBitmapList.searchItem(elem);
if (pos <= 0) return NULL;
const wchar_t *it = elem->getId();
pos--;
SkinBitmapElement *aelem = skinBitmapList.enumItem(pos);
if (aelem == NULL) return NULL;
const wchar_t *ait = aelem->getId();
if (!WCSICMP(it, ait)) return aelem;
return NULL;
}
SkinBitmapElement *PaletteManager::enumBitmapElement(int n)
{
return skinBitmapList.enumItem(n);
}
int PaletteManager::getNumBitmapElement()
{
return skinBitmapList.getNumItems();
}
const wchar_t *PaletteManager::getSkinBitmapFilename(const wchar_t *id, int *x, int *y, int *w, int *h, const wchar_t **root_path, ifc_xmlreaderparams **params)
{
int i = getBitmapElement(id); // can return skinBitmapList.getNumItems(), check for that.
if (i < 0) return id;
SkinBitmapElement *sbe = skinBitmapList.enumItem(i);
if (i < skinBitmapList.getNumItems() && !WCSICMP(id, sbe->getId()))
{
if (x) *x = sbe->getX();
if (y) *y = sbe->getY();
if (w) *w = sbe->getW();
if (h) *h = sbe->getH();
if (params) *params = sbe->getParams();
if (root_path) *root_path = sbe->getXmlRootPath();
return sbe->getFilename();
}
return id; //FUCKO: const
}
const wchar_t *PaletteManager::getGammaGroupFromId(const wchar_t *id)
{
int i = getBitmapElement(id);
if (i < 0)
return NULL;
return skinBitmapList[i]->getParams()->getItemValue(L"gammagroup");
}
int PaletteManager::getLayerFromId(const wchar_t *id)
{
int i = getBitmapElement(id);
if (i < 0) return 0;
const wchar_t *a = skinBitmapList[i]->getParams()->getItemValue(L"layer");
if (a == NULL) return 0;
return WTOI(a);
}
void PaletteManager::onGarbageCollect()
{
for (int i = 0;i < regsrvGC.getNumItems();i++)
{
ElementRegionServer *srv = regsrvGC.enumItem(i);
if (srv->getNumRefs() == 0)
{
delete srv;
regsrvGC.removeByPos(i);
i--;
}
}
}
void PaletteManager::garbageCollectRegionServer(ElementRegionServer *rs)
{
if (rs->getNumRefs() == 0)
{
delete rs;
return ;
}
regsrvGC.addItem(rs);
}
int PaletteGC::gccb_onGarbageCollect()
{
parent->onGarbageCollect();
return 1;
}
#define CBCLASS PaletteManager
START_DISPATCH;
VCB(API_PALETTE_STARTTRANSACTION, StartTransaction)
VCB(API_PALETTE_ENDTRANSACTION, EndTransaction)
VCB(API_PALETTE_RESET, Reset)
CB(API_PALETTE_GETSKINPARTITERATORPTR,getSkinPartIteratorPtr)
CB(API_PALETTE_NEWSKINPART,newSkinPart)
CB(API_PALETTE_GETSKINPARTITERATOR,getSkinPartIterator)
VCB(API_PALETTE_UNLOADELEMENTS,UnloadElements)
VCB(API_PALETTE_ADDALIAS,AddAlias)
CB(API_PALETTE_GETELEMENTALIAS,getElementAlias)
CB(API_PALETTE_GETALIASANCESTOR,getAliasAncestor)
VCB(API_PALETTE_ADDCOLOR,AddColor)
CB(API_PALETTE_GETNUMCOLORELEMENTS,getNumColorElements)
CB(API_PALETTE_ENUMCOLORELEMENT,enumColorElement)
CB(API_PALETTE_GETCOLORELEMENTREF,getColorElementRef)
CB(API_PALETTE_GETCOLORANCESTOR,getColorAncestor)
CB(API_PALETTE_GETCOLORELEMENT,getColorElement)
VCB(API_PALETTE_ADDCURSOR,AddCursor)
CB(API_PALETTE_GETCURSORELEMENT,getCursorElement)
CB(API_PALETTE_GETCURSOR,getCursor)
CB(API_PALETTE_GETCURSORANCESTOR,getCursorAncestor)
CB(API_PALETTE_GETSKINCURSORBITMAPID,getSkinCursorBitmapId)
VCB(API_PALETTE_ADDBITMAP,AddBitmap)
CB(API_PALETTE_GETBITMAPELEMENT,getBitmapElement)
CB(API_PALETTE_GETBITMAPANCESTOR,getBitmapAncestor)
CB(API_PALETTE_GETNUMBITMAPELEMENT,getNumBitmapElement)
CB(API_PALETTE_GETSKINBITMAPFILENAME,getSkinBitmapFilename)
CB(API_PALETTE_GETGAMMAGROUPFROMID,getGammaGroupFromId)
CB(API_PALETTE_GETLAYERFROMID,getLayerFromId)
CB(API_PALETTE_REQUESTSKINREGION,requestSkinRegion)
VCB(API_PALETTE_CACHESKINREGION,cacheSkinRegion)
END_DISPATCH;
#undef CBCLASS
|