Skip to content

Commit

Permalink
Lua: Set warn function
Browse files Browse the repository at this point in the history
Previously, warnings went nowhere.
  • Loading branch information
glebm authored and AJenbo committed Nov 2, 2023
1 parent 4fe90f7 commit dbfa204
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ set(devilutionx_assets
fonts/buttonface.trn
fonts/buttonpushed.trn
fonts/gamedialogwhite.trn
fonts/gamedialogyellow.trn
fonts/gamedialogred.trn
fonts/golduis.trn
fonts/goldui.trn
Expand Down
Binary file not shown.
49 changes: 25 additions & 24 deletions Source/DiabloUI/ui_flags.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,33 @@ enum class UiFlags : uint32_t {
ColorUiGoldDark = 1 << 8,
ColorUiSilverDark = 1 << 9,
ColorDialogWhite = 1 << 10,
ColorDialogRed = 1 << 11,
ColorYellow = 1 << 12,
ColorGold = 1 << 13,
ColorBlack = 1 << 14,
ColorWhite = 1 << 15,
ColorWhitegold = 1 << 16,
ColorRed = 1 << 17,
ColorBlue = 1 << 18,
ColorOrange = 1 << 19,
ColorButtonface = 1 << 20,
ColorButtonpushed = 1 << 21,

AlignCenter = 1 << 22,
AlignRight = 1 << 23,
VerticalCenter = 1 << 24,

KerningFitSpacing = 1 << 25,

ElementDisabled = 1 << 26,
ElementHidden = 1 << 27,

PentaCursor = 1 << 28,
Outlined = 1 << 29,
ColorDialogYellow = 1 << 11,
ColorDialogRed = 1 << 12,
ColorYellow = 1 << 13,
ColorGold = 1 << 14,
ColorBlack = 1 << 15,
ColorWhite = 1 << 16,
ColorWhitegold = 1 << 17,
ColorRed = 1 << 18,
ColorBlue = 1 << 19,
ColorOrange = 1 << 20,
ColorButtonface = 1 << 21,
ColorButtonpushed = 1 << 22,

AlignCenter = 1 << 23,
AlignRight = 1 << 24,
VerticalCenter = 1 << 25,

KerningFitSpacing = 1 << 26,

ElementDisabled = 1 << 27,
ElementHidden = 1 << 28,

PentaCursor = 1 << 29,
Outlined = 1 << 30,

/** @brief Ensures that the if current element is active that the next element is also visible. */
NeedsNextElement = 1 << 30,
NeedsNextElement = 1U << 31U,
// clang-format on
};
use_enum_as_flags(UiFlags);
Expand Down
7 changes: 5 additions & 2 deletions Source/engine/render/text_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ constexpr std::array<int, 6> LineHeights = { 12, 26, 38, 42, 50, 22 };
constexpr int SmallFontTallLineHeight = 16;
std::array<int, 6> BaseLineOffset = { -3, -2, -3, -6, -7, 3 };

std::array<const char *, 18> ColorTranslations = {
std::array<const char *, 19> ColorTranslations = {
"fonts\\goldui.trn",
"fonts\\grayui.trn",
"fonts\\golduis.trn",
Expand All @@ -68,10 +68,11 @@ std::array<const char *, 18> ColorTranslations = {
"fonts\\buttonface.trn",
"fonts\\buttonpushed.trn",
"fonts\\gamedialogwhite.trn",
"fonts\\gamedialogyellow.trn",
"fonts\\gamedialogred.trn",
};

std::array<std::optional<std::array<uint8_t, 256>>, 18> ColorTranslationsData;
std::array<std::optional<std::array<uint8_t, 256>>, 19> ColorTranslationsData;

text_color GetColorFromFlags(UiFlags flags)
{
Expand All @@ -97,6 +98,8 @@ text_color GetColorFromFlags(UiFlags flags)
return ColorUiSilverDark;
if (HasAnyOf(flags, UiFlags::ColorDialogWhite))
return gbRunGame ? ColorInGameDialogWhite : ColorDialogWhite;
if (HasAnyOf(flags, UiFlags::ColorDialogYellow))
return ColorInGameDialogYellow;
if (HasAnyOf(flags, UiFlags::ColorDialogRed))
return ColorInGameDialogRed;
if (HasAnyOf(flags, UiFlags::ColorYellow))
Expand Down
5 changes: 3 additions & 2 deletions Source/engine/render/text_render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ enum text_color : uint8_t {
ColorButtonface,
ColorButtonpushed,

ColorInGameDialogWhite, // Dialog white in-game
ColorInGameDialogRed, // Dialog red in-game
ColorInGameDialogWhite, // Dialog white in-game
ColorInGameDialogYellow, // Dialog yellow in-game
ColorInGameDialogRed, // Dialog red in-game
};

constexpr GameFontTables GetFontSizeFromUiFlags(UiFlags flags)
Expand Down
11 changes: 11 additions & 0 deletions Source/lua/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ int LuaPrint(lua_State *state)
return 0;
}

void LuaWarn(void *userData, const char *message, int continued)
{
static std::string warnBuffer;
warnBuffer.append(message);
if (continued != 0)
return;
LogWarn("{}", warnBuffer);
warnBuffer.clear();
}

bool CheckResult(sol::protected_function_result result, bool optional)
{
const bool valid = result.valid();
Expand Down Expand Up @@ -141,6 +151,7 @@ void LuaInitialize()
.compiledScripts = {},
});
sol::state &lua = CurrentLuaState->sol;
lua_setwarnf(lua.lua_state(), LuaWarn, /*ud=*/nullptr);
lua.open_libraries(
sol::lib::base,
sol::lib::package,
Expand Down
11 changes: 11 additions & 0 deletions Source/lua/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ namespace {

std::optional<sol::environment> replEnv;

void LuaConsoleWarn(void *userData, const char *message, int continued)
{
static std::string warnBuffer;
warnBuffer.append(message);
if (continued != 0)
return;
PrintWarningToConsole(warnBuffer);
warnBuffer.clear();
}

int LuaPrintToConsole(lua_State *state)
{
std::string result;
Expand All @@ -41,6 +51,7 @@ void CreateReplEnvironment()
sol::state &lua = GetLuaState();
replEnv.emplace(lua, sol::create, lua.globals());
replEnv->set("print", LuaPrintToConsole);
lua_setwarnf(replEnv->lua_state(), LuaConsoleWarn, /*ud=*/nullptr);
}

sol::environment &ReplEnvironment()
Expand Down
13 changes: 12 additions & 1 deletion Source/panels/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct ConsoleLine {
Help,
Input,
Output,
Warning,
Error
};

Expand Down Expand Up @@ -98,6 +99,7 @@ bool FirstRender;
constexpr UiFlags TextUiFlags = UiFlags::FontSizeDialog;
constexpr UiFlags InputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite;
constexpr UiFlags OutputTextUiFlags = TextUiFlags | UiFlags::ColorDialogWhite;
constexpr UiFlags WarningTextUiFlags = TextUiFlags | UiFlags::ColorDialogYellow;
constexpr UiFlags ErrorTextUiFlags = TextUiFlags | UiFlags::ColorDialogRed;

constexpr int TextSpacing = 0;
Expand Down Expand Up @@ -210,6 +212,10 @@ void DrawConsoleLines(const Surface &out)
DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = OutputTextUiFlags, .spacing = TextSpacing });
break;
case ConsoleLine::Warning:
DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = WarningTextUiFlags, .spacing = TextSpacing });
break;
case ConsoleLine::Error:
DrawString(out, line, { 0, lineYEnd },
TextRenderOptions { .flags = ErrorTextUiFlags, .spacing = TextSpacing });
Expand Down Expand Up @@ -292,7 +298,7 @@ void NextInput()
bool IsHistoryOutputLine(const ConsoleLine &line)
{
return !line.text.empty()
&& (line.type == ConsoleLine::Output || line.type == ConsoleLine::Error)
&& (line.type == ConsoleLine::Output || line.type == ConsoleLine::Warning || line.type == ConsoleLine::Error)
&& (HistoryIndex == -1
|| GetConsoleLineFromEnd(HistoryIndex).textWithoutPrompt() != line.text);
}
Expand Down Expand Up @@ -471,5 +477,10 @@ void PrintToConsole(std::string_view text)
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Output, .text = std::string(text) });
}

void PrintWarningToConsole(std::string_view text)
{
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Warning, .text = std::string(text) });
}

} // namespace devilution
#endif // _DEBUG
1 change: 1 addition & 0 deletions Source/panels/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void OpenConsole();
bool ConsoleHandleEvent(const SDL_Event &event);
void DrawConsole(const Surface &out);
void PrintToConsole(std::string_view text);
void PrintWarningToConsole(std::string_view text);

} // namespace devilution
#endif // _DEBUG

0 comments on commit dbfa204

Please sign in to comment.