Skip to content

Commit

Permalink
Merge pull request #3037 from JosJuice/titles-txt-sort
Browse files Browse the repository at this point in the history
DolphinWX: Fix sorting games by custom titles
  • Loading branch information
phire committed Sep 18, 2015
2 parents 3ef103d + 210aa77 commit 3f56480
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Source/Core/DolphinWX/Cheats/CreateCodeDialog.cpp
Expand Up @@ -87,7 +87,7 @@ void CreateCodeDialog::PressOK(wxCommandEvent& ev)

// pretty hacky - add the code to the gameini
{
CISOProperties isoprops(SConfig::GetInstance().m_LastFilename, this);
CISOProperties isoprops(GameListItem(SConfig::GetInstance().m_LastFilename, std::unordered_map<std::string, std::string>()), this);
// add the code to the isoproperties arcode list
arCodes->push_back(new_cheat);
// save the gameini
Expand Down
77 changes: 28 additions & 49 deletions Source/Core/DolphinWX/GameListCtrl.cpp
Expand Up @@ -9,6 +9,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <wx/bitmap.h>
#include <wx/buffer.h>
Expand Down Expand Up @@ -83,13 +84,10 @@ static int CompareGameListItems(const GameListItem* iso1, const GameListItem* is
sortData = -sortData;
}

DiscIO::IVolume::ELanguage languageOne = SConfig::GetInstance().GetCurrentLanguage(iso1->GetPlatform() != DiscIO::IVolume::GAMECUBE_DISC);
DiscIO::IVolume::ELanguage languageOther = SConfig::GetInstance().GetCurrentLanguage(iso2->GetPlatform() != DiscIO::IVolume::GAMECUBE_DISC);

switch (sortData)
{
case CGameListCtrl::COLUMN_TITLE:
if (!strcasecmp(iso1->GetName(languageOne).c_str(), iso2->GetName(languageOther).c_str()))
if (!strcasecmp(iso1->GetName().c_str(), iso2->GetName().c_str()))
{
if (iso1->GetUniqueID() != iso2->GetUniqueID())
return t * (iso1->GetUniqueID() > iso2->GetUniqueID() ? 1 : -1);
Expand All @@ -98,8 +96,7 @@ static int CompareGameListItems(const GameListItem* iso1, const GameListItem* is
if (iso1->GetDiscNumber() != iso2->GetDiscNumber())
return t * (iso1->GetDiscNumber() > iso2->GetDiscNumber() ? 1 : -1);
}
return strcasecmp(iso1->GetName(languageOne).c_str(),
iso2->GetName(languageOther).c_str()) * t;
return strcasecmp(iso1->GetName().c_str(), iso2->GetName().c_str()) * t;
case CGameListCtrl::COLUMN_MAKER:
return strcasecmp(iso1->GetCompany().c_str(), iso2->GetCompany().c_str()) * t;
case CGameListCtrl::COLUMN_ID:
Expand Down Expand Up @@ -388,46 +385,6 @@ void CGameListCtrl::InsertItemInReportView(long _Index)

wxString name = StrToWxStr(rISOFile.GetName());

// Attempt to load game titles from titles.txt
// http://www.gametdb.com/Wii/Downloads
std::ifstream titlestxt;
OpenFStream(titlestxt, File::GetUserPath(D_LOAD_IDX) + "titles.txt", std::ios::in);

if (!titlestxt.is_open())
{
OpenFStream(titlestxt, File::GetUserPath(D_LOAD_IDX) + "wiitdb.txt", std::ios::in);
}

if (titlestxt.is_open() && rISOFile.GetUniqueID().size() > 3)
{
while (!titlestxt.eof())
{
std::string line;

if (!std::getline(titlestxt, line) && titlestxt.eof())
break;

const size_t equals_index = line.find('=');
std::string game_id = rISOFile.GetUniqueID();

// Ignore publisher ID for WAD files
if (rISOFile.GetPlatform() == DiscIO::IVolume::WII_WAD)
game_id.erase(game_id.size() - 2);

if (line.substr(0, equals_index - 1) == game_id)
{
name = StrToWxStr(StripSpaces(line.substr(equals_index + 1)));
break;
}
}
titlestxt.close();
}

std::string title;
IniFile gameini = SConfig::LoadGameIni(rISOFile.GetUniqueID(), rISOFile.GetRevision());
if (gameini.GetIfExists("EmuState", "Title", &title))
name = StrToWxStr(title);

int disc_number = rISOFile.GetDiscNumber() + 1;
if (disc_number > 1 && name.Lower().find(wxString::Format("disc %i", disc_number)) == std::string::npos
&& name.Lower().find(wxString::Format("disc%i", disc_number)) == std::string::npos)
Expand Down Expand Up @@ -483,6 +440,28 @@ void CGameListCtrl::ScanForISOs()
{
ClearIsoFiles();

// Load custom game titles from titles.txt
// http://www.gametdb.com/Wii/Downloads
std::unordered_map<std::string, std::string> custom_title_map;
std::ifstream titlestxt;
OpenFStream(titlestxt, File::GetUserPath(D_LOAD_IDX) + "titles.txt", std::ios::in);

if (!titlestxt.is_open())
OpenFStream(titlestxt, File::GetUserPath(D_LOAD_IDX) + "wiitdb.txt", std::ios::in);

if (titlestxt.is_open())
{
std::string line;
while (!titlestxt.eof() && std::getline(titlestxt, line))
{
const size_t equals_index = line.find('=');
if (equals_index != std::string::npos)
custom_title_map.emplace(StripSpaces(line.substr(0, equals_index)),
StripSpaces(line.substr(equals_index + 1)));
}
titlestxt.close();
}

std::vector<std::string> Extensions;

if (SConfig::GetInstance().m_ListGC)
Expand Down Expand Up @@ -529,7 +508,7 @@ void CGameListCtrl::ScanForISOs()
if (dialog.WasCancelled())
break;

auto iso_file = std::make_unique<GameListItem>(rFilenames[i]);
auto iso_file = std::make_unique<GameListItem>(rFilenames[i], custom_title_map);

if (iso_file->IsValid())
{
Expand Down Expand Up @@ -624,7 +603,7 @@ void CGameListCtrl::ScanForISOs()

for (const auto& drive : drives)
{
auto gli = std::make_unique<GameListItem>(drive);
auto gli = std::make_unique<GameListItem>(drive, custom_title_map);

if (gli->IsValid())
m_ISOFiles.push_back(gli.release());
Expand Down Expand Up @@ -1035,7 +1014,7 @@ void CGameListCtrl::OnProperties(wxCommandEvent& WXUNUSED (event))
if (!iso)
return;

CISOProperties* ISOProperties = new CISOProperties(iso->GetFileName(), this);
CISOProperties* ISOProperties = new CISOProperties(*iso, this);
ISOProperties->Show();
}

Expand Down
45 changes: 32 additions & 13 deletions Source/Core/DolphinWX/ISOFile.cpp
Expand Up @@ -8,6 +8,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include <wx/app.h>
Expand Down Expand Up @@ -62,7 +63,7 @@ static std::string GetLanguageString(DiscIO::IVolume::ELanguage language, std::m
return "";
}

GameListItem::GameListItem(const std::string& _rFileName)
GameListItem::GameListItem(const std::string& _rFileName, const std::unordered_map<std::string, std::string>& custom_titles)
: m_FileName(_rFileName)
, m_emu_state(0)
, m_FileSize(0)
Expand All @@ -73,6 +74,7 @@ GameListItem::GameListItem(const std::string& _rFileName)
, m_ImageWidth(0)
, m_ImageHeight(0)
, m_disc_number(0)
, m_has_custom_name(false)
{
if (LoadFromCache())
{
Expand Down Expand Up @@ -130,6 +132,24 @@ GameListItem::GameListItem(const std::string& _rFileName)
IniFile ini = SConfig::LoadGameIni(m_UniqueID, m_Revision);
ini.GetIfExists("EmuState", "EmulationStateId", &m_emu_state);
ini.GetIfExists("EmuState", "EmulationIssues", &m_issues);
m_has_custom_name = ini.GetIfExists("EmuState", "Title", &m_custom_name);

if (!m_has_custom_name)
{
std::string game_id = m_UniqueID;

// Ignore publisher ID for WAD files
if (m_Platform == DiscIO::IVolume::WII_WAD)
game_id.erase(game_id.size() - 2);

auto end = custom_titles.end();
auto it = custom_titles.find(game_id);
if (it != end)
{
m_custom_name = it->second;
m_has_custom_name = true;
}
}
}

if (!IsValid() && IsElfOrDol())
Expand Down Expand Up @@ -200,12 +220,10 @@ void GameListItem::DoState(PointerWrap &p)

bool GameListItem::IsElfOrDol() const
{
const std::string name = GetName();
const size_t pos = name.rfind('.');

const size_t pos = m_FileName.rfind('.');
if (pos != std::string::npos)
{
std::string ext = name.substr(pos);
std::string ext = m_FileName.substr(pos);
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);

return ext == ".elf" || ext == ".dol";
Expand Down Expand Up @@ -290,17 +308,18 @@ std::string GameListItem::GetName(DiscIO::IVolume::ELanguage language) const

std::string GameListItem::GetName() const
{
if (m_has_custom_name)
return m_custom_name;

bool wii = m_Platform != DiscIO::IVolume::GAMECUBE_DISC;
std::string name = GetName(SConfig::GetInstance().GetCurrentLanguage(wii));
if (name.empty())
{
std::string ext;
if (!name.empty())
return name;

// No usable name, return filename (better than nothing)
SplitPath(GetFileName(), nullptr, &name, &ext);
return name + ext;
}
return name;
// No usable name, return filename (better than nothing)
std::string ext;
SplitPath(GetFileName(), nullptr, &name, &ext);
return name + ext;
}

std::vector<DiscIO::IVolume::ELanguage> GameListItem::GetLanguages() const
Expand Down
8 changes: 6 additions & 2 deletions Source/Core/DolphinWX/ISOFile.h
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

Expand All @@ -17,10 +18,10 @@
#endif

class PointerWrap;
class GameListItem : NonCopyable
class GameListItem
{
public:
GameListItem(const std::string& _rFileName);
GameListItem(const std::string& _rFileName, const std::unordered_map<std::string, std::string>& custom_titles);
~GameListItem();

bool IsValid() const {return m_Valid;}
Expand Down Expand Up @@ -78,6 +79,9 @@ class GameListItem : NonCopyable
int m_ImageWidth, m_ImageHeight;
u8 m_disc_number;

std::string m_custom_name;
bool m_has_custom_name;

bool LoadFromCache();
void SaveToCache();

Expand Down
28 changes: 13 additions & 15 deletions Source/Core/DolphinWX/ISOProperties.cpp
Expand Up @@ -99,11 +99,12 @@ BEGIN_EVENT_TABLE(CISOProperties, wxDialog)
EVT_CHOICE(ID_LANG, CISOProperties::OnChangeBannerLang)
END_EVENT_TABLE()

CISOProperties::CISOProperties(const std::string& fileName, wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& position, const wxSize& size, long style)
CISOProperties::CISOProperties(const GameListItem& game_list_item, wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& position, const wxSize& size, long style)
: wxDialog(parent, id, title, position, size, style)
, OpenGameListItem(game_list_item)
{
// Load ISO data
OpenISO = DiscIO::CreateVolumeFromFilename(fileName);
OpenISO = DiscIO::CreateVolumeFromFilename(OpenGameListItem.GetFileName());

// Is it really necessary to use GetTitleID if GetUniqueID fails?
game_id = OpenISO->GetUniqueID();
Expand All @@ -122,8 +123,6 @@ CISOProperties::CISOProperties(const std::string& fileName, wxWindow* parent, wx
GameIniLocal = SConfig::LoadLocalGameIni(game_id, OpenISO->GetRevision());

// Setup GUI
OpenGameListItem = new GameListItem(fileName);

bRefreshList = false;

CreateGUIControls();
Expand Down Expand Up @@ -191,7 +190,7 @@ CISOProperties::CISOProperties(const std::string& fileName, wxWindow* parent, wx
bool wii = OpenISO->GetVolumeType() != DiscIO::IVolume::GAMECUBE_DISC;
ChangeBannerDetails(SConfig::GetInstance().GetCurrentLanguage(wii));

m_Banner->SetBitmap(OpenGameListItem->GetBitmap());
m_Banner->SetBitmap(OpenGameListItem.GetBitmap());
m_Banner->Bind(wxEVT_RIGHT_DOWN, &CISOProperties::RightClickOnBanner, this);

// Filesystem browser/dumper
Expand All @@ -205,7 +204,7 @@ CISOProperties::CISOProperties(const std::string& fileName, wxWindow* parent, wx
{
for (u32 i = 0; i < 0xFFFFFFFF; i++) // yes, technically there can be OVER NINE THOUSAND partitions...
{
std::unique_ptr<DiscIO::IVolume> volume(DiscIO::CreateVolumeFromFilename(fileName, group, i));
std::unique_ptr<DiscIO::IVolume> volume(DiscIO::CreateVolumeFromFilename(OpenGameListItem.GetFileName(), group, i));
if (volume != nullptr)
{
std::unique_ptr<DiscIO::IFileSystem> file_system(DiscIO::CreateFileSystem(volume.get()));
Expand Down Expand Up @@ -247,7 +246,6 @@ CISOProperties::~CISOProperties()
{
if (OpenISO->GetVolumeType() == DiscIO::IVolume::GAMECUBE_DISC && pFileSystem)
delete pFileSystem;
delete OpenGameListItem;
delete OpenISO;
}

Expand Down Expand Up @@ -497,7 +495,7 @@ void CISOProperties::CreateGUIControls()
bool wii = OpenISO->GetVolumeType() != DiscIO::IVolume::GAMECUBE_DISC;
DiscIO::IVolume::ELanguage preferred_language = SConfig::GetInstance().GetCurrentLanguage(wii);

std::vector<DiscIO::IVolume::ELanguage> languages = OpenGameListItem->GetLanguages();
std::vector<DiscIO::IVolume::ELanguage> languages = OpenGameListItem.GetLanguages();
int preferred_language_index = 0;
for (size_t i = 0; i < languages.size(); ++i)
{
Expand Down Expand Up @@ -1248,7 +1246,7 @@ void CISOProperties::OnComputeMD5Sum(wxCommandEvent& WXUNUSED (event))
u64 read_offset = 0;
md5_context ctx;

File::IOFile file(OpenGameListItem->GetFileName(), "rb");
File::IOFile file(OpenGameListItem.GetFileName(), "rb");
u64 game_size = file.GetSize();

wxProgressDialog progressDialog(
Expand Down Expand Up @@ -1497,23 +1495,23 @@ void CISOProperties::ActionReplayButtonClicked(wxCommandEvent& event)

void CISOProperties::OnChangeBannerLang(wxCommandEvent& event)
{
ChangeBannerDetails(OpenGameListItem->GetLanguages()[event.GetSelection()]);
ChangeBannerDetails(OpenGameListItem.GetLanguages()[event.GetSelection()]);
}

void CISOProperties::ChangeBannerDetails(DiscIO::IVolume::ELanguage language)
{
wxString const name = StrToWxStr(OpenGameListItem->GetName(language));
wxString const comment = StrToWxStr(OpenGameListItem->GetDescription(language));
wxString const maker = StrToWxStr(OpenGameListItem->GetCompany());
wxString const name = StrToWxStr(OpenGameListItem.GetName(language));
wxString const comment = StrToWxStr(OpenGameListItem.GetDescription(language));
wxString const maker = StrToWxStr(OpenGameListItem.GetCompany());

// Updates the information shown in the window
m_Name->SetValue(name);
m_Comment->SetValue(comment);
m_Maker->SetValue(maker);//dev too

std::string filename, extension;
SplitPath(OpenGameListItem->GetFileName(), nullptr, &filename, &extension);
SplitPath(OpenGameListItem.GetFileName(), nullptr, &filename, &extension);
// Also sets the window's title
SetTitle(StrToWxStr(StringFromFormat("%s%s: %s - ", filename.c_str(),
extension.c_str(), OpenGameListItem->GetUniqueID().c_str())) + name);
extension.c_str(), OpenGameListItem.GetUniqueID().c_str())) + name);
}
5 changes: 3 additions & 2 deletions Source/Core/DolphinWX/ISOProperties.h
Expand Up @@ -19,6 +19,7 @@
#include "DiscIO/Volume.h"
#include "DiscIO/VolumeCreator.h"
#include "DolphinWX/ARCodeAddEdit.h"
#include "DolphinWX/ISOFile.h"
#include "DolphinWX/PatchAddEdit.h"

class GameListItem;
Expand Down Expand Up @@ -58,7 +59,7 @@ struct PHackData
class CISOProperties : public wxDialog
{
public:
CISOProperties(const std::string& fileName,
CISOProperties(const GameListItem& game_list_item,
wxWindow* parent,
wxWindowID id = wxID_ANY,
const wxString& title = _("Properties"),
Expand Down Expand Up @@ -215,7 +216,7 @@ class CISOProperties : public wxDialog
void SetRefresh(wxCommandEvent& event);
void OnChangeBannerLang(wxCommandEvent& event);

GameListItem* OpenGameListItem;
const GameListItem OpenGameListItem;

typedef std::vector<const DiscIO::SFileInfo*>::iterator fileIter;

Expand Down

0 comments on commit 3f56480

Please sign in to comment.