Skip to content

Commit

Permalink
Add '--title-id' parameter and desktop shortcut creation (#889)
Browse files Browse the repository at this point in the history
* Add '--title-id' launch option to launch titles by title id
* Add title id column to game list
* Add option to create game shortcuts

Co-authored-by: Exzap <13877693+Exzap@users.noreply.github.com>
  • Loading branch information
capitalistspz and Exzap committed Jul 7, 2023
1 parent ea86c77 commit f1c200a
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/config/CemuConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void CemuConfig::Load(XMLConfigParser& parser)
column_width.game_time = loadColumnSize("game_time_width", DefaultColumnSize::game_time);
column_width.game_started = loadColumnSize("game_started_width", DefaultColumnSize::game_started);
column_width.region = loadColumnSize("region_width", DefaultColumnSize::region);
column_width.title_id = loadColumnSize("title_id", DefaultColumnSize::title_id);

recent_launch_files.clear();
auto launch_parser = parser.get("RecentLaunchFiles");
Expand Down Expand Up @@ -398,6 +399,7 @@ void CemuConfig::Save(XMLConfigParser& parser)
gamelist.set("game_time_width", column_width.game_time);
gamelist.set("game_started_width", column_width.game_started);
gamelist.set("region_width", column_width.region);
gamelist.set("title_id", column_width.title_id);

auto launch_files_parser = config.set("RecentLaunchFiles");
for (const auto& entry : recent_launch_files)
Expand Down
2 changes: 2 additions & 0 deletions src/config/CemuConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ namespace DefaultColumnSize {
game_time = 140u,
game_started = 160u,
region = 80u,
title_id = 160u
};
};

Expand Down Expand Up @@ -427,6 +428,7 @@ struct CemuConfig
uint32 game_time = DefaultColumnSize::game_time;
uint32 game_started = DefaultColumnSize::game_started;
uint32 region = DefaultColumnSize::region;
uint32 title_id = 0;
} column_width{};

// graphics
Expand Down
16 changes: 16 additions & 0 deletions src/config/LaunchSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ bool LaunchSettings::HandleCommandline(const std::vector<std::wstring>& args)
("version,v", "Displays the version of Cemu")

("game,g", po::wvalue<std::wstring>(), "Path of game to launch")
("title-id,t", po::value<std::string>(), "Title ID of the title to be launched (overridden by --game)")
("mlc,m", po::wvalue<std::wstring>(), "Custom mlc folder location")

("fullscreen,f", po::value<bool>()->implicit_value(true), "Launch games in fullscreen mode")
Expand Down Expand Up @@ -133,6 +134,21 @@ bool LaunchSettings::HandleCommandline(const std::vector<std::wstring>& args)

s_load_game_file = tmp;
}
if (vm.count("title-id"))
{
auto title_param = vm["title-id"].as<std::string>();
try {

if (title_param.starts_with('=')){
title_param.erase(title_param.begin());
}
s_load_title_id = std::stoull(title_param, nullptr, 16);
}
catch (std::invalid_argument const& e)
{
std::cerr << "Expected title_param ID as an unsigned 64-bit hexadecimal string\n";
}
}

