diff --git a/pcsx2/CMakeLists.txt b/pcsx2/CMakeLists.txt index 2a3e256fab..634e8f45c0 100644 --- a/pcsx2/CMakeLists.txt +++ b/pcsx2/CMakeLists.txt @@ -194,6 +194,7 @@ set(pcsx2Headers Gif_Unit.h GS.h Hardware.h + Host.h Hw.h IopBios.h IopCommon.h @@ -937,6 +938,7 @@ set(pcsx2GuiSources gui/AppCoreThread.cpp gui/AppEventSources.cpp gui/AppGameDatabase.cpp + gui/AppHost.cpp gui/AppUserMode.cpp gui/AppInit.cpp gui/AppMain.cpp diff --git a/pcsx2/Config.h b/pcsx2/Config.h index b85ee9fd39..8b4366a41c 100644 --- a/pcsx2/Config.h +++ b/pcsx2/Config.h @@ -627,6 +627,7 @@ namespace EmuFolders extern wxDirName Logs; extern wxDirName Cheats; extern wxDirName CheatsWS; + extern wxDirName Resources; } // namespace EmuFolders ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/pcsx2/Host.h b/pcsx2/Host.h index 48d876b606..b980ac2980 100644 --- a/pcsx2/Host.h +++ b/pcsx2/Host.h @@ -16,6 +16,9 @@ #pragma once #include "common/Pcsx2Defs.h" +#include +#include +#include struct HostKeyEvent { @@ -29,3 +32,13 @@ struct HostKeyEvent Type type; u32 key; }; + +namespace Host +{ + /// Reads a file from the resources directory of the application. + /// This may be outside of the "normally" filesystem on platforms such as Mac. + std::optional> ReadResourceFile(const char* filename); + + /// Reads a resource file file from the resources directory as a string. + std::optional ReadResourceFileToString(const char* filename); +} // namespace Host diff --git a/pcsx2/Pcsx2Config.cpp b/pcsx2/Pcsx2Config.cpp index 01452841bb..1916289795 100644 --- a/pcsx2/Pcsx2Config.cpp +++ b/pcsx2/Pcsx2Config.cpp @@ -40,6 +40,7 @@ namespace EmuFolders wxDirName Logs; wxDirName Cheats; wxDirName CheatsWS; + wxDirName Resources; } // namespace EmuFolders void TraceLogFilters::LoadSave(SettingsWrapper& wrap) diff --git a/pcsx2/gui/AppConfig.cpp b/pcsx2/gui/AppConfig.cpp index c8edbcf02c..ffccfc0b02 100644 --- a/pcsx2/gui/AppConfig.cpp +++ b/pcsx2/gui/AppConfig.cpp @@ -102,7 +102,13 @@ namespace PathDefs static const wxDirName retval(L"docs"); return retval; } - }; // namespace Base + + const wxDirName& Resources() + { + static const wxDirName retval(L"resources"); + return retval; + } + }; // Specifies the root folder for the application install. // (currently it's the CWD, but in the future I intend to move all binaries to a "bin" @@ -239,6 +245,15 @@ namespace PathDefs #endif } + wxDirName GetResources() + { +#ifdef __APPLE__ + return wxDirName(wxStandardPaths::Get().GetResourcesDir()); +#else + return AppRoot() + Base::Resources(); +#endif + } + wxDirName Get(FoldersEnum_t folderidx) { switch (folderidx) @@ -687,6 +702,7 @@ AppConfig::FolderOptions::FolderOptions() , Logs(PathDefs::GetLogs()) , Cheats(PathDefs::GetCheats()) , CheatsWS(PathDefs::GetCheatsWS()) + , Resources(PathDefs::GetResources()) , RunIso(PathDefs::GetDocuments()) // raw default is always the Documents folder. , RunELF(PathDefs::GetDocuments()) // raw default is always the Documents folder. @@ -752,6 +768,7 @@ void AppSetEmuFolders() EmuFolders::Langs = GetResolvedFolder(FolderId_Langs); EmuFolders::Cheats = GetResolvedFolder(FolderId_Cheats); EmuFolders::CheatsWS = GetResolvedFolder(FolderId_CheatsWS); + EmuFolders::Resources = g_Conf->Folders.Resources; } // ------------------------------------------------------------------------ diff --git a/pcsx2/gui/AppConfig.h b/pcsx2/gui/AppConfig.h index ce3149f2d2..877b5d9170 100644 --- a/pcsx2/gui/AppConfig.h +++ b/pcsx2/gui/AppConfig.h @@ -138,7 +138,8 @@ public: Langs, Logs, Cheats, - CheatsWS; + CheatsWS, + Resources; wxDirName RunIso; // last used location for Iso loading. wxDirName RunELF; // last used location for ELF loading. diff --git a/pcsx2/gui/AppHost.cpp b/pcsx2/gui/AppHost.cpp new file mode 100644 index 0000000000..fb6e65eca7 --- /dev/null +++ b/pcsx2/gui/AppHost.cpp @@ -0,0 +1,68 @@ +/* PCSX2 - PS2 Emulator for PCs + * Copyright (C) 2002-2021 PCSX2 Dev Team + * + * PCSX2 is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with PCSX2. + * If not, see . + */ + +#include "PrecompiledHeader.h" + +#include "common/Console.h" +#include "common/FileSystem.h" +#include "common/Path.h" + +#include "AppConfig.h" +#include "Host.h" + +static auto OpenResourceCFile(const char* filename, const char* mode) +{ + const std::string full_filename(Path::CombineStdString(EmuFolders::Resources, filename)); + auto fp = FileSystem::OpenManagedCFile(full_filename.c_str(), mode); + if (!fp) + Console.Error("Failed to open resource file '%s'", filename); + + return fp; +} + +std::optional> Host::ReadResourceFile(const char* filename) +{ + auto fp = OpenResourceCFile(filename, "rb"); + if (!fp) + return std::nullopt; + + const size_t size = FileSystem::FSize64(fp.get()); + std::vector ret(size); + if (std::fread(ret.data(), size, 1, fp.get()) != 1) + { + Console.Error("Failed to read resource file '%s'", filename); + return std::nullopt; + } + + return ret; +} + +std::optional Host::ReadResourceFileToString(const char* filename) +{ + auto fp = OpenResourceCFile(filename, "rb"); + if (!fp) + return std::nullopt; + + const size_t size = FileSystem::FSize64(fp.get()); + std::string ret; + ret.resize(size); + if (std::fread(ret.data(), size, 1, fp.get()) != 1) + { + Console.Error("Failed to read resource file '%s' to string", filename); + return std::nullopt; + } + + return ret; +} diff --git a/pcsx2/pcsx2.vcxproj b/pcsx2/pcsx2.vcxproj index 8f751e79df..edd96db5b9 100644 --- a/pcsx2/pcsx2.vcxproj +++ b/pcsx2/pcsx2.vcxproj @@ -308,6 +308,7 @@ + diff --git a/pcsx2/pcsx2.vcxproj.filters b/pcsx2/pcsx2.vcxproj.filters index 19a9d38b35..eea503422a 100644 --- a/pcsx2/pcsx2.vcxproj.filters +++ b/pcsx2/pcsx2.vcxproj.filters @@ -1646,6 +1646,9 @@ AppHost + + AppHost +