add 'write-to-user' permission & refactor checking for writeability

This commit is contained in:
MihailRis 2025-12-07 00:08:57 +03:00 committed by ShiftyX1
parent 8831d676d4
commit 56d808e3e2
6 changed files with 48 additions and 17 deletions

View File

@ -12,7 +12,7 @@ Project::~Project() = default;
dv::value Project::serialize() const {
auto permissionsList = dv::list();
for (const auto& perm : permissions) {
for (const auto& perm : permissions.permissions) {
permissionsList.add(perm);
}
return dv::object({
@ -31,10 +31,11 @@ void Project::deserialize(const dv::value& src) {
if (src.has("permissions")) {
std::vector<std::string> perms;
dv::get(src, "permissions", perms);
permissions = std::set<std::string>(perms.begin(), perms.end());
permissions.permissions =
std::set<std::string>(perms.begin(), perms.end());
}
logger.info() << "permissions: ";
for (const auto& perm : permissions) {
for (const auto& perm : permissions.permissions) {
logger.info() << " - " << perm;
}
}
@ -58,3 +59,7 @@ void Project::loadProjectStartScript() {
logger.warning() << "project start script does not exists";
}
}
bool Permissions::has(const std::string& name) const {
return permissions.find(name) != permissions.end();
}

View File

@ -12,13 +12,21 @@ namespace scripting {
class IClientProjectScript;
}
struct Permissions {
static inline std::string WRITE_TO_USER = "write-to-user";
std::set<std::string> permissions;
bool has(const std::string& name) const;
};
struct Project : Serializable {
std::string name;
std::string title;
std::vector<std::string> basePacks;
std::unique_ptr<scripting::IClientProjectScript> clientScript;
std::unique_ptr<Process> setupCoroutine;
std::set<std::string> permissions;
Permissions permissions;
~Project();

View File

@ -134,6 +134,7 @@ void Engine::initialize(CoreParameters coreParameters) {
}
paths = std::make_unique<EnginePaths>(params);
loadProject();
paths->setupProject(*project);
editor = std::make_unique<devtools::Editor>(*this);
cmd = std::make_unique<cmd::CommandsInterpreter>();

View File

@ -6,6 +6,7 @@
#include "io/devices/ZipFileDevice.hpp"
#include "maths/util.hpp"
#include "typedefs.hpp"
#include "devtools/Project.hpp"
#include "util/platform.hpp"
#include "util/random.hpp"
#include "util/stringutil.hpp"
@ -43,7 +44,10 @@ static std::string generate_random_base64() {
EnginePaths::EnginePaths(CoreParameters& params)
: resourcesFolder(params.resFolder),
userFilesFolder(params.userFolder),
projectFolder(params.projectFolder) {
projectFolder(params.projectFolder),
initiallyWriteables({
"world", "export", "config"
}) {
if (!params.scriptFile.empty()) {
scriptFolder = params.scriptFile.parent_path();
io::set_device("script", std::make_shared<io::StdfsDevice>(*scriptFolder));
@ -239,6 +243,22 @@ void EnginePaths::setEntryPoints(std::vector<PathsRoot> entryPoints) {
this->entryPoints = std::move(entryPoints);
}
void EnginePaths::setupProject(const Project& project) {
if (project.permissions.has(Permissions::WRITE_TO_USER)) {
initiallyWriteables.insert("user");
}
}
bool EnginePaths::isWriteable(const std::string& entryPoint) const {
if (entryPoint.length() < 2) {
return false;
}
if (entryPoint.substr(0, 2) == "W.") {
return true;
}
return initiallyWriteables.find(entryPoint) != initiallyWriteables.end();
}
std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view path) {
size_t separator = path.find(':');
if (separator == std::string::npos) {

View File

@ -9,6 +9,7 @@
#include <string>
#include <vector>
#include <tuple>
#include <set>
struct PathsRoot {
std::string name;
@ -42,6 +43,8 @@ private:
std::vector<PathsRoot> roots;
};
struct Project;
class EnginePaths {
public:
ResPaths resPaths;
@ -65,6 +68,10 @@ public:
void setEntryPoints(std::vector<PathsRoot> entryPoints);
void setupProject(const Project& project);
bool isWriteable(const std::string& entryPoint) const;
std::vector<io::path> scanForWorlds() const;
static std::tuple<std::string, std::string> parsePath(std::string_view view);
@ -81,6 +88,7 @@ private:
std::vector<PathsRoot> entryPoints;
std::unordered_map<std::string, std::string> writeables;
std::vector<std::string> mounted;
std::set<std::string> initiallyWriteables;
void cleanup();
};

View File

@ -39,18 +39,7 @@ static int l_read(lua::State* L) {
);
}
static std::set<std::string> writeable_entry_points {
"world", "export", "config"
};
static bool is_writeable(const std::string& entryPoint) {
if (entryPoint.length() < 2) {
return false;
}
if (entryPoint.substr(0, 2) == "W.") {
return true;
}
// todo: do better
auto device = io::get_device(entryPoint);
if (device == nullptr) {
return false;
@ -58,7 +47,7 @@ static bool is_writeable(const std::string& entryPoint) {
if (dynamic_cast<io::MemoryDevice*>(device.get())) {
return true;
}
if (writeable_entry_points.find(entryPoint) != writeable_entry_points.end()) {
if (engine->getPaths().isWriteable(entryPoint)) {
return true;
}
return false;