usbloadergx/source/prompts/TitleBrowser.cpp

491 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
* TitleBrowser
* USB Loader GX 2009
*
* TitleBrowser.cpp *giantpune*
***************************************************************************/
#include <dirent.h>
#include <zlib.h>
#include "language/gettext.h"
#include "GUI/gui.h"
#include "GUI/gui_optionbrowser.h"
#include "prompts/PromptWindows.h"
#include "prompts/ProgressWindow.h"
#include "network/networkops.h"
#include "network/http.h"
#include "FileOperations/fileops.h"
#include "themes/CTheme.h"
#include "sys.h"
#include "menu.h"
#include "audio.h"
#include "wad/wad.h"
#include "xml/WiiTDB.hpp"
#include "wad/nandtitle.h"
#include "../usbloader/utils.h"
#include "../gecko.h"
extern u8 shutdown;
extern u8 reset;
/********************************************************************************
2010-09-24 02:48:03 +02:00
* TitleBrowser- opens a browser with a list of installed Titles
*********************************************************************************/
bool TitleSelector(char output[])
{
gprintf("TitleSelector()\n");
s32 num_titles;
s32 r = -1;
bool ret = false;
u64 *titleList = NULL;
ISFS_Initialize();//initialize for "titles.Exists()"
// Get count of titles of the good titles
2010-09-24 15:46:32 +02:00
num_titles = NandTitles.SetType(0x10001);
u32 n = num_titles;
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < n; i++)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
break;
}
//remove ones not actually installed on the nand
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
{
num_titles--;
}
}
//make a list of just the tids we are adding to the titlebrowser
2010-09-24 02:48:03 +02:00
titleList = (u64*) memalign(32, num_titles * sizeof(u64));
if (!titleList)
{
2010-09-24 02:48:03 +02:00
gprintf("TitleLister(): out of memory!\n");
return false;
}
OptionList options4;
//write the titles on the option browser
std::string Filepath = Settings.titlestxt_path;
Filepath += "wiitdb.xml";
WiiTDB *XML_DB = new WiiTDB(Filepath.c_str());
XML_DB->SetLanguageCode(Settings.db_language);
s32 i = 0;
2010-09-24 15:46:32 +02:00
NandTitles.SetType(0x10001);
2010-09-24 02:48:03 +02:00
while (i < num_titles)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
gprintf("shit happened\n");
break;
}
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
continue;
char id[5];
2010-09-24 15:46:32 +02:00
NandTitles.AsciiTID(tid, (char*) &id);
2010-09-24 02:48:03 +02:00
const char* name = NULL;
std::string TitleName;
if(XML_DB->GetTitle(id, TitleName))
name = TitleName.c_str();
else
name = NandTitles.NameOf(tid);
2010-09-24 02:48:03 +02:00
//gprintf("%016llx: %s: %s\n%p\t%p\n", tid, id, name, &id, name );
options4.SetName(i, "%s", id);
options4.SetValue(i, "%s", name ? name : tr( "Unknown" ));
2010-09-24 02:48:03 +02:00
titleList[i] = tid;
i++;
}
delete XML_DB;
XML_DB = NULL;
2010-09-24 02:48:03 +02:00
options4.SetName(i, " ");
options4.SetValue(i, "%s", tr( "Clear" ));
ISFS_Deinitialize();
bool exit = false;
GuiImageData btnOutline(Resources::GetFile("button_dialogue_box.png"), Resources::GetFileSize("button_dialogue_box.png"));
GuiImageData settingsbg(Resources::GetFile("settings_background.png"), Resources::GetFileSize("settings_background.png"));
2010-09-24 02:48:03 +02:00
GuiImage settingsbackground(&settingsbg);
GuiButton settingsbackgroundbtn(settingsbackground.GetWidth(), settingsbackground.GetHeight());
settingsbackgroundbtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
settingsbackgroundbtn.SetPosition(0, 0);
settingsbackgroundbtn.SetImage(&settingsbackground);
GuiTrigger trigA;
2010-09-24 02:48:03 +02:00
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
GuiTrigger trigB;
2010-09-24 02:48:03 +02:00
trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B);
GuiText cancelBtnTxt(tr( "Back" ), 22, thColor("r=0 g=0 b=0 a=255 - prompt windows button text color"));
2010-09-24 02:48:03 +02:00
cancelBtnTxt.SetMaxWidth(btnOutline.GetWidth() - 30);
GuiImage cancelBtnImg(&btnOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
cancelBtnTxt.SetWidescreen(Settings.widescreen);
cancelBtnImg.SetWidescreen(Settings.widescreen);
}
GuiButton cancelBtn(&cancelBtnImg, &cancelBtnImg, 2, 3, 180, 400, &trigA, btnSoundOver, btnSoundClick2, 1);
2010-09-24 02:48:03 +02:00
cancelBtn.SetLabel(&cancelBtnTxt);
cancelBtn.SetTrigger(&trigB);
GuiOptionBrowser optionBrowser4(396, 280, &options4, "bg_options_settings.png");
2010-09-24 02:48:03 +02:00
optionBrowser4.SetPosition(0, 90);
optionBrowser4.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
HaltGui();
2010-09-24 02:48:03 +02:00
GuiWindow w(screenwidth, screenheight);
w.Append(&settingsbackgroundbtn);
w.Append(&cancelBtn);
w.Append(&optionBrowser4);
mainWindow->SetState(STATE_DISABLED);
mainWindow->Append(&w);
ResumeGui();
2010-09-24 02:48:03 +02:00
while (!exit)
{
2010-09-24 02:48:03 +02:00
VIDEO_WaitVSync();
if (shutdown == 1) Sys_Shutdown();
if (reset == 1) Sys_Reboot();
r = optionBrowser4.GetClickedOption();
if (r > -1)
{ //if a click happened
if (r < num_titles)
{
u64 tid = titleList[r];
sprintf(output, "%08x", TITLE_LOWER( tid ));
}
else output[0] = 0;
ret = true;
exit = true;
}
else if (cancelBtn.GetState() == STATE_CLICKED)
{
//break the loop and end the function
exit = true;
}
}
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->SetState(STATE_DEFAULT);
mainWindow->Remove(&w);
free(titleList);
ResumeGui();
return ret;
}
int TitleBrowser()
{
u32 num_titles;
u32 num_sys_titles;
s32 ret = -1;
u64 *titleList = NULL;
ISFS_Initialize();//initialize for "titles.Exists()"
// Get count of titles of the good titles
2010-09-24 15:46:32 +02:00
num_titles = NandTitles.SetType(0x10001);
u32 n = num_titles;
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < n; i++)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
break;
}
//remove ones not actually installed on the nand
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
{
num_titles--;
}
}
// Get count of system titles
2010-09-24 15:46:32 +02:00
num_sys_titles = NandTitles.SetType(0x10002);
n = num_sys_titles;
2010-09-24 02:48:03 +02:00
for (u32 i = 0; i < n; i++)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
break;
}
//these can't be booted anyways
if (TITLE_LOWER( tid ) == 0x48414741 || TITLE_LOWER( tid ) == 0x48414141 || TITLE_LOWER( tid ) == 0x48414641)
{
num_sys_titles--;
continue;
}
//these aren't installed on the nand
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
{
num_sys_titles--;
}
}
//make a list of just the tids we are adding to the titlebrowser
2010-09-24 02:48:03 +02:00
titleList = (u64*) memalign(32, (num_titles + num_sys_titles) * sizeof(u64));
if (!titleList)
{
2010-09-24 02:48:03 +02:00
gprintf("TitleBrowser(): out of memory!\n");
return -1;
}
OptionList options3;
//write the titles on the option browser
std::string Filepath = Settings.titlestxt_path;
Filepath += "wiitdb.xml";
WiiTDB *XML_DB = new WiiTDB(Filepath.c_str());
XML_DB->SetLanguageCode(Settings.db_language);
u32 i = 0;
2010-09-24 15:46:32 +02:00
NandTitles.SetType(0x10001);
//first add the good stuff
2010-09-24 02:48:03 +02:00
while (i < num_titles)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
gprintf("shit happened3\n");
break;
}
2010-09-24 15:46:32 +02:00
gprintf("[ %u ] tid: %016llx\t%s\n", i, tid, NandTitles.NameOf(tid));
2010-09-24 02:48:03 +02:00
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
{
continue;
}
char id[5];
2010-09-24 15:46:32 +02:00
NandTitles.AsciiTID(tid, (char*) &id);
2010-09-24 02:48:03 +02:00
const char* name = NULL;
std::string TitleName;
if(XML_DB->GetTitle(id, TitleName))
name = TitleName.c_str();
else
name = NandTitles.NameOf(tid);
2010-09-24 02:48:03 +02:00
options3.SetName(i, "%s", id);
options3.SetValue(i, "%s", name ? name : tr( "Unknown" ));
2010-09-24 02:48:03 +02:00
titleList[i] = tid;
i++;
}
2010-09-24 15:46:32 +02:00
NandTitles.SetType(0x10002);
2010-09-24 02:48:03 +02:00
while (i < num_sys_titles + num_titles)
{
2010-09-24 15:46:32 +02:00
u64 tid = NandTitles.Next();
2010-09-24 02:48:03 +02:00
if (!tid)
{
break;
}
if (TITLE_LOWER( tid ) == 0x48414741 || TITLE_LOWER( tid ) == 0x48414141 || TITLE_LOWER( tid ) == 0x48414641) continue;
2010-09-24 15:46:32 +02:00
if (!NandTitles.Exists(tid))
2010-09-24 02:48:03 +02:00
{
continue;
}
char id[5];
2010-09-24 15:46:32 +02:00
NandTitles.AsciiTID(tid, (char*) &id);
const char* name = NULL;
std::string TitleName;
if(XML_DB->GetTitle(id, TitleName))
name = TitleName.c_str();
else
name = NandTitles.NameOf(tid);
2010-09-24 02:48:03 +02:00
options3.SetName(i, "%s", id);
options3.SetValue(i, "%s", name ? name : tr( "Unknown" ));
2010-09-24 02:48:03 +02:00
titleList[i] = tid;
i++;
}
ISFS_Deinitialize();
delete XML_DB;
XML_DB = NULL;
2010-09-24 02:48:03 +02:00
if (i == num_titles + num_sys_titles)
{
2010-09-24 02:48:03 +02:00
options3.SetName(i, " ");
options3.SetValue(i, "%s", tr( "Wii Settings" ));
}
bool exit = false;
int total = num_titles + num_sys_titles;
2010-09-24 02:48:03 +02:00
if (IsNetworkInit()) ResumeNetworkWait();
GuiImageData btnOutline(Resources::GetFile("button_dialogue_box.png"), Resources::GetFileSize("button_dialogue_box.png"));
GuiImageData settingsbg(Resources::GetFile("settings_background.png"), Resources::GetFileSize("settings_background.png"));
GuiTrigger trigA;
2010-09-24 02:48:03 +02:00
trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A);
GuiTrigger trigHome;
2010-09-24 02:48:03 +02:00
trigHome.SetButtonOnlyTrigger(-1, WPAD_BUTTON_HOME | WPAD_CLASSIC_BUTTON_HOME, 0);
GuiTrigger trigB;
2010-09-24 02:48:03 +02:00
trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B);
GuiText titleTxt(tr( "Title Launcher" ), 28, ( GXColor )
{ 0, 0, 0, 255});
titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
titleTxt.SetPosition(12, 40);
titleTxt.SetMaxWidth(356, SCROLL_HORIZONTAL);
GuiImage settingsbackground(&settingsbg);
GuiButton settingsbackgroundbtn(settingsbackground.GetWidth(), settingsbackground.GetHeight());
settingsbackgroundbtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP);
settingsbackgroundbtn.SetPosition(0, 0);
settingsbackgroundbtn.SetImage(&settingsbackground);
GuiText cancelBtnTxt(tr( "Back" ), 22, thColor("r=0 g=0 b=0 a=255 - prompt windows button text color"));
2010-09-24 02:48:03 +02:00
cancelBtnTxt.SetMaxWidth(btnOutline.GetWidth() - 30);
GuiImage cancelBtnImg(&btnOutline);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
cancelBtnTxt.SetWidescreen(Settings.widescreen);
cancelBtnImg.SetWidescreen(Settings.widescreen);
}
GuiButton cancelBtn(&cancelBtnImg, &cancelBtnImg, 2, 3, 180, 400, &trigA, btnSoundOver, btnSoundClick2, 1);
2010-09-24 02:48:03 +02:00
cancelBtn.SetScale(0.9);
cancelBtn.SetLabel(&cancelBtnTxt);
cancelBtn.SetTrigger(&trigB);
GuiOptionBrowser optionBrowser3(396, 280, &options3, "bg_options_settings.png");
2010-09-24 02:48:03 +02:00
optionBrowser3.SetPosition(0, 90);
optionBrowser3.SetAlignment(ALIGN_CENTRE, ALIGN_TOP);
GuiImageData wifiImgData(Resources::GetFile("wifi_btn.png"), Resources::GetFileSize("wifi_btn.png"));
2010-09-24 02:48:03 +02:00
GuiImage wifiImg(&wifiImgData);
2010-10-27 16:45:27 +02:00
if (Settings.wsprompt)
{
2010-09-24 02:48:03 +02:00
wifiImg.SetWidescreen(Settings.widescreen);
}
2010-09-24 02:48:03 +02:00
GuiButton wifiBtn(wifiImg.GetWidth(), wifiImg.GetHeight());
wifiBtn.SetImage(&wifiImg);
wifiBtn.SetPosition(100, 400);
wifiBtn.SetEffectGrow();
2010-09-24 02:48:03 +02:00
wifiBtn.SetAlpha(80);
wifiBtn.SetTrigger(&trigA);
HaltGui();
2010-09-24 02:48:03 +02:00
GuiWindow w(screenwidth, screenheight);
w.Append(&settingsbackgroundbtn);
w.Append(&titleTxt);
w.Append(&cancelBtn);
w.Append(&wifiBtn);
w.Append(&optionBrowser3);
mainWindow->Append(&w);
ResumeGui();
2010-09-24 02:48:03 +02:00
while (!exit)
{
2010-09-24 02:48:03 +02:00
VIDEO_WaitVSync();
if (shutdown == 1) Sys_Shutdown();
if (reset == 1)
Sys_Reboot();
else if (wifiBtn.GetState() == STATE_CLICKED)
{
ResumeNetworkWait();
wifiBtn.ResetState();
}
if (IsNetworkInit())
{
wifiBtn.SetAlpha(255);
}
ret = optionBrowser3.GetClickedOption();
if (ret > -1)
{ //if a click happened
if (ret < total)
{
//set the title's name, number, ID to text
char text[0x100];
char id[5];
2010-09-24 15:46:32 +02:00
NandTitles.AsciiTID(titleList[ret], (char*) &id);
2010-09-24 02:48:03 +02:00
2010-09-24 15:46:32 +02:00
snprintf(text, sizeof(text), "%s : %s", id, NandTitles.NameOf(titleList[ret]));
2010-09-24 02:48:03 +02:00
//prompt to boot selected title
if (WindowPrompt(tr( "Boot?" ), text, tr( "OK" ), tr( "Cancel" )))
{ //if they say yes
ExitApp();
2010-09-24 02:48:03 +02:00
WII_Initialize();
WII_LaunchTitle(titleList[ret]);
//this really shouldn't be needed because the title will be booted
exit = true;
break;
}
else
{
//if they said no to booting the title
ret = -1;
optionBrowser3.ResetState();
}
}
else if (ret == total)
{ //if they clicked to go to the wii settings
ExitApp();
2010-09-24 02:48:03 +02:00
WII_Initialize();
WII_ReturnToSettings();
}
}
if (cancelBtn.GetState() == STATE_CLICKED)
{
//break the loop and end the function
exit = true;
ret = -10;
}
}
CloseConnection();
2010-09-24 02:48:03 +02:00
if (IsNetworkInit()) HaltNetworkThread();
HaltGui();
2010-09-24 02:48:03 +02:00
mainWindow->Remove(&w);
free(titleList);
ResumeGui();
return ret;
}