Skip to content

Commit

Permalink
fix(skyrim-platform): mirror unhandled errors to cmd (skyrim-multipla…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Mar 30, 2024
1 parent 592c652 commit 850cba8
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/release/dev/sp-cmd-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Embedded console](https://github.com/skyrim-multiplayer/skymp/blob/592c6527ed91e6c97a38d143f4ae1cdab9c3268e/docs/release/sp-2.8.md?plain=1#L68) now mirrors unhandled exceptions from the game console properly.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include "Validators.h"
#include "WindowsConsolePrinter.h"

namespace {
// TODO: Add printers switching
static std::shared_ptr<IConsolePrinter> g_printer(new InGameConsolePrinter);
std::shared_ptr<IConsolePrinter> g_printer(new InGameConsolePrinter);
std::shared_ptr<IConsolePrinter> g_windowsConsolePrinter = nullptr;

namespace {
struct ConsoleCommand
{
std::string longName;
Expand All @@ -23,7 +23,6 @@ struct ConsoleCommand
};
static std::map<std::string, ConsoleCommand> g_replacedConsoleCmd;
static bool g_printConsolePrefixesEnabled = true;
static std::unique_ptr<WindowsConsolePrinter> g_windowsConsolePrinter = NULL;

bool IsNameEqual(const std::string& first, const std::string& second)
{
Expand Down Expand Up @@ -69,7 +68,7 @@ void ConsoleApi::InitCmd(int offsetLeft, int offsetTop, int width, int height,
bool isAlwaysOnTop)
{

g_windowsConsolePrinter = std::make_unique<WindowsConsolePrinter>(
g_windowsConsolePrinter = std::make_shared<WindowsConsolePrinter>(
offsetLeft, offsetTop, width, height, isAlwaysOnTop);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "ExceptionPrinter.h"
#include "ConsoleApi.h"
#include "IConsolePrinter.h"

// ConsoleApi.cpp
extern std::shared_ptr<IConsolePrinter> g_printer;
extern std::shared_ptr<IConsolePrinter> g_windowsConsolePrinter;

const char* ExceptionPrinter::RemoveMultiplePrefixes(const char* str,
const char* prefix)
Expand Down Expand Up @@ -41,7 +46,18 @@ void ExceptionPrinter::PrintException(const char* what)
msg += '...';
}
const char* prefix = ConsoleApi::GetExceptionPrefix();
console->Print("%s%s", (i ? "" : prefix), msg.data());

if (i > 0) {
g_printer->PrintRaw(msg.data());
} else {
msg = std::string(prefix) + msg;
g_printer->PrintRaw(msg.data());
}

if (g_windowsConsolePrinter) {
g_windowsConsolePrinter->PrintRaw(msg.data());
}

++i;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class IConsolePrinter
virtual ~IConsolePrinter() = default;

virtual void Print(const JsFunctionArguments& args) = 0;
virtual void PrintRaw(const char* str) = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ void InGameConsolePrinter::Print(const JsFunctionArguments& args)
const char* prefix = ConsoleApi::GetScriptPrefix();
console->Print("%s%s", prefix, s.data());
}

void InGameConsolePrinter::PrintRaw(const char* str)
{
auto console = RE::ConsoleLog::GetSingleton();
if (!console)
throw NullPointerException("console");

console->Print(str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class InGameConsolePrinter : public IConsolePrinter
{
public:
void Print(const JsFunctionArguments& args) override;
void PrintRaw(const char* str) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ void WindowsConsolePrinter::Print(const JsFunctionArguments& args)

std::cout << s << std::endl;
}

void WindowsConsolePrinter::PrintRaw(const char* str)
{
std::cout << str << std::endl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class WindowsConsolePrinter : public IConsolePrinter
~WindowsConsolePrinter() override;

void Print(const JsFunctionArguments& args) override;
void PrintRaw(const char* str) override;
};

0 comments on commit 850cba8

Please sign in to comment.