Skip to content

Commit

Permalink
Host: Make resource routines fill error object
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 22, 2024
1 parent 485f81a commit 4963dc1
Showing 9 changed files with 43 additions and 52 deletions.
10 changes: 6 additions & 4 deletions src/core/game_database.cpp
Original file line number Diff line number Diff line change
@@ -1066,10 +1066,11 @@ void GameDatabase::SetRymlCallbacks()

bool GameDatabase::LoadGameDBYaml()
{
std::optional<DynamicHeapArray<u8>> gamedb_data = Host::ReadResourceFile(GAMEDB_YAML_FILENAME, false);
Error error;
std::optional<DynamicHeapArray<u8>> gamedb_data = Host::ReadResourceFile(GAMEDB_YAML_FILENAME, false, &error);
if (!gamedb_data.has_value())
{
ERROR_LOG("Failed to read game database");
ERROR_LOG("Failed to read game database: {}", error.GetDescription());
return false;
}

@@ -1384,10 +1385,11 @@ bool GameDatabase::LoadTrackHashes()
{
Common::Timer load_timer;

std::optional<std::string> gamedb_data(Host::ReadResourceFileToString(DISCDB_YAML_FILENAME, false));
Error error;
std::optional<std::string> gamedb_data(Host::ReadResourceFileToString(DISCDB_YAML_FILENAME, false, &error));
if (!gamedb_data.has_value())
{
ERROR_LOG("Failed to read game database");
ERROR_LOG("Failed to read disc database: {}", error.GetDescription());
return false;
}

12 changes: 4 additions & 8 deletions src/core/host.cpp
Original file line number Diff line number Diff line change
@@ -44,16 +44,12 @@ SettingsInterface* Host::GetSettingsInterface()
return &s_layered_settings_interface;
}

std::optional<DynamicHeapArray<u8>> Host::ReadCompressedResourceFile(std::string_view filename, bool allow_override)
std::optional<DynamicHeapArray<u8>> Host::ReadCompressedResourceFile(std::string_view filename, bool allow_override,
Error* error)
{
std::optional<DynamicHeapArray<u8>> ret = Host::ReadResourceFile(filename, allow_override);
std::optional<DynamicHeapArray<u8>> ret = Host::ReadResourceFile(filename, allow_override, error);
if (ret.has_value())
{
Error error;
ret = CompressHelpers::DecompressFile(filename, std::move(ret), std::nullopt, &error);
if (!ret.has_value())
ERROR_LOG("Failed to decompress '{}': {}", Path::GetFileName(filename), error.GetDescription());
}
ret = CompressHelpers::DecompressFile(filename, std::move(ret), std::nullopt, error);

return ret;
}
14 changes: 4 additions & 10 deletions src/duckstation-qt/qthost.cpp
Original file line number Diff line number Diff line change
@@ -2020,22 +2020,16 @@ bool Host::ResourceFileExists(std::string_view filename, bool allow_override)
return FileSystem::FileExists(path.c_str());
}

std::optional<DynamicHeapArray<u8>> Host::ReadResourceFile(std::string_view filename, bool allow_override)
std::optional<DynamicHeapArray<u8>> Host::ReadResourceFile(std::string_view filename, bool allow_override, Error* error)
{
const std::string path = QtHost::GetResourcePath(filename, allow_override);
std::optional<DynamicHeapArray<u8>> ret(FileSystem::ReadBinaryFile(path.c_str()));
if (!ret.has_value())
ERROR_LOG("Failed to read resource file '{}'", filename);
return ret;
return FileSystem::ReadBinaryFile(path.c_str(), error);
}

std::optional<std::string> Host::ReadResourceFileToString(std::string_view filename, bool allow_override)
std::optional<std::string> Host::ReadResourceFileToString(std::string_view filename, bool allow_override, Error* error)
{
const std::string path = QtHost::GetResourcePath(filename, allow_override);
std::optional<std::string> ret(FileSystem::ReadFileToString(path.c_str()));
if (!ret.has_value())
ERROR_LOG("Failed to read resource file to string '{}'", filename);
return ret;
return FileSystem::ReadFileToString(path.c_str(), error);
}

std::optional<std::time_t> Host::GetResourceFileTimestamp(std::string_view filename, bool allow_override)
14 changes: 4 additions & 10 deletions src/duckstation-regtest/regtest_host.cpp
Original file line number Diff line number Diff line change
@@ -223,22 +223,16 @@ bool Host::ResourceFileExists(std::string_view filename, bool allow_override)
return FileSystem::FileExists(path.c_str());
}

