#include "simsbasic.h" UniException::UniException(const QString& msg) :_error_store(msg) { } QString UniException::content() const noexcept { return _error_store; } RtEntity::RtEntity() {} void RtEntity::resetTemplet(const QString& name) { this->_templet_name = name; } QString RtEntity::templetName() const { return this->_templet_name; } void RtEntity::resetID(uint64_t id) { this->_entity_id = id; } uint64_t RtEntity::entityID() const { return this->_entity_id; } void RtEntity::resetName(const QString& name) { this->_runtime_name = name; } QString RtEntity::name() const { return this->_runtime_name; } void RtEntity::append(std::shared_ptr ins) { if (this->_comps_list.contains(ins->name())) return; ins->bindEntity(this->shared_from_this()); this->_comps_list[ins->name()] = ins; } void RtEntity::remove(const QString& component_type) { this->_comps_list.remove(component_type); } QList> RtEntity::components() const { return _comps_list.values(); } std::shared_ptr RtEntity::defaultNew() const { auto newx = std::make_shared(); newx->_entity_id = this->_entity_id; newx->_templet_name = this->_templet_name; newx->_runtime_name = this->_runtime_name; for (auto c : this->_comps_list) newx->append(c->defaultNew()); return newx; } #include QList RtEntity::inputTypes() const { QList types; for (auto c : this->_comps_list) types.append(c->inputTypes()); return types.toSet().toList(); } #include #include QList RtEntity::getEntryWithSignature(const WsRespondSignatureType& t) const { QList list; for (auto c : this->_comps_list) list.append(c->getEntryWithSignature(t)); return list; } QList RtEntity::getEntryWithInType(const QString& msg_type) const { QList list; for (auto c : this->_comps_list) list.append(c->getEntrysWithInType(msg_type)); return list; } void RtEntity::recoveryFrom(const QJsonObject& obj) { this->_entity_id = obj["entity_id"].toVariant().toULongLong(); this->_runtime_name = obj["entity_name"].toString(); this->_templet_name = obj["entity_templet"].toString(); auto array = obj["component_array"].toArray(); for (auto idx = 0; idx < array.size(); ++idx) { auto value = array.at(idx).toObject(); auto type = value["component_type"].toString(); auto compins = this->_comps_list[type]; if (!compins) throw new UniException(type + "类型插件不存在!"); compins->recoveryFrom(value); } } void RtEntity::saveTo(QJsonObject& obj) const { obj["entity_id"] = QJsonValue::fromVariant(QVariant::fromValue(this->_entity_id)); obj["entity_name"] = this->_runtime_name; obj["entity_templet"] = this->_templet_name; QJsonArray array; for (auto c : this->_comps_list) { QJsonObject comp_obj; comp_obj["component_type"] = c->name(); c->saveTo(comp_obj); array.append(comp_obj); } obj["component_array"] = array; } QString EntitiesManager::templetName() const { return "实体管理器"; } uint64_t EntitiesManager::entityID() const { return const_id; } QString EntitiesManager::name() const { return "实体管理器唯一实例"; } void EntitiesManager::append(std::shared_ptr ins) { _comps_list[ins->name()] = ins; } std::shared_ptr EntitiesManager::defaultNew() const { return nullptr; } QList EntitiesManager::getEntryWithSignature(const WsRespondSignatureType& t) const { QList list; for (auto c : this->_comps_list) list.append(c->getEntryWithSignature(t)); return list; } QList EntitiesManager::getEntryWithInType(const QString& msg_type) const { QList list; for (auto c : this->_comps_list) list.append(c->getEntrysWithInType(msg_type)); return list; } void EntitiesManager::recoveryFrom(const QJsonObject& obj) { auto array = obj["component_array"].toArray(); for (auto idx = 0; idx < array.size(); ++idx) { auto value = array.at(idx).toObject(); auto type = value["component_type"].toString(); auto compins = this->_comps_list[type]; if (!compins) throw new UniException(type + "类型插件不存在!"); compins->recoveryFrom(value); } } void EntitiesManager::saveTo(QJsonObject& obj) const { QJsonArray array; for (auto c : this->_comps_list) { QJsonObject comp_obj; comp_obj["component_type"] = c->name(); c->saveTo(comp_obj); array.append(comp_obj); } obj["component_array"] = array; } void EntitiesManager::remove(const QString& component_type) { this->_comps_list.remove(component_type); } QList> EntitiesManager::components() const { return this->_comps_list.values(); } ImmediateCore::ImmediateCore(std::shared_ptr bind) :_bind_entity(bind) {} uint64_t ImmediateCore::entityManagerID() const { return EntitiesManager::const_id; } QList> ImmediateCore::execute(const WsRespondSignatureType& resp_signature, std::shared_ptr in) { QList> rets; auto list = this->_bind_entity->getEntryWithSignature(resp_signature); for (auto func : list) { func(shared_from_this(), in, rets); } return rets; } QList> ImmediateCore::execute(std::shared_ptr in) { QList> rets; auto list = this->_bind_entity->getEntryWithInType(in->topicString()); for (auto func : list) { func(shared_from_this(), in, rets); } return rets; }