1#include "ExportCommand.h"
2#include "CommandParseHelpers.h"
4#include "Components/Core/ModelComponent.h"
5#include "Components/Core/MeshComponent.h"
6#include "Components/Core/SceneComponent.h"
7#include "Components/Core/StaticModelComponent.h"
8#include "Resources/Model.h"
9#include "Serialization/ModelWriter.h"
11#include "Foundation/Logging/Logger.h"
12#include "Foundation/Platform/IO.h"
24 if (entity ==
nullptr)
return;
26 if (
auto result = std::find_if(entities.begin(), entities.end(), [e = entity](
Cogs::ComponentModel::Entity*& x) { return x->getId() == e->getId(); });
27 result != entities.end()) {
32 if (!sceneComp)
return;
36 if (!meshComp && !staticModelComp)
return;
38 LOG_DEBUG(logger,
"export entity id: %zu", entity->
getId());
39 entities.push_back(entity);
53 if (fileName.empty() && !stem.empty()) {
54 fileName = stem +
".cogsbin";
57 if (!fileName.empty()) {
62 if (destination.size()) {
63 if (!IO::exists(destination)) {
64 fs::create_directories(destination);
67 outputName = IO::combine(destination, outputName);
70 fileName = outputName;
73 if (fileName.empty()) {
74 std::string name =
"Model.cogsbin";
77 if (modelComp && modelComp->
model) {
79 std::string modelFileName = IO::fileName(modelPath);
81 stem = IO::stem(modelFileName);
84 name = stem +
".cogsbin";
93 std::string path = fileName;
95 if (IO::isRelative(path)) {
96 path = IO::combine(state->directory, path);
104 std::vector<ComponentModel::Entity*> entities;
105 const std::unordered_map<EntityId, EntityPtr>& entityMap = context->
store->
getEntities();
107 for (
const auto& e : entityMap) {
109 getEntity(context, entities, entity);
112 ok = writeModels(context, numVertes, numIndexes, path, entities.data(), entities.size(), WriteModelFlags::COMPRESS_ZSTD | WriteModelFlags::COMPRESS_MAX);
115 if (1 < state->selected.size()) {
119 std::vector<ComponentModel::Entity*> entities;
121 for (
auto id : state->selected) {
124 ok = writeModels(context, numVertes, numIndexes, path, entities.data(), entities.size(), WriteModelFlags::COMPRESS_ZSTD | WriteModelFlags::COMPRESS_MAX);
127 ok = writeModel(context, path, state->
getSelected(), WriteModelFlags::COMPRESS_ZSTD | WriteModelFlags::COMPRESS_MAX);
132 LOG_ERROR(logger,
"Could not save model to: %s", path.c_str());
135 LOG_DEBUG(logger,
"Wrote %s", path.c_str());
142void Cogs::Core::ExportCommand::undo()
Container for components, providing composition of dynamic entities.
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
constexpr size_t getId() const noexcept
Get the unique identifier of this entity.
const std::string & getName() const noexcept
Get the name of this entity.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
class EntityStore * store
Entity store.
ComponentModel::Entity * getEntityPtr(const EntityId entityId)
Get a raw pointer to the entity with the given id.
const std::unordered_map< EntityId, EntityPtr > & getEntities() const
Return map of entities with global ownership.
virtual void apply() override
Run the command.
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
Contains information on how the entity behaves in the scene.
Log implementation class.
std::string to_string() const
String conversion method.
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
std::string replaceFileMacros(std::string outputName, const std::vector< ParsedValue > &options)
Replace common macroes for file name generation. I.e.: "A/$Filename" etc.
std::string fixInvalidPathChars(const std::string &filename)
Remove characters that are not valid for filenames lik "<>:/\?" etc.
StringView getOption(const std::vector< ParsedValue > &options, const StringView &key, const StringView &defaultValue="")
Find and get value of option in vector as a string. Return default if not found.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
std::vector< ParsedValue > options
Options passed to the command when running in batch mode.
ComponentModel::Entity * getSelected() const
Gets entity Pointer to the single selected entity. Nullptr if not one selected or not found.
Contains a model reference to instance as children to the entity the ModelComponent belongs to.
ModelHandle model
Handle to a model resource that will be instanced onto the entity this ModelComponent belongs to.