std::optional<DynamicHeapArray<u8>> Host::ReadResourceFile(std::string_view filename, bool allow_override)
std::optional<DynamicHeapArray<u8>> Host::ReadResourceFile(std::string_view filename, bool allow_override, Error* error)
{
const std::string path(Path::Combine(EmuFolders::Resources, filename));
std::optional<DynamicHeapArray<u8>> ret(FileSystem::ReadBinaryFile(path.c_str()));
if (!ret.has_value())
ERROR_LOG("Failed to read resource file '{}'", filename);
return ret;
return FileSystem::ReadBinaryFile(path.c_str(), error);
}

std::optional<std::string> Host::ReadResourceFileToString(std::string_view filename, bool allow_override)
std::optional<std::string> Host::ReadResourceFileToString(std::string_view filename, bool allow_override, Error* error)
{
const std::string path(Path::Combine(EmuFolders::Resources, filename));
std::optional<std::string> ret(FileSystem::ReadFileToString(path.c_str()));
if (!ret.has_value())
ERROR_LOG("Failed to read resource file to string '{}'", filename);
return ret;
return FileSystem::ReadFileToString(path.c_str(), error);
}

std::optional<std::time_t> Host::GetResourceFileTimestamp(std::string_view filename, bool allow_override)
11 changes: 8 additions & 3 deletions src/util/host.h
Original file line number Diff line number Diff line change
@@ -12,22 +12,27 @@
#include <string>
#include <string_view>

class Error;

