Compare commits
2 Commits
b1becb2128
...
c284e0779e
Author | SHA1 | Date | |
---|---|---|---|
c284e0779e | |||
491c30988e |
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2025 Erik Scholz
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -1,5 +1,7 @@
|
||||
#include "./message_serializer.hpp"
|
||||
|
||||
#include <solanaceae/contact/contact_store_impl.hpp>
|
||||
|
||||
#include <solanaceae/message3/components.hpp>
|
||||
#include <solanaceae/contact/components.hpp>
|
||||
#include <solanaceae/object_store/meta_components.hpp>
|
||||
@ -8,18 +10,9 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
static Contact3 findContactByID(Contact3Registry& cr, const std::vector<uint8_t>& id) {
|
||||
// TODO: id lookup table, this is very inefficent
|
||||
for (const auto& [c_it, id_it] : cr.view<Contact::Components::ID>().each()) {
|
||||
if (id == id_it.data) {
|
||||
return c_it;
|
||||
}
|
||||
}
|
||||
nlohmann::json MessageSerializerNJ::serlContactByID(Contact4 c) const {
|
||||
const auto& cr = cs.registry();
|
||||
|
||||
return entt::null;
|
||||
}
|
||||
|
||||
nlohmann::json MessageSerializerNJ::serlContactByID(Contact3 c) const {
|
||||
if (!cr.valid(c)) {
|
||||
// while this is invalid registry state, it is valid serialization
|
||||
std::cerr << "MSC warning: encountered invalid contact\n";
|
||||
@ -35,7 +28,7 @@ nlohmann::json MessageSerializerNJ::serlContactByID(Contact3 c) const {
|
||||
return nlohmann::json::binary(cr.get<Contact::Components::ID>(c).data);
|
||||
}
|
||||
|
||||
Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
|
||||
Contact4 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
|
||||
std::vector<uint8_t> id;
|
||||
if (j.is_binary()) {
|
||||
id = j.get_binary();
|
||||
@ -43,11 +36,13 @@ Contact3 MessageSerializerNJ::deserlContactByID(const nlohmann::json& j) {
|
||||
j.at("bytes").get_to(id);
|
||||
}
|
||||
|
||||
Contact3 other_c = findContactByID(cr, id);
|
||||
if (!cr.valid(other_c)) {
|
||||
auto other_c = cs.getOneContactByID(ByteSpan{id});
|
||||
if (!other_c) {
|
||||
auto& cr = cs.registry();
|
||||
// create sparse contact with id only
|
||||
other_c = cr.create();
|
||||
other_c = {cr, cr.create()};
|
||||
cr.emplace_or_replace<Contact::Components::ID>(other_c, id);
|
||||
cs.throwEventConstruct(other_c);
|
||||
}
|
||||
|
||||
return other_c;
|
||||
@ -86,7 +81,7 @@ ObjectHandle MessageSerializerNJ::deserlFileObjByID(const nlohmann::json& j) {
|
||||
|
||||
template<>
|
||||
bool MessageSerializerNJ::component_get_json<Message::Components::ContactFrom>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
|
||||
const Contact3 c = h.get<Message::Components::ContactFrom>().c;
|
||||
const Contact4 c = h.get<Message::Components::ContactFrom>().c;
|
||||
j = msc.serlContactByID(c);
|
||||
|
||||
return true;
|
||||
@ -108,7 +103,7 @@ bool MessageSerializerNJ::component_emplace_or_replace_json<Message::Components:
|
||||
|
||||
template<>
|
||||
bool MessageSerializerNJ::component_get_json<Message::Components::ContactTo>(MessageSerializerNJ& msc, const Handle h, nlohmann::json& j) {
|
||||
const Contact3 c = h.get<Message::Components::ContactTo>().c;
|
||||
const Contact4 c = h.get<Message::Components::ContactTo>().c;
|
||||
j = msc.serlContactByID(c);
|
||||
|
||||
return true;
|
||||
@ -137,8 +132,10 @@ bool MessageSerializerNJ::component_get_json<Message::Components::ReceivedBy>(Me
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto& cr = msc.cs.registry();
|
||||
|
||||
for (const auto& [c, v] : comp.ts) {
|
||||
if (!msc.cr.valid(c) || !msc.cr.all_of<Contact::Components::ID>(c)) {
|
||||
if (!cr.valid(c) || !cr.all_of<Contact::Components::ID>(c)) {
|
||||
// while this is invalid registry state, it is valid serialization
|
||||
// we just skip this contact entirely and drop the time
|
||||
std::cerr << "MSC warning: encountered invalid contact / contact without ID\n";
|
||||
|
@ -12,9 +12,9 @@ struct MessageSerializerNJ {
|
||||
using Registry = Message3Registry;
|
||||
using Handle = Message3Handle;
|
||||
|
||||
static constexpr const char* version {"2"};
|
||||
static constexpr const char* version {"3"};
|
||||
|
||||
Contact3Registry& cr;
|
||||
ContactStore4I& cs;
|
||||
ObjectStore2& os;
|
||||
|
||||
// nlohmann
|
||||
@ -76,8 +76,8 @@ struct MessageSerializerNJ {
|
||||
|
||||
|
||||
// helper
|
||||
nlohmann::json serlContactByID(Contact3 c) const;
|
||||
Contact3 deserlContactByID(const nlohmann::json& j);
|
||||
nlohmann::json serlContactByID(Contact4 c) const;
|
||||
Contact4 deserlContactByID(const nlohmann::json& j);
|
||||
nlohmann::json serlFileObjByID(ObjectHandle o) const;
|
||||
ObjectHandle deserlFileObjByID(const nlohmann::json& j);
|
||||
};
|
||||
|
Reference in New Issue
Block a user