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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
#include "system.h"
#include "TextureBundle.h"
#include "Texture.h"
#include "GraphicContext.h"
#include "DirectXGraphics.h"
#include "utils/log.h"
#ifndef _LINUX
#include <sys/stat.h>
#include "utils/CharsetConverter.h"
#include "lib/liblzo/LZO1X.H"
#else
#include <lzo/lzo1x.h>
#endif
#include "SkinInfo.h"
#include "GUISettings.h"
#include "Util.h"
#include "FileSystem/SpecialProtocol.h"
#if !defined(__GNUC__)
#pragma comment(lib,"../../xbmc/lib/liblzo/lzo.lib")
#endif
// alignment of file blocks - should be a multiple of the sector size of the disk and a power of 2
// HDD sector = 512 bytes, DVD/CD sector = 2048 bytes
#undef ALIGN
#define ALIGN (512)
// Use SDL macros to perform byte swapping on big-endian systems
// (same texture bundle used on little and big-endian systems)
#ifndef HAS_SDL
#define SDL_SwapLE32(X) (X)
#define SDL_SwapLE16(X) (X)
#endif
enum XPR_FLAGS
{
XPRFLAG_PALETTE = 0x00000001,
XPRFLAG_ANIM = 0x00000002
};
class CAutoBuffer
{
BYTE* p;
public:
CAutoBuffer() { p = 0; }
explicit CAutoBuffer(size_t s) { p = (BYTE*)malloc(s); }
~CAutoBuffer() { free(p); }
operator BYTE*() { return p; }
void Set(BYTE* buf) { free(p); p = buf; }
bool Resize(size_t s);
void Release() { p = 0; }
};
bool CAutoBuffer::Resize(size_t s)
{
if (s == 0)
{
if (!p)
return false;
free(p);
p = 0;
return true;
}
void* q = realloc(p, s);
if (q)
{
p = (BYTE*)q;
return true;
}
return false;
}
// as above but for texture allocation (do not change from XPhysicalAlloc!)
class CAutoTexBuffer
{
BYTE* p;
public:
CAutoTexBuffer() { p = 0; }
explicit CAutoTexBuffer(size_t s) { p = (BYTE*)XPhysicalAlloc(s, MAXULONG_PTR, 128, PAGE_READWRITE); }
~CAutoTexBuffer() { if (p) XPhysicalFree(p); }
operator BYTE*() { return p; }
BYTE* Set(BYTE* buf) { if (p) XPhysicalFree(p); return p = buf; }
void Release() { p = 0; }
};
CTextureBundle::CTextureBundle(void)
{
m_hFile = NULL;
m_themeBundle = false;
}
CTextureBundle::~CTextureBundle(void)
{
if (m_hFile != NULL)
fclose(m_hFile);
}
bool CTextureBundle::OpenBundle()
{
DWORD AlignedSize;
DWORD HeaderSize;
int Version;
XPR_HEADER* pXPRHeader;
if (m_hFile != NULL)
Cleanup();
CStdString strPath;
if (m_themeBundle)
{
// if we are the theme bundle, we only load if the user has chosen
// a valid theme (or the skin has a default one)
CStdString themeXPR = g_guiSettings.GetString("lookandfeel.skintheme");
if (!themeXPR.IsEmpty() && themeXPR.CompareNoCase("SKINDEFAULT"))
{
strPath = CUtil::AddFileToFolder(g_graphicsContext.GetMediaDir(), "media");
strPath = CUtil::AddFileToFolder(strPath, themeXPR);
}
else
return false;
}
else
strPath = CUtil::AddFileToFolder(g_graphicsContext.GetMediaDir(), "media/Textures.xpr");
strPath = PTH_IC(strPath);
#ifndef _LINUX
CStdStringW strPathW;
g_charsetConverter.utf8ToW(_P(strPath), strPathW, false);
m_hFile = _wfopen(strPathW.c_str(), L"rb");
#else
m_hFile = fopen(strPath.c_str(), "rb");
#endif
if (m_hFile == NULL)
return false;
struct stat fileStat;
if (fstat(fileno(m_hFile), &fileStat) == -1)
return false;
m_TimeStamp = fileStat.st_mtime;
CAutoBuffer HeaderBuf(ALIGN);
DWORD n;
n = fread(HeaderBuf, 1, ALIGN, m_hFile);
if (n < ALIGN)
goto LoadError;
pXPRHeader = (XPR_HEADER*)(BYTE*)HeaderBuf;
pXPRHeader->dwMagic = SDL_SwapLE32(pXPRHeader->dwMagic);
Version = (pXPRHeader->dwMagic >> 24) - '0';
pXPRHeader->dwMagic -= Version << 24;
Version &= 0x0f;
if (pXPRHeader->dwMagic != XPR_MAGIC_VALUE || Version < 2)
goto LoadError;
HeaderSize = SDL_SwapLE32(pXPRHeader->dwHeaderSize);
AlignedSize = (HeaderSize - 1) & ~(ALIGN - 1); // align to sector, but remove the first sector
HeaderBuf.Resize(AlignedSize + ALIGN);
if (fseek(m_hFile, ALIGN, SEEK_SET) == -1)
goto LoadError;
n = fread(HeaderBuf + ALIGN, 1, AlignedSize, m_hFile);
if (n < ALIGN)
goto LoadError;
struct DiskFileHeader_t
{
char Name[116];
DWORD Offset;
DWORD UnpackedSize;
DWORD PackedSize;
}
*FileHeader;
FileHeader = (DiskFileHeader_t*)(HeaderBuf + sizeof(XPR_HEADER));
n = (HeaderSize - sizeof(XPR_HEADER)) / sizeof(DiskFileHeader_t);
for (unsigned i = 0; i < n; ++i)
{
std::pair<CStdString, FileHeader_t> entry;
entry.first = Normalize(FileHeader[i].Name);
entry.second.Offset = SDL_SwapLE32(FileHeader[i].Offset);
entry.second.UnpackedSize = SDL_SwapLE32(FileHeader[i].UnpackedSize);
entry.second.PackedSize = SDL_SwapLE32(FileHeader[i].PackedSize);
m_FileHeaders.insert(entry);
}
if (lzo_init() != LZO_E_OK)
goto LoadError;
return true;
LoadError:
CLog::Log(LOGERROR, "Unable to load file: %s: %s", strPath.c_str(), strerror(errno));
fclose(m_hFile);
m_hFile = NULL;
return false;
}
void CTextureBundle::Cleanup()
{
if (m_hFile != NULL)
fclose(m_hFile);
m_hFile = NULL;
m_FileHeaders.clear();
}
bool CTextureBundle::HasFile(const CStdString& Filename)
{
if (m_hFile == NULL && !OpenBundle())
return false;
struct stat fileStat;
if (fstat(fileno(m_hFile), &fileStat) == -1)
return false;
if (fileStat.st_mtime > m_TimeStamp)
{
CLog::Log(LOGINFO, "Texture bundle has changed, reloading");
Cleanup();
if (!OpenBundle())
return false;
}
CStdString name = Normalize(Filename);
return m_FileHeaders.find(name) != m_FileHeaders.end();
}
void CTextureBundle::GetTexturesFromPath(const CStdString &path, std::vector<CStdString> &textures)
{
if (path.GetLength() > 1 && path[1] == ':')
return;
if (m_hFile == NULL && !OpenBundle())
return;
CStdString testPath = Normalize(path);
if (!CUtil::HasSlashAtEnd(testPath))
testPath += "\\";
int testLength = testPath.GetLength();
std::map<CStdString, FileHeader_t>::iterator it;
for (it = m_FileHeaders.begin(); it != m_FileHeaders.end(); it++)
{
if (it->first.Left(testLength).Equals(testPath))
textures.push_back(it->first);
}
}
HRESULT CTextureBundle::LoadFile(const CStdString& Filename, CAutoTexBuffer& UnpackedBuf)
{
if (Filename == "-")
return 0;
CStdString name = Normalize(Filename);
std::map<CStdString, FileHeader_t>::iterator file = m_FileHeaders.find(name);
if (file == m_FileHeaders.end())
return E_FAIL;
// found texture - allocate the necessary buffers
DWORD ReadSize = (file->second.PackedSize + (ALIGN - 1)) & ~(ALIGN - 1);
BYTE *buffer = (BYTE*)malloc(ReadSize);
if (!buffer || !UnpackedBuf.Set((BYTE*)XPhysicalAlloc(file->second.UnpackedSize, MAXULONG_PTR, 128, PAGE_READWRITE)))
{ // failed due to lack of memory
#ifndef _LINUX
MEMORYSTATUS stat;
GlobalMemoryStatus(&stat);
CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %lu bytes, have %lu bytes)", name.c_str(),
file->second.UnpackedSize + file->second.PackedSize, stat.dwAvailPhys);
#elif defined(__APPLE__)
CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %lu bytes)", name.c_str(),
file->second.UnpackedSize + file->second.PackedSize);
#else
struct sysinfo info;
sysinfo(&info);
CLog::Log(LOGERROR, "Out of memory loading texture: %s "
"(need %u bytes, have %lu bytes)",
name.c_str(), file->second.UnpackedSize + file->second.PackedSize,
info.totalram);
#endif
free(buffer);
return E_OUTOFMEMORY;
}
// read the file into our buffer
DWORD n;
fseek(m_hFile, file->second.Offset, SEEK_SET);
n = fread(buffer, 1, ReadSize, m_hFile);
if (n < ReadSize && !feof(m_hFile))
{
CLog::Log(LOGERROR, "Error loading texture: %s: %s", Filename.c_str(), strerror(ferror(m_hFile)));
free(buffer);
return E_FAIL;
}
// allocate a buffer for our unpacked texture
lzo_uint s = file->second.UnpackedSize;
HRESULT hr = S_OK;
if (lzo1x_decompress(buffer, file->second.PackedSize, UnpackedBuf, &s, NULL) != LZO_E_OK ||
s != file->second.UnpackedSize)
{
CLog::Log(LOGERROR, "Error loading texture: %s: Decompression error", Filename.c_str());
hr = E_FAIL;
}
try
{
free(buffer);
}
catch (...)
{
CLog::Log(LOGERROR, "Error freeing preload buffer.");
}
return hr;
}
HRESULT CTextureBundle::LoadTexture(const CStdString& Filename, CBaseTexture** ppTexture,
int &width, int &height)
{
DWORD ResDataOffset;
*ppTexture = NULL;
CAutoTexBuffer UnpackedBuf;
HRESULT r = LoadFile(Filename, UnpackedBuf);
if (r != S_OK)
return r;
D3DTexture *pTex = (D3DTexture *)(new char[sizeof (D3DTexture)]);
D3DPalette* pPal = 0;
void* ResData = 0;
WORD RealSize[2];
enum XPR_FLAGS
{
XPRFLAG_PALETTE = 0x00000001,
XPRFLAG_ANIM = 0x00000002
};
BYTE* Next = UnpackedBuf;
DWORD flags = SDL_SwapLE32(*(DWORD*)Next);
Next += sizeof(DWORD);
if ((flags & XPRFLAG_ANIM) || (flags >> 16) > 1)
goto PackedLoadError;
if (flags & XPRFLAG_PALETTE)
Next += sizeof(D3DPalette);
memcpy(pTex, Next, sizeof(D3DTexture));
pTex->Common = SDL_SwapLE32(pTex->Common);
pTex->Data = SDL_SwapLE32(pTex->Data);
pTex->Lock = SDL_SwapLE32(pTex->Lock);
pTex->Format = SDL_SwapLE32(pTex->Format);
pTex->Size = SDL_SwapLE32(pTex->Size);
Next += sizeof(D3DTexture);
memcpy(RealSize, Next, 4);
Next += 4;
ResDataOffset = ((Next - UnpackedBuf) + 127) & ~127;
ResData = UnpackedBuf + ResDataOffset;
if ((pTex->Common & D3DCOMMON_TYPE_MASK) != D3DCOMMON_TYPE_TEXTURE)
goto PackedLoadError;
GetTextureFromData(pTex, ResData, ppTexture);
delete[] pTex;
width = SDL_SwapLE16(RealSize[0]);
height = SDL_SwapLE16(RealSize[1]);
/* DXMERGE - this was previously used to specify the format of the image - probably only affects directx?
#ifndef HAS_SDL
D3DSURFACE_DESC desc;
(*ppTexture)->GetLevelDesc(0, &desc);
pInfo->Format = desc.Format;
#endif
*/
return S_OK;
PackedLoadError:
CLog::Log(LOGERROR, "Error loading texture: %s: Invalid data", Filename.c_str());
delete[] pTex;
delete pPal;
return E_FAIL;
}
int CTextureBundle::LoadAnim(const CStdString& Filename, CBaseTexture*** ppTextures,
int &width, int &height, int& nLoops, int** ppDelays)
{
DWORD ResDataOffset;
int nTextures = 0;
*ppTextures = NULL; *ppDelays = NULL;
CAutoTexBuffer UnpackedBuf;
HRESULT r = LoadFile(Filename, UnpackedBuf);
if (r != S_OK)
return 0;
struct AnimInfo_t
{
DWORD nLoops;
WORD RealSize[2];
}
*pAnimInfo;
D3DTexture** ppTex = 0;
void* ResData = 0;
BYTE* Next = UnpackedBuf;
DWORD flags = SDL_SwapLE32(*(DWORD*)Next);
Next += sizeof(DWORD);
if (!(flags & XPRFLAG_ANIM))
goto PackedAnimError;
pAnimInfo = (AnimInfo_t*)Next;
Next += sizeof(AnimInfo_t);
nLoops = SDL_SwapLE32(pAnimInfo->nLoops);
if (flags & XPRFLAG_PALETTE)
Next += sizeof(D3DPalette);
nTextures = flags >> 16;
ppTex = new D3DTexture * [nTextures];
*ppDelays = new int[nTextures];
for (int i = 0; i < nTextures; ++i)
{
ppTex[i] = (D3DTexture *)(new char[sizeof (D3DTexture)+ sizeof (DWORD)]);
memcpy(ppTex[i], Next, sizeof(D3DTexture));
ppTex[i]->Common = SDL_SwapLE32(ppTex[i]->Common);
ppTex[i]->Data = SDL_SwapLE32(ppTex[i]->Data);
ppTex[i]->Lock = SDL_SwapLE32(ppTex[i]->Lock);
ppTex[i]->Format = SDL_SwapLE32(ppTex[i]->Format);
ppTex[i]->Size = SDL_SwapLE32(ppTex[i]->Size);
Next += sizeof(D3DTexture);
(*ppDelays)[i] = SDL_SwapLE32(*(int*)Next);
Next += sizeof(int);
}
ResDataOffset = ((DWORD)(Next - UnpackedBuf) + 127) & ~127;
ResData = UnpackedBuf + ResDataOffset;
*ppTextures = new CBaseTexture*[nTextures];
for (int i = 0; i < nTextures; ++i)
{
if ((ppTex[i]->Common & D3DCOMMON_TYPE_MASK) != D3DCOMMON_TYPE_TEXTURE)
goto PackedAnimError;
GetTextureFromData(ppTex[i], ResData, &(*ppTextures)[i]);
delete[] ppTex[i];
}
delete[] ppTex;
ppTex = 0;
width = SDL_SwapLE16(pAnimInfo->RealSize[0]);
height = SDL_SwapLE16(pAnimInfo->RealSize[1]);
return nTextures;
PackedAnimError:
CLog::Log(LOGERROR, "Error loading texture: %s: Invalid data", Filename.c_str());
if (ppTex)
{
for (int i = 0; i < nTextures; ++i)
delete [] ppTex[i];
delete [] ppTex;
}
delete [] *ppDelays;
return 0;
}
void CTextureBundle::SetThemeBundle(bool themeBundle)
{
m_themeBundle = themeBundle;
}
// normalize to how it's stored within the bundle
// lower case + using \\ rather than /
CStdString CTextureBundle::Normalize(const CStdString &name)
{
CStdString newName(name);
newName.Normalize();
newName.Replace('/','\\');
return newName;
}
|