Skip to content

Commit

Permalink
Add script reading
Browse files Browse the repository at this point in the history
Console will read scripts from resources/commands

Also create Console class.
  • Loading branch information
vieiraa committed Feb 20, 2020
1 parent d36e618 commit 672cff5
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/freeablo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ add_library(freeablo_lib # split into a library so I can link to it from tests
fasavegame/gameloader.cpp
)

target_link_libraries(freeablo_lib PUBLIC NuklearMisc Render Audio Serial Input Random enet cxxopts fmt::fmt)
target_link_libraries(freeablo_lib PUBLIC NuklearMisc Render Audio Serial Input Random enet cxxopts fmt::fmt Script)
target_include_directories(freeablo_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(freeablo main.cpp)
Expand Down
20 changes: 7 additions & 13 deletions apps/freeablo/fagui/guimanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <memory>
#include <misc/misc.h>
#include <misc/stringops.h>
#include <script/console.h>
#include <script/luascript.h>
#include <serial/textstream.h>
#include <string>

Expand Down Expand Up @@ -532,29 +534,21 @@ namespace FAGui

void GuiManager::consolePanel(nk_context* ctx)
{
static constexpr size_t bufferSize = 1024 * 1024;
static std::string boxBuffer;
static int boxLen;
static constexpr size_t inputSize = 512;
static char input[inputSize];
static int inputLen;
auto& c = Script::Console::getInstance();
drawPanel(ctx,
PanelType::console,
[&]() {
nk_layout_space_begin(ctx, NK_STATIC, 0, INT_MAX);
nk_layout_space_push(ctx, nk_rect(5, 5, 490, 250));
nk_edit_string(ctx, NK_EDIT_BOX | NK_EDIT_READ_ONLY, const_cast<char*>(boxBuffer.c_str()), &boxLen, bufferSize, nk_filter_default);
nk_edit_string(ctx, NK_EDIT_BOX | NK_EDIT_READ_ONLY, c->getBufferPtr(), c->getBufferLen(), c->getBuffer().length(), nk_filter_default);
const int32_t height = FARender::Renderer::get()->smallFont()->height + 15;
nk_layout_space_push(ctx, nk_rect(5, 270, 490, height));
nk_flags active = nk_edit_string(ctx, NK_EDIT_FIELD | NK_EDIT_SIG_ENTER, input, &inputLen, inputSize, nk_filter_ascii);
nk_flags active =
nk_edit_string(ctx, NK_EDIT_FIELD | NK_EDIT_SIG_ENTER, c->getInput(), c->getInputLen(), c->getInputSize(), nk_filter_ascii);

if (active & NK_EDIT_COMMITED)
{
input[inputLen] = '\n';
++inputLen;
boxBuffer.append(input, inputLen);
boxLen += inputLen;
inputLen = 0;
c->inputCommited();
}
nk_layout_space_end(ctx);
},
Expand Down
15 changes: 14 additions & 1 deletion components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ add_library(DiabloExe
diabloexe/characterstats.cpp
diabloexe/affix.cpp
diabloexe/affix.h
diabloexe/talkdata.h)
diabloexe/talkdata.h)
target_link_libraries(DiabloExe Misc FAIO)
set_target_properties(DiabloExe PROPERTIES COMPILE_FLAGS "${FA_COMPILER_FLAGS}")

Expand Down Expand Up @@ -144,5 +144,18 @@ add_library(NuklearMisc
nuklearmisc/standaloneguispritehandler.cpp
nuklearmisc/standaloneguispritehandler.h
)

set_target_properties(NuklearMisc PROPERTIES COMPILE_FLAGS "${FA_COMPILER_FLAGS}")
target_link_libraries(NuklearMisc nuklear nativefiledialog Input Render)

add_library(Script
script/luascript.cpp
script/luascript.h
script/console.cpp
script/console.h
)

find_package(Lua REQUIRED)

target_link_libraries(Script ${LUA_LIBRARIES} Filesystem)
set_target_properties(Script PROPERTIES COMPILE_FLAGS "${FA_COMPILER_FLAGS}")
73 changes: 73 additions & 0 deletions components/script/console.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "console.h"
#include "luascript.h"

#include <filesystem/resolver.h>
#include <misc/stringops.h>

namespace Script
{
static std::string luaStdOut;
std::unique_ptr<Console> Console::mInstance = nullptr;

Console::Console() : mBuffer({}), bufferLen(0), inputLen(0), mScript(LuaScript::getInstance()), mCommandsPath("resources/commands/")
{
// Hack. Redirects the print output to the console.
mScript->registerGlobalFunction("print", [](lua_State* state) -> int {
int n = lua_gettop(state);
if (n >= 1)
{
for (int i = n; i > 0; --i)
{
const char* str = lua_tostring(state, -i);
luaStdOut += str;
}
}

else
{
luaStdOut = "";
}
return 0;
});
}

std::unique_ptr<Console>& Console::getInstance()
{
if (!mInstance)
{
mInstance = std::unique_ptr<Console>(new Console());
}

return mInstance;
}

void Console::inputCommited()
{
std::string command = "";
command.append(mInput, inputLen);
mInput[inputLen] = '\n';
++inputLen;
mBuffer.append(mInput, inputLen);
bufferLen += inputLen;
inputLen = 0;

auto args = Misc::StringUtils::split(command, ' '); // TODO: parse args and pass them to the script
filesystem::path scriptPath = filesystem::resolver().resolve(mCommandsPath / (args[0] + ".lua"));
if (scriptPath.exists())
{
mScript->runScript(scriptPath.str());
std::string msg = luaStdOut.empty() ? ">> nil\n" : (">> \"" + luaStdOut + "\"\n");
mBuffer += msg;
bufferLen += msg.length();
luaStdOut = "";
}

else
{
std::string msg = ">> Command '" + command + "' not found\n";
mBuffer += msg;
bufferLen += msg.length();
luaStdOut = "";
}
}
}
37 changes: 37 additions & 0 deletions components/script/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "luascript.h"

#include <filesystem/path.h>
#include <memory>
#include <string>

namespace Script
{
class Console
{
std::string mBuffer;
int bufferLen;
static constexpr size_t inputSize = 512;
char mInput[inputSize];
int inputLen;
std::unique_ptr<LuaScript>& mScript;
filesystem::path mCommandsPath;
static std::unique_ptr<Console> mInstance;

public:
static std::unique_ptr<Console>& getInstance();

char* getInput() { return mInput; };
std::string& getBuffer() { return mBuffer; };
char* getBufferPtr() const { return const_cast<char*>(mBuffer.c_str()); }
int* getBufferLen() { return &bufferLen; }
int* getInputLen() { return &inputLen; }
constexpr size_t getInputSize() const { return inputSize; }

void inputCommited();

private:
Console();
};
}
21 changes: 21 additions & 0 deletions components/script/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Script
{
std::unique_ptr<LuaScript> LuaScript::mInstance = nullptr;
LuaScript::LuaScript() : mState(luaL_newstate()) { luaL_openlibs(mState); }

LuaScript::~LuaScript()
Expand All @@ -12,6 +13,14 @@ namespace Script
lua_close(mState);
}

std::unique_ptr<LuaScript>& LuaScript::getInstance()
{
if (!mInstance)
mInstance = std::unique_ptr<LuaScript>(new LuaScript());

return mInstance;
}

void LuaScript::printError(const std::string& variable, const std::string& reason)
{
#ifndef NDEBUG
Expand Down Expand Up @@ -111,4 +120,16 @@ namespace Script
exit(1);
}
}

void LuaScript::eval(const char* script)
{
lua_settop(mState, 0);

if (luaL_dostring(mState, script))
{
std::cerr << "Lua error: " << lua_tostring(mState, -1) << "\n";
lua_pop(mState, 1);
exit(1);
}
}
}
14 changes: 13 additions & 1 deletion components/script/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <misc/assert.h>

#include <iostream>
#include <memory>
#include <string>
#include <vector>

Expand All @@ -14,15 +15,23 @@ namespace Script
{
lua_State* mState;
int mLevel;
static std::unique_ptr<LuaScript> mInstance;

public:
LuaScript();
~LuaScript();

static std::unique_ptr<LuaScript>& getInstance();

void printError(const std::string& variable, const std::string& reason);

template <typename T> void registerType(const std::string& metatable_name);

template <typename Func> void registerGlobalFunction(const std::string& functionName, Func func)
{
lua_pushcfunction(mState, func);
lua_setglobal(mState, functionName.c_str());
}

template <typename T> T get(const std::string& variable)
{
release_assert(mState);
Expand Down Expand Up @@ -74,8 +83,11 @@ namespace Script
}

void runScript(const std::string& path);
void eval(const char* script);

private:
LuaScript();

bool luaGetToStack(const std::string& variable);

void clean();
Expand Down
1 change: 1 addition & 0 deletions resources/commands/hello.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("ola mundo", 1)

0 comments on commit 672cff5

Please sign in to comment.