Fixed integer division by zero error when loading files

This commit is contained in:
Yousef Khadadeh 2026-04-03 14:18:04 +03:00
parent 58fb02f449
commit 97d308268d
19 changed files with 97 additions and 30 deletions

View file

@ -2,7 +2,9 @@
#include "DrawingProgramLayerManager.hpp"
#include "../DrawingProgram.hpp"
#include "../../World.hpp"
#include <Helpers/NetworkingObjects/NetObjOrderedList.hpp>
#include <Helpers/Parallel.hpp>
#include <Helpers/Logger.hpp>
void DrawingProgramLayer::draw(SkCanvas* canvas, const DrawData& drawData) const {
for(auto& p : *components)
@ -32,7 +34,7 @@ void DrawingProgramLayer::set_component_list_callbacks(DrawingProgramLayerListIt
components->set_insert_callback(insertCallback);
components->set_erase_callback(eraseCallback);
components->set_move_callback([&](const CanvasComponentContainer::ObjInfoIterator& c, uint32_t oldPos) {
layerMan.drawP.drawCache.invalidate_cache_at_aabb(c->obj->get_world_bounds());
layerMan.drawP.drawCache.invalidate_cache_at_optional_aabb(c->obj->get_world_bounds());
if(layerMan.drawP.selection.is_selected(&(*c)))
layerMan.drawP.selection.sort_selection(); // If item is selected, selected items will be unordered, so sort everything again
});
@ -67,6 +69,18 @@ void DrawingProgramLayer::load_file(cereal::PortableBinaryInputArchive& a, Versi
}
}
void DrawingProgramLayer::erase_invalid_components() {
std::vector<NetworkingObjects::NetObjOrderedListIterator<CanvasComponentContainer>> compsToErase;
for(auto it = components->begin(); it != components->end(); ++it) {
if(!it->obj->get_world_bounds().has_value())
compsToErase.emplace_back(it);
}
if(!compsToErase.empty()) {
Logger::get().log("INFO", "[DrawingProgramLayer::erase_invalid_components] Erased " + std::to_string(compsToErase.size()) + " invalid component(s) from layer");
components->erase_list(components, compsToErase);
}
}
void DrawingProgramLayer::save_file(cereal::PortableBinaryOutputArchive& a) const {
a(components->size());
for(auto& comp : *components)

View file

@ -25,6 +25,7 @@ class DrawingProgramLayer {
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void get_used_resources(std::unordered_set<NetworkingObjects::NetObjID>& resourceSet) const;
void erase_invalid_components();
private:
std::function<void(const CanvasComponentContainer::ObjInfoIterator& c)> eraseCallback;
};

View file

@ -75,6 +75,11 @@ void DrawingProgramLayerFolder::save_file(cereal::PortableBinaryOutputArchive& a
listItem.obj->save_file(a);
}
void DrawingProgramLayerFolder::erase_invalid_components() {
for(auto& listItem : *folderList)
listItem.obj->erase_invalid_components();
}
void DrawingProgramLayerFolder::get_used_resources(std::unordered_set<NetworkingObjects::NetObjID>& resourceSet) const {
for(auto& listItem : *folderList)
listItem.obj->get_used_resources(resourceSet);

View file

@ -23,4 +23,5 @@ class DrawingProgramLayerFolder {
void save_file(cereal::PortableBinaryOutputArchive& a) const;
void get_used_resources(std::unordered_set<NetworkingObjects::NetObjID>& resourceSet) const;
void erase_invalid_components();
};

View file

@ -271,6 +271,13 @@ DrawingProgramLayerListItemMetaInfo DrawingProgramLayerListItem::get_metainfo()
};
}
void DrawingProgramLayerListItem::erase_invalid_components() {
if(folderData)
folderData->erase_invalid_components();
else
layerData->erase_invalid_components();
}
void DrawingProgramLayerListItem::register_class(World& w) {
w.netObjMan.register_class<DrawingProgramLayerListItem, DrawingProgramLayerListItem, DrawingProgramLayerListItem, DrawingProgramLayerListItem>({
.writeConstructorFuncClient = write_constructor_func,

View file

@ -66,6 +66,8 @@ class DrawingProgramLayerListItem {
DrawingProgramLayerListItemMetaInfo get_metainfo() const;
uint32_t get_component_count() const;
void erase_invalid_components();
private:
static void write_constructor_func(const NetworkingObjects::NetObjTemporaryPtr<DrawingProgramLayerListItem>& o, cereal::PortableBinaryOutputArchive& a);
static void read_constructor_func(const NetworkingObjects::NetObjTemporaryPtr<DrawingProgramLayerListItem>& o, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>& c);

View file

@ -284,6 +284,7 @@ void DrawingProgramLayerManager::load_file(cereal::PortableBinaryInputArchive& a
parallel_loop_container(flattenedCompList, [&drawP = drawP](CanvasComponentContainer::ObjInfo* comp) {
comp->obj->commit_update_dont_invalidate_cache(drawP);
});
layerTreeRoot->erase_invalid_components();
drawP.rebuild_cache();
editingLayer = layerTreeRoot->get_folder().get_initial_editing_layer();
}