if (vm.count("mlc"))
{
Expand Down
2 changes: 2 additions & 0 deletions src/config/LaunchSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class LaunchSettings
static bool HandleCommandline(const std::vector<std::wstring>& args);

static std::optional<fs::path> GetLoadFile() { return s_load_game_file; }
static std::optional<uint64> GetLoadTitleID() {return s_load_title_id;}
static std::optional<fs::path> GetMLCPath() { return s_mlc_path; }

static std::optional<bool> RenderUpsideDownEnabled() { return s_render_upside_down; }
Expand All @@ -35,6 +36,7 @@ class LaunchSettings

private:
inline static std::optional<fs::path> s_load_game_file{};
inline static std::optional<uint64> s_load_title_id{};
inline static std::optional<fs::path> s_mlc_path{};

inline static std::optional<bool> s_render_upside_down{};
Expand Down
33 changes: 27 additions & 6 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,32 @@ MainWindow::MainWindow()
#endif

auto* main_sizer = new wxBoxSizer(wxVERTICAL);
if (!LaunchSettings::GetLoadFile().has_value())
auto load_file = LaunchSettings::GetLoadFile();
auto load_title_id = LaunchSettings::GetLoadTitleID();
bool quick_launch = false;

if (load_file)
{
MainWindow::RequestLaunchGame(load_file.value(), wxLaunchGameEvent::INITIATED_BY::COMMAND_LINE);
quick_launch = true;
}
else if (load_title_id)
{
TitleInfo info;
TitleId baseId;
if (CafeTitleList::FindBaseTitleId(load_title_id.value(), baseId) && CafeTitleList::GetFirstByTitleId(baseId, info))
{
MainWindow::RequestLaunchGame(info.GetPath(), wxLaunchGameEvent::INITIATED_BY::COMMAND_LINE);
quick_launch = true;
}
else
{
wxString errorMsg = fmt::format("Title ID {:016x} not found", load_title_id.value());
wxMessageBox(errorMsg, _("Error"), wxOK | wxCENTRE | wxICON_ERROR);

}
}
if (!quick_launch)
{
{
m_main_panel = new wxPanel(this);
Expand All @@ -328,7 +353,7 @@ MainWindow::MainWindow()
}
else
{
// launching game via -g option. Dont setup or load game list
// launching game via -g or -t option. Don't set up or load game list
m_game_list = nullptr;
m_info_bar = nullptr;
}
Expand All @@ -350,10 +375,6 @@ MainWindow::MainWindow()
Bind(wxEVT_OPEN_GRAPHIC_PACK, &MainWindow::OnGraphicWindowOpen, this);
Bind(wxEVT_LAUNCH_GAME, &MainWindow::OnLaunchFromFile, this);

if (LaunchSettings::GetLoadFile().has_value())
{
MainWindow::RequestLaunchGame(LaunchSettings::GetLoadFile().value(), wxLaunchGameEvent::INITIATED_BY::COMMAND_LINE);
}
if (LaunchSettings::GDBStubEnabled())
{
g_gdbstub = std::make_unique<GDBServer>(config.gdb_port);
Expand Down
138 changes: 138 additions & 0 deletions src/gui/components/wxGameList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <wx/textdlg.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/wfstream.h>
#include <wx/imagpng.h>
#include <wx/string.h>

#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
Expand All @@ -30,6 +33,17 @@

#include "Cafe/IOSU/PDM/iosu_pdm.h" // for last played and play time

#if BOOST_OS_WINDOWS
// for shortcut creation
#include <windows.h>
#include <winnls.h>
#include <shobjidl.h>
#include <objbase.h>
#include <objidl.h>
#include <shlguid.h>
#include <shlobj.h>
#endif

// public events
wxDEFINE_EVENT(wxEVT_OPEN_SETTINGS, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_GAMELIST_BEGIN_UPDATE, wxCommandEvent);
Expand Down Expand Up @@ -79,6 +93,7 @@ wxGameList::wxGameList(wxWindow* parent, wxWindowID id)
InsertColumn(ColumnGameTime, _("You've played"), wxLIST_FORMAT_LEFT, config.column_width.game_time);
InsertColumn(ColumnGameStarted, _("Last played"), wxLIST_FORMAT_LEFT, config.column_width.game_started);
InsertColumn(ColumnRegion, _("Region"), wxLIST_FORMAT_LEFT, config.column_width.region);
InsertColumn(ColumnTitleID, _("Title ID"), wxLIST_FORMAT_LEFT, config.column_width.title_id);

const char transparent_bitmap[kIconWidth * kIconWidth * 4] = {0};
wxBitmap blank(transparent_bitmap, kIconWidth, kIconWidth);
Expand Down Expand Up @@ -244,6 +259,8 @@ int wxGameList::GetColumnDefaultWidth(int column)
return DefaultColumnSize::game_started;
case ColumnRegion:
return DefaultColumnSize::region;
case ColumnTitleID:
return DefaultColumnSize::title_id;
default:
return 80;
}
Expand Down Expand Up @@ -528,6 +545,7 @@ enum ContextMenuEntries
kContextMenuStyleList,
kContextMenuStyleIcon,
kContextMenuStyleIconSmall,
kContextMenuCreateShortcut
};
void wxGameList::OnContextMenu(wxContextMenuEvent& event)
{
Expand Down Expand Up @@ -568,6 +586,10 @@ void wxGameList::OnContextMenu(wxContextMenuEvent& event)
menu.Append(kContextMenuEditGraphicPacks, _("&Edit graphic packs"));
menu.Append(kContextMenuEditGameProfile, _("&Edit game profile"));

menu.AppendSeparator();
#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
menu.Append(kContextMenuCreateShortcut, _("&Create shortcut"));
#endif
menu.AppendSeparator();
}
}
Expand Down Expand Up @@ -687,6 +709,11 @@ void wxGameList::OnContextMenuSelected(wxCommandEvent& event)
(new GameProfileWindow(GetParent(), title_id))->Show();
break;
}
case kContextMenuCreateShortcut:
#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
CreateShortcut(gameInfo);
#endif
break;
}
}
}
Expand Down Expand Up @@ -729,6 +756,7 @@ void wxGameList::OnColumnRightClick(wxListEvent& event)
ShowGameTime,
ShowLastPlayed,
ShowRegion,
ShowTitleId
};
const int column = event.GetColumn();
wxMenu menu;
Expand All @@ -744,6 +772,7 @@ void wxGameList::OnColumnRightClick(wxListEvent& event)
menu.AppendCheckItem(ShowGameTime, _("Show &game time"))->Check(GetColumnWidth(ColumnGameTime) > 0);
menu.AppendCheckItem(ShowLastPlayed, _("Show &last played"))->Check(GetColumnWidth(ColumnGameStarted) > 0);
menu.AppendCheckItem(ShowRegion, _("Show &region"))->Check(GetColumnWidth(ColumnRegion) > 0);
menu.AppendCheckItem(ShowTitleId, _("Show &title ID"))->Check(GetColumnWidth(ColumnTitleID) > 0);

