diff --git a/CMake/Assets.cmake b/CMake/Assets.cmake index ffab7a8f581..6d23cee9803 100644 --- a/CMake/Assets.cmake +++ b/CMake/Assets.cmake @@ -142,6 +142,7 @@ set(devilutionx_assets levels/towndata/automap.amp lua/init.lua lua/inspect.lua + lua/repl_prelude.lua nlevels/cutl5w.clx nlevels/cutl6w.clx nlevels/l5data/cornerstone.dun diff --git a/Packaging/resources/assets/lua/repl_prelude.lua b/Packaging/resources/assets/lua/repl_prelude.lua new file mode 100644 index 00000000000..b0b272e8fa7 --- /dev/null +++ b/Packaging/resources/assets/lua/repl_prelude.lua @@ -0,0 +1,5 @@ +log = require('devilutionx.log') +audio = require('devilutionx.audio') +render = require('devilutionx.render') +message = require('devilutionx.message') +inspect = require('inspect') diff --git a/Source/panels/console.cpp b/Source/panels/console.cpp index 2457fd04ce7..ead7f35ca72 100644 --- a/Source/panels/console.cpp +++ b/Source/panels/console.cpp @@ -14,6 +14,7 @@ #include "DiabloUI/text_input.hpp" #include "control.h" #include "engine.h" +#include "engine/assets.hpp" #include "engine/displacement.hpp" #include "engine/dx.h" #include "engine/palette.h" @@ -39,6 +40,7 @@ constexpr std::string_view HelpText = " Up/Down to fill the input from history," " Shift+Up/Down to fill the input from output history," " Ctrl+L to clear history, Esc to close."; +std::optional> ConsolePrelude; bool IsConsoleVisible; char ConsoleInputBuffer[4096]; @@ -313,12 +315,33 @@ void NextOutput() NextHistoryItem(IsHistoryOutputLine); } +void AddInitialConsoleLines() +{ + if (ConsolePrelude->has_value()) { + std::string_view prelude { **ConsolePrelude }; + if (!prelude.empty() && prelude.back() == '\n') + prelude.remove_suffix(1); + AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = StrCat(HelpText, "\n", prelude) }); + } else { + AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) }); + AddConsoleLine(ConsoleLine { .type = ConsoleLine::Error, .text = ConsolePrelude->error() }); + } +} + void ClearConsole() { ConsoleLines.clear(); HistoryIndex = -1; ScrollOffset = 0; - AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) }); + AddInitialConsoleLines(); +} + +void InitConsole() +{ + ConsolePrelude = LoadAsset("lua\\repl_prelude.lua"); + AddInitialConsoleLines(); + if (ConsolePrelude->has_value()) + RunLuaReplLine(std::string_view(**ConsolePrelude)); } } // namespace @@ -442,7 +465,7 @@ void DrawConsole(const Surface &out) SDL_StartTextInput(); FirstRender = false; if (ConsoleLines.empty()) { - AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) }); + InitConsole(); } }