Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture saving fixes, icon load fix #18747

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Core/ELF/ParamSFO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ std::vector<std::string> ParamSFOData::GetKeys() const {
std::string ParamSFOData::GetDiscID() {
const std::string discID = GetValueString("DISC_ID");
if (discID.empty()) {
std::string fakeID = GenerateFakeID();
std::string fakeID = GenerateFakeID(Path());
WARN_LOG(LOADER, "No DiscID found - generating a fake one: '%s' (from %s)", fakeID.c_str(), PSP_CoreParameter().fileToStart.c_str());
ValueData data;
data.type = VT_UTF8;
Expand Down Expand Up @@ -320,15 +320,15 @@ void ParamSFOData::ValueData::SetData(const u8* data, int size) {
u_size = size;
}

std::string ParamSFOData::GenerateFakeID(const std::string &filename) const {
std::string ParamSFOData::GenerateFakeID(const Path &filename) const {
// Generates fake gameID for homebrew based on it's folder name.
// Should probably not be a part of ParamSFO, but it'll be called in same places.
std::string file = PSP_CoreParameter().fileToStart.ToString();
// FileToStart here is actually a directory name, not a file, so taking GetFilename on it gets what we want.
Path path = PSP_CoreParameter().fileToStart;
if (!filename.empty())
file = filename;
path = filename;

std::size_t lslash = file.find_last_of("/");
file = file.substr(lslash + 1);
std::string file = path.GetFilename();

int sumOfAllLetters = 0;
for (char &c : file) {
Expand Down
4 changes: 3 additions & 1 deletion Core/ELF/ParamSFO.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "Common/CommonTypes.h"
#include "Common/Log.h"

class Path;

class ParamSFOData
{
public:
Expand All @@ -36,7 +38,7 @@ class ParamSFOData
const u8 *GetValueData(const std::string &key, unsigned int *size) const;

std::vector<std::string> GetKeys() const;
std::string GenerateFakeID(const std::string &filename = "") const;
std::string GenerateFakeID(const Path &filename) const;

std::string GetDiscID();

Expand Down
2 changes: 1 addition & 1 deletion Core/PSPLoaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ bool Load_PSP_ELF_PBP(FileLoader *fileLoader, std::string *error_string) {
homebrewTitle = homebrewName;
std::string discID = g_paramSFO.GetDiscID();
std::string discVersion = g_paramSFO.GetValueString("DISC_VERSION");
std::string madeUpID = g_paramSFO.GenerateFakeID();
std::string madeUpID = g_paramSFO.GenerateFakeID(Path());

std::string title = StringFromFormat("%s : %s", discID.c_str(), homebrewTitle.c_str());
INFO_LOG(LOADER, "%s", title.c_str());
Expand Down
2 changes: 1 addition & 1 deletion Core/SaveState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ namespace SaveState
std::string discId = g_paramSFO.GetValueString("DISC_ID");
std::string discVer = g_paramSFO.GetValueString("DISC_VERSION");
if (discId.empty()) {
discId = g_paramSFO.GenerateFakeID();
discId = g_paramSFO.GenerateFakeID(Path());
discVer = "1.00";
}
return StringFromFormat("%s_%s", discId.c_str(), discVer.c_str());
Expand Down
6 changes: 3 additions & 3 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int *
// Short circuit the non-enabled case.
// Otherwise, due to bReplaceTexturesAllowLate, we'll still spawn tasks looking for replacements
// that then won't be used.
if (!replacer_.Enabled()) {
if (!replacer_.ReplaceEnabled()) {
return nullptr;
}

Expand All @@ -1562,7 +1562,7 @@ ReplacedTexture *TextureCacheCommon::FindReplacement(TexCacheEntry *entry, int *
}

double replaceStart = time_now_d();
u64 cachekey = replacer_.Enabled() ? entry->CacheKey() : 0;
u64 cachekey = entry->CacheKey();
ReplacedTexture *replaced = replacer_.FindReplacement(cachekey, entry->fullhash, *w, *h);
replacementTimeThisFrame_ += time_now_d() - replaceStart;
if (!replaced) {
Expand Down Expand Up @@ -2915,7 +2915,7 @@ bool TextureCacheCommon::PrepareBuildTexture(BuildTexturePlan &plan, TexCacheEnt
// But, we still need to create the texture at a larger size.
plan.replaced->GetSize(0, &plan.createW, &plan.createH);
} else {
if (replacer_.Enabled() && !plan.doReplace && plan.depth == 1 && canReplace) {
if (replacer_.SaveEnabled() && !plan.doReplace && plan.depth == 1 && canReplace) {
ReplacedTextureDecodeInfo replacedInfo;
// TODO: Do we handle the race where a replacement becomes valid AFTER this but before we save?
replacedInfo.cachekey = entry->CacheKey();
Expand Down
40 changes: 20 additions & 20 deletions GPU/Common/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,38 @@ TextureReplacer::~TextureReplacer() {
void TextureReplacer::NotifyConfigChanged() {
gameID_ = g_paramSFO.GetDiscID();

bool wasEnabled = enabled_;
enabled_ = g_Config.bReplaceTextures || g_Config.bSaveNewTextures;
if (enabled_) {
bool wasReplaceEnabled = replaceEnabled_;
replaceEnabled_ = g_Config.bReplaceTextures;
saveEnabled_ = g_Config.bSaveNewTextures;
if (replaceEnabled_ || saveEnabled_) {
basePath_ = GetSysDirectory(DIRECTORY_TEXTURES) / gameID_;

replaceEnabled_ = replaceEnabled_ && File::IsDirectory(basePath_);
newTextureDir_ = basePath_ / NEW_TEXTURE_DIR;

// If we're saving, auto-create the directory.
if (g_Config.bSaveNewTextures && !File::Exists(newTextureDir_)) {
if (saveEnabled_ && !File::Exists(newTextureDir_)) {
INFO_LOG(G3D, "Creating new texture directory: '%s'", newTextureDir_.ToVisualString().c_str());
File::CreateFullPath(newTextureDir_);
// We no longer create a nomedia file here, since we put one
// in the TEXTURES root.
}
}

enabled_ = File::IsDirectory(basePath_);
if (saveEnabled_) {
// Somewhat crude message, re-using translation strings.
auto d = GetI18NCategory(I18NCat::DEVELOPER);
auto di = GetI18NCategory(I18NCat::DIALOG);
g_OSD.Show(OSDType::MESSAGE_INFO, std::string(d->T("Save new textures")) + ": " + di->T("Enabled"), 2.0f);
}

if (g_Config.bSaveNewTextures) {
// Somewhat crude message, re-using translation strings.
auto d = GetI18NCategory(I18NCat::DEVELOPER);
auto di = GetI18NCategory(I18NCat::DIALOG);
g_OSD.Show(OSDType::MESSAGE_INFO, std::string(d->T("Save new textures")) + ": " + di->T("Enabled"), 2.0f);
}
} else if (wasEnabled) {
if (!replaceEnabled_ && wasReplaceEnabled) {
delete vfs_;
vfs_ = nullptr;
Decimate(ReplacerDecimateMode::ALL);
}

if (enabled_) {
enabled_ = LoadIni();
if (replaceEnabled_) {
replaceEnabled_ = LoadIni();
}
}

Expand Down Expand Up @@ -315,7 +316,7 @@ bool TextureReplacer::LoadIniValues(IniFile &ini, VFSBackend *dir, bool isOverri
if (ini.HasSection("hashes")) {
auto hashes = ini.GetOrCreateSection("hashes")->ToMap();
// Format: hashname = filename.png
bool checkFilenames = g_Config.bSaveNewTextures && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;
bool checkFilenames = saveEnabled_ && !g_Config.bIgnoreTextureFilenames && !vfsIsZip_;

for (const auto &item : hashes) {
ReplacementCacheKey key(0, 0);
Expand Down Expand Up @@ -479,7 +480,7 @@ void TextureReplacer::ParseReduceHashRange(const std::string& key, const std::st
}

u32 TextureReplacer::ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV) {
_dbg_assert_msg_(enabled_, "Replacement not enabled");
_dbg_assert_msg_(replaceEnabled_ || saveEnabled_, "Replacement not enabled");

// TODO: Take swizzled into account, like in QuickTexHash().
// Note: Currently, only the MLB games are known to need this.
Expand Down Expand Up @@ -723,8 +724,7 @@ class SaveTextureTask : public Task {
};

bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) {
_assert_msg_(enabled_, "Replacement not enabled");
if (!g_Config.bSaveNewTextures)
if (!saveEnabled_)
return false;
// Don't save the PPGe texture.
if (replacedInfo.addr > 0x05000000 && replacedInfo.addr < PSP_GetKernelMemoryEnd())
Expand All @@ -736,7 +736,7 @@ bool TextureReplacer::WillSave(const ReplacedTextureDecodeInfo &replacedInfo) {
}

void TextureReplacer::NotifyTextureDecoded(ReplacedTexture *texture, const ReplacedTextureDecodeInfo &replacedInfo, const void *data, int pitch, int level, int origW, int origH, int scaledW, int scaledH) {
_assert_msg_(enabled_, "Replacement not enabled");
_assert_msg_(saveEnabled_, "Texture saving not enabled");
_assert_(pitch >= 0);

if (!WillSave(replacedInfo)) {
Expand Down
8 changes: 6 additions & 2 deletions GPU/Common/TextureReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class TextureReplacer {

void NotifyConfigChanged();

bool Enabled() const { return enabled_; }
bool Enabled() const { return replaceEnabled_ || saveEnabled_; } // used to check hashing method etc.
bool ReplaceEnabled() const { return replaceEnabled_; }
bool SaveEnabled() const { return saveEnabled_; }

bool AllowVideo() const { return allowVideo_; }

u32 ComputeHash(u32 addr, int bufw, int w, int h, bool swizzled, GETextureFormat fmt, u16 maxSeenV);
Expand Down Expand Up @@ -140,7 +143,8 @@ class TextureReplacer {
void ScanForHashNamedFiles(VFSBackend *dir, std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);
void ComputeAliasMap(const std::map<ReplacementCacheKey, std::map<int, std::string>> &filenameMap);

bool enabled_ = false;
bool replaceEnabled_ = false;
bool saveEnabled_ = false;
bool allowVideo_ = false;
bool ignoreAddress_ = false;
bool reduceHash_ = false;
Expand Down
3 changes: 3 additions & 0 deletions GPU/Vulkan/TextureCacheVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) {
}

if (plan.saveTexture) {
INFO_LOG(G3D, "About to save texture");
actualFmt = VULKAN_8888_FORMAT;
}

Expand Down Expand Up @@ -647,6 +648,8 @@ void TextureCacheVulkan::BuildTexture(TexCacheEntry *const entry) {
}
// Format might be wrong in lowMemoryMode_, so don't save.
if (plan.saveTexture && !lowMemoryMode_) {
INFO_LOG(G3D, "Calling NotifyTextureDecoded %08x", entry->addr);

// When hardware texture scaling is enabled, this saves the original.
int w = dataScaled ? mipWidth : mipUnscaledWidth;
int h = dataScaled ? mipHeight : mipUnscaledHeight;
Expand Down
2 changes: 1 addition & 1 deletion UI/CwCheatScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool CwCheatScreen::TryLoadCheatInfo() {
}
if ((info->id.empty() || !info->disc_total)
&& gamePath_.FilePathContainsNoCase("PSP/GAME/")) {
gameID = g_paramSFO.GenerateFakeID(gamePath_.ToString());
gameID = g_paramSFO.GenerateFakeID(gamePath_);
}

if (!engine_ || gameID != gameID_) {
Expand Down
6 changes: 3 additions & 3 deletions UI/GameInfoCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ static bool ReadFileToString(IFileSystem *fs, const char *filename, std::string

static bool ReadLocalFileToString(const Path &path, std::string *contents, std::mutex *mtx) {
std::string data;
if (!File::ReadFileToString(false, path, *contents)) {
if (!File::ReadFileToString(false, path, data)) {
return false;
}
if (mtx) {
Expand Down Expand Up @@ -472,7 +472,7 @@ class GameInfoWorkItem : public Task {
if ((info_->id.empty() || !info_->disc_total)
&& gamePath_.FilePathContainsNoCase("PSP/GAME/")
&& info_->fileType == IdentifiedFileType::PSP_PBP_DIRECTORY) {
info_->id = g_paramSFO.GenerateFakeID(gamePath_.ToString());
info_->id = g_paramSFO.GenerateFakeID(gamePath_);
info_->id_version = info_->id + "_1.00";
info_->region = GAMEREGION_MAX + 1; // Homebrew
}
Expand Down Expand Up @@ -529,7 +529,7 @@ class GameInfoWorkItem : public Task {
// An elf on its own has no usable information, no icons, no nothing.
{
std::lock_guard<std::mutex> lock(info_->lock);
info_->id = g_paramSFO.GenerateFakeID(gamePath_.ToString());
info_->id = g_paramSFO.GenerateFakeID(gamePath_);
info_->id_version = info_->id + "_1.00";
info_->region = GAMEREGION_MAX + 1; // Homebrew

Expand Down
2 changes: 1 addition & 1 deletion UI/GameScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ UI::EventReturn GameScreen::OnGameSettings(UI::EventParams &e) {
if (info && info->paramSFOLoaded) {
std::string discID = info->paramSFO.GetValueString("DISC_ID");
if ((discID.empty() || !info->disc_total) && gamePath_.FilePathContainsNoCase("PSP/GAME/"))
discID = g_paramSFO.GenerateFakeID(gamePath_.ToString());
discID = g_paramSFO.GenerateFakeID(gamePath_);
screenManager()->push(new GameSettingsScreen(gamePath_, discID, true));
}
return UI::EVENT_DONE;
Expand Down