SimsWorld/SimsBasic/simsbasic.cpp

220 lines
5.1 KiB
C++
Raw Normal View History

2025-05-25 04:43:37 +00:00
#include "simsbasic.h"
UniException::UniException(const QString& msg)
:_error_store(msg) {
}
QString UniException::content() const noexcept
{
return _error_store;
}
2025-06-07 15:58:50 +00:00
RtEntity::RtEntity() {}
2025-05-25 04:43:37 +00:00
2025-06-07 15:58:50 +00:00
void RtEntity::resetTemplet(const QString& name) {
2025-05-25 04:43:37 +00:00
this->_templet_name = name;
}
2025-06-07 15:58:50 +00:00
QString RtEntity::templetName() const {
2025-05-25 04:43:37 +00:00
return this->_templet_name;
}
2025-06-07 15:58:50 +00:00
void RtEntity::resetID(uint64_t id) {
2025-05-25 04:43:37 +00:00
this->_entity_id = id;
}
2025-06-07 15:58:50 +00:00
uint64_t RtEntity::entityID() const {
2025-05-25 04:43:37 +00:00
return this->_entity_id;
}
2025-06-07 15:58:50 +00:00
void RtEntity::resetName(const QString& name) {
2025-05-25 04:43:37 +00:00
this->_runtime_name = name;
}
2025-06-07 15:58:50 +00:00
QString RtEntity::name() const {
2025-05-25 04:43:37 +00:00
return this->_runtime_name;
}
2025-06-07 15:58:50 +00:00
void RtEntity::append(std::shared_ptr<WsComponent> ins) {
2025-05-25 04:43:37 +00:00
if (this->_comps_list.contains(ins->name()))
return;
this->_comps_list[ins->name()] = ins;
}
2025-06-07 15:58:50 +00:00
void RtEntity::remove(const QString& component_type) {
2025-05-25 04:43:37 +00:00
this->_comps_list.remove(component_type);
}
2025-06-07 15:58:50 +00:00
QList<std::shared_ptr<WsComponent>> RtEntity::components() const {
2025-05-25 04:43:37 +00:00
return _comps_list.values();
}
2025-06-07 15:58:50 +00:00
std::shared_ptr<WsEntity> RtEntity::defaultNew() const {
auto newx = std::make_shared<RtEntity>();
2025-05-25 04:43:37 +00:00
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 <QSet>
2025-06-07 15:58:50 +00:00
QList<QString> RtEntity::inputTypes() const
2025-05-25 04:43:37 +00:00
{
QList<QString> types;
for (auto c : this->_comps_list)
types.append(c->inputTypes());
return types.toSet().toList();
}
#include <QVariant>
#include <QJsonArray>
2025-06-07 15:58:50 +00:00
QList<WsRespondEntry> RtEntity::getEntryWithSignature(const WsRespondSignatureType& t) const
2025-05-25 04:43:37 +00:00
{
QList<WsRespondEntry> list;
2025-06-07 15:58:50 +00:00
for (auto c : this->_comps_list)
2025-05-25 04:43:37 +00:00
list.append(c->getEntryWithSignature(t));
return list;
}
2025-06-07 15:58:50 +00:00
QList<WsRespondEntry> RtEntity::getEntryWithInType(const QString& msg_type) const
2025-05-25 04:43:37 +00:00
{
QList<WsRespondEntry> list;
2025-06-07 15:58:50 +00:00
for (auto c : this->_comps_list)
2025-05-25 04:43:37 +00:00
list.append(c->getEntrysWithInType(msg_type));
return list;
}
2025-06-07 15:58:50 +00:00
void RtEntity::recoveryFrom(const QJsonObject& obj)
2025-05-25 04:43:37 +00:00
{
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 + "<EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>");
compins->recoveryFrom(value);
}
}
2025-06-07 15:58:50 +00:00
void RtEntity::saveTo(QJsonObject& obj) const
2025-05-25 04:43:37 +00:00
{
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;
}
2025-06-07 15:58:50 +00:00
QString EntitiesManager::templetName() const
{
return "ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
}
uint64_t EntitiesManager::entityID() const
{
return const_id;
}
QString EntitiesManager::name() const
{
return "ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ψһʵ<EFBFBD><EFBFBD>";
}
void EntitiesManager::append(std::shared_ptr<WsComponent> ins) {
_comps_list[ins->name()] = ins;
}
std::shared_ptr<WsEntity> EntitiesManager::defaultNew() const
{
return nullptr;
}
QList<WsRespondEntry> EntitiesManager::getEntryWithSignature(const WsRespondSignatureType& t) const
{
QList<WsRespondEntry> list;
for (auto c : this->_comps_list)
list.append(c->getEntryWithSignature(t));
return list;
}
QList<WsRespondEntry> EntitiesManager::getEntryWithInType(const QString& msg_type) const
{
QList<WsRespondEntry> 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 + "<EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>");
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;
}
ImmediateCore::ImmediateCore(std::shared_ptr<WsEntity> bind) :_bind_entity(bind) {}
uint64_t ImmediateCore::entityManagerID() const
{
return EntitiesManager::const_id;
}
QList<std::shared_ptr<WsMessage>> ImmediateCore::execute(const WsRespondSignatureType& resp_signature, std::shared_ptr<const WsMessage> in)
{
QList<std::shared_ptr<WsMessage>> rets;
auto list = this->_bind_entity->getEntryWithSignature(resp_signature);
for (auto func : list) {
func(shared_from_this(), in, rets);
2025-06-07 15:58:50 +00:00
}
return rets;
}
QList<std::shared_ptr<WsMessage>> ImmediateCore::execute(std::shared_ptr<const WsMessage> in)
{
QList<std::shared_ptr<WsMessage>> rets;
auto list = this->_bind_entity->getEntryWithInType(in->topicString());
for (auto func : list) {
func(shared_from_this(), in, rets);
2025-06-07 15:58:50 +00:00
}
return rets;
}