|
1 | 1 | #include "Serialization.hpp" |
2 | | -#include "Serialization.hpp" |
3 | | -#include "Serialization.hpp" |
4 | 2 | #include "../Content/AssetID.hpp" |
5 | 3 | #include "../Objects/Impl/Objects.hpp" |
6 | 4 | #include "../Objects/ObjectList.hpp" |
| 5 | +#include "../Reflection/Event.hpp" |
7 | 6 | #include <fstream> |
| 7 | +#include "EventInvocationRepresentation.hpp" |
8 | 8 |
|
9 | 9 | using namespace Engine3DRadSpace; |
10 | 10 | using namespace Engine3DRadSpace::Content; |
@@ -33,6 +33,48 @@ static void deserializeObjectHierarchy(ObjectList* lst, IObject* obj, const json |
33 | 33 | } |
34 | 34 | } |
35 | 35 |
|
| 36 | +static void validateEvents(IObject* obj, ObjectList* lst) |
| 37 | +{ |
| 38 | + auto refl = GetReflDataFromUUID(obj->GetUUID()); |
| 39 | + |
| 40 | + std::vector<std::pair<EventInvocationRepresentation, IReflectedFunction*>> invocations; |
| 41 | + |
| 42 | + for (auto& field : *refl) |
| 43 | + { |
| 44 | + auto repr = field->Representation(); |
| 45 | + |
| 46 | + for (auto& [reprType, sFieldName] : repr) |
| 47 | + { |
| 48 | + if (reprType == FieldRepresentationType::Event) |
| 49 | + { |
| 50 | + auto event = std::launder<Event>(reinterpret_cast<Event*>(reinterpret_cast<std::byte*>(obj) + field->FieldOffset())); |
| 51 | + |
| 52 | + for (auto& binding : *event) |
| 53 | + { |
| 54 | + EventInvocationRepresentation invocRepr; |
| 55 | + invocRepr.FunctionID = binding.FunctionID; |
| 56 | + invocRepr.OwnerObject = binding.ObjectID; |
| 57 | + |
| 58 | + auto reflObj = GetReflDataFromUUID(lst->operator[](binding.ObjectID)->GetUUID()); |
| 59 | + invocations.emplace_back(invocRepr, invocRepr.FindFunction(reflObj, binding.FunctionID)); |
| 60 | + } |
| 61 | + |
| 62 | + event->Reset(); |
| 63 | + |
| 64 | + for (auto& [invocRepr, fn] : invocations) |
| 65 | + { |
| 66 | + if (fn == nullptr) continue; |
| 67 | + auto objPtr = lst->operator[](invocRepr.OwnerObject); |
| 68 | + |
| 69 | + auto fn2 = dynamic_cast<IReflectedFunction*>(fn->Clone().release()); |
| 70 | + event->Bind(std::move(std::unique_ptr<IReflectedFunction>(fn2)), invocRepr.OwnerObject, invocRepr.FunctionID); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | +} |
| 77 | + |
36 | 78 | static void serializeObjectHierarchy(std::unordered_map<IObject*, int> dictPtrID, ObjectList* lst, IObject* obj, json& j) |
37 | 79 | { |
38 | 80 | json children = json::array(); |
@@ -98,10 +140,18 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj) |
98 | 140 | intptr_t offset = 0; |
99 | 141 | auto repr = field->Representation(); |
100 | 142 | bool useDirectGet = (field->FieldOffset() == 0 && repr.Size() == 1); |
| 143 | + |
| 144 | + if (field->TypeSize() == 0 || repr.Size() == 0) |
| 145 | + continue; |
| 146 | + |
101 | 147 | for (int i = 0; auto &[reprType, sFieldName] : repr) |
102 | 148 | { |
103 | | - if(reprType == FieldRepresentationType::None) continue; |
104 | | - if(reprType == FieldRepresentationType::Function) continue; |
| 149 | + if ((reprType == FieldRepresentationType::None) || |
| 150 | + (reprType == FieldRepresentationType::Function)) |
| 151 | + { |
| 152 | + ++i; |
| 153 | + continue; |
| 154 | + } |
105 | 155 |
|
106 | 156 | auto subFieldName = !sFieldName.empty() ? sFieldName : "f"; |
107 | 157 |
|
@@ -307,10 +357,17 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj) |
307 | 357 | } |
308 | 358 | case FieldRepresentationType::Event: |
309 | 359 | { |
310 | | - //Projects::EventRepresentation e; |
311 | | - //jsonField[subFieldName][str_i] = e; |
| 360 | + auto event = std::launder<Event>(reinterpret_cast<Event*>(static_cast<std::byte*>(objPtr) + field->FieldOffset() + offset)); |
312 | 361 |
|
313 | | - //offset += sizeof(Event); |
| 362 | + jsonField[subFieldName][str_i]["NumInvocations"] = event->Count(); |
| 363 | + |
| 364 | + for (auto k = 0; k < event->Count(); k++) |
| 365 | + { |
| 366 | + jsonField[subFieldName][str_i]["Invocations"][std::to_string(k)]["OwnerObject"] = event->At(k).ObjectID; |
| 367 | + jsonField[subFieldName][str_i]["Invocations"][std::to_string(k)]["FunctionID"] = event->At(k).FunctionID; |
| 368 | + } |
| 369 | + |
| 370 | + offset += sizeof(Event); |
314 | 371 | break; |
315 | 372 | } |
316 | 373 | case FieldRepresentationType::ObjectID: |
@@ -350,6 +407,9 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj) |
350 | 407 | auto newStruct = std::make_unique<uint8_t[]>(structSize); |
351 | 408 | int offset = 0; |
352 | 409 |
|
| 410 | + if (field->TypeSize() == 0 || repr.Size() == 0) |
| 411 | + continue; |
| 412 | + |
353 | 413 | bool useDirectSet = (field->FieldOffset() == 0 && repr.Size() == 1); |
354 | 414 |
|
355 | 415 | for (int i = 0; auto & [reprType, sFieldName] : repr) |
@@ -632,13 +692,29 @@ json Engine3DRadSpace::Projects::Serializer::SerializeObject(IObject* obj) |
632 | 692 | } |
633 | 693 | case FieldRepresentationType::Event: |
634 | 694 | { |
635 | | - //EventRepresentation eventData = jsonField.get<EventRepresentation>(); |
636 | | - //Event event = eventData.Reconstruct(uuid).value(); |
| 695 | + auto numInvocations = jsonField["NumInvocations"].get<size_t>(); |
| 696 | + |
| 697 | + Event event; |
637 | 698 |
|
638 | | - //void* dest = newStruct.get() + offset; |
639 | | - //new (dest) Event(); |
640 | | - //offset += sizeof(Event); |
| 699 | + for (auto i = 0; i < numInvocations; i++) |
| 700 | + { |
| 701 | + auto invocation = jsonField["Invocations"][std::to_string(i)]; |
| 702 | + |
| 703 | + EventInvocationRepresentation repr; |
| 704 | + |
| 705 | + repr.OwnerObject = invocation["OwnerObject"].get<size_t>(); |
| 706 | + repr.FunctionID = invocation["FunctionID"].get<size_t>(); |
| 707 | + |
| 708 | + //TODO: Support arguments. |
| 709 | + |
| 710 | + event.BindIncomplete( |
| 711 | + repr.OwnerObject, |
| 712 | + repr.FunctionID |
| 713 | + ); |
| 714 | + } |
641 | 715 |
|
| 716 | + new (newStruct.get() + offset) Event(std::move(event)); |
| 717 | + offset += sizeof(Event); |
642 | 718 | break; |
643 | 719 | } |
644 | 720 | case FieldRepresentationType::ObjectID: |
@@ -696,6 +772,7 @@ bool Engine3DRadSpace::Projects::Serializer::LoadProject(ObjectList* lst, Conten |
696 | 772 | { |
697 | 773 | auto obj = lst->operator[](i); |
698 | 774 | deserializeObjectHierarchy(lst, obj, j["objects"][std::to_string(i)]); |
| 775 | + validateEvents(obj, lst); |
699 | 776 | } |
700 | 777 |
|
701 | 778 | return true; |
|
0 commit comments