Host: Add the ability to read files from the resources directory

This commit is contained in:
Connor McLaughlin 2021-09-25 13:20:12 +10:00 committed by refractionpcsx2
parent a083343c6e
commit 0029dac32d
9 changed files with 109 additions and 2 deletions

View File

@ -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

View File

@ -627,6 +627,7 @@ namespace EmuFolders
extern wxDirName Logs;
extern wxDirName Cheats;
extern wxDirName CheatsWS;
extern wxDirName Resources;
} // namespace EmuFolders
/////////////////////////////////////////////////////////////////////////////////////////

View File

@ -16,6 +16,9 @@
#pragma once
#include "common/Pcsx2Defs.h"
#include <optional>
#include <string>
#include <vector>
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<std::vector<u8>> ReadResourceFile(const char* filename);
/// Reads a resource file file from the resources directory as a string.
std::optional<std::string> ReadResourceFileToString(const char* filename);
} // namespace Host

View File

@ -40,6 +40,7 @@ namespace EmuFolders
wxDirName Logs;
wxDirName Cheats;
wxDirName CheatsWS;
wxDirName Resources;
} // namespace EmuFolders
void TraceLogFilters::LoadSave(SettingsWrapper& wrap)

View File

@ -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;
}
// ------------------------------------------------------------------------

View File

@ -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.

68
pcsx2/gui/AppHost.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<std::vector<u8>> 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<u8> 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<std::string> 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;
}

View File

@ -308,6 +308,7 @@
<ClCompile Include="GS\Renderers\DX11\D3D.cpp" />
<ClCompile Include="GS\Window\GSwxDialog.cpp" />
<ClCompile Include="gui\AppGameDatabase.cpp" />
<ClCompile Include="gui\AppHost.cpp" />
<ClCompile Include="gui\AppUserMode.cpp" />
<ClCompile Include="gui\CheckedStaticBox.cpp" />
<ClCompile Include="gui\Debugger\BreakpointWindow.cpp" />

View File

@ -1646,6 +1646,9 @@
<ClCompile Include="gui\pxWindowTextWriter.cpp">
<Filter>AppHost</Filter>
</ClCompile>
<ClCompile Include="gui\AppHost.cpp">
<Filter>AppHost</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Patch.h">