Initial work on LUA scripting.

This LUA scripting implementation uses the sol header only C++ wrapper for the native LUA libraries.

The API does not follow the original LU scripting. Each instance of a script is in its own LUA-State, and has 'self' as a global. There are hooks to all the implemented CppScript methods, as well as a subset of the entity functionallity. Has to be expanded upon.

This is local work which has been sitting for awhile; thought someone might like to take a look at it.
This commit is contained in:
wincent
2022-04-13 19:51:14 +02:00
parent a0a9936e47
commit 5f689104af
18 changed files with 31747 additions and 1 deletions

View File

@@ -63,6 +63,12 @@
#include "GameConfig.h"
#include "ScriptedActivityComponent.h"
#if __include_lua__ == 1
#include "LuaScript.h"
#include "ScriptComponent.h"
#include <filesystem>
#endif
void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) {
std::string chatCommand;
std::vector<std::string> args;
@@ -614,6 +620,78 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
return;
}
#if __include_lua__ == 1
if (chatCommand == "lua" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() >= 1) {
std::string input = "";
// If the first argument is "load", load the file
bool isFile = false;
if (args[0] == "load") {
isFile = true;
}
// Join the args
for (size_t i = 0; i < args.size(); i++) {
if (i == 0 && isFile) continue;
input += args[i] + " ";
}
// If isFile is true, load the file
if (isFile) {
// Trim last space
input.pop_back();
std::string path = input;
std::ifstream infile(path);
std::stringstream buffer;
if (infile.is_open()) {
std::string line;
while (std::getline(infile, line)) {
buffer << line << "\n";
}
}
else {
ChatPackets::SendSystemMessage(sysAddr, u"Could not open file " + GeneralUtils::ASCIIToUTF16(path));
return;
}
input = buffer.str();
infile.close();
}
LuaScript* lua = new LuaScript(LuaTerminateBehavior::ResetScriptAndDelete);
ScriptComponent* script = entity->GetComponent<ScriptComponent>();
if (script == nullptr) {
script = new ScriptComponent(entity, false);
entity->AddComponent(ScriptComponent::ComponentType, script);
}
script->SetScript(lua);
lua->SetEntity(entity);
// Run the code, and print any errors
try {
lua->Script(input);
}
// Catch sol::error
catch (const sol::error& e) {
ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(e.what()));
}
// Catch *any* error
catch (...) {
ChatPackets::SendSystemMessage(sysAddr, u"Unknown error!");
}
}
#endif
if (chatCommand == "addmission" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) {
if (args.size() == 0) return;