add 'write to read-only entry point' warnings
This commit is contained in:
parent
f22108f6b4
commit
71eadb2fde
@ -1,12 +1,19 @@
|
|||||||
history = session.get_entry("commands_history")
|
history = session.get_entry("commands_history")
|
||||||
history_pointer = #history
|
history_pointer = #history
|
||||||
|
|
||||||
|
local warnings_all = {}
|
||||||
|
|
||||||
local warning_id = 0
|
local warning_id = 0
|
||||||
events.on("core:warning", function (wtype, text)
|
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", {
|
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
|
warning_id = warning_id + 1
|
||||||
|
table.insert(warnings_all, full)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
function setup_variables()
|
function setup_variables()
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "files/files.hpp"
|
#include "files/files.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
#include "../lua_engine.hpp"
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
@ -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) {
|
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);
|
std::string text = lua::require_string(L, 2);
|
||||||
files::write_string(path, text);
|
files::write_string(path, text);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::set<std::string> writeable_entry_points {
|
|
||||||
"world", "export", "config"
|
|
||||||
};
|
|
||||||
|
|
||||||
static int l_remove(lua::State* L) {
|
static int l_remove(lua::State* L) {
|
||||||
std::string rawpath = lua::require_string(L, 1);
|
std::string rawpath = lua::require_string(L, 1);
|
||||||
fs::path path = resolve_path(rawpath);
|
fs::path path = resolve_path(rawpath);
|
||||||
@ -155,15 +170,9 @@ static int read_bytes_from_table(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int l_write_bytes(lua::State* L) {
|
static int l_write_bytes(lua::State* L) {
|
||||||
int pathIndex = 1;
|
fs::path path = get_writeable_path(L);
|
||||||
|
|
||||||
if (!lua::isstring(L, pathIndex)) {
|
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 2)) {
|
||||||
throw std::runtime_error("string expected");
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::path path = resolve_path(lua::require_string(L, pathIndex));
|
|
||||||
|
|
||||||
if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1)) {
|
|
||||||
auto& bytes = bytearray->data();
|
auto& bytes = bytearray->data();
|
||||||
return lua::pushboolean(
|
return lua::pushboolean(
|
||||||
L, files::write_bytes(path, bytes.data(), bytes.size())
|
L, files::write_bytes(path, bytes.data(), bytes.size())
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user