-
-
Notifications
You must be signed in to change notification settings - Fork 15
fix(Model): check new component uuid wrt other actual uuids in a model #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,141 +43,141 @@ | |
| #include <geode/model/mixin/core/bitsery_archive.hpp> | ||
| #include <geode/model/mixin/core/component_type.hpp> | ||
|
|
||
| namespace geode | ||
| namespace geode::detail | ||
| { | ||
| namespace detail | ||
| template < typename Component > | ||
| class ComponentsStorage | ||
| { | ||
| template < typename Component > | ||
| class ComponentsStorage | ||
| { | ||
| public: | ||
| using ComponentPtr = std::unique_ptr< Component >; | ||
| using ComponentsStore = absl::linked_hash_map< uuid, ComponentPtr >; | ||
| using Iterator = typename ComponentsStore::const_iterator; | ||
| public: | ||
| using ComponentPtr = std::unique_ptr< Component >; | ||
| using ComponentsStore = absl::linked_hash_map< uuid, ComponentPtr >; | ||
| using Iterator = typename ComponentsStore::const_iterator; | ||
|
|
||
| [[nodiscard]] index_t nb_components() const | ||
| { | ||
| return components_.size(); | ||
| } | ||
| [[nodiscard]] index_t nb_components() const | ||
| { | ||
| return components_.size(); | ||
| } | ||
|
|
||
| [[nodiscard]] bool has_component( const uuid& id ) const | ||
| { | ||
| return components_.contains( id ); | ||
| } | ||
| [[nodiscard]] bool has_component( const uuid& component_id ) const | ||
| { | ||
| return components_.contains( component_id ); | ||
| } | ||
|
|
||
| [[nodiscard]] const Component& component( const uuid& id ) const | ||
| { | ||
| return *components_.at( id ); | ||
| } | ||
| [[nodiscard]] const Component& component( | ||
| const uuid& component_id ) const | ||
| { | ||
| return *components_.at( component_id ); | ||
| } | ||
|
|
||
| [[nodiscard]] Component& component( const uuid& id ) | ||
| { | ||
| return *components_.at( id ); | ||
| } | ||
| [[nodiscard]] Component& component( const uuid& component_id ) | ||
| { | ||
| return *components_.at( component_id ); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator begin() const | ||
| { | ||
| return components_.begin(); | ||
| } | ||
| [[nodiscard]] Iterator begin() const | ||
| { | ||
| return components_.begin(); | ||
| } | ||
|
|
||
| [[nodiscard]] Iterator end() const | ||
| { | ||
| return components_.end(); | ||
| } | ||
| [[nodiscard]] Iterator end() const | ||
| { | ||
| return components_.end(); | ||
| } | ||
|
|
||
| void add_component( ComponentPtr component ) | ||
| { | ||
| void add_component( ComponentPtr component ) | ||
| { | ||
| const auto [itr, new_uuid] = | ||
| components_.emplace( component->id(), std::move( component ) ); | ||
| } | ||
|
|
||
| void save_components( std::string_view filename ) const | ||
| { | ||
| std::ofstream file{ to_string( filename ), | ||
| std::ofstream::binary }; | ||
| TContext context{}; | ||
| BitseryExtensions::register_serialize_pcontext( | ||
| std::get< 0 >( context ) ); | ||
| Serializer archive{ context, file }; | ||
| archive.object( *this ); | ||
| archive.adapter().flush(); | ||
| OpenGeodeModelException::check_exception( | ||
| std::get< 1 >( context ).isValid(), nullptr, | ||
| OpenGeodeException::TYPE::internal, | ||
| "[ComponentsStorage::save_components] Error while writing " | ||
| "file: ", | ||
| filename ); | ||
| } | ||
| OpenGeodeModelException::check_exception( new_uuid, nullptr, | ||
| OpenGeodeException::TYPE::internal, | ||
| "[ComponentsStorage::add_component] Component with id ", | ||
| itr->first.string(), " already exists" ); | ||
| } | ||
|
|
||
| void delete_component( const uuid& id ) | ||
| { | ||
| components_.erase( components_.find( id ) ); | ||
| } | ||
| void save_components( std::string_view filename ) const | ||
| { | ||
| std::ofstream file{ to_string( filename ), std::ofstream::binary }; | ||
| TContext context{}; | ||
| BitseryExtensions::register_serialize_pcontext( | ||
| std::get< 0 >( context ) ); | ||
| Serializer archive{ context, file }; | ||
| archive.object( *this ); | ||
| archive.adapter().flush(); | ||
| OpenGeodeModelException::check_exception( | ||
| std::get< 1 >( context ).isValid(), nullptr, | ||
| OpenGeodeException::TYPE::internal, | ||
| "[ComponentsStorage::save_components] Error while writing " | ||
| "file: ", | ||
| filename ); | ||
| } | ||
|
|
||
| void delete_component( const uuid& component_id ) | ||
| { | ||
| components_.erase( components_.find( component_id ) ); | ||
| } | ||
|
|
||
| void load_components( std::string_view filename ) | ||
| void load_components( std::string_view filename ) | ||
| { | ||
| if( !std::filesystem::exists( to_string( filename ) ) ) | ||
| { | ||
| if( !std::filesystem::exists( to_string( filename ) ) ) | ||
| { | ||
| return; | ||
| } | ||
| std::ifstream file{ to_string( filename ), | ||
| std::ifstream::binary }; | ||
| TContext context{}; | ||
| BitseryExtensions::register_deserialize_pcontext( | ||
| std::get< 0 >( context ) ); | ||
| Deserializer archive{ context, file }; | ||
| archive.object( *this ); | ||
| const auto& adapter = archive.adapter(); | ||
| OpenGeodeModelException::check_exception( | ||
| adapter.error() == bitsery::ReaderError::NoError | ||
| && adapter.isCompletedSuccessfully() | ||
| && std::get< 1 >( context ).isValid(), | ||
| nullptr, OpenGeodeException::TYPE::internal, | ||
| "[ComponentsStorage::load_components] Error while reading " | ||
| "file: ", | ||
| filename ); | ||
| return; | ||
| } | ||
|
|
||
| [[nodiscard]] absl::flat_hash_map< std::string, std::string > | ||
| file_mapping( std::string_view directory ) const | ||
| std::ifstream file{ to_string( filename ), std::ifstream::binary }; | ||
| TContext context{}; | ||
| BitseryExtensions::register_deserialize_pcontext( | ||
| std::get< 0 >( context ) ); | ||
| Deserializer archive{ context, file }; | ||
| archive.object( *this ); | ||
| const auto& adapter = archive.adapter(); | ||
| OpenGeodeModelException::check_exception( | ||
| adapter.error() == bitsery::ReaderError::NoError | ||
| && adapter.isCompletedSuccessfully() | ||
| && std::get< 1 >( context ).isValid(), | ||
| nullptr, OpenGeodeException::TYPE::internal, | ||
| "[ComponentsStorage::load_components] Error while reading " | ||
| "file: ", | ||
| filename ); | ||
| } | ||
|
|
||
| [[nodiscard]] absl::flat_hash_map< std::string, std::string > | ||
| file_mapping( std::string_view directory ) const | ||
| { | ||
| absl::flat_hash_map< std::string, std::string > mapping; | ||
| for( const auto& file : | ||
| std::filesystem::directory_iterator( to_string( directory ) ) ) | ||
| { | ||
| absl::flat_hash_map< std::string, std::string > mapping; | ||
| for( const auto& file : std::filesystem::directory_iterator( | ||
| to_string( directory ) ) ) | ||
| auto path = file.path(); | ||
| auto filename = path.replace_extension( "" ).string(); | ||
| if( filename.size() > 36 ) | ||
|
Check warning on line 151 in include/geode/model/mixin/core/detail/components_storage.hpp
|
||
| { | ||
| auto path = file.path(); | ||
| auto filename = path.replace_extension( "" ).string(); | ||
| if( filename.size() > 36 ) | ||
| { | ||
| auto uuid = filename.substr( filename.size() - 36 ); | ||
| mapping.emplace( | ||
| std::move( uuid ), file.path().string() ); | ||
| } | ||
| auto uuid = filename.substr( filename.size() - 36 ); | ||
|
Check warning on line 153 in include/geode/model/mixin/core/detail/components_storage.hpp
|
||
| mapping.emplace( std::move( uuid ), file.path().string() ); | ||
| } | ||
| return mapping; | ||
| } | ||
| return mapping; | ||
| } | ||
|
|
||
| private: | ||
| friend class bitsery::Access; | ||
| template < typename Archive > | ||
| void serialize( Archive& serializer ) | ||
| { | ||
| serializer.ext( *this, | ||
| Growable< Archive, ComponentsStorage >{ | ||
| { []( Archive& archive, ComponentsStorage& storage ) { | ||
| archive.ext( storage.components_, | ||
| bitsery::ext::StdMap{ | ||
| storage.components_.max_size() }, | ||
| []( Archive& archive2, uuid& id, | ||
| ComponentPtr& item ) { | ||
| archive2.object( id ); | ||
| archive2.ext( | ||
| item, bitsery::ext::StdSmartPtr{} ); | ||
| } ); | ||
| } } } ); | ||
| } | ||
|
|
||
| private: | ||
| ComponentsStore components_; | ||
| }; | ||
| } // namespace detail | ||
| } // namespace geode | ||
| private: | ||
| friend class bitsery::Access; | ||
| template < typename Archive > | ||
| void serialize( Archive& serializer ) | ||
| { | ||
| serializer.ext( *this, | ||
| Growable< Archive, ComponentsStorage >{ | ||
| { []( Archive& archive, ComponentsStorage& storage ) { | ||
| archive.ext( storage.components_, | ||
| bitsery::ext::StdMap{ | ||
| storage.components_.max_size() }, | ||
| []( Archive& archive2, uuid& component_id, | ||
| ComponentPtr& item ) { | ||
| archive2.object( component_id ); | ||
| archive2.ext( | ||
| item, bitsery::ext::StdSmartPtr{} ); | ||
|
Check failure on line 175 in include/geode/model/mixin/core/detail/components_storage.hpp
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing bitsery/ext/std_smart_ptr.h include ? |
||
| } ); | ||
| } } } ); | ||
| } | ||
|
|
||
| private: | ||
| ComponentsStore components_; | ||
| }; | ||
| } // namespace geode::detail | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know how to decide if this is an internal or data error. This code cannot work because of its input parameters. Is it internal?