Skip to content

Commit

Permalink
Async load: Handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Nov 9, 2024
1 parent 3c2a31c commit 04f0fb0
Show file tree
Hide file tree
Showing 47 changed files with 730 additions and 438 deletions.
3 changes: 3 additions & 0 deletions Source/DiabloUI/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ bool Init(std::string_view caption, std::string_view text, bool error, bool rend
LogError("{}", SDL_GetError());
}
}
if (!IsHardwareCursor() && !ArtCursor) {
ArtCursor = LoadPcx("ui_art\\cursor", /*transparentColor=*/0);
}
LoadDialogButtonGraphics();

OptionalClxSprite dialogSprite = LoadDialogSprite(!caption.empty(), error);
Expand Down
42 changes: 21 additions & 21 deletions Source/appfat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ void FreeDlg()
SNetDestroy();
}

[[noreturn]] void DisplayFatalErrorAndExit(std::string_view title, std::string_view body)
{
FreeDlg();
UiErrorOkDialog(title, body);
diablo_quit(1);
}

} // namespace

void app_fatal(std::string_view str)
{
FreeDlg();
UiErrorOkDialog(_("Error"), str);
diablo_quit(1);
DisplayFatalErrorAndExit(_("Error"), str);
}

#ifdef _DEBUG
Expand All @@ -62,32 +67,27 @@ void assert_fail(int nLineNo, const char *pszFile, const char *pszFail)

void ErrDlg(const char *title, std::string_view error, std::string_view logFilePath, int logLineNr)
{
FreeDlg();

std::string text = fmt::format(fmt::runtime(_(/* TRANSLATORS: Error message that displays relevant information for bug report */ "{:s}\n\nThe error occurred at: {:s} line {:d}")), error, logFilePath, logLineNr);

UiErrorOkDialog(title, text);
diablo_quit(1);
DisplayFatalErrorAndExit(
title,
fmt::format(fmt::runtime(_(/* TRANSLATORS: Error message that displays relevant information for bug report */ "{:s}\n\nThe error occurred at: {:s} line {:d}")),
error, logFilePath, logLineNr));
}

void InsertCDDlg(std::string_view archiveName)
{
std::string text = fmt::format(
fmt::runtime(_("Unable to open main data archive ({:s}).\n"
"\n"
"Make sure that it is in the game folder.")),
archiveName);

UiErrorOkDialog(_("Data File Error"), text);
diablo_quit(1);
DisplayFatalErrorAndExit(_("Data File Error"),
fmt::format(fmt::runtime(_("Unable to open main data archive ({:s}).\n"
"\n"
"Make sure that it is in the game folder.")),
archiveName));
}

void DirErrorDlg(std::string_view error)
{
std::string text = fmt::format(fmt::runtime(_(/* TRANSLATORS: Error when Program is not allowed to write data */ "Unable to write to location:\n{:s}")), error);

UiErrorOkDialog(_("Read-Only Directory Error"), text);
diablo_quit(1);
DisplayFatalErrorAndExit(
_("Read-Only Directory Error"),
fmt::format(fmt::runtime(_(/* TRANSLATORS: Error when Program is not allowed to write data */ "Unable to write to location:\n{:s}")),
error));
}

} // namespace devilution
25 changes: 14 additions & 11 deletions Source/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "utils/screen_reader.hpp"
#include "utils/sdl_geometry.h"
#include "utils/sdl_ptrs.h"
#include "utils/status_macros.hpp"
#include "utils/str_case.hpp"
#include "utils/str_cat.hpp"
#include "utils/string_or_view.hpp"
Expand Down Expand Up @@ -876,22 +877,22 @@ void UpdateLifeManaPercent()
MyPlayer->UpdateHitPointPercentage();
}