menu.Bind(wxEVT_COMMAND_MENU_SELECTED,
[this](wxCommandEvent& event) {
Expand Down Expand Up @@ -773,6 +802,9 @@ void wxGameList::OnColumnRightClick(wxListEvent& event)
case ShowRegion:
config.column_width.region = menu->IsChecked(ShowRegion) ? DefaultColumnSize::region : 0;
break;
case ShowTitleId:
config.column_width.title_id = menu->IsChecked(ShowTitleId) ? DefaultColumnSize::title_id : 0;
break;
case ResetWidth:
{
switch (column)
Expand All @@ -797,6 +829,8 @@ void wxGameList::OnColumnRightClick(wxListEvent& event)
case ColumnRegion:
config.column_width.region = DefaultColumnSize::region;
break;
case ColumnTitleID:
config.column_width.title_id = DefaultColumnSize::title_id;
default:
return;
}
Expand Down Expand Up @@ -836,6 +870,7 @@ void wxGameList::ApplyGameListColumnWidths()
SetColumnWidth(ColumnGameTime, config.column_width.game_time);
SetColumnWidth(ColumnGameStarted, config.column_width.game_started);
SetColumnWidth(ColumnRegion, config.column_width.region);
SetColumnWidth(ColumnTitleID, config.column_width.title_id);

AdjustLastColumnWidth();
}
Expand Down Expand Up @@ -1003,6 +1038,7 @@ void wxGameList::OnGameEntryUpdatedByTitleId(wxTitleIdEvent& event)

const auto region_text = fmt::format("{}", gameInfo.GetRegion());
SetItem(index, ColumnRegion, _(region_text));
SetItem(index, ColumnTitleID, _(fmt::format("{:016x}", titleId)));
}
else if (m_style == Style::kIcons)
{
Expand Down Expand Up @@ -1189,3 +1225,105 @@ void wxGameList::DeleteCachedStrings()
{
m_name_cache.clear();
}

#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
void wxGameList::CreateShortcut(GameInfo2& gameInfo) {
const auto title_id = gameInfo.GetBaseTitleId();
const auto title_name = gameInfo.GetTitleName();
const auto exe_path = ActiveSettings::GetExecutablePath();

#if BOOST_OS_LINUX
const wxString desktop_entry_name = wxString::Format("%s.desktop", title_name);
wxFileDialog entry_dialog(this, _("Choose desktop entry location"), "~/.local/share/applications", desktop_entry_name,
"Desktop file (*.desktop)|*.desktop", wxFD_SAVE | wxFD_CHANGE_DIR | wxFD_OVERWRITE_PROMPT);
#elif BOOST_OS_WINDOWS
// Get '%APPDATA%\Microsoft\Windows\Start Menu\Programs' path
PWSTR user_shortcut_folder;
SHGetKnownFolderPath(FOLDERID_Programs, 0, NULL, &user_shortcut_folder);
const wxString shortcut_name = wxString::Format("%s.lnk", title_name);
wxFileDialog entry_dialog(this, _("Choose shortcut location"), _pathToUtf8(user_shortcut_folder), shortcut_name,
"Shortcut (*.lnk)|*.lnk", wxFD_SAVE | wxFD_CHANGE_DIR | wxFD_OVERWRITE_PROMPT);
#endif
const auto result = entry_dialog.ShowModal();
if (result == wxID_CANCEL)
return;
const auto output_path = entry_dialog.GetPath();

#if BOOST_OS_LINUX
std::optional<fs::path> icon_path;
// Obtain and convert icon
{
m_icon_cache_mtx.lock();
const auto icon_iter = m_icon_cache.find(title_id);
const auto result_index = (icon_iter != m_icon_cache.cend()) ? std::optional<int>(icon_iter->second.first) : std::nullopt;
m_icon_cache_mtx.unlock();

// In most cases it should find it
if (!result_index){
wxMessageBox("Icon is yet to load, so will not be used by the shortcut", "Warning", wxOK | wxCENTRE | wxICON_WARNING);
}
else {
const auto out_icon_dir = ActiveSettings::GetDataPath("icons");
fs::create_directories(out_icon_dir);
icon_path = out_icon_dir / fmt::format("{:016x}.png", gameInfo.GetBaseTitleId());

auto image = m_image_list->GetIcon(result_index.value()).ConvertToImage();

wxFileOutputStream png_file(_pathToUtf8(icon_path.value()));
wxPNGHandler pngHandler;
pngHandler.SaveFile(&image, png_file, false);
}
}
const auto desktop_entry_string =
fmt::format("[Desktop Entry]\n"
"Name={}\n"
"Comment=Play {} on Cemu\n"
"Exec={} --title-id {:016x}\n"
"Icon={}\n"
"Terminal=false\n"
"Type=Application\n"
"Categories=Game;",
title_name,
title_name,
_pathToUtf8(exe_path),
title_id,
_pathToUtf8(icon_path.value_or("")));

std::ofstream output_stream(output_path);
if (!output_stream.good())
{
const wxString errorMsg = fmt::format("Failed to save desktop entry to {}", output_path.utf8_string());
wxMessageBox(errorMsg, _("Error"), wxOK | wxCENTRE | wxICON_ERROR);
return;
}
output_stream << desktop_entry_string;

#elif BOOST_OS_WINDOWS
IShellLinkW *shell_link;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&shell_link));
if (SUCCEEDED(hres))
{
const auto description = wxString::Format("Play %s on Cemu", title_name);
const auto args = wxString::Format("-t %016llx", title_id);

shell_link->SetPath(exe_path.wstring().c_str());
shell_link->SetDescription(description.wc_str());
shell_link->SetArguments(args.wc_str());
shell_link->SetWorkingDirectory(exe_path.parent_path().wstring().c_str());
// Use icon from Cemu exe for now since we can't embed icons into the shortcut
// in the future we could convert and store icons in AppData or ProgramData
shell_link->SetIconLocation(exe_path.wstring().c_str(), 0);

IPersistFile *shell_link_file;
// save the shortcut
hres = shell_link->QueryInterface(IID_IPersistFile, reinterpret_cast<LPVOID*>(&shell_link_file));
if (SUCCEEDED(hres))
{
hres = shell_link_file->Save(output_path.wc_str(), TRUE);
shell_link_file->Release();
}
shell_link->Release();
}
#endif
}
#endif
10 changes: 8 additions & 2 deletions src/gui/components/wxGameList.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <wx/listctrl.h>
#include <wx/timer.h>
#include <wx/panel.h>
#include <Cafe/TitleList/GameInfo.h>
#include "util/helpers/Semaphore.h"

class wxTitleIdEvent : public wxCommandEvent
Expand Down Expand Up @@ -52,6 +53,10 @@ class wxGameList : public wxListCtrl
void ReloadGameEntries(bool cached = false);
void DeleteCachedStrings();

#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
void CreateShortcut(GameInfo2& gameInfo);
#endif

long FindListItemByTitleId(uint64 title_id) const;
void OnClose(wxCloseEvent& event);

Expand All @@ -75,8 +80,9 @@ class wxGameList : public wxListCtrl
ColumnGameTime,
ColumnGameStarted,
ColumnRegion,
//ColumnFavorite,
ColumnCounts
ColumnTitleID,
//ColumnFavorite,
ColumnCounts,
};

int s_last_column = ColumnName;
Expand Down

0 comments on commit f1c200a

Please sign in to comment.