add app.reset_content 'non_reset_packs' optional argument

This commit is contained in:
MihailRis 2025-11-09 23:04:50 +03:00
parent 50d520e747
commit 0e6fb878bf
9 changed files with 51 additions and 14 deletions

View File

@ -742,15 +742,36 @@ function require(path)
return __load_script(prefix .. ":modules/" .. file .. ".lua", nil, env) return __load_script(prefix .. ":modules/" .. file .. ".lua", nil, env)
end end
function __scripts_cleanup() -- TODO: move to string
local function join(t, sep)
local s = ""
for i, v in ipairs(t) do
s = s..tostring(v)
if i < #t then
s = s..sep
end
end
return s
end
function __scripts_cleanup(non_reset_packs)
debug.log("cleaning scripts cache") debug.log("cleaning scripts cache")
if #non_reset_packs == 0 then
debug.log("no non-reset packs")
else
debug.log("non-reset packs: "..join(non_reset_packs, ", "))
end
for k, v in pairs(__cached_scripts) do for k, v in pairs(__cached_scripts) do
local packname, _ = parse_path(k) local packname, _ = parse_path(k)
if table.has(non_reset_packs, packname) then
goto continue
end
if packname ~= "core" then if packname ~= "core" then
debug.log("unloaded "..k) debug.log("unloaded "..k)
__cached_scripts[k] = nil __cached_scripts[k] = nil
package.loaded[k] = nil package.loaded[k] = nil
end end
::continue::
end end
end end

View File

@ -49,10 +49,10 @@ std::vector<std::string>& ContentControl::getBasePacks() {
return basePacks; return basePacks;
} }
void ContentControl::resetContent() { void ContentControl::resetContent(const std::vector<std::string>& nonReset) {
paths.setCurrentWorldFolder(""); paths.setCurrentWorldFolder("");
scripting::cleanup(); scripting::cleanup(nonReset);
std::vector<PathsRoot> resRoots; std::vector<PathsRoot> resRoots;
{ {
auto pack = ContentPack::createCore(); auto pack = ContentPack::createCore();
@ -79,8 +79,6 @@ void ContentControl::loadContent(const std::vector<std::string>& names) {
} }
void ContentControl::loadContent() { void ContentControl::loadContent() {
scripting::cleanup();
std::vector<std::string> names; std::vector<std::string> names;
for (auto& pack : contentPacks) { for (auto& pack : contentPacks) {
names.push_back(pack.id); names.push_back(pack.id);

View File

@ -34,7 +34,7 @@ public:
std::vector<std::string>& getBasePacks(); std::vector<std::string>& getBasePacks();
/// @brief Reset content to base packs list /// @brief Reset content to base packs list
void resetContent(); void resetContent(const std::vector<std::string>& nonReset);
void loadContent(const std::vector<std::string>& names); void loadContent(const std::vector<std::string>& names);

View File

@ -394,6 +394,7 @@ void Engine::setScreen(std::shared_ptr<Screen> screen) {
} }
if (project->clientScript && this->screen) { if (project->clientScript && this->screen) {
project->clientScript->onScreenChange(this->screen->getName(), true); project->clientScript->onScreenChange(this->screen->getName(), true);
window->setShouldRefresh();
} }
} }

View File

@ -26,7 +26,7 @@ MenuScreen::MenuScreen(Engine& engine)
MenuScreen::~MenuScreen() = default; MenuScreen::~MenuScreen() = default;
void MenuScreen::onOpen() { void MenuScreen::onOpen() {
engine.getContentControl().resetContent(); engine.getContentControl().resetContent({});
auto menu = engine.getGUI().getMenu(); auto menu = engine.getGUI().getMenu();
menu->reset(); menu->reset();

View File

@ -43,7 +43,16 @@ static int l_reset_content(lua::State* L) {
if (level != nullptr) { if (level != nullptr) {
throw std::runtime_error("world must be closed before"); throw std::runtime_error("world must be closed before");
} }
content_control->resetContent(); std::vector<std::string> nonResetPacks;
if (lua::istable(L, 1)) {
int len = lua::objlen(L, 1);
for (int i = 0; i < len; i++) {
lua::rawgeti(L, i + 1, 1);
nonResetPacks.emplace_back(lua::require_lstring(L, -1));
lua::pop(L);
}
}
content_control->resetContent(std::move(nonResetPacks));
return 0; return 0;
} }

View File

@ -348,10 +348,13 @@ void scripting::on_world_quit() {
scripting::controller = nullptr; scripting::controller = nullptr;
} }
void scripting::cleanup() { void scripting::cleanup(const std::vector<std::string>& nonReset) {
auto L = lua::get_main_state(); auto L = lua::get_main_state();
lua::requireglobal(L, "pack"); lua::requireglobal(L, "pack");
for (auto& pack : content_control->getAllContentPacks()) { for (auto& pack : content_control->getAllContentPacks()) {
if (std::find(nonReset.begin(), nonReset.end(), pack.id) != nonReset.end()) {
continue;
}
lua::requirefield(L, "unload"); lua::requirefield(L, "unload");
lua::pushstring(L, pack.id); lua::pushstring(L, pack.id);
lua::call_nothrow(L, 1); lua::call_nothrow(L, 1);
@ -359,7 +362,12 @@ void scripting::cleanup() {
lua::pop(L); lua::pop(L);
if (lua::getglobal(L, "__scripts_cleanup")) { if (lua::getglobal(L, "__scripts_cleanup")) {
lua::call_nothrow(L, 0); lua::createtable(L, nonReset.size(), 0);
for (size_t i = 0; i < nonReset.size(); i++) {
lua::pushstring(L, nonReset[i]);
lua::rawseti(L, i + 1);
}
lua::call_nothrow(L, 1);
} }
} }

View File

@ -82,7 +82,7 @@ namespace scripting {
void on_world_tick(int tps); void on_world_tick(int tps);
void on_world_save(); void on_world_save();
void on_world_quit(); void on_world_quit();
void cleanup(); void cleanup(const std::vector<std::string>& nonReset);
void on_blocks_tick(const Block& block, int tps); void on_blocks_tick(const Block& block, int tps);
void update_block(const Block& block, const glm::ivec3& pos); void update_block(const Block& block, const glm::ivec3& pos);
void random_update_block(const Block& block, const glm::ivec3& pos); void random_update_block(const Block& block, const glm::ivec3& pos);

View File

@ -382,11 +382,11 @@ public:
} }
void setShouldRefresh() override { void setShouldRefresh() override {
shouldRefresh = true; shouldRefresh = 2;
} }
bool checkShouldRefresh() override { bool checkShouldRefresh() override {
if (shouldRefresh) { if ((--shouldRefresh) == 0) {
shouldRefresh = false; shouldRefresh = false;
return true; return true;
} }
@ -573,7 +573,7 @@ private:
double prevSwap = 0.0; double prevSwap = 0.0;
int posX = 0; int posX = 0;
int posY = 0; int posY = 0;
bool shouldRefresh = true; int shouldRefresh = 1;
}; };
static_assert(!std::is_abstract<GLFWWindow>()); static_assert(!std::is_abstract<GLFWWindow>());