void InitMainPanel()
tl::expected<void, std::string> InitMainPanel()
{
if (!HeadlessMode) {
BottomBuffer.emplace(GetMainPanel().size.width, (GetMainPanel().size.height + PanelPaddingHeight) * (IsChatAvailable() ? 2 : 1));
pManaBuff.emplace(88, 88);
pLifeBuff.emplace(88, 88);

LoadCharPanel();
LoadLargeSpellIcons();
RETURN_IF_ERROR(LoadCharPanel());
RETURN_IF_ERROR(LoadLargeSpellIcons());
{
const OwnedClxSpriteList sprite = LoadCel("ctrlpan\\panel8", GetMainPanel().size.width);
ASSIGN_OR_RETURN(const OwnedClxSpriteList sprite, LoadCelWithStatus("ctrlpan\\panel8", GetMainPanel().size.width));
ClxDraw(*BottomBuffer, { 0, (GetMainPanel().size.height + PanelPaddingHeight) - 1 }, sprite[0]);
}
{
const Point bulbsPosition { 0, 87 };
const OwnedClxSpriteList statusPanel = LoadCel("ctrlpan\\p8bulbs", 88);
ASSIGN_OR_RETURN(const OwnedClxSpriteList statusPanel, LoadCelWithStatus("ctrlpan\\p8bulbs", 88));
ClxDraw(*pLifeBuff, bulbsPosition, statusPanel[0]);
ClxDraw(*pManaBuff, bulbsPosition, statusPanel[1]);
}
Expand All @@ -901,7 +902,7 @@ void InitMainPanel()
if (IsChatAvailable()) {
if (!HeadlessMode) {
{
const OwnedClxSpriteList sprite = LoadCel("ctrlpan\\talkpanl", GetMainPanel().size.width);
ASSIGN_OR_RETURN(const OwnedClxSpriteList sprite, LoadCelWithStatus("ctrlpan\\talkpanl", GetMainPanel().size.width));
ClxDraw(*BottomBuffer, { 0, (GetMainPanel().size.height + PanelPaddingHeight) * 2 - 1 }, sprite[0]);
}
multiButtons = LoadCel("ctrlpan\\p8but2", 33);
Expand All @@ -917,11 +918,11 @@ void InitMainPanel()
MainPanelFlag = false;
LevelButtonDown = false;
if (!HeadlessMode) {
LoadMainPanel();
pMainPanelButtons = LoadCel("ctrlpan\\panel8bu", 71);
RETURN_IF_ERROR(LoadMainPanel());
ASSIGN_OR_RETURN(pMainPanelButtons, LoadCelWithStatus("ctrlpan\\panel8bu", 71));

static const uint16_t CharButtonsFrameWidths[9] { 95, 41, 41, 41, 41, 41, 41, 41, 41 };
pChrButtons = LoadCel("data\\charbut", CharButtonsFrameWidths);
ASSIGN_OR_RETURN(pChrButtons, LoadCelWithStatus("data\\charbut", CharButtonsFrameWidths));
}
ResetMainPanelButtons();
if (!HeadlessMode)
Expand All @@ -939,14 +940,16 @@ void InitMainPanel()

if (!HeadlessMode) {
InitSpellBook();
pQLogCel = LoadCel("data\\quest", static_cast<uint16_t>(SidePanelSize.width));
GoldBoxBuffer = LoadCel("ctrlpan\\golddrop", 261);
ASSIGN_OR_RETURN(pQLogCel, LoadCelWithStatus("data\\quest", static_cast<uint16_t>(SidePanelSize.width)));
ASSIGN_OR_RETURN(GoldBoxBuffer, LoadCelWithStatus("ctrlpan\\golddrop", 261));
}
CloseGoldDrop();
CalculatePanelAreas();

if (!HeadlessMode)
InitModifierHints();

return {};
}

void DrawMainPanel(const Surface &out)
Expand Down
4 changes: 3 additions & 1 deletion Source/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>

#include <SDL.h>
#include <expected.hpp>

#ifdef USE_SDL1
#include "utils/sdl2_to_1_2_backports.h"
Expand Down Expand Up @@ -127,7 +129,7 @@ void DrawFlaskValues(const Surface &out, Point pos, int currValue, int maxValue)
*/
void UpdateLifeManaPercent();

void InitMainPanel();
tl::expected<void, std::string> InitMainPanel();
void DrawMainPanel(const Surface &out);

/**
Expand Down
Loading

0 comments on commit 04f0fb0

Please # to comment.