Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YGOmod committed Sep 3, 2024
1 parent e3432d4 commit 2c262aa
Show file tree
Hide file tree
Showing 51 changed files with 586 additions and 678 deletions.
24 changes: 0 additions & 24 deletions src/android/app/src/main/cpp/AndroidGameIconLoadedCallback.h

This file was deleted.

21 changes: 17 additions & 4 deletions src/android/app/src/main/cpp/AndroidGameTitleLoadedCallback.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include "GameTitleLoader.h"
#include "JNIUtils.h"

class AndroidGameTitleLoadedCallback : public GameTitleLoadedCallback {
class AndroidGameTitleLoadedCallback : public GameTitleLoadedCallback
{
jmethodID m_onGameTitleLoadedMID;
JNIUtils::Scopedjobject m_gameTitleLoadedCallbackObj;

Expand All @@ -11,12 +13,23 @@ class AndroidGameTitleLoadedCallback : public GameTitleLoadedCallback {
: m_onGameTitleLoadedMID(onGameTitleLoadedMID),
m_gameTitleLoadedCallbackObj(gameTitleLoadedCallbackObj) {}

void onTitleLoaded(const Game& game) override
void onTitleLoaded(const Game& game, const std::shared_ptr<Image>& icon) override
{
JNIUtils::ScopedJNIENV env;
jstring name = env->NewStringUTF(game.name.c_str());
jlong titleId = static_cast<jlong>(game.titleId);
env->CallVoidMethod(*m_gameTitleLoadedCallbackObj, m_onGameTitleLoadedMID, titleId, name);
jlong titleId = static_cast<const jlong>(game.titleId);
int width = -1, height = -1;
jintArray jIconData = nullptr;
if (icon)
{
width = icon->m_width;
height = icon->m_height;
jIconData = env->NewIntArray(width * height);
env->SetIntArrayRegion(jIconData, 0, width * height, reinterpret_cast<const jint*>(icon->intColors()));
}
env->CallVoidMethod(*m_gameTitleLoadedCallbackObj, m_onGameTitleLoadedMID, static_cast<jlong>(titleId), name, jIconData, width, height);
if (jIconData != nullptr)
env->DeleteLocalRef(jIconData);
env->DeleteLocalRef(name);
}
};
3 changes: 0 additions & 3 deletions src/android/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ add_library(CemuAndroid SHARED
AndroidEmulatedController.cpp
AndroidEmulatedController.h
AndroidFilesystemCallbacks.h
AndroidGameIconLoadedCallback.h
AndroidGameTitleLoadedCallback.h
CMakeLists.txt
CafeSystemUtils.cpp
CafeSystemUtils.h
EmulationState.h
GameIconLoader.cpp
GameIconLoader.h
GameTitleLoader.cpp
GameTitleLoader.h
Image.cpp
Expand Down
34 changes: 18 additions & 16 deletions src/android/app/src/main/cpp/EmulationState.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "CafeSystemUtils.h"
#include "Cafe/CafeSystem.h"
#include "Cemu/GuiSystem/GuiSystem.h"
#include "GameIconLoader.h"
#include "GameTitleLoader.h"
#include "Utils.h"
#include "input/ControllerFactory.h"
Expand All @@ -23,7 +22,6 @@ void CemuCommonInit();

class EmulationState
{
GameIconLoader m_gameIconLoader;
GameTitleLoader m_gameTitleLoader;
std::unordered_map<int64_t, GraphicPackPtr> m_graphicPacks;
void fillGraphicPacks()
Expand Down Expand Up @@ -241,24 +239,28 @@ class EmulationState
m_gameTitleLoader.setOnTitleLoaded(onGameTitleLoaded);
}

const Image& getGameIcon(TitleId titleId)
void addGamesPath(const std::string& gamePath)
{
return m_gameIconLoader.getGameIcon(titleId);
}

void setOnGameIconLoaded(const std::shared_ptr<class GameIconLoadedCallback>& onGameIconLoaded)
{
m_gameIconLoader.setOnIconLoaded(onGameIconLoaded);
}

void addGamePath(const fs::path& gamePath)
{
m_gameTitleLoader.addGamePath(gamePath);
auto& gamePaths = g_config.data().game_paths;
if (std::any_of(gamePaths.begin(), gamePaths.end(), [&](auto path) { return path == gamePath; }))
return;
gamePaths.push_back(gamePath);
g_config.Save();
CafeTitleList::ClearScanPaths();
for (auto& it : gamePaths)
CafeTitleList::AddScanPath(it);
CafeTitleList::Refresh();
}

void requestGameIcon(TitleId titleId)
void removeGamesPath(const std::string& gamePath)
{
m_gameIconLoader.requestIcon(titleId);
auto& gamePaths = g_config.data().game_paths;
std::erase_if(gamePaths, [&](auto path) { return path == gamePath; });
g_config.Save();
CafeTitleList::ClearScanPaths();
for (auto& it : gamePaths)
CafeTitleList::AddScanPath(it);
CafeTitleList::Refresh();
}

void reloadGameTitles()
Expand Down
86 changes: 0 additions & 86 deletions src/android/app/src/main/cpp/GameIconLoader.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions src/android/app/src/main/cpp/GameIconLoader.h

This file was deleted.

70 changes: 45 additions & 25 deletions src/android/app/src/main/cpp/GameTitleLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include "GameTitleLoader.h"

std::optional<TitleInfo> getFirstTitleInfoByTitleId(TitleId titleId)
{
TitleInfo titleInfo;
if (CafeTitleList::GetFirstByTitleId(titleId, titleInfo))
return titleInfo;
return {};
}

GameTitleLoader::GameTitleLoader()
{
m_loaderThread = std::thread(&GameTitleLoader::loadGameTitles, this);
Expand Down Expand Up @@ -27,11 +35,14 @@ void GameTitleLoader::reloadGameTitles()
{
if (m_callbackIdTitleList.has_value())
{
CafeTitleList::Refresh();
CafeTitleList::UnregisterCallback(m_callbackIdTitleList.value());
}
m_gameInfos.clear();
registerCallback();
CafeTitleList::ClearScanPaths();
for (auto&& gamePath : g_config.data().game_paths)
CafeTitleList::AddScanPath(gamePath);
CafeTitleList::Refresh();
m_callbackIdTitleList = CafeTitleList::RegisterCallback([](CafeTitleListCallbackEvent* evt, void* ctx) { static_cast<GameTitleLoader*>(ctx)->HandleTitleListCallback(evt); }, this);
}

GameTitleLoader::~GameTitleLoader()
Expand All @@ -57,11 +68,14 @@ void GameTitleLoader::titleRefresh(TitleId titleId)
isNewEntry = true;
m_gameInfos[baseTitleId] = Game();
}

Game& game = m_gameInfos[baseTitleId];
std::optional<TitleInfo> titleInfo = getFirstTitleInfoByTitleId(titleId);
game.titleId = baseTitleId;
game.name = GetNameByTitleId(baseTitleId);
game.name = getNameByTitleId(baseTitleId, titleInfo);
game.version = gameInfo.GetVersion();
game.region = gameInfo.GetRegion();
std::shared_ptr<Image> icon = loadIcon(baseTitleId, titleInfo);
if (gameInfo.HasAOC())
{
game.dlc = gameInfo.GetAOCVersion();
Expand All @@ -72,7 +86,7 @@ void GameTitleLoader::titleRefresh(TitleId titleId)
if (iosu::pdm::GetStatForGamelist(baseTitleId, playTimeStat))
game.secondsPlayed = playTimeStat.numMinutesPlayed * 60;
if (m_gameTitleLoadedCallback)
m_gameTitleLoadedCallback->onTitleLoaded(game);
m_gameTitleLoadedCallback->onTitleLoaded(game, icon);
}
else
{
Expand All @@ -84,11 +98,10 @@ void GameTitleLoader::loadGameTitles()
{
while (m_continueLoading)
{
TitleId titleId = 0;
TitleId titleId;
{
std::unique_lock lock(m_threadMutex);
m_condVar.wait(lock, [this] { return (!m_titlesToLoad.empty()) ||
!m_continueLoading; });
m_condVar.wait(lock, [this] { return (!m_titlesToLoad.empty()) || !m_continueLoading; });
if (!m_continueLoading)
return;
titleId = m_titlesToLoad.front();
Expand All @@ -97,29 +110,43 @@ void GameTitleLoader::loadGameTitles()
titleRefresh(titleId);
}
}

std::string GameTitleLoader::GetNameByTitleId(uint64 titleId)
std::string GameTitleLoader::getNameByTitleId(TitleId titleId, const std::optional<TitleInfo>& titleInfo)
{
auto it = m_name_cache.find(titleId);
if (it != m_name_cache.end())
return it->second;
TitleInfo titleInfo;
if (!CafeTitleList::GetFirstByTitleId(titleId, titleInfo))
if (!titleInfo.has_value())
return "Unknown title";
std::string name;
if (!GetConfig().GetGameListCustomName(titleId, name))
name = titleInfo.GetMetaTitleName();
name = titleInfo.value().GetMetaTitleName();
m_name_cache.emplace(titleId, name);
return name;
}

void GameTitleLoader::registerCallback()
std::shared_ptr<Image> GameTitleLoader::loadIcon(TitleId titleId, const std::optional<TitleInfo>& titleInfo)
{
m_callbackIdTitleList = CafeTitleList::RegisterCallback(
[](CafeTitleListCallbackEvent* evt, void* ctx) {
static_cast<GameTitleLoader*>(ctx)->HandleTitleListCallback(evt);
},
this);
if (auto iconIt = m_iconCache.find(titleId); iconIt != m_iconCache.end())
return iconIt->second;
std::string tempMountPath = TitleInfo::GetUniqueTempMountingPath();
if (!titleInfo.has_value())
return {};
auto titleInfoValue = titleInfo.value();
if (!titleInfoValue.Mount(tempMountPath, "", FSC_PRIORITY_BASE))
return {};
auto tgaData = fsc_extractFile((tempMountPath + "/meta/iconTex.tga").c_str());
if (!tgaData || tgaData->size() <= 16)
{
cemuLog_log(LogType::Force, "Failed to load icon for title {:016x}", titleId);
titleInfoValue.Unmount(tempMountPath);
return {};
}
auto image = std::make_shared<Image>(tgaData.value());
titleInfoValue.Unmount(tempMountPath);
if (!image->isOk())
return {};
m_iconCache.emplace(titleId, image);
return image;
}

void GameTitleLoader::HandleTitleListCallback(CafeTitleListCallbackEvent* evt)
Expand All @@ -129,10 +156,3 @@ void GameTitleLoader::HandleTitleListCallback(CafeTitleListCallbackEvent* evt)
queueTitle(evt->titleInfo->GetAppTitleId());
}
}

void GameTitleLoader::addGamePath(const fs::path& path)
{
CafeTitleList::ClearScanPaths();
CafeTitleList::AddScanPath(path);
CafeTitleList::Refresh();
}
Loading

0 comments on commit 2c262aa

Please sign in to comment.