Skip to content

Commit

Permalink
Merge pull request EasyRPG#2875 from carstene1ns/feature/error-close
Browse files Browse the repository at this point in the history
Add custom error display for platforms
  • Loading branch information
Ghabry authored Jun 5, 2024
2 parents e544c0e + 0fb5083 commit 69a20d7
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 46 deletions.
8 changes: 8 additions & 0 deletions src/baseui.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class BaseUi {
*/
virtual bool ShowCursor(bool /* flag */) { return true; };

/**
* Outputs the error message in a custom way depending on platform
*
* @param message message string.
* @return wether error has been handled
*/
virtual bool HandleErrorOutput(const std::string & /* message */) { return false; }

/**
* Gets if fullscreen mode is active.
*
Expand Down
27 changes: 12 additions & 15 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <fmt/ostream.h>
#ifdef EMSCRIPTEN
# include "platform/emscripten/interface.h"
#elif defined(__vita__)
# include <psp2/kernel/processmgr.h>
#endif

#include "output.h"
Expand Down Expand Up @@ -203,10 +201,7 @@ static void HandleErrorOutput(const std::string& err) {
BitmapRef surface = DisplayUi->GetDisplaySurface();
surface->FillRect(surface->GetRect(), Color(255, 0, 0, 128));

std::string error = "Error:\n";
error += err;

error += "\n\nEasyRPG Player will close now.\nPress [ENTER] key to exit...";
std::string error = err + "\nPress [ENTER] key to exit...";

Text::Draw(*surface, 11, 11, *Font::DefaultBitmapFont(), Color(0, 0, 0, 255), error);
Text::Draw(*surface, 10, 10, *Font::DefaultBitmapFont(), Color(255, 255, 255, 255), error);
Expand Down Expand Up @@ -294,16 +289,19 @@ void Output::ToggleLog() {

void Output::ErrorStr(std::string const& err) {
WriteLog(LogLevel::Error, err);
std::string error = "Error:\n" + err + "\n\nEasyRPG Player will close now.";

static bool recursive_call = false;
if (!recursive_call && DisplayUi) {
recursive_call = true;
HandleErrorOutput(err);
// Try platform handler first, then global one
if (!DisplayUi->HandleErrorOutput(error)) {
HandleErrorOutput(error);
}
DisplayUi.reset();
} else {
// Fallback to Console if the display is not ready yet
std::cout << err << std::endl;
std::cout << std::endl;
std::cout << "EasyRPG Player will close now.";
std::cout << error << std::endl;
#if defined (PLAYER_NINTENDO) || defined(__vita__)
// stdin is non-blocking
Game_Clock::SleepFor(5s);
Expand All @@ -316,11 +314,10 @@ void Output::ErrorStr(std::string const& err) {
#endif
}

// FIXME: This does not go through platform teardown code
#ifdef __vita__
sceKernelExitProcess(EXIT_FAILURE);
#endif
exit(EXIT_FAILURE);
Player::exit_code = EXIT_FAILURE;

// FIXME: No idea how to indicate error from core in libretro
exit(Player::exit_code);
}

void Output::WarningStr(std::string const& warn) {
Expand Down
24 changes: 15 additions & 9 deletions src/platform/3ds/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserDat
}
}

void n3dsExit() {
romfsExit();
stop3DSLink();
gfxExit();

if (old_time_limit != UINT32_MAX) {
APT_SetAppCpuTimeLimit(old_time_limit);
}
}

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

Expand Down Expand Up @@ -174,17 +184,13 @@ int main(int argc, char* argv[]) {
args.push_back(ctr_dir);
}

// Setup teardown code
atexit(n3dsExit);

// Run Player
Player::Init(std::move(args));
Player::Run();

romfsExit();
stop3DSLink();
gfxExit();

if (old_time_limit != UINT32_MAX) {
APT_SetAppCpuTimeLimit(old_time_limit);
}

return EXIT_SUCCESS;
// Close
return Player::exit_code;
}
12 changes: 12 additions & 0 deletions src/platform/3ds/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,15 @@ void CtrUi::vGetConfig(Game_ConfigVideo& cfg) const {
cfg.touch_ui.SetDescription("Toggle the backlight of the bottom screen");
cfg.touch_ui.Set(bottom_state != screen_state::off);
}

bool CtrUi::HandleErrorOutput(const std::string &message) {
errorConf errCnf;
std::string error = Player::GetFullVersionString();
error += "\n\n" + message;

errorInit(&errCnf, ERROR_TEXT_WORD_WRAP, CFG_LANGUAGE_EN);
errorText(&errCnf, error.c_str());
errorDisp(&errCnf);

return true;
}
1 change: 1 addition & 0 deletions src/platform/3ds/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class CtrUi final : public BaseUi {
void ToggleStretch() override;
void ToggleTouchUi() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;
bool HandleErrorOutput(const std::string &message) override;

#ifdef SUPPORT_AUDIO
AudioInterface& GetAudio();
Expand Down
3 changes: 2 additions & 1 deletion src/platform/emscripten/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ extern "C" int main(int argc, char* argv[]) {

emscripten_set_main_loop(main_loop, 0, 0);

return EXIT_SUCCESS;
// Close
return Player::exit_code;
}
10 changes: 8 additions & 2 deletions src/platform/psvita/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserDat
sceClibPrintf("[%s] %s: %s\n", GAME_TITLE, prefix.c_str(), msg.c_str());
}

void VitaExit() {
sceKernelExitProcess(Player::exit_code);
}

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

Expand Down Expand Up @@ -102,11 +106,13 @@ int main(int argc, char* argv[]) {
args.push_back(psp2_dir);
}

// Setup teardown code
atexit(VitaExit);

// Run Player
Player::Init(std::move(args));
Player::Run();

// Close
sceKernelExitProcess(EXIT_SUCCESS);
return EXIT_SUCCESS;
return Player::exit_code;
}
3 changes: 2 additions & 1 deletion src/platform/sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ extern "C" int main(int argc, char* argv[]) {
Player::Init(std::move(args));
Player::Run();

return EXIT_SUCCESS;
// Close
return Player::exit_code;
}
18 changes: 18 additions & 0 deletions src/platform/sdl/sdl2_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ bool Sdl2Ui::ShowCursor(bool flag) {
return temp_flag;
}

bool Sdl2Ui::HandleErrorOutput(const std::string &message) {
std::string title = Player::GetFullVersionString();

// Manually Restore window from fullscreen, since message would not be visible otherwise
if ((current_display_mode.flags & SDL_WINDOW_FULLSCREEN_DESKTOP)
== SDL_WINDOW_FULLSCREEN_DESKTOP) {
SDL_SetWindowFullscreen(sdl_window, 0);
SDL_SetWindowSize(sdl_window, 0, 0);
}

if(SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title.c_str(),
message.c_str(), sdl_window) != 0) {
return false;
}

return true;
}

void Sdl2Ui::ProcessEvent(SDL_Event &evnt) {
switch (evnt.type) {
case SDL_WINDOWEVENT:
Expand Down
1 change: 1 addition & 0 deletions src/platform/sdl/sdl2_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Sdl2Ui final : public BaseUi {
void ToggleVsync() override;
void vGetConfig(Game_ConfigVideo& cfg) const override;
Rect GetWindowMetrics() const override;
bool HandleErrorOutput(const std::string &message) override;

#ifdef SUPPORT_AUDIO
AudioInterface& GetAudio() override;
Expand Down
35 changes: 21 additions & 14 deletions src/platform/switch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,27 @@ static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserDat
}
}

void SwitchExit() {
romfsExit();

// Close debug log
if (nxlinkSocket >= 0) {
close(nxlinkSocket);
socketExit();
nxlinkSocket = -1;
}

// HOS will close us immediately afterwards, if requested by home menu.
// So no further cleanup possible.
appletUnlockExit();
}

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

appletLockExit();

// yuzu/nso
// suyu/nso
is_nro = envHasArgv();
Output::SetLogCallback(LogCallback);

Expand Down Expand Up @@ -85,21 +100,13 @@ int main(int argc, char* argv[]) {
}
}

// Setup platform teardown code
atexit(SwitchExit);

// Run Player
Player::Init(std::move(args));
Player::Run();

romfsExit();

// Close debug log
if (nxlinkSocket >= 0) {
close(nxlinkSocket);
socketExit();
nxlinkSocket = -1;
}

// HOS will close us immediately afterwards, if requested by home menu.
// So no further cleanup possible.
appletUnlockExit();
return EXIT_SUCCESS;
// Close
return Player::exit_code;
}
3 changes: 2 additions & 1 deletion src/platform/wii/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ extern "C" int main(int argc, char* argv[]) {
Player::Init(std::move(args));
Player::Run();

return EXIT_SUCCESS;
// Close
return Player::exit_code;
}
6 changes: 3 additions & 3 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ namespace Player {
int menu_offset_y = (screen_height - MENU_HEIGHT) / 2;
int message_box_offset_x = (screen_width - MENU_WIDTH) / 2;
bool has_custom_resolution = false;

bool exit_flag;
bool reset_flag;
int exit_code = EXIT_SUCCESS;
bool exit_flag = false;
bool reset_flag = false;
bool debug_flag;
bool hide_title_flag;
int load_game_id;
Expand Down
3 changes: 3 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ namespace Player {
/** Set the desired rendering frames per second */
void SetTargetFps(int fps);

/** Exit code (optionally indicting an error) when program terminates. */
extern int exit_code;

/** Exit flag, if true will exit application on next Player::Update. */
extern bool exit_flag;

Expand Down

0 comments on commit 69a20d7

Please sign in to comment.