2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2006-03-24 14:15:45 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
2006-04-16 13:01:41 +00:00
|
|
|
* $Id$
|
2006-03-24 14:15:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gui/theme.h"
|
2008-08-16 14:06:26 +00:00
|
|
|
#include "common/fs.h"
|
2006-10-08 10:57:48 +00:00
|
|
|
#include "common/unzip.h"
|
|
|
|
|
2006-03-24 14:15:45 +00:00
|
|
|
namespace GUI {
|
|
|
|
|
2008-08-13 16:50:50 +00:00
|
|
|
Theme::Theme() : _loadedThemeX(0), _loadedThemeY(0) {}
|
2006-03-24 14:15:45 +00:00
|
|
|
|
2008-08-13 16:50:50 +00:00
|
|
|
Theme::~Theme() {}
|
2006-10-08 10:57:48 +00:00
|
|
|
|
|
|
|
const Graphics::Font *Theme::loadFont(const char *filename) {
|
|
|
|
const Graphics::NewFont *font = 0;
|
|
|
|
Common::String cacheFilename = genCacheFilename(filename);
|
|
|
|
Common::File fontFile;
|
|
|
|
|
|
|
|
if (!cacheFilename.empty()) {
|
|
|
|
if (fontFile.open(cacheFilename))
|
|
|
|
font = Graphics::NewFont::loadFromCache(fontFile);
|
|
|
|
if (font)
|
|
|
|
return font;
|
|
|
|
|
|
|
|
#ifdef USE_ZLIB
|
2008-08-14 10:58:28 +00:00
|
|
|
unzFile zipFile = unzOpen((getThemeFileName()).c_str());
|
2006-10-08 10:57:48 +00:00
|
|
|
if (zipFile && unzLocateFile(zipFile, cacheFilename.c_str(), 2) == UNZ_OK) {
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
unzOpenCurrentFile(zipFile);
|
|
|
|
unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
|
|
|
|
uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
|
|
|
|
assert(buffer);
|
|
|
|
memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
|
|
|
|
unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
|
|
|
|
unzCloseCurrentFile(zipFile);
|
|
|
|
Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-10-08 10:57:48 +00:00
|
|
|
font = Graphics::NewFont::loadFromCache(stream);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-05-07 14:31:45 +00:00
|
|
|
delete[] buffer;
|
2006-10-08 10:57:48 +00:00
|
|
|
buffer = 0;
|
|
|
|
}
|
|
|
|
unzClose(zipFile);
|
|
|
|
#endif
|
|
|
|
if (font)
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal open
|
|
|
|
if (fontFile.open(filename)) {
|
|
|
|
font = Graphics::NewFont::loadFont(fontFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_ZLIB
|
|
|
|
if (!font) {
|
2008-08-14 10:58:28 +00:00
|
|
|
unzFile zipFile = unzOpen((getThemeFileName()).c_str());
|
2006-10-08 10:57:48 +00:00
|
|
|
if (zipFile && unzLocateFile(zipFile, filename, 2) == UNZ_OK) {
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
unzOpenCurrentFile(zipFile);
|
|
|
|
unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
|
|
|
|
uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
|
|
|
|
assert(buffer);
|
|
|
|
memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
|
|
|
|
unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
|
|
|
|
unzCloseCurrentFile(zipFile);
|
|
|
|
Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2006-10-08 10:57:48 +00:00
|
|
|
font = Graphics::NewFont::loadFont(stream);
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2008-05-07 14:31:45 +00:00
|
|
|
delete[] buffer;
|
2006-10-08 10:57:48 +00:00
|
|
|
buffer = 0;
|
|
|
|
}
|
|
|
|
unzClose(zipFile);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (font) {
|
|
|
|
if (!cacheFilename.empty()) {
|
|
|
|
if (!Graphics::NewFont::cacheFontData(*font, cacheFilename)) {
|
|
|
|
warning("Couldn't create cache file for font '%s'", filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String Theme::genCacheFilename(const char *filename) {
|
|
|
|
Common::String cacheName(filename);
|
|
|
|
for (int i = cacheName.size() - 1; i >= 0; --i) {
|
|
|
|
if (cacheName[i] == '.') {
|
|
|
|
while ((uint)i < cacheName.size() - 1) {
|
|
|
|
cacheName.deleteLastChar();
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheName += "fcc";
|
|
|
|
return cacheName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2008-08-13 16:50:50 +00:00
|
|
|
bool Theme::isThemeLoadingRequired() {
|
|
|
|
int x = g_system->getOverlayWidth(), y = g_system->getOverlayHeight();
|
2006-10-08 10:57:48 +00:00
|
|
|
|
2008-08-13 16:50:50 +00:00
|
|
|
if (_loadedThemeX == x && _loadedThemeY == y)
|
2006-10-08 10:57:48 +00:00
|
|
|
return false;
|
2008-08-13 16:50:50 +00:00
|
|
|
|
|
|
|
_loadedThemeX = x;
|
|
|
|
_loadedThemeY = y;
|
2006-10-08 10:57:48 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-13 23:07:26 +00:00
|
|
|
bool Theme::themeConfigParseHeader(Common::String header, Common::String &themeName) {
|
|
|
|
header.trim();
|
|
|
|
|
|
|
|
if (header[0] != '[' || header.lastChar() != ']')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
header.deleteChar(0);
|
|
|
|
header.deleteLastChar();
|
|
|
|
|
|
|
|
Common::StringTokenizer tok(header, ":");
|
|
|
|
|
|
|
|
if (tok.nextToken() != SCUMMVM_THEME_VERSION_STR)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
themeName = tok.nextToken();
|
|
|
|
Common::String author = tok.nextToken();
|
|
|
|
|
|
|
|
return tok.empty();
|
|
|
|
}
|
|
|
|
|
2008-08-16 14:06:26 +00:00
|
|
|
bool Theme::themeConfigUseable(const FilesystemNode &node, Common::String &themeName) {
|
|
|
|
char stxHeader[128];
|
|
|
|
bool foundHeader = false;
|
|
|
|
|
2006-10-08 12:00:19 +00:00
|
|
|
if (ConfMan.hasKey("themepath"))
|
|
|
|
Common::File::addDefaultDirectory(ConfMan.get("themepath"));
|
|
|
|
|
|
|
|
#ifdef DATA_PATH
|
|
|
|
Common::File::addDefaultDirectoryRecursive(DATA_PATH);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ConfMan.hasKey("extrapath"))
|
|
|
|
Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
|
2008-08-13 23:07:26 +00:00
|
|
|
|
2008-08-16 14:06:26 +00:00
|
|
|
if (node.getName().hasSuffix(".zip")) {
|
2008-08-13 23:07:26 +00:00
|
|
|
#ifdef USE_ZLIB
|
2008-08-16 14:06:26 +00:00
|
|
|
unzFile zipFile = unzOpen(node.getPath().c_str());
|
2008-08-13 23:07:26 +00:00
|
|
|
|
2008-08-16 14:06:26 +00:00
|
|
|
if (zipFile && unzLocateFile(zipFile, "THEMERC", 2) == UNZ_OK) {
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
unzOpenCurrentFile(zipFile);
|
|
|
|
unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
|
|
|
|
uint8 *buffer = new uint8[fileInfo.uncompressed_size+1];
|
|
|
|
assert(buffer);
|
|
|
|
memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(uint8));
|
|
|
|
unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
|
|
|
|
unzCloseCurrentFile(zipFile);
|
|
|
|
Common::MemoryReadStream stream(buffer, fileInfo.uncompressed_size+1);
|
|
|
|
stream.readLine(stxHeader, 128);
|
2008-08-13 23:07:26 +00:00
|
|
|
|
2008-08-16 14:06:26 +00:00
|
|
|
if (themeConfigParseHeader(stxHeader, themeName))
|
|
|
|
foundHeader = true;
|
2008-08-13 23:07:26 +00:00
|
|
|
|
2008-08-16 14:06:26 +00:00
|
|
|
delete[] buffer;
|
|
|
|
buffer = 0;
|
|
|
|
}
|
|
|
|
unzClose(zipFile);
|
2008-08-13 23:07:26 +00:00
|
|
|
#else
|
2008-08-16 14:06:26 +00:00
|
|
|
return false;
|
2008-08-13 23:07:26 +00:00
|
|
|
#endif
|
2008-08-16 14:06:26 +00:00
|
|
|
} else if (node.isDirectory()) {
|
|
|
|
FilesystemNode headerfile = node.getChild("THEMERC");
|
|
|
|
if (!headerfile.exists() || !headerfile.isReadable() || headerfile.isDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Common::File f;
|
|
|
|
f.open(headerfile);
|
|
|
|
f.readLine(stxHeader, 128);
|
|
|
|
|
|
|
|
if (themeConfigParseHeader(stxHeader, themeName))
|
|
|
|
foundHeader = true;
|
|
|
|
}
|
2006-10-08 12:00:19 +00:00
|
|
|
|
2008-08-13 23:07:26 +00:00
|
|
|
return foundHeader;
|
|
|
|
}
|
2006-10-08 12:00:19 +00:00
|
|
|
|
2006-03-24 14:15:45 +00:00
|
|
|
} // End of namespace GUI
|
2007-11-04 03:38:30 +00:00
|
|
|
|