add pack.request_writeable(...) & update gui.confirm screen

This commit is contained in:
MihailRis 2025-03-12 17:00:33 +03:00
parent 4cdb1fbae2
commit 88b45e5098
6 changed files with 84 additions and 14 deletions

View File

@ -35,6 +35,7 @@ GUI::GUI()
menu = std::make_shared<Menu>();
menu->setId("menu");
menu->setZIndex(10);
container->add(menu);
container->setScrollable(false);

View File

@ -91,7 +91,11 @@ void guiutil::confirm(
if (yestext.empty()) yestext = langs::get(L"Yes");
if (notext.empty()) notext = langs::get(L"No");
auto container = std::make_shared<Container>(glm::vec2(5000, 5000));
container->setColor(glm::vec4(0.05f, 0.05f, 0.05f, 0.7f));
auto panel = std::make_shared<Panel>(glm::vec2(600, 200), glm::vec4(8.0f), 8.0f);
panel->setGravity(Gravity::center_center);
container->add(panel);
panel->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
panel->add(std::make_shared<Label>(text));
auto subpanel = std::make_shared<Panel>(glm::vec2(600, 53));
@ -103,8 +107,8 @@ void guiutil::confirm(
menu->removePage("<confirm>");
if (on_confirm) {
on_confirm();
} else {
menu->back();
} else if (!menu->back()) {
menu->reset();
}
};
@ -112,8 +116,8 @@ void guiutil::confirm(
menu->removePage("<confirm>");
if (on_deny) {
on_deny();
} else {
menu->back();
} else if (!menu->back()) {
menu->reset();
}
};
@ -136,7 +140,7 @@ void guiutil::confirm(
}));
panel->refresh();
menu->addPage("<confirm>", panel, true);
menu->addPage("<confirm>", container, true);
menu->setPage("<confirm>");
}

View File

@ -150,6 +150,37 @@ void EnginePaths::setCurrentWorldFolder(io::path folder) {
io::create_subdevice("world", "user", currentWorldFolder);
}
#include <chrono>
#include "maths/util.hpp"
std::string EnginePaths::createWriteablePackDevice(const std::string& name) {
const auto& found = writeablePacks.find(name);
if (found != writeablePacks.end()) {
return found->second;
}
io::path folder;
for (const auto& pack : *contentPacks) {
if (pack.id == name) {
folder = pack.folder;
break;
}
}
if (folder.emptyOrInvalid()) {
throw std::runtime_error("pack not found");
}
auto now = std::chrono::high_resolution_clock::now();
auto seed = now.time_since_epoch().count();
util::PseudoRandom random(seed); // fixme: replace with safe random
auto number = random.rand64();
auto entryPoint = std::string("W.") + util::base64_urlsafe_encode(reinterpret_cast<ubyte*>(&number), 6);
io::create_subdevice(entryPoint, folder.entryPoint(), folder.pathPart());
writeablePacks[name] = entryPoint;
return entryPoint;
}
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
// Remove previous content entry-points
for (const auto& id : contentEntryPoints) {

View File

@ -1,5 +1,6 @@
#pragma once
#include <unordered_map>
#include <stdexcept>
#include <optional>
#include <string>
@ -33,6 +34,8 @@ public:
io::path getControlsFile() const;
io::path getSettingsFile() const;
std::string createWriteablePackDevice(const std::string& name);
void setContentPacks(std::vector<ContentPack>* contentPacks);
std::vector<io::path> scanForWorlds() const;
@ -47,6 +50,7 @@ private:
std::optional<std::filesystem::path> scriptFolder;
std::vector<ContentPack>* contentPacks = nullptr;
std::vector<std::string> contentEntryPoints;
std::unordered_map<std::string, std::string> writeablePacks;
};
struct PathsRoot {

View File

@ -41,10 +41,23 @@ 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;
}
if (writeable_entry_points.find(entryPoint) != writeable_entry_points.end()) {
return true;
}
return false;
}
static io::path get_writeable_path(lua::State* L) {
io::path path = lua::require_string(L, 1);
auto entryPoint = path.entryPoint();
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
if (!is_writeable(entryPoint)) {
throw std::runtime_error("access denied");
}
return path;
@ -60,7 +73,7 @@ static int l_write(lua::State* L) {
static int l_remove(lua::State* L) {
io::path path = lua::require_string(L, 1);
auto entryPoint = path.entryPoint();
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
if (!is_writeable(entryPoint)) {
throw std::runtime_error("access denied");
}
return lua::pushboolean(L, io::remove(path));
@ -69,7 +82,7 @@ static int l_remove(lua::State* L) {
static int l_remove_tree(lua::State* L) {
io::path path = lua::require_string(L, 1);
auto entryPoint = path.entryPoint();
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
if (!is_writeable(entryPoint)) {
throw std::runtime_error("access denied");
}
return lua::pushinteger(L, io::remove_all(path));
@ -222,10 +235,7 @@ static int l_read_combined_object(lua::State* L) {
static int l_is_writeable(lua::State* L) {
io::path path = lua::require_string(L, 1);
auto entryPoint = path.entryPoint();
if (writeable_entry_points.find(entryPoint) == writeable_entry_points.end()) {
return lua::pushboolean(L, false);
}
return lua::pushboolean(L, true);
return lua::pushboolean(L, is_writeable(entryPoint));
}
const luaL_Reg filelib[] = {
@ -249,4 +259,5 @@ const luaL_Reg filelib[] = {
{"read_combined_list", lua::wrap<l_read_combined_list>},
{"read_combined_object", lua::wrap<l_read_combined_object>},
{"is_writeable", lua::wrap<l_is_writeable>},
{NULL, NULL}};
{NULL, NULL}
};

View File

@ -7,6 +7,9 @@
#include "assets/AssetsLoader.hpp"
#include "content/Content.hpp"
#include "engine/Engine.hpp"
#include "graphics/ui/gui_util.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "frontend/locale.hpp"
#include "world/files/WorldFiles.hpp"
#include "io/engine_paths.hpp"
#include "world/Level.hpp"
@ -247,6 +250,20 @@ static int l_pack_assemble(lua::State* L) {
return 1;
}
static int l_pack_request_writeable(lua::State* L) {
auto packid = lua::require_string(L, 1);
lua::pushvalue(L, 2);
auto handler = lua::create_lambda_nothrow(L);
auto str = langs::get(L"Grant %{0} pack modification permission?");
util::replaceAll(str, L"%{0}", util::str2wstr_utf8(packid));
guiutil::confirm(*engine, str, [packid, handler]() {
handler({engine->getPaths().createWriteablePackDevice(packid)});
engine->getGUI()->getMenu()->reset();
});
return 0;
}
const luaL_Reg packlib[] = {
{"get_folder", lua::wrap<l_pack_get_folder>},
{"get_installed", lua::wrap<l_pack_get_installed>},
@ -254,4 +271,6 @@ const luaL_Reg packlib[] = {
{"get_info", lua::wrap<l_pack_get_info>},
{"get_base_packs", lua::wrap<l_pack_get_base_packs>},
{"assemble", lua::wrap<l_pack_assemble>},
{NULL, NULL}};
{"request_writeable", lua::wrap<l_pack_request_writeable>},
{NULL, NULL}
};