-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Console will read scripts from resources/commands Also create Console class.
- Loading branch information
Showing
8 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ""; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
print("ola mundo", 1) |