contentpack remove feature WIP

This commit is contained in:
MihailRis 2024-02-29 23:47:07 +03:00
parent a1f313bedc
commit 3862dbda66
18 changed files with 541 additions and 433 deletions

View File

@ -1,4 +1,5 @@
// Example of a GLSL library
#define PI 3.1415926535897932384626433832795
#define PI2 (PI*2)
vec4 decompress_light(float compressed_light) {
vec4 result;

View File

@ -1,6 +1,7 @@
# Menu
menu.missing-content=Missing Content!
world.convert-request=Content indices have changed! Convert world files?
pack.remove-confirm=Do you want to erase all pack content from the world forever?
error.pack-not-found=Could not to find pack
error.dependency-not-found=Dependency pack is not found
world.delete-confirm=Do you want to delete world forever?

View File

@ -10,6 +10,8 @@ Add=Добавить
error.pack-not-found=Не удалось найти пакет
error.dependency-not-found=Используемая зависимость не найдена
pack.remove-confirm=Удалить весь поставляемый паком контент из мира (безвозвратно)?
# Меню
menu.New World=Новый Мир
menu.Quit=Выход

BIN
res/textures/gui/cross.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@ -70,6 +70,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/no_icon.png", "gui/no_icon");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/warning.png", "gui/warning");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/error.png", "gui/error");
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/cross.png", "gui/cross");
if (content) {
loader.add(ASSET_SHADER, SHADERS_FOLDER"/ui3d", "ui3d");
loader.add(ASSET_SHADER, SHADERS_FOLDER"/background", "background");
@ -79,7 +80,8 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) {
loader.add(ASSET_TEXTURE, TEXTURES_FOLDER"/gui/crosshair.png", "gui/crosshair");
addLayouts(0, "core", loader.getPaths()->getMainRoot()/fs::path("layouts"), loader);
for (auto& pack : content->getPacks()) {
for (auto& entry : content->getPacks()) {
auto pack = entry.second.get();
auto& info = pack->getInfo();
fs::path folder = info.folder / fs::path("layouts");
addLayouts(pack->getEnvironment()->getId(), info.id, folder, loader);

View File

@ -26,7 +26,7 @@ void ContentBuilder::add(ItemDef* def) {
}
void ContentBuilder::add(ContentPackRuntime* pack) {
packs.push_back(std::unique_ptr<ContentPackRuntime>(pack));
packs.emplace(pack->getId(), pack);
}
Block& ContentBuilder::createBlock(std::string id) {
@ -136,7 +136,7 @@ Content::Content(ContentIndices* indices,
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::vector<std::unique_ptr<ContentPackRuntime>> packs)
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs)
: blockDefs(blockDefs),
itemDefs(itemDefs),
indices(indices),
@ -179,6 +179,14 @@ ItemDef& Content::requireItem(std::string id) const {
return *found->second;
}
const std::vector<std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
const ContentPackRuntime* Content::getPackRuntime(std::string id) const {
auto found = packs.find(id);
if (found == packs.end()) {
return nullptr;
}
return found->second.get();
}
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& Content::getPacks() const {
return packs;
}

View File

@ -48,7 +48,7 @@ class ContentBuilder {
std::unordered_map<std::string, ItemDef*> itemDefs;
std::vector<std::string> itemIds;
std::vector<std::unique_ptr<ContentPackRuntime>> packs;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public:
~ContentBuilder();
@ -108,7 +108,7 @@ class Content {
std::unordered_map<std::string, Block*> blockDefs;
std::unordered_map<std::string, ItemDef*> itemDefs;
std::unique_ptr<ContentIndices> indices;
std::vector<std::unique_ptr<ContentPackRuntime>> packs;
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs;
public:
std::unique_ptr<DrawGroups> const drawGroups;
@ -116,7 +116,7 @@ public:
std::unique_ptr<DrawGroups> drawGroups,
std::unordered_map<std::string, Block*> blockDefs,
std::unordered_map<std::string, ItemDef*> itemDefs,
std::vector<std::unique_ptr<ContentPackRuntime>> packs);
std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>> packs);
~Content();
inline ContentIndices* getIndices() const {
@ -129,7 +129,9 @@ public:
ItemDef* findItem(std::string id) const;
ItemDef& requireItem(std::string id) const;
const std::vector<std::unique_ptr<ContentPackRuntime>>& getPacks() const;
const ContentPackRuntime* getPackRuntime(std::string id) const;
const std::unordered_map<std::string, std::unique_ptr<ContentPackRuntime>>& getPacks() const;
};
#endif // CONTENT_CONTENT_H_

View File

@ -294,6 +294,7 @@ void ContentLoader::load(ContentBuilder& builder) {
auto runtime = new ContentPackRuntime(*pack, scripting::create_pack_environment(*pack));
builder.add(runtime);
env = runtime->getEnvironment()->getId();
ContentPackStats& stats = runtime->getStatsWriteable();
fixPackIndices();
@ -306,6 +307,7 @@ void ContentLoader::load(ContentBuilder& builder) {
if (!fs::is_regular_file(pack->getContentFile()))
return;
auto root = files::read_json(pack->getContentFile());
auto blocksarr = root->list("blocks");
if (blocksarr) {
@ -314,6 +316,7 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string full = pack->id+":"+name;
auto& def = builder.createBlock(full);
loadBlock(def, full, name);
stats.totalBlocks++;
if (!def.hidden) {
auto& item = builder.createItem(full+BLOCK_ITEM_SUFFIX);
item.generated = true;
@ -324,6 +327,7 @@ void ContentLoader::load(ContentBuilder& builder) {
for (uint j = 0; j < 4; j++) {
item.emission[j] = def.emission[j];
}
stats.totalItems++;
}
}
}
@ -334,6 +338,7 @@ void ContentLoader::load(ContentBuilder& builder) {
std::string name = itemsarr->str(i);
std::string full = pack->id+":"+name;
loadItem(builder.createItem(full), full, name);
stats.totalItems++;
}
}
}

View File

@ -77,8 +77,18 @@ struct ContentPack {
);
};
struct ContentPackStats {
size_t totalBlocks;
size_t totalItems;
inline bool hasSavingContent() const {
return totalBlocks + totalItems > 0;
}
};
class ContentPackRuntime {
ContentPack info;
ContentPackStats stats {};
std::unique_ptr<scripting::Environment> env;
public:
ContentPackRuntime(
@ -86,6 +96,14 @@ public:
std::unique_ptr<scripting::Environment> env
);
inline const ContentPackStats& getStats() const {
return stats;
}
inline ContentPackStats& getStatsWriteable() {
return stats;
}
inline const std::string& getId() {
return info.id;
}

View File

@ -592,3 +592,25 @@ void WorldFiles::addPack(const World* world, const std::string& id) {
}
files::write_string(file, ss.str());
}
void WorldFiles::removePack(const World* world, const std::string& id) {
fs::path file = getPacksFile();
if (!fs::is_regular_file(file)) {
if (!fs::is_directory(directory)) {
fs::create_directories(directory);
}
writePacks(world);
}
auto packs = files::read_list(file);
auto found = std::find(packs.begin(), packs.end(), id);
if (found != packs.end()) {
packs.erase(found);
}
std::stringstream ss;
ss << "# autogenerated; do not modify\n";
for (const auto& pack : packs) {
ss << pack << "\n";
}
files::write_string(file, ss.str());
}

View File

@ -86,31 +86,27 @@ class WorldFiles {
WorldRegion* getRegion(regionsmap& regions, int x, int z);
WorldRegion* getOrCreateRegion(regionsmap& regions, int x, int z);
/* Compress buffer with extrle
@param src source buffer
@param srclen length of source buffer
@param len (out argument) length of result buffer */
/// @brief Compress buffer with extrle
/// @param src source buffer
/// @param srclen length of the source buffer
/// @param len (out argument) length of result buffer
/// @return compressed bytes array
ubyte* compress(const ubyte* src, size_t srclen, size_t& len);
/* Decompress buffer with extrle
@param src compressed buffer
@param srclen length of compressed buffer
@param dstlen max expected length of source buffer */
/// @brief Decompress buffer with extrle
/// @param src compressed buffer
/// @param srclen length of compressed buffer
/// @param dstlen max expected length of source buffer
/// @return decompressed bytes array
ubyte* decompress(const ubyte* src, size_t srclen, size_t dstlen);
ubyte* readChunkData(int x, int y,
uint32_t& length,
fs::path folder,
int layer);
void fetchChunks(WorldRegion* region, int x, int y,
fs::path folder, int layer);
ubyte* readChunkData(int x, int y, uint32_t& length, fs::path folder, int layer);
void writeRegions(regionsmap& regions,
const fs::path& folder, int layer);
void fetchChunks(WorldRegion* region, int x, int y, fs::path folder, int layer);
ubyte* getData(regionsmap& regions,
const fs::path& folder,
int x, int z, int layer, bool compression);
void writeRegions(regionsmap& regions, const fs::path& folder, int layer);
ubyte* getData(regionsmap& regions, const fs::path& folder, int x, int z, int layer, bool compression);
regfile* getRegFile(glm::ivec3 coord, const fs::path& folder);
@ -145,18 +141,31 @@ public:
bool readWorldInfo(World* world);
bool readPlayer(std::shared_ptr<Player> player);
void writeRegion(int x, int y,
WorldRegion* entry,
fs::path file,
int layer);
void writeRegion(int x, int y, WorldRegion* entry, fs::path file, int layer);
/// @brief Write player data to world files
/// @param player target player
void writePlayer(std::shared_ptr<Player> player);
/* @param world world info to save (nullable) */
/// @brief Write all unsaved data to world files
/// @param world target world
/// @param content world content
void write(const World* world, const Content* content);
void writePacks(const World* world);
void writeIndices(const ContentIndices* indices);
/* Append pack to packs.list without duplicate check */
/// @brief Append pack to the packs list without duplicate check and
/// dependencies resolve
/// @param world target world
/// @param id pack id
void addPack(const World* world, const std::string& id);
/// @brief Remove pack from the list (does not remove indices)
/// @param world target world
/// @param id pack id
void removePack(const World* world, const std::string& id);
static const char* WORLD_FILE;
};

View File

@ -170,6 +170,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
shader->uniform1f("u_gamma", settings.graphics.gamma);
shader->uniform1f("u_fogFactor", fogFactor);
shader->uniform1f("u_fogCurve", settings.graphics.fogCurve);
shader->uniform1f("u_dayTime", level->world->daytime);
shader->uniform3f("u_cameraPos", camera->position);
shader->uniform1i("u_cubemap", 1);
{

View File

@ -342,7 +342,8 @@ std::shared_ptr<Panel> create_packs_panel(
const std::vector<ContentPack>& packs,
Engine* engine,
bool backbutton,
packconsumer callback
packconsumer callback,
packconsumer remover
){
auto assets = engine->getAssets();
auto panel = std::make_shared<Panel>(vec2(550, 200), vec4(5.0f));
@ -358,7 +359,12 @@ std::shared_ptr<Panel> create_packs_panel(
callback(pack);
});
}
auto idlabel = std::make_shared<Label>("["+pack.id+"]");
auto runtime = engine->getContent()->getPackRuntime(pack.id);
auto idlabel = std::make_shared<Label>(
(runtime && runtime->getStats().hasSavingContent())
? "*["+pack.id+"]"
: "["+pack.id+"]"
);
idlabel->setColor(vec4(1, 1, 1, 0.5f));
idlabel->setSize(vec2(300, 25));
idlabel->setAlign(Align::right);
@ -390,6 +396,19 @@ std::shared_ptr<Panel> create_packs_panel(
packpanel->add(descriptionlabel, vec2(80, 28));
packpanel->add(std::make_shared<Image>(icon, vec2(64)), vec2(8));
if (remover && pack.id != "base") {
auto remimg = std::make_shared<Image>("gui/cross", vec2(32));
remimg->setColor(vec4(1.f, 1.f, 1.f, 0.5f));
auto rembtn = std::make_shared<Button>(remimg, vec4(2));
rembtn->setColor(vec4(0.0f));
rembtn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
rembtn->listenAction([=](GUI* gui) {
remover(pack);
});
packpanel->add(rembtn, vec2(470, 22));
}
panel->add(packpanel);
}
if (backbutton) {
@ -398,8 +417,15 @@ std::shared_ptr<Panel> create_packs_panel(
return panel;
}
static void reopen_world(Engine* engine, World* world) {
std::string wname = world->getName();
engine->setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine));
open_world(wname, engine);
}
// TODO: refactor
void create_content_panel(Engine* engine) {
void create_content_panel(Engine* engine, Level* level) {
auto menu = engine->getGUI()->getMenu();
auto paths = engine->getPaths();
auto mainPanel = create_page(engine, "content", 550, 0.0f, 5);
@ -415,16 +441,29 @@ void create_content_panel(Engine* engine) {
}
}
auto panel = create_packs_panel(engine->getContentPacks(), engine, false, nullptr);
auto panel = create_packs_panel(
engine->getContentPacks(), engine, false, nullptr,
[=](const ContentPack& pack) {
auto runtime = engine->getContent()->getPackRuntime(pack.id);
if (runtime->getStats().hasSavingContent()) {
guiutil::confirm(engine->getGUI(), langs::get(L"remove-confirm", L"pack")+
L" ("+util::str2wstr_utf8(pack.id)+L")", [=]() {
// FIXME: work in progress
});
} else {
auto world = level->getWorld();
world->wfile->removePack(world, pack.id);
reopen_world(engine, world);
}
}
);
mainPanel->add(panel);
mainPanel->add(create_button(
langs::get(L"Add", L"content"), vec4(10.0f), vec4(1), [=](GUI* gui) {
auto panel = create_packs_panel(scanned, engine, true,
[=](const ContentPack& pack) {
auto screen = dynamic_cast<LevelScreen*>(engine->getScreen().get());
auto level = screen->getLevel();
auto world = level->getWorld();
auto worldFolder = paths->getWorldFolder();
for (const auto& dependency : pack.dependencies) {
fs::path folder = ContentPack::findPack(paths, worldFolder, dependency);
@ -437,14 +476,9 @@ void create_content_panel(Engine* engine) {
world->wfile->addPack(world, dependency);
}
}
world->wfile->addPack(world, pack.id);
std::string wname = world->getName();
engine->setScreen(nullptr);
engine->setScreen(std::make_shared<MenuScreen>(engine));
open_world(wname, engine);
});
reopen_world(engine, world);
}, nullptr);
menu->addPage("content-packs", panel);
menu->setPage("content-packs");
}));
@ -691,7 +725,7 @@ void create_settings_panel(Engine* engine) {
panel->add(guiutil::backButton(menu));
}
void create_pause_panel(Engine* engine) {
void menus::create_pause_panel(Engine* engine, Level* level) {
auto menu = engine->getGUI()->getMenu();
auto panel = create_page(engine, "pause", 400, 0.0f, 1);
@ -699,7 +733,7 @@ void create_pause_panel(Engine* engine) {
menu->reset();
}));
panel->add(create_button(L"Content", vec4(10.0f), vec4(1), [=](GUI*) {
create_content_panel(engine);
create_content_panel(engine, level);
menu->setPage("content");
}));
panel->add(guiutil::gotoButton(L"Settings", "settings", menu));
@ -717,10 +751,9 @@ void menus::create_menus(Engine* engine) {
create_new_world_panel(engine);
create_settings_panel(engine);
create_controls_panel(engine);
create_pause_panel(engine);
create_languages_panel(engine);
create_world_generators_panel(engine);
create_main_menu_panel(engine);
create_world_generators_panel(engine);
}
void menus::refresh_menus(Engine* engine) {

View File

@ -2,9 +2,11 @@
#define FRONTEND_MENU_H_
class Engine;
class Level;
namespace menus {
void create_version_label(Engine* engine);
void create_pause_panel(Engine* engine, Level* level);
void create_menus(Engine* engine);
void refresh_menus(Engine* engine);
}

View File

@ -87,22 +87,25 @@ void MenuScreen::draw(float delta) {
static bool backlight;
LevelScreen::LevelScreen(Engine* engine, Level* level)
: Screen(engine),
level(level),
frontend(std::make_unique<LevelFrontend>(level, engine->getAssets())),
hud(std::make_unique<Hud>(engine, frontend.get())),
worldRenderer(std::make_unique<WorldRenderer>(engine, frontend.get())),
controller(std::make_unique<LevelController>(engine->getSettings(), level)) {
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine), level(level){
menus::create_pause_panel(engine, level);
auto& settings = engine->getSettings();
auto assets = engine->getAssets();
controller = std::make_unique<LevelController>(settings, level);
frontend = std::make_unique<LevelFrontend>(level, assets);
worldRenderer = std::make_unique<WorldRenderer>(engine, frontend.get());
hud = std::make_unique<Hud>(engine, frontend.get());
backlight = settings.graphics.backlight;
animator.reset(new TextureAnimator());
animator->addAnimations(engine->getAssets()->getAnimations());
animator = std::make_unique<TextureAnimator>();
animator->addAnimations(assets->getAnimations());
auto content = level->content;
for (auto& pack : content->getPacks()) {
for (auto& entry : content->getPacks()) {
auto pack = entry.second.get();
const ContentPack& info = pack->getInfo();
fs::path scriptFile = info.folder/fs::path("scripts/hud.lua");
if (fs::is_regular_file(scriptFile)) {

View File

@ -4,9 +4,11 @@
#include <GLFW/glfw3.h>
#include <string.h>
bool Events::_keys[KEYS_BUFFER_SIZE] = {};
uint Events::_frames[KEYS_BUFFER_SIZE] = {};
uint Events::_current = 0;
inline constexpr short _MOUSE_KEYS_OFFSET = 1024;
bool Events::keys[KEYS_BUFFER_SIZE] = {};
uint Events::frames[KEYS_BUFFER_SIZE] = {};
uint Events::currentFrame = 0;
int Events::scroll = 0;
glm::vec2 Events::delta = {};
glm::vec2 Events::cursor = {};
@ -22,10 +24,9 @@ bool Events::pressed(keycode keycode) {
bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
fprintf(stderr, "pressed %i\n", keycode); //FIXME: unreasonable cstdio usage
return false;
}
return _keys[keycode];
return keys[keycode];
}
bool Events::jpressed(keycode keycode) {
@ -33,7 +34,7 @@ bool Events::jpressed(keycode keycode) {
}
bool Events::jpressed(int keycode) {
return Events::pressed(keycode) && _frames[keycode] == _current;
return Events::pressed(keycode) && frames[keycode] == currentFrame;
}
bool Events::clicked(mousecode button) {
@ -58,7 +59,7 @@ void Events::toggleCursor() {
}
void Events::pollEvents() {
_current++;
currentFrame++;
delta.x = 0.f;
delta.y = 0.f;
scroll = 0;
@ -120,8 +121,8 @@ bool Events::jactive(std::string name) {
}
void Events::setKey(int key, bool b) {
Events::_keys[key] = b;
Events::_frames[key] = Events::_current;
Events::keys[key] = b;
Events::frames[key] = currentFrame;
}
void Events::setButton(int button, bool b) {

View File

@ -7,21 +7,19 @@
#include <string>
#include <vector>
#include <unordered_map>
#include "../typedefs.h"
typedef unsigned int uint;
const short KEYS_BUFFER_SIZE = 1036;
const short _MOUSE_KEYS_OFFSET = 1024;
inline constexpr short KEYS_BUFFER_SIZE = 1036;
class Events {
static bool keys[KEYS_BUFFER_SIZE];
static uint frames[KEYS_BUFFER_SIZE];
static uint currentFrame;
static bool cursor_drag;
public:
static bool _keys[KEYS_BUFFER_SIZE];
static uint _frames[KEYS_BUFFER_SIZE];
static uint _current;
static int scroll;
static glm::vec2 delta;
static glm::vec2 cursor;
static bool cursor_drag;
static bool _cursor_locked;
static std::vector<uint> codepoints;
static std::vector<keycode> pressedKeys;