18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#ifndef SCENEPLUGIN_ECSSERIALIZER_H 178bf80f4bSopenharmony_ci#define SCENEPLUGIN_ECSSERIALIZER_H 188bf80f4bSopenharmony_ci 198bf80f4bSopenharmony_ci#include <functional> 208bf80f4bSopenharmony_ci 218bf80f4bSopenharmony_ci#include <base/containers/unique_ptr.h> 228bf80f4bSopenharmony_ci#include <base/containers/unordered_map.h> 238bf80f4bSopenharmony_ci#include <base/containers/vector.h> 248bf80f4bSopenharmony_ci#include <core/property/property.h> 258bf80f4bSopenharmony_ci#include <render/intf_render_context.h> 268bf80f4bSopenharmony_ci 278bf80f4bSopenharmony_ci#include <scene_plugin/interface/intf_ecs_serializer.h> 288bf80f4bSopenharmony_ci 298bf80f4bSopenharmony_ciSCENE_BEGIN_NAMESPACE() 308bf80f4bSopenharmony_ci 318bf80f4bSopenharmony_ciclass EcsSerializer : public IEcsSerializer { 328bf80f4bSopenharmony_cipublic: 338bf80f4bSopenharmony_ci EcsSerializer(RENDER_NS::IRenderContext& renderContext); 348bf80f4bSopenharmony_ci void Destroy() override; 358bf80f4bSopenharmony_ci // 368bf80f4bSopenharmony_ci // From IEcsSerializer 378bf80f4bSopenharmony_ci // 388bf80f4bSopenharmony_ci void SetListener(IListener* listener) override; 398bf80f4bSopenharmony_ci 408bf80f4bSopenharmony_ci void SetDefaultSerializers() override; 418bf80f4bSopenharmony_ci void SetSerializer(const CORE_NS::PropertyTypeDecl& type, IPropertySerializer& serializer) override; 428bf80f4bSopenharmony_ci 438bf80f4bSopenharmony_ci bool WriteEntityCollection(const IEntityCollection& ec, CORE_NS::json::standalone_value& jsonOut) const override; 448bf80f4bSopenharmony_ci bool WriteComponents( 458bf80f4bSopenharmony_ci const IEntityCollection& ec, CORE_NS::Entity entity, CORE_NS::json::standalone_value& jsonOut) const override; 468bf80f4bSopenharmony_ci bool WriteComponent(const IEntityCollection& ec, CORE_NS::Entity entity, const CORE_NS::IComponentManager& cm, 478bf80f4bSopenharmony_ci CORE_NS::IComponentManager::ComponentId id, CORE_NS::json::standalone_value& jsonOut) const override; 488bf80f4bSopenharmony_ci bool WriteProperty(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset, 498bf80f4bSopenharmony_ci CORE_NS::json::standalone_value& jsonOut) const override; 508bf80f4bSopenharmony_ci 518bf80f4bSopenharmony_ci bool GatherExternalCollections(const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri, 528bf80f4bSopenharmony_ci BASE_NS::vector<ExternalCollection>& externalCollectionsOut) const override; 538bf80f4bSopenharmony_ci 548bf80f4bSopenharmony_ci /*RUNTIME_NS::IIoUtil::SerializationResult*/ int ReadEntityCollection( 558bf80f4bSopenharmony_ci IEntityCollection& ec, const CORE_NS::json::value& jsonIn, BASE_NS::string_view contextUri) const override; 568bf80f4bSopenharmony_ci bool ReadComponents(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, bool setId) const override; 578bf80f4bSopenharmony_ci bool ReadComponent(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, CORE_NS::Entity entity, 588bf80f4bSopenharmony_ci CORE_NS::IComponentManager& component) const override; 598bf80f4bSopenharmony_ci bool ReadProperty(IEntityCollection& ec, const CORE_NS::json::value& jsonIn, const CORE_NS::Property& property, 608bf80f4bSopenharmony_ci uintptr_t offset) const override; 618bf80f4bSopenharmony_ci 628bf80f4bSopenharmony_ci RENDER_NS::RenderHandleReference LoadImageResource(BASE_NS::string_view uri) const override; 638bf80f4bSopenharmony_ci 648bf80f4bSopenharmony_ciprivate: 658bf80f4bSopenharmony_ci using PropertyToJsonFunc = std::function<bool( 668bf80f4bSopenharmony_ci const IEntityCollection& ec, const CORE_NS::Property&, uintptr_t, CORE_NS::json::standalone_value&)>; 678bf80f4bSopenharmony_ci using PropertyFromJsonFunc = std::function<bool( 688bf80f4bSopenharmony_ci const IEntityCollection& ec, const CORE_NS::json::value&, const CORE_NS::Property&, uintptr_t)>; 698bf80f4bSopenharmony_ci IPropertySerializer& Add(PropertyToJsonFunc toJson, PropertyFromJsonFunc fromJson); 708bf80f4bSopenharmony_ci 718bf80f4bSopenharmony_ci // 728bf80f4bSopenharmony_ci // A little wrapper class to create a serializer with functions for writing and reading a value. 738bf80f4bSopenharmony_ci // 748bf80f4bSopenharmony_ci class SimpleJsonSerializer : public IPropertySerializer { 758bf80f4bSopenharmony_ci public: 768bf80f4bSopenharmony_ci bool ToJson(const IEntityCollection& ec, const CORE_NS::Property& property, uintptr_t offset, 778bf80f4bSopenharmony_ci CORE_NS::json::standalone_value& jsonOut) const override; 788bf80f4bSopenharmony_ci bool FromJson(const IEntityCollection& ec, const CORE_NS::json::value& jsonIn, 798bf80f4bSopenharmony_ci const CORE_NS::Property& property, uintptr_t offset) const override; 808bf80f4bSopenharmony_ci 818bf80f4bSopenharmony_ci SimpleJsonSerializer(PropertyToJsonFunc toJson, PropertyFromJsonFunc fromJson); 828bf80f4bSopenharmony_ci virtual ~SimpleJsonSerializer() = default; 838bf80f4bSopenharmony_ci 848bf80f4bSopenharmony_ci private: 858bf80f4bSopenharmony_ci const PropertyToJsonFunc PropertyToJson; 868bf80f4bSopenharmony_ci const PropertyFromJsonFunc PropertyFromJson; 878bf80f4bSopenharmony_ci }; 888bf80f4bSopenharmony_ci BASE_NS::vector<BASE_NS::unique_ptr<SimpleJsonSerializer>> ownedSerializers_; 898bf80f4bSopenharmony_ci 908bf80f4bSopenharmony_ci RENDER_NS::IRenderContext& renderContext_; 918bf80f4bSopenharmony_ci 928bf80f4bSopenharmony_ci // 938bf80f4bSopenharmony_ci // Mapping from each property type to a specific serializer. 948bf80f4bSopenharmony_ci // 958bf80f4bSopenharmony_ci using SerializerMap = BASE_NS::unordered_map<CORE_NS::PropertyTypeDecl, IPropertySerializer*>; 968bf80f4bSopenharmony_ci SerializerMap typetoSerializerMap_; 978bf80f4bSopenharmony_ci 988bf80f4bSopenharmony_ci IListener* listener_ {}; 998bf80f4bSopenharmony_ci}; 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_ciSCENE_END_NAMESPACE() 1028bf80f4bSopenharmony_ci 1038bf80f4bSopenharmony_ci#endif // SCENEPLUGIN_ECSSERIALIZER_H 104