summaryrefslogtreecommitdiff
path: root/Source/Core/DiscIO/BannerLoaderWii.cpp
blob: dc32765b3ba3f5e7fcdb74c102bdbbeecd20bd00 (plain)
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
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <string>
#include <vector>

#include "Common/ColorUtil.h"
#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"

#include "DiscIO/BannerLoaderWii.h"
#include "DiscIO/Volume.h"

namespace DiscIO
{

CBannerLoaderWii::CBannerLoaderWii(DiscIO::IVolume *pVolume)
{
	u64 TitleID;
	pVolume->GetTitleID((u8*)&TitleID);
	TitleID = Common::swap64(TitleID);

	std::string Filename = StringFromFormat("%stitle/%08x/%08x/data/banner.bin",
		File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);

	if (!File::Exists(Filename))
	{
		// 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
		// Creating title folder
		std::string titleFolder = StringFromFormat("%stitle/%08x/%08x/data/",
			File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);
		if (!File::Exists(titleFolder))
			File::CreateFullPath(titleFolder);

		// Extracting banner.bin from opening.bnr
		std::string bnrFilename = StringFromFormat("%stitle/%08x/%08x/data/opening.bnr",
			File::GetUserPath(D_WIIUSER_IDX).c_str(), (u32)(TitleID>>32), (u32)TitleID);

		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
		m_IsValid = false;
		return;
#endif
	}

	// load the banner.bin
	size_t FileSize = (size_t) File::GetSize(Filename);

	if (FileSize > 0)
	{
		m_pBannerFile = new u8[FileSize];
		File::IOFile pFile(Filename, "rb");
		if (pFile)
		{
			pFile.ReadBytes(m_pBannerFile, FileSize);
			m_IsValid = true;
		}
	}
}

CBannerLoaderWii::~CBannerLoaderWii()
{
	if (m_pBannerFile)
	{
		delete [] m_pBannerFile;
		m_pBannerFile = nullptr;
	}
}

std::vector<u32> CBannerLoaderWii::GetBanner(int* pWidth, int* pHeight)
{
	SWiiBanner* pBanner = (SWiiBanner*)m_pBannerFile;
	std::vector<u32> Buffer;
	Buffer.resize(192 * 64);
	ColorUtil::decode5A3image(&Buffer[0], (u16*)pBanner->m_BannerTexture, 192, 64);
	*pWidth = 192;
	*pHeight = 64;
	return Buffer;
}

bool CBannerLoaderWii::GetStringFromComments(const CommentIndex index, std::string& result)
{
	if (IsValid())
	{
		auto const banner = reinterpret_cast<const SWiiBanner*>(m_pBannerFile);
		auto const src_ptr = banner->m_Comment[index];

		// Trim at first nullptr
		auto const length = std::find(src_ptr, src_ptr + COMMENT_SIZE, 0x0) - src_ptr;

		std::wstring src;
		src.resize(length);
		std::transform(src_ptr, src_ptr + src.size(), src.begin(), (u16(&)(u16))Common::swap16);
		result = UTF16ToUTF8(src);

		return true;
	}

	return false;
}

std::vector<std::string> CBannerLoaderWii::GetNames()
{
	std::vector<std::string> ret(1);

	if (!GetStringFromComments(NAME_IDX, ret[0]))
		ret.clear();

	return ret;
}

std::string CBannerLoaderWii::GetCompany()
{
	return "";
}

std::vector<std::string> CBannerLoaderWii::GetDescriptions()
{
	std::vector<std::string> result(1);
	if (!GetStringFromComments(DESC_IDX, result[0]))
		result.clear();
	return result;
}

} // namespace