namespace Host {
/// Returns true if the specified resource file exists.
bool ResourceFileExists(std::string_view filename, bool allow_override);

/// Reads a file from the resources directory of the application.
/// This may be outside of the "normal" filesystem on platforms such as Mac.
std::optional<DynamicHeapArray<u8>> ReadResourceFile(std::string_view filename, bool allow_override);
std::optional<DynamicHeapArray<u8>> ReadResourceFile(std::string_view filename, bool allow_override,
Error* error = nullptr);

/// Reads a resource file file from the resources directory as a string.
std::optional<std::string> ReadResourceFileToString(std::string_view filename, bool allow_override);
std::optional<std::string> ReadResourceFileToString(std::string_view filename, bool allow_override,
Error* error = nullptr);

/// Returns the modified time of a resource.
std::optional<std::time_t> GetResourceFileTimestamp(std::string_view filename, bool allow_override);

/// Reads a potentially-compressed file from the resources directory of the application.
std::optional<DynamicHeapArray<u8>> ReadCompressedResourceFile(std::string_view filename, bool allow_override);
std::optional<DynamicHeapArray<u8>> ReadCompressedResourceFile(std::string_view filename, bool allow_override,
Error* error = nullptr);

/// Reports a fatal error on the main thread. This does not assume that the main window exists,
/// unlike ReportErrorAsync(), and will exit the application after the popup is closed.
6 changes: 3 additions & 3 deletions src/util/imgui_fullscreen.cpp
Original file line number Diff line number Diff line change
@@ -309,7 +309,7 @@ std::optional<RGBA8Image> ImGuiFullscreen::LoadTextureImage(std::string_view pat
if (Path::IsAbsolute(path))
svg_data = FileSystem::ReadBinaryFile(std::string(path).c_str(), &error);
else
svg_data = Host::ReadResourceFile(path, true);
svg_data = Host::ReadResourceFile(path, true, &error);

if (svg_data.has_value())
{
@@ -345,7 +345,7 @@ std::optional<RGBA8Image> ImGuiFullscreen::LoadTextureImage(std::string_view pat
}
else
{
std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(path, true);
std::optional<DynamicHeapArray<u8>> data = Host::ReadResourceFile(path, true, &error);
if (data.has_value())
{
image = RGBA8Image();
@@ -357,7 +357,7 @@ std::optional<RGBA8Image> ImGuiFullscreen::LoadTextureImage(std::string_view pat
}
else
{
ERROR_LOG("Failed to open texture resource '{}'", path);
ERROR_LOG("Failed to open texture resource '{}: {}'", path, error.GetDescription());
}
}

22 changes: 11 additions & 11 deletions src/util/imgui_manager.cpp
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ static_assert(std::is_same_v<WCharType, ImWchar>);
static void UpdateScale();
static void SetStyle(ImGuiStyle& style, float scale);
static void SetKeyMap();
static bool LoadFontData();
static bool LoadFontData(Error* error);
static void ReloadFontDataIfActive();
static bool AddImGuiFonts(bool fullscreen_fonts);
static ImFont* AddTextFont(float size, bool full_glyph_range);
@@ -229,9 +229,9 @@ void ImGuiManager::SetShowOSDMessages(bool enable)

bool ImGuiManager::Initialize(float global_scale, float screen_margin, Error* error)
{
if (!LoadFontData())
if (!LoadFontData(error))
{
Error::SetString(error, "Failed to load font data");
Error::AddPrefix(error, "Failed to load font data: ");
return false;
}

@@ -558,13 +558,13 @@ void ImGuiManager::SetKeyMap()
}
}

bool ImGuiManager::LoadFontData()
bool ImGuiManager::LoadFontData(Error* error)
{
if (s_standard_font_data.empty())
{
std::optional<DynamicHeapArray<u8>> font_data = s_font_path.empty() ?
Host::ReadResourceFile("fonts/Roboto-Regular.ttf", true) :
FileSystem::ReadBinaryFile(s_font_path.c_str());
Host::ReadResourceFile("fonts/Roboto-Regular.ttf", true, error) :
FileSystem::ReadBinaryFile(s_font_path.c_str(), error);
if (!font_data.has_value())
return false;

@@ -573,7 +573,7 @@ bool ImGuiManager::LoadFontData()

if (s_fixed_font_data.empty())
{
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/RobotoMono-Medium.ttf", true);
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/RobotoMono-Medium.ttf", true, error);
if (!font_data.has_value())
return false;

@@ -582,7 +582,7 @@ bool ImGuiManager::LoadFontData()

if (s_icon_fa_font_data.empty())
{
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/fa-solid-900.ttf", true);
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/fa-solid-900.ttf", true, error);
if (!font_data.has_value())
return false;

@@ -591,7 +591,7 @@ bool ImGuiManager::LoadFontData()

if (s_icon_pf_font_data.empty())
{
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/promptfont.otf", true);
std::optional<DynamicHeapArray<u8>> font_data = Host::ReadResourceFile("fonts/promptfont.otf", true, error);
if (!font_data.has_value())
return false;

@@ -601,7 +601,7 @@ bool ImGuiManager::LoadFontData()
if (s_emoji_font_data.empty())
{
std::optional<DynamicHeapArray<u8>> font_data =
Host::ReadCompressedResourceFile("fonts/TwitterColorEmoji-SVGinOT.ttf.zst", true);
Host::ReadCompressedResourceFile("fonts/TwitterColorEmoji-SVGinOT.ttf.zst", true, error);
if (!font_data.has_value())
return false;

@@ -732,7 +732,7 @@ void ImGuiManager::ReloadFontDataIfActive()

ImGui::EndFrame();

if (!LoadFontData())
if (!LoadFontData(nullptr))
Panic("Failed to load font data");

if (!AddImGuiFonts(HasFullscreenFonts()))
4 changes: 2 additions & 2 deletions src/util/postprocessing.cpp
Original file line number Diff line number Diff line change
@@ -725,7 +725,7 @@ std::unique_ptr<PostProcessing::Shader> PostProcessing::TryLoadingShader(const s

filename =
fmt::format("shaders/reshade" FS_OSPATH_SEPARATOR_STR "Shaders" FS_OSPATH_SEPARATOR_STR "{}.fx", shader_name);
resource_str = Host::ReadResourceFileToString(filename.c_str(), true);
resource_str = Host::ReadResourceFileToString(filename.c_str(), true, error);
if (resource_str.has_value())
{
std::unique_ptr<ReShadeFXShader> shader = std::make_unique<ReShadeFXShader>();
@@ -737,7 +737,7 @@ std::unique_ptr<PostProcessing::Shader> PostProcessing::TryLoadingShader(const s
}

filename = fmt::format("shaders" FS_OSPATH_SEPARATOR_STR "{}.glsl", shader_name);
resource_str = Host::ReadResourceFileToString(filename.c_str(), true);
resource_str = Host::ReadResourceFileToString(filename.c_str(), true, error);
if (resource_str.has_value())
{
std::unique_ptr<GLSLShader> shader = std::make_unique<GLSLShader>();
2 changes: 1 addition & 1 deletion src/util/postprocessing_shader_fx.cpp
Original file line number Diff line number Diff line change
@@ -1128,7 +1128,7 @@ bool PostProcessing::ReShadeFXShader::CreatePasses(GPUTexture::Format backbuffer
{
// Might be a base file/resource instead.
const std::string resource_name = Path::Combine("shaders/reshade/Textures", source);
if (std::optional<DynamicHeapArray<u8>> resdata = Host::ReadResourceFile(resource_name.c_str(), true);
if (std::optional<DynamicHeapArray<u8>> resdata = Host::ReadResourceFile(resource_name.c_str(), true, error);
!resdata.has_value() || !image.LoadFromBuffer(resource_name.c_str(), resdata->cspan(), error))
{
Error::AddPrefixFmt(error, "Failed to load image '{}' (from '{}'): ", source, image_path);

0 comments on commit 4963dc1

Please # to comment.