Merge pull request #713 from MihailRis/fixes

0.30.2 fixes
This commit is contained in:
MihailRis 2025-12-01 22:42:54 +03:00 • committed by GitHub
commit 3c69400944
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 55 additions and 23 deletions

View file

@ -5,6 +5,7 @@
#include <utility> #include <utility>
#include "coders/imageio.hpp" #include "coders/imageio.hpp"
#include "coders/commons.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "content/ContentPack.hpp" #include "content/ContentPack.hpp"
@ -71,20 +72,26 @@ aloader_func AssetsLoader::getLoader(AssetType tag) {
void AssetsLoader::loadNext() { void AssetsLoader::loadNext() {
const aloader_entry& entry = entries.front(); const aloader_entry& entry = entries.front();
logger.info() << "loading " << entry.filename << " as " << entry.alias; logger.info() << "loading " << entry.filename << " as " << entry.alias;
std::string error {};
try { try {
aloader_func loader = getLoader(entry.tag); aloader_func loader = getLoader(entry.tag);
auto postfunc = auto postfunc =
loader(this, paths, entry.filename, entry.alias, entry.config); loader(this, paths, entry.filename, entry.alias, entry.config);
postfunc(&assets); postfunc(&assets);
entries.pop(); } catch (const parsing_error& err) {
} catch (std::runtime_error& err) { error = err.errorLog();
logger.error() << err.what(); } catch (const std::runtime_error& err) {
auto type = entry.tag; error = err.what();
std::string filename = entry.filename;
std::string reason = err.what();
entries.pop();
throw assetload::error(type, std::move(filename), std::move(reason));
} }
if (!error.empty()) {
logger.error() << error;
auto tag = entry.tag;
auto filename = entry.filename;
entries.pop();
throw assetload::error(tag, std::move(filename), std::move(error));
}
entries.pop();
} }
static void add_layouts( static void add_layouts(

View file

@ -10,20 +10,21 @@ class ObjParser : BasicParser<char> {
std::vector<glm::vec2> uvs {{0, 0}}; std::vector<glm::vec2> uvs {{0, 0}};
std::vector<glm::vec3> normals {{0, 1, 0}}; std::vector<glm::vec3> normals {{0, 1, 0}};
// TODO: refactor
void parseFace(Mesh& mesh) { void parseFace(Mesh& mesh) {
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
while (hasNext()) { while (hasNext()) {
auto c = peekInLine(); auto c = peekInLine();
if (c == '\n') { if (c == '\n') {
break; break;
} else { } else if (hasNext()) {
uint indices[3] {}; uint indices[3] {};
uint i = 0; uint i = 0;
do { do {
char next = peekInLine(); char next = peekInLine();
if (is_digit(next)) { if (is_digit(next)) {
indices[i] = parseSimpleInt(10); indices[i] = parseSimpleInt(10);
if (peekInLine() == '/') { if (hasNext() && peekInLine() == '/') {
pos++; pos++;
} }
} else if (next == '/') { } else if (next == '/') {
@ -31,13 +32,13 @@ class ObjParser : BasicParser<char> {
} else { } else {
break; break;
} }
} while (peekInLine() != '\n' && ++i < 3); } while (hasNext() && peekInLine() != '\n' && ++i < 3);
vertices.push_back(Vertex { vertices.push_back(Vertex {
coords[indices[0]], uvs[indices[1]], normals[indices[2]]}); coords[indices[0]], uvs[indices[1]], normals[indices[2]]});
} }
} }
if (peekInLine() != '\n' && hasNext()) { if (hasNext() && peekInLine() != '\n') {
skipLine(); skipLine();
} }
if (vertices.size() >= 3) { if (vertices.size() >= 3) {

View file

@ -492,6 +492,7 @@ void ContentLoader::loadScripts(Content& content) {
load_scripts(content, content.items); load_scripts(content, content.items);
for (const auto& [packid, runtime] : content.getPacks()) { for (const auto& [packid, runtime] : content.getPacks()) {
auto env = runtime->getEnvironment();
const auto& pack = runtime->getInfo(); const auto& pack = runtime->getInfo();
const auto& folder = pack.folder; const auto& folder = pack.folder;
@ -500,9 +501,10 @@ void ContentLoader::loadScripts(Content& content) {
// Load entity components // Load entity components
io::path componentsDir = folder / "scripts/components"; io::path componentsDir = folder / "scripts/components";
foreach_file(componentsDir, [&pack](const io::path& file) { foreach_file(componentsDir, [&pack, env](const io::path& file) {
auto name = pack.id + ":" + file.stem(); auto name = pack.id + ":" + file.stem();
scripting::load_entity_component( scripting::load_entity_component(
env,
name, name,
file, file,
pack.id + ":scripts/components/" + file.name() pack.id + ":scripts/components/" + file.name()

View file

@ -45,7 +45,7 @@ std::shared_ptr<gui::UINode> UiDocument::get(const std::string& id) const {
if (found == map.end()) { if (found == map.end()) {
return nullptr; return nullptr;
} }
return found->second; return found->second.lock();
} }
const uidocscript& UiDocument::getScript() const { const uidocscript& UiDocument::getScript() const {

View file

@ -19,7 +19,7 @@ struct uidocscript {
bool onclose : 1; bool onclose : 1;
}; };
using UINodesMap = std::unordered_map<std::string, std::shared_ptr<gui::UINode>>; using UINodesMap = std::unordered_map<std::string, std::weak_ptr<gui::UINode>>;
class UiDocument { class UiDocument {
std::string id; std::string id;

View file

@ -385,12 +385,15 @@ bool UINode::isSubnodeOf(const UINode* node) {
void UINode::getIndices( void UINode::getIndices(
const std::shared_ptr<UINode>& node, const std::shared_ptr<UINode>& node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map std::unordered_map<std::string, std::weak_ptr<UINode>>& map
) { ) {
const std::string& id = node->getId(); const std::string& id = node->getId();
if (!id.empty()) { if (!id.empty()) {
const auto& found = map.find(id);
if (found == map.end() || found->second.expired()) {
map[id] = node; map[id] = node;
} }
}
auto container = std::dynamic_pointer_cast<gui::Container>(node); auto container = std::dynamic_pointer_cast<gui::Container>(node);
if (container) { if (container) {
for (const auto& subnode : container->getNodes()) { for (const auto& subnode : container->getNodes()) {

View file

@ -289,7 +289,7 @@ namespace gui {
/// @brief collect all nodes having id /// @brief collect all nodes having id
static void getIndices( static void getIndices(
const std::shared_ptr<UINode>& node, const std::shared_ptr<UINode>& node,
std::unordered_map<std::string, std::shared_ptr<UINode>>& map std::unordered_map<std::string, std::weak_ptr<UINode>>& map
); );
static std::shared_ptr<UINode> find( static std::shared_ptr<UINode> find(

View file

@ -1,6 +1,7 @@
#include "libentity.hpp" #include "libentity.hpp"
#include "content/Content.hpp" #include "content/Content.hpp"
#include "content/ContentPack.hpp"
#include "engine/Engine.hpp" #include "engine/Engine.hpp"
#include "engine/EnginePaths.hpp" #include "engine/EnginePaths.hpp"
#include "objects/Entities.hpp" #include "objects/Entities.hpp"
@ -236,7 +237,14 @@ static int l_reload_component(lua::State* L) {
} }
auto filename = name.substr(0, pos + 1) + "scripts/components/" + auto filename = name.substr(0, pos + 1) + "scripts/components/" +
name.substr(pos + 1) + ".lua"; name.substr(pos + 1) + ".lua";
scripting::load_entity_component(name, filename, filename); auto prefix = name.substr(0, pos);
auto runtime = content->getPackRuntime(prefix);
if (runtime == nullptr) {
throw std::runtime_error("pack '" + prefix + "' content is not loaded");
}
scripting::load_entity_component(
runtime->getEnvironment(), name, filename, filename
);
return 0; return 0;
} }

View file

@ -708,12 +708,15 @@ void scripting::load_content_script(
} }
void scripting::load_entity_component( void scripting::load_entity_component(
const std::string& name, const io::path& file, const std::string& fileName const scriptenv& env,
const std::string& name,
const io::path& file,
const std::string& fileName
) { ) {
auto L = lua::get_main_state(); auto L = lua::get_main_state();
std::string src = io::read_string(file); std::string src = io::read_string(file);
logger.info() << "script (component) " << file.string(); logger.info() << "script (component) " << file.string();
lua::loadbuffer(L, 0, src, fileName); lua::loadbuffer(L, *env, src, fileName);
lua::store_in(L, lua::CHUNKS_TABLE, name); lua::store_in(L, lua::CHUNKS_TABLE, name);
} }

View file

@ -186,10 +186,12 @@ namespace scripting {
); );
/// @brief Load component script /// @brief Load component script
/// @param env environment
/// @param name component full name (packid:name) /// @param name component full name (packid:name)
/// @param file component script file path /// @param file component script file path
/// @param fileName script file path using the engine format /// @param fileName script file path using the engine format
void load_entity_component( void load_entity_component(
const scriptenv& env,
const std::string& name, const std::string& name,
const io::path& file, const io::path& file,
const std::string& fileName const std::string& fileName

View file

@ -33,7 +33,9 @@ namespace util {
std::shared_ptr<T> create(Args&&... args) { std::shared_ptr<T> create(Args&&... args) {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
if (freeObjects.empty()) { if (freeObjects.empty()) {
allocateNew(); if (!allocateNew()) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
} }
auto ptr = freeObjects.front(); auto ptr = freeObjects.front();
freeObjects.pop(); freeObjects.pop();
@ -49,7 +51,7 @@ namespace util {
std::queue<void*> freeObjects; std::queue<void*> freeObjects;
std::mutex mutex; std::mutex mutex;
void allocateNew() { bool allocateNew() {
std::unique_ptr<void, AlignedDeleter> ptr( std::unique_ptr<void, AlignedDeleter> ptr(
#if defined(_WIN32) #if defined(_WIN32)
_aligned_malloc(sizeof(T), alignof(T)) _aligned_malloc(sizeof(T), alignof(T))
@ -57,8 +59,12 @@ namespace util {
std::aligned_alloc(alignof(T), sizeof(T)) std::aligned_alloc(alignof(T), sizeof(T))
#endif #endif
); );
if (ptr == nullptr) {
return false;
}
freeObjects.push(ptr.get()); freeObjects.push(ptr.get());
objects.push_back(std::move(ptr)); objects.push_back(std::move(ptr));
return true;
} }
}; };
} }