2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "libsquish/squish.h"
23 #include "TextureBundleXBT.h"
25 #include "GraphicContext.h"
26 #include "utils/log.h"
27 #include "addons/Skin.h"
28 #include "settings/Settings.h"
29 #include "filesystem/SpecialProtocol.h"
30 #include "utils/EndianSwap.h"
31 #include "utils/URIUtils.h"
33 #include <lzo/lzo1x.h>
34 #include "utils/StringUtils.h"
37 #pragma comment(lib,"liblzo2.lib")
40 CTextureBundleXBT::CTextureBundleXBT(void)
42 m_themeBundle = false;
46 CTextureBundleXBT::~CTextureBundleXBT(void)
51 bool CTextureBundleXBT::OpenBundle()
55 // Find the correct texture file (skin or theme)
60 // if we are the theme bundle, we only load if the user has chosen
61 // a valid theme (or the skin has a default one)
62 CStdString theme = CSettings::Get().GetString("lookandfeel.skintheme");
63 if (!theme.IsEmpty() && theme.CompareNoCase("SKINDEFAULT"))
65 CStdString themeXBT(URIUtils::ReplaceExtension(theme, ".xbt"));
66 strPath = URIUtils::AddFileToFolder(g_graphicsContext.GetMediaDir(), "media");
67 strPath = URIUtils::AddFileToFolder(strPath, themeXBT);
76 strPath = URIUtils::AddFileToFolder(g_graphicsContext.GetMediaDir(), "media/Textures.xbt");
79 strPath = CSpecialProtocol::TranslatePathConvertCase(strPath);
81 // Load the texture file
82 if (!m_XBTFReader.Open(strPath))
87 CLog::Log(LOGDEBUG, "%s - Opened bundle %s", __FUNCTION__, strPath.c_str());
89 m_TimeStamp = m_XBTFReader.GetLastModificationTimestamp();
91 if (lzo_init() != LZO_E_OK)
99 bool CTextureBundleXBT::HasFile(const CStdString& Filename)
101 if (!m_XBTFReader.IsOpen() && !OpenBundle())
104 if (m_XBTFReader.GetLastModificationTimestamp() > m_TimeStamp)
106 CLog::Log(LOGINFO, "Texture bundle has changed, reloading");
111 CStdString name = Normalize(Filename);
112 return m_XBTFReader.Exists(name);
115 void CTextureBundleXBT::GetTexturesFromPath(const CStdString &path, std::vector<CStdString> &textures)
117 if (path.GetLength() > 1 && path[1] == ':')
120 if (!m_XBTFReader.IsOpen() && !OpenBundle())
123 CStdString testPath = Normalize(path);
124 URIUtils::AddSlashAtEnd(testPath);
126 std::vector<CXBTFFile>& files = m_XBTFReader.GetFiles();
127 for (size_t i = 0; i < files.size(); i++)
129 CStdString path = files[i].GetPath();
130 if (StringUtils::StartsWithNoCase(path, testPath))
131 textures.push_back(path);
135 bool CTextureBundleXBT::LoadTexture(const CStdString& Filename, CBaseTexture** ppTexture,
136 int &width, int &height)
138 CStdString name = Normalize(Filename);
140 CXBTFFile* file = m_XBTFReader.Find(name);
144 if (file->GetFrames().size() == 0)
147 CXBTFFrame& frame = file->GetFrames().at(0);
148 if (!ConvertFrameToTexture(Filename, frame, ppTexture))
153 width = frame.GetWidth();
154 height = frame.GetHeight();
159 int CTextureBundleXBT::LoadAnim(const CStdString& Filename, CBaseTexture*** ppTextures,
160 int &width, int &height, int& nLoops, int** ppDelays)
162 CStdString name = Normalize(Filename);
164 CXBTFFile* file = m_XBTFReader.Find(name);
168 if (file->GetFrames().size() == 0)
171 size_t nTextures = file->GetFrames().size();
172 *ppTextures = new CBaseTexture*[nTextures];
173 *ppDelays = new int[nTextures];
175 for (size_t i = 0; i < nTextures; i++)
177 CXBTFFrame& frame = file->GetFrames().at(i);
179 if (!ConvertFrameToTexture(Filename, frame, &((*ppTextures)[i])))
184 (*ppDelays)[i] = frame.GetDuration();
187 width = file->GetFrames().at(0).GetWidth();
188 height = file->GetFrames().at(0).GetHeight();
189 nLoops = file->GetLoop();
194 bool CTextureBundleXBT::ConvertFrameToTexture(const CStdString& name, CXBTFFrame& frame, CBaseTexture** ppTexture)
196 // found texture - allocate the necessary buffers
197 squish::u8 *buffer = new squish::u8[(size_t)frame.GetPackedSize()];
200 CLog::Log(LOGERROR, "Out of memory loading texture: %s (need %"PRIu64" bytes)", name.c_str(), frame.GetPackedSize());
204 // load the compressed texture
205 if (!m_XBTFReader.Load(frame, buffer))
207 CLog::Log(LOGERROR, "Error loading texture: %s", name.c_str());
212 // check if it's packed with lzo
213 if (frame.IsPacked())
215 squish::u8 *unpacked = new squish::u8[(size_t)frame.GetUnpackedSize()];
216 if (unpacked == NULL)
218 CLog::Log(LOGERROR, "Out of memory unpacking texture: %s (need %"PRIu64" bytes)", name.c_str(), frame.GetUnpackedSize());
222 lzo_uint s = (lzo_uint)frame.GetUnpackedSize();
223 if (lzo1x_decompress_safe(buffer, (lzo_uint)frame.GetPackedSize(), unpacked, &s, NULL) != LZO_E_OK ||
224 s != frame.GetUnpackedSize())
226 CLog::Log(LOGERROR, "Error loading texture: %s: Decompression error", name.c_str());
235 // create an xbmc texture
236 *ppTexture = new CTexture();
237 (*ppTexture)->LoadFromMemory(frame.GetWidth(), frame.GetHeight(), 0, frame.GetFormat(), frame.HasAlpha(), buffer);
244 void CTextureBundleXBT::Cleanup()
246 if (m_XBTFReader.IsOpen())
248 m_XBTFReader.Close();
249 CLog::Log(LOGDEBUG, "%s - Closed %sbundle", __FUNCTION__, m_themeBundle ? "theme " : "");
253 void CTextureBundleXBT::SetThemeBundle(bool themeBundle)
255 m_themeBundle = themeBundle;
258 // normalize to how it's stored within the bundle
259 // lower case + using forward slash rather than back slash
260 CStdString CTextureBundleXBT::Normalize(const CStdString &name)
262 CStdString newName(name);
264 newName.Replace('\\','/');