302 lines
9.6 KiB
C++
302 lines
9.6 KiB
C++
#include <iterator>
|
|
|
|
#include <cppbase/logger.hpp>
|
|
#include <cppbase/result.hpp>
|
|
#include <cppbase/strings.hpp>
|
|
|
|
#include <ikarus/objects/blueprint.h>
|
|
|
|
#include <objects/blueprint.hpp>
|
|
#include <objects/entity.hpp>
|
|
#include <persistence/function_context.hpp>
|
|
#include <persistence/project.hpp>
|
|
|
|
IkarusBlueprint::IkarusBlueprint(IkarusProject * project, IkarusId id):
|
|
IkarusObject{project, id} {}
|
|
|
|
IkarusBlueprint * ikarus_blueprint_create(struct IkarusProject * project, char const * name) {
|
|
LOG_INFO("creating new blueprint");
|
|
|
|
if (project == nullptr) {
|
|
LOG_ERROR("project is nullptr");
|
|
return nullptr;
|
|
}
|
|
|
|
auto * ctx = project->get_function_context();
|
|
|
|
if (name == nullptr) {
|
|
ctx->set_error("name is nullptr", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
|
return nullptr;
|
|
}
|
|
|
|
if (cppbase::is_empty_or_blank(name)) {
|
|
ctx->set_error("name is empty or blank", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
|
return nullptr;
|
|
}
|
|
|
|
LOG_DEBUG("project={}; name={}", project->get_path().c_str(), name);
|
|
|
|
VTRYRV(
|
|
auto const id,
|
|
nullptr,
|
|
project->get_db()
|
|
->transact([name](auto * db) -> cppbase::Result<IkarusId, sqlitecpp::TransactionError> {
|
|
LOG_VERBOSE("creating blueprint in objects table");
|
|
|
|
TRY(db->execute(
|
|
"INSERT INTO `objects` (`object_type`, `name`) VALUES(?, ?);",
|
|
static_cast<int>(IkarusObjectType_Blueprint),
|
|
name
|
|
));
|
|
|
|
auto id = ikarus_id_from_data_and_type(db->last_insert_rowid(), IkarusObjectType_Blueprint);
|
|
|
|
LOG_DEBUG("blueprint is {}", id);
|
|
|
|
LOG_VERBOSE("inserting blueprint into blueprints table");
|
|
|
|
TRY(db->execute("INSERT INTO `blueprints`(`id`) VALUES(?);", id));
|
|
|
|
return cppbase::ok(id);
|
|
})
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("unable to insert blueprint into database: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_VERBOSE("successfully created blueprint");
|
|
|
|
return project->get_blueprint(id);
|
|
}
|
|
|
|
void ikarus_blueprint_delete(IkarusBlueprint * blueprint) {
|
|
LOG_INFO("deleting blueprint");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return;
|
|
}
|
|
|
|
auto * ctx = blueprint->get_project()->get_function_context();
|
|
|
|
LOG_DEBUG("blueprint={}", blueprint->get_id());
|
|
|
|
TRYRV(
|
|
,
|
|
blueprint->get_project()
|
|
->get_db()
|
|
->execute("DELETE FROM `objects` WHERE `id` = ?", blueprint->get_id())
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("failed to delete blueprint from objects table: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_VERBOSE("blueprint was successfully deleted from database, freeing blueprint");
|
|
|
|
blueprint->get_project()->uncache_blueprint(blueprint);
|
|
|
|
LOG_VERBOSE("successfully deleted blueprint");
|
|
}
|
|
|
|
size_t ikarus_blueprint_get_property_count(IkarusBlueprint const * blueprint) {
|
|
LOG_VERBOSE("fetching blueprint property count");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return 0;
|
|
}
|
|
|
|
auto * ctx = blueprint->get_project()->get_function_context();
|
|
|
|
LOG_DEBUG("blueprint={}", blueprint->get_id());
|
|
|
|
VTRYRV(
|
|
auto count,
|
|
0,
|
|
blueprint->get_project()
|
|
->get_db()
|
|
->query_one<int>("SELECT COUNT(*) FROM `blueprint_properties` WHERE `blueprint` = ?;", blueprint->get_id())
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("failed to fetch blueprint property count: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_DEBUG("blueprint property count: {}", count);
|
|
|
|
LOG_VERBOSE("successfully fetched blueprint property count");
|
|
|
|
return static_cast<size_t>(count);
|
|
}
|
|
|
|
void ikarus_blueprint_get_properties(
|
|
IkarusBlueprint const * blueprint, struct IkarusProperty ** properties_out, size_t properties_out_size
|
|
) {
|
|
LOG_VERBOSE("fetching blueprint properties");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return;
|
|
}
|
|
|
|
auto * ctx = blueprint->get_project()->get_function_context();
|
|
|
|
if (properties_out == nullptr) {
|
|
ctx->set_error("properties_out is null", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
|
return;
|
|
}
|
|
|
|
LOG_DEBUG("blueprint={}; properties_out_size={}", blueprint->get_id(), properties_out_size);
|
|
|
|
IkarusId ids[properties_out_size];
|
|
|
|
TRYRV(
|
|
,
|
|
blueprint->get_project()
|
|
->get_db()
|
|
->query_many_buffered<IkarusId>(
|
|
"SELECT `id` FROM `properties` WHERE `source` = ?",
|
|
static_cast<IkarusId *>(ids),
|
|
properties_out_size,
|
|
blueprint->get_id()
|
|
)
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("failed to fetch blueprint property ids: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_DEBUG("blueprint properties: [{}]", fmt::join(ids, ids + properties_out_size, ", "));
|
|
|
|
for (size_t i = 0; i < properties_out_size; ++i) {
|
|
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
|
properties_out[i] = blueprint->get_project()->get_property(ids[i]);
|
|
}
|
|
|
|
LOG_VERBOSE("successfully fetched blueprint properties");
|
|
}
|
|
|
|
size_t ikarus_blueprint_get_linked_entity_count(IkarusBlueprint const * blueprint) {
|
|
LOG_VERBOSE("fetching blueprint linked entity count");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return 0;
|
|
}
|
|
|
|
auto * ctx = blueprint->get_project()->get_function_context();
|
|
|
|
LOG_DEBUG("blueprint={}", blueprint->get_id());
|
|
|
|
VTRYRV(
|
|
auto count,
|
|
0,
|
|
blueprint->get_project()
|
|
->get_db()
|
|
->query_one<int>("SELECT COUNT(*) FROM `entity_blueprint_links` WHERE `blueprint` = ?;", blueprint->get_id())
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("failed to fetch blueprint linked entity count: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_DEBUG("blueprint linked entity count: {}", count);
|
|
|
|
LOG_VERBOSE("successfully fetched blueprint linked entity count: {}", count);
|
|
|
|
return static_cast<size_t>(count);
|
|
}
|
|
|
|
void ikarus_blueprint_get_linked_entities(
|
|
IkarusBlueprint const * blueprint, struct IkarusEntity ** entities_out, size_t entities_out_size
|
|
) {
|
|
LOG_VERBOSE("fetching blueprint linked entities");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return;
|
|
}
|
|
|
|
auto * ctx = blueprint->get_project()->get_function_context();
|
|
|
|
if (entities_out == nullptr) {
|
|
ctx->set_error("entities_out is null", true, IkarusErrorInfo_Source_Client, IkarusErrorInfo_Type_Client_Input);
|
|
return;
|
|
}
|
|
|
|
LOG_DEBUG("blueprint={}; entities_out_size={}", blueprint->get_id(), entities_out_size);
|
|
|
|
IkarusId ids[entities_out_size];
|
|
|
|
TRYRV(
|
|
,
|
|
blueprint->get_project()
|
|
->get_db()
|
|
->query_many_buffered<IkarusId>(
|
|
"SELECT `entity` FROM `entity_blueprint_links` WHERE `blueprint` = ?",
|
|
static_cast<IkarusId *>(ids),
|
|
entities_out_size,
|
|
blueprint->get_id()
|
|
)
|
|
.on_error([ctx](auto const& err) {
|
|
ctx->set_error(
|
|
fmt::format("failed to fetch blueprint linked entity ids: {}", err),
|
|
true,
|
|
IkarusErrorInfo_Source_SubSystem,
|
|
IkarusErrorInfo_Type_SubSystem_Database
|
|
);
|
|
})
|
|
);
|
|
|
|
LOG_DEBUG("blueprint linked entities: [{}]", fmt::join(ids, ids + entities_out_size, ", "));
|
|
|
|
for (size_t i = 0; i < entities_out_size; ++i) {
|
|
/// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
|
entities_out[i] = blueprint->get_project()->get_entity(ids[i]);
|
|
}
|
|
|
|
LOG_VERBOSE("successfully fetched blueprint linked entities");
|
|
}
|
|
|
|
IkarusObject * ikarus_blueprint_to_object(IkarusBlueprint * blueprint) {
|
|
return const_cast<IkarusObject *>(ikarus_blueprint_to_object_const(blueprint));
|
|
}
|
|
|
|
IkarusObject const * ikarus_blueprint_to_object_const(IkarusBlueprint const * blueprint) {
|
|
LOG_VERBOSE("casting blueprint to object");
|
|
|
|
if (blueprint == nullptr) {
|
|
LOG_ERROR("blueprint is nullptr");
|
|
return nullptr;
|
|
}
|
|
|
|
// auto * ctx = blueprint->project->function_context();
|
|
|
|
LOG_DEBUG("blueprint={}", blueprint->get_id());
|
|
|
|
LOG_VERBOSE("successfully casted blueprint to object");
|
|
|
|
return static_cast<IkarusObject const *>(blueprint);
|
|
}
|