Skip to content

Commit

Permalink
add 'write to read-only entry point' warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 7, 2024
1 parent f22108f commit 71eadb2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
9 changes: 8 additions & 1 deletion res/layouts/console.xml.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
history = session.get_entry("commands_history")
history_pointer = #history

local warnings_all = {}

local warning_id = 0
events.on("core:warning", function (wtype, text)
local full = wtype..": "..text
if table.has(warnings_all, full) then
return
end
document.problemsLog:add(gui.template("problem", {
type="warning", text=wtype..": "..text, id=tostring(warning_id)
type="warning", text=full, id=tostring(warning_id)
}))
warning_id = warning_id + 1
table.insert(warnings_all, full)
end)

function setup_variables()
Expand Down
35 changes: 22 additions & 13 deletions src/logic/scripting/lua/libs/libfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "files/files.hpp"
#include "util/stringutil.hpp"
#include "api_lua.hpp"
#include "../lua_engine.hpp"

namespace fs = std::filesystem;
using namespace scripting;
Expand Down Expand Up @@ -47,17 +48,31 @@ static int l_read(lua::State* L) {
);
}

static std::set<std::string> writeable_entry_points {
"world", "export", "config"
};

static fs::path get_writeable_path(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
auto entryPoint = rawpath.substr(0, rawpath.find(':'));
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
lua::emit_event(L, "core:warning", [=](auto L) {
lua::pushstring(L, "writing to read-only entry point");
lua::pushstring(L, entryPoint);
return 2;
});
}
return path;
}

static int l_write(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
fs::path path = get_writeable_path(L);
std::string text = lua::require_string(L, 2);
files::write_string(path, text);
return 1;
}

static std::set<std::string> writeable_entry_points {
"world", "export", "config"
};

static int l_remove(lua::State* L) {
std::string rawpath = lua::require_string(L, 1);
fs::path path = resolve_path(rawpath);
Expand Down Expand Up @@ -155,15 +170,9 @@ static int read_bytes_from_table(
}

static int l_write_bytes(lua::State* L) {
int pathIndex = 1;

if (!lua::isstring(L, pathIndex)) {
throw std::runtime_error("string expected");
}

fs::path path = resolve_path(lua::require_string(L, pathIndex));
fs::path path = get_writeable_path(L);

if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1)) {
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 2)) {
auto& bytes = bytearray->data();
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
Expand Down

0 comments on commit 71eadb2

Please sign in to comment.