2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 05:30:24 +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, version 2.0.
|
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2013-03-02 01:33:17 +00:00
|
|
|
#include <algorithm>
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#include "Common.h"
|
2009-02-22 12:43:25 +00:00
|
|
|
#include "ColorUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "BannerLoaderWii.h"
|
2009-06-07 02:54:07 +00:00
|
|
|
#include "VolumeCreator.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
#include "FileUtil.h"
|
2009-06-06 14:33:52 +00:00
|
|
|
#include "FileHandlerARC.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2013-03-02 01:33:17 +00:00
|
|
|
// HyperIris: dunno if this suitable, may be need move.
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/param.h>
|
|
|
|
#ifndef ANDROID
|
|
|
|
#include <iconv.h>
|
|
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ICONV_CONST
|
|
|
|
#if defined __FreeBSD__ || __NetBSD__
|
|
|
|
#define ICONV_CONST const
|
|
|
|
#else
|
|
|
|
#define ICONV_CONST
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
namespace DiscIO
|
|
|
|
{
|
2009-03-20 18:13:31 +00:00
|
|
|
|
2009-06-07 02:54:07 +00:00
|
|
|
CBannerLoaderWii::CBannerLoaderWii(DiscIO::IVolume *pVolume)
|
2008-12-08 05:30:24 +00:00
|
|
|
: m_pBannerFile(NULL)
|
|
|
|
, m_IsValid(false)
|
|
|
|
{
|
|
|
|
char Filename[260];
|
2009-04-28 23:47:18 +00:00
|
|
|
u64 TitleID;
|
|
|
|
|
2010-01-17 03:25:42 +00:00
|
|
|
pVolume->GetTitleID((u8*)&TitleID);
|
2009-06-07 02:54:07 +00:00
|
|
|
|
2009-04-28 23:47:18 +00:00
|
|
|
TitleID = Common::swap64(TitleID);
|
|
|
|
|
2010-02-02 21:56:29 +00:00
|
|
|
sprintf(Filename, "%stitle/%08x/%08x/data/banner.bin",
|
2011-02-28 20:40:15 +00:00
|
|
|
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
|
2009-03-20 18:13:31 +00:00
|
|
|
|
|
|
|
if (!File::Exists(Filename))
|
|
|
|
{
|
2009-06-06 14:33:52 +00:00
|
|
|
// TODO(XK): Finish the 'commented' code. Turns out the banner.bin
|
|
|
|
// from the savefiles is very different from the banner.bin
|
|
|
|
// inside opening.bnr
|
|
|
|
#if 0
|
|
|
|
char bnrFilename[260], titleFolder[260];
|
|
|
|
|
|
|
|
// Creating title folder
|
2010-02-02 21:56:29 +00:00
|
|
|
sprintf(titleFolder, "%stitle/%08x/%08x/data/",
|
2011-02-28 20:40:15 +00:00
|
|
|
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
|
2009-06-06 14:33:52 +00:00
|
|
|
if(!File::Exists(titleFolder))
|
|
|
|
File::CreateFullPath(titleFolder);
|
|
|
|
|
|
|
|
// Extracting banner.bin from opening.bnr
|
2010-02-02 21:56:29 +00:00
|
|
|
sprintf(bnrFilename, "%stitle/%08x/%08x/data/opening.bnr",
|
2011-02-28 20:40:15 +00:00
|
|
|
File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
|
2009-06-06 14:33:52 +00:00
|
|
|
|
|
|
|
if(!_rFileSystem.ExportFile("opening.bnr", bnrFilename)) {
|
|
|
|
m_IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CARCFile bnrArc (bnrFilename, 0x600);
|
|
|
|
|
|
|
|
if(!bnrArc.ExportFile("meta/banner.bin", Filename)) {
|
|
|
|
m_IsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have an LZ77-compressed file with a short IMD5 header
|
|
|
|
// TODO: Finish the job
|
|
|
|
|
|
|
|
File::Delete(bnrFilename);
|
|
|
|
#else
|
2009-03-20 18:13:31 +00:00
|
|
|
m_IsValid = false;
|
|
|
|
return;
|
2009-06-06 14:33:52 +00:00
|
|
|
#endif
|
2009-03-20 18:13:31 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-06 14:33:52 +00:00
|
|
|
// load the banner.bin
|
2008-12-20 18:46:49 +00:00
|
|
|
size_t FileSize = (size_t) File::GetSize(Filename);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
if (FileSize > 0)
|
|
|
|
{
|
|
|
|
m_pBannerFile = new u8[FileSize];
|
2011-03-11 10:21:46 +00:00
|
|
|
File::IOFile pFile(Filename, "rb");
|
2009-03-20 18:13:31 +00:00
|
|
|
if (pFile)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2011-03-11 10:21:46 +00:00
|
|
|
pFile.ReadBytes(m_pBannerFile, FileSize);
|
2008-12-08 05:30:24 +00:00
|
|
|
m_IsValid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CBannerLoaderWii::~CBannerLoaderWii()
|
|
|
|
{
|
|
|
|
if (m_pBannerFile)
|
|
|
|
{
|
|
|
|
delete [] m_pBannerFile;
|
|
|
|
m_pBannerFile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:13:31 +00:00
|
|
|
bool CBannerLoaderWii::IsValid()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-03-20 18:13:31 +00:00
|
|
|
return m_IsValid;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2010-01-11 23:24:32 +00:00
|
|
|
static inline u32 Average32(u32 a, u32 b) {
|
|
|
|
return ((a >> 1) & 0x7f7f7f7f) + ((b >> 1) & 0x7f7f7f7f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 GetPixel(u32 *buffer, unsigned int x, unsigned int y) {
|
|
|
|
// thanks to unsignedness, these also check for <0 automatically.
|
|
|
|
if (x > 191) return 0;
|
|
|
|
if (y > 63) return 0;
|
|
|
|
return buffer[y * 192 + x];
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:13:31 +00:00
|
|
|
bool CBannerLoaderWii::GetBanner(u32* _pBannerImage)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-02-07 17:31:35 +00:00
|
|
|
if (IsValid())
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-02-07 17:31:35 +00:00
|
|
|
SWiiBanner* pBanner = (SWiiBanner*)m_pBannerFile;
|
2009-08-03 22:51:13 +00:00
|
|
|
u32* Buffer = new u32[192 * 64];
|
2009-02-07 17:31:35 +00:00
|
|
|
decode5A3image(Buffer, (u16*)pBanner->m_BannerTexture, 192, 64);
|
2009-03-20 18:13:31 +00:00
|
|
|
for (int y = 0; y < 32; y++)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2009-03-20 18:13:31 +00:00
|
|
|
for (int x = 0; x < 96; x++)
|
2009-02-07 17:31:35 +00:00
|
|
|
{
|
2010-01-11 23:24:32 +00:00
|
|
|
// simplified plus-shaped "gaussian"
|
|
|
|
u32 surround = Average32(
|
|
|
|
Average32(GetPixel(Buffer, x*2 - 1, y*2), GetPixel(Buffer, x*2 + 1, y*2)),
|
|
|
|
Average32(GetPixel(Buffer, x*2, y*2 - 1), GetPixel(Buffer, x*2, y*2 + 1)));
|
|
|
|
_pBannerImage[y * 96 + x] = Average32(GetPixel(Buffer, x*2, y*2), surround);
|
2009-02-07 17:31:35 +00:00
|
|
|
}
|
2009-08-03 22:51:13 +00:00
|
|
|
}
|
|
|
|
delete[] Buffer;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-28 07:17:10 +00:00
|
|
|
bool CBannerLoaderWii::GetStringFromComments(const CommentIndex index, std::string& s)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2011-11-28 07:17:10 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2009-02-07 17:31:35 +00:00
|
|
|
if (IsValid())
|
2009-02-03 15:03:34 +00:00
|
|
|
{
|
2009-02-07 17:31:35 +00:00
|
|
|
// find Banner type
|
2011-11-28 07:17:10 +00:00
|
|
|
SWiiBanner *pBanner = (SWiiBanner*)m_pBannerFile;
|
2013-03-02 01:33:17 +00:00
|
|
|
// TODO: trim NULLs
|
|
|
|
ret = CopyBeUnicodeToString(s, pBanner->m_Comment[index], COMMENT_SIZE);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2011-11-28 07:17:10 +00:00
|
|
|
|
|
|
|
return ret;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 07:17:10 +00:00
|
|
|
bool CBannerLoaderWii::GetStringFromComments(const CommentIndex index, std::wstring& s)
|
2011-12-19 05:56:13 +00:00
|
|
|
{
|
|
|
|
if (IsValid())
|
|
|
|
{
|
|
|
|
// find Banner type
|
|
|
|
SWiiBanner* pBanner = (SWiiBanner*)m_pBannerFile;
|
|
|
|
|
2011-11-28 07:17:10 +00:00
|
|
|
std::wstring description;
|
|
|
|
for (int i = 0; i < COMMENT_SIZE; ++i)
|
|
|
|
description.push_back(Common::swap16(pBanner->m_Comment[index][i]));
|
|
|
|
|
|
|
|
s = description;
|
2011-12-19 05:56:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-03 01:46:55 +00:00
|
|
|
std::vector<std::string> CBannerLoaderWii::GetNames()
|
2011-11-28 07:17:10 +00:00
|
|
|
{
|
2013-03-03 01:46:55 +00:00
|
|
|
std::vector<std::string> ret(1);
|
|
|
|
|
|
|
|
if (!GetStringFromComments(NAME_IDX, ret[0]))
|
|
|
|
ret.clear();
|
2011-11-28 07:17:10 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-03 01:46:55 +00:00
|
|
|
std::string CBannerLoaderWii::GetCompany()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-03-03 01:46:55 +00:00
|
|
|
return "";
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 01:46:55 +00:00
|
|
|
std::vector<std::string> CBannerLoaderWii::GetDescriptions()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-03-03 01:46:55 +00:00
|
|
|
std::vector<std::string> result(1);
|
|
|
|
if (!GetStringFromComments(DESC_IDX, result[0]))
|
|
|
|
result.clear();
|
|
|
|
return result;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2009-03-20 18:13:31 +00:00
|
|
|
void CBannerLoaderWii::decode5A3image(u32* dst, u16* src, int width, int height)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
|
|
|
for (int y = 0; y < height; y += 4)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < width; x += 4)
|
|
|
|
{
|
|
|
|
for (int iy = 0; iy < 4; iy++, src += 4)
|
|
|
|
{
|
|
|
|
for (int ix = 0; ix < 4; ix++)
|
|
|
|
{
|
2009-02-22 12:43:25 +00:00
|
|
|
u32 RGBA = ColorUtil::Decode5A3(Common::swap16(src[ix]));
|
2008-12-08 05:30:24 +00:00
|
|
|
dst[(y + iy) * width + (x + ix)] = RGBA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 01:33:17 +00:00
|
|
|
bool CBannerLoaderWii::CopyBeUnicodeToString( std::string& _rDestination, const u16* _src, int length )
|
|
|
|
{
|
|
|
|
bool returnCode = false;
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (_src)
|
|
|
|
{
|
|
|
|
std::wstring src;
|
|
|
|
src.resize(length);
|
|
|
|
std::transform(_src, _src + length, &src[0], (u16(&)(u16))Common::swap16);
|
|
|
|
|
|
|
|
_rDestination = UTF16ToUTF8(src);
|
|
|
|
returnCode = true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef ANDROID
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (_src)
|
|
|
|
{
|
|
|
|
iconv_t conv_desc = iconv_open("UTF-8", "CP932");
|
|
|
|
if (conv_desc == (iconv_t) -1)
|
|
|
|
{
|
|
|
|
// Initialization failure.
|
|
|
|
if (errno == EINVAL)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Conversion from CP932 to UTF-8 is not supported.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "Iconv initialization failure: %s\n", strerror (errno));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* src_buffer = new char[length];
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
src_buffer[i] = swap16(_src[i]);
|
|
|
|
|
|
|
|
size_t inbytes = sizeof(char) * length;
|
|
|
|
size_t outbytes = 2 * inbytes;
|
|
|
|
char* utf8_buffer = new char[outbytes + 1];
|
|
|
|
memset(utf8_buffer, 0, (outbytes + 1) * sizeof(char));
|
|
|
|
|
|
|
|
// Save the buffer locations because iconv increments them
|
|
|
|
char* utf8_buffer_start = utf8_buffer;
|
|
|
|
char* src_buffer_start = src_buffer;
|
|
|
|
|
|
|
|
size_t iconv_size = iconv(conv_desc,
|
|
|
|
(ICONV_CONST char**)&src_buffer, &inbytes,
|
|
|
|
&utf8_buffer, &outbytes);
|
|
|
|
|
|
|
|
// Handle failures
|
|
|
|
if (iconv_size == (size_t) -1)
|
|
|
|
{
|
|
|
|
ERROR_LOG(DISCIO, "iconv failed.");
|
|
|
|
switch (errno) {
|
|
|
|
case EILSEQ:
|
|
|
|
ERROR_LOG(DISCIO, "Invalid multibyte sequence.");
|
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
ERROR_LOG(DISCIO, "Incomplete multibyte sequence.");
|
|
|
|
break;
|
|
|
|
case E2BIG:
|
|
|
|
ERROR_LOG(DISCIO, "Insufficient space allocated for output buffer.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR_LOG(DISCIO, "Error: %s.", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_rDestination = utf8_buffer_start;
|
|
|
|
returnCode = true;
|
|
|
|
}
|
|
|
|
delete[] utf8_buffer_start;
|
|
|
|
delete[] src_buffer_start;
|
|
|
|
iconv_close(conv_desc);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return returnCode;
|
|
|
|
}
|
|
|
|
|
2010-12-22 06:45:59 +00:00
|
|
|
} // namespace
|