Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua console: Add a prelude file #6766

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Packaging/resources/assets/lua/repl_prelude.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
log = require('devilutionx.log')
audio = require('devilutionx.audio')
render = require('devilutionx.render')
message = require('devilutionx.message')
inspect = require('inspect')
27 changes: 25 additions & 2 deletions Source/panels/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<tl::expected<AssetData, std::string>> ConsolePrelude;

bool IsConsoleVisible;
char ConsoleInputBuffer[4096];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

Expand Down
Loading