Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions engine/includes/resources/prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ENGINE_EXPORT Prefab : public Resource {
bool contains(uint32_t uuid);
Object *protoObject(uint32_t uuid);

ConstObjectList absentObjects(const ConstObjectList &objects);
ObjectList absentInCloned(const ConstObjectList &cloned);

private:
void loadUserData(const VariantMap &data) override;
Expand All @@ -38,7 +38,7 @@ class ENGINE_EXPORT Prefab : public Resource {

mutable Actor *m_actor;

ObjectMap m_dictionary;
mutable ObjectMap m_dictionary;

};

Expand Down
2 changes: 0 additions & 2 deletions engine/includes/resources/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class ENGINE_EXPORT Resource : public Object {
typedef std::list<std::pair<Resource::ResourceUpdatedCallback, void *>> Callbacks;

Callbacks m_observers;
Callbacks m_toSubscribe;
std::list<void *> m_toUnsubscribe;

State m_state;
State m_last;
Expand Down
5 changes: 2 additions & 3 deletions engine/includes/systems/resourcesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#define RESOURCESYSTEM_H

#include "system.h"

class Resource;
#include "resource.h"

class ENGINE_EXPORT ResourceSystem : public System {
public:
Expand Down Expand Up @@ -48,7 +47,7 @@ class ENGINE_EXPORT ResourceSystem : public System {
std::unordered_map<std::string, Resource *> m_resourceCache;
std::unordered_map<Resource *, std::string> m_referenceCache;

std::list<Resource *> m_deleteList;
ObjectList m_deleteList;

};

Expand Down
71 changes: 33 additions & 38 deletions engine/src/components/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void Actor::setLayers(const int layers) {
Transform *Actor::transform() {
PROFILE_FUNCTION();
if(m_transform == nullptr) {
setTransform(static_cast<Transform *>(component(gTransform)));
setTransform(getComponent<Transform>());
}
return m_transform;
}
Expand Down Expand Up @@ -287,11 +287,12 @@ Object *Actor::cloneStructure(ObjectPairs &pairs) {
PROFILE_FUNCTION();
Actor *result = static_cast<Actor *>(Object::cloneStructure(pairs));
Prefab *prefab = dynamic_cast<Prefab *>(Object::parent());
if(prefab) {
result->setPrefab(prefab);
} else {
result->setPrefab(m_prefab);
if(prefab == nullptr) {
prefab = m_prefab;
}

result->setPrefab(prefab);

return result;
}
/*!
Expand Down Expand Up @@ -425,6 +426,7 @@ void Actor::loadObjectData(const VariantMap &data) {
for(auto &it : children) {
it->setParent(this);
}
ObjectSystem::replaceClonedUUID(this, actor->clonedFrom());
delete actor;

auto it = data.find(gStatic);
Expand Down Expand Up @@ -552,7 +554,7 @@ VariantMap Actor::saveUserData() const {
enumConstObjects(this, objects);

VariantList deletedList;
for(auto it : m_prefab->absentObjects(objects)) {
for(auto it : m_prefab->absentInCloned(objects)) {
deletedList.push_back(it->uuid());
}
if(!deletedList.empty()) {
Expand Down Expand Up @@ -662,54 +664,47 @@ void Actor::prefabUpdated(int state, void *ptr) {
p->m_data = p->saveUserData();
} break;
case Resource::Ready: {
p->m_transform = nullptr;
Object::ObjectList prefabObjects;
Object::enumObjects(p->m_prefab->actor(), prefabObjects);
prefabObjects.pop_front();
p->m_transform = nullptr; // What the reason?

Object::ObjectList deleteObjects;
Object::enumObjects(p, deleteObjects);
deleteObjects.pop_front();
ObjectList objects;
Engine::enumObjects(p, objects);

ObjectPairs pairs;

for(auto prefabObject : prefabObjects) {
bool create = true;
auto it = deleteObjects.begin();
while(it != deleteObjects.end()) {
Object *clone = *it;
if(prefabObject->uuid() == clone->clonedFrom()) {
pairs.push_back(std::make_pair(prefabObject, clone));
it = deleteObjects.erase(it);
create = false;
break;
} else if(clone->clonedFrom() == 0) { // probably was created right in instance we don't need to sync it
it = deleteObjects.erase(it);
create = false;
break;
Prefab::ConstObjectList objectsList;
Prefab::ConstObjectList objectsToDelete;
for(auto &it : objects) {
uint32_t originID = it->clonedFrom();
if(originID != 0) {
Object *protoObject = p->m_prefab->protoObject(originID);
if(protoObject) {
pairs.push_back(std::make_pair(protoObject, it));
objectsList.push_back(it);
} else {
objectsToDelete.push_back(it);
}
++it;
}

if(create) { // New object
static_cast<Actor *>(prefabObject)->cloneStructure(pairs);
}
}

// New objects
objects = p->m_prefab->absentInCloned(objectsList);
for(auto it : objects) {
static_cast<Actor *>(it)->cloneStructure(pairs);
}

Actor::syncProperties(p, pairs);

/// \todo Incomlete filter for new added and removed elements from prefab
//deleteObjects.reverse();
//for(auto it : deleteObjects) {
// delete it;
//}
objectsToDelete.reverse();
for(auto it : objectsToDelete) {
delete it;
}

p->loadUserData(p->m_data);
} break;
case Resource::ToBeDeleted: {
/// \todo Unload prefab related components
for(auto &it : p->getChildren()) {
if(it != p->transform()) {
if(p->m_prefab->contains(it->clonedFrom())) {
it->deleteLater();
}
}
Expand Down
12 changes: 3 additions & 9 deletions engine/src/editor/assetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,14 +860,13 @@ void AssetManager::convert(AssetConverterSettings *settings) {
registerAsset(path, value, type);

if(QFileInfo::exists(m_projectManager->importPath() + "/" + value)) {
Object *res = Engine::loadResource(value.toStdString());
Engine::resourceSystem()->reloadResource(static_cast<Resource *>(res), true);
Engine::reloadResource(value.toStdString());
emit imported(path, type);
}
}

Object *res = Engine::loadResource(guid.toStdString());
Engine::resourceSystem()->reloadResource(static_cast<Resource *>(res), true);
Engine::reloadResource(guid.toStdString());

emit imported(source, type);

settings->saveSettings();
Expand Down Expand Up @@ -907,11 +906,6 @@ void AssetManager::registerAsset(const QFileInfo &source, const QString &guid, c
if(QFileInfo::exists(m_projectManager->importPath() + "/" + guid)) {
QString path = pathToLocal(source);

if(path.contains(".embedded/{00000000-0202-0000-0000-000000000000}/Arrow")) {
qDebug() << source;
path = path;
}

m_indices[path.toStdString()] = std::pair<std::string, std::string>(type.toStdString(), guid.toStdString());
m_paths[guid.toStdString()] = source.absoluteFilePath().toStdString();
m_labels.insert(type);
Expand Down
17 changes: 9 additions & 8 deletions engine/src/editor/converters/assimpconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiM
}
} else {
Actor *actor = Engine::objectCreate<Actor>(name, parent);
actor->addComponent("Transform");
Transform *transform = static_cast<Transform *>(actor->addComponent("Transform"));

if(fbxSettings->m_rootActor == nullptr) {
fbxSettings->m_rootActor = actor;
Expand All @@ -323,9 +323,9 @@ Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiM
pos = Vector3(-pos.x, pos.z, pos.y);
}

actor->transform()->setPosition(pos);
actor->transform()->setRotation(Vector3(euler.x, euler.y, euler.z) * RAD2DEG);
actor->transform()->setScale(Vector3(scale.x, scale.y, scale.z));
transform->setPosition(pos);
transform->setRotation(Vector3(euler.x, euler.y, euler.z) * RAD2DEG);
transform->setScale(Vector3(scale.x, scale.y, scale.z));

Mesh *result = AssimpConverter::importMesh(scene, element, actor, fbxSettings);
if(result) {
Expand Down Expand Up @@ -375,9 +375,6 @@ Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, A
return mesh;
}

// Creating a new one
mesh = Engine::objectCreate<Mesh>(element->mName.C_Str());

size_t total_v = 0;
size_t total_i = 0;

Expand All @@ -387,6 +384,10 @@ Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, A
for(uint32_t index = 0; index < element->mNumMeshes; index++) {
const aiMesh *item = scene->mMeshes[element->mMeshes[index]];

if(mesh == nullptr) {
mesh = Engine::objectCreate<Mesh>(item->mName.C_Str());
}

count_v += item->mNumVertices;
count_i += static_cast<uint32_t>(item->mNumFaces * 3);
}
Expand Down Expand Up @@ -779,7 +780,7 @@ void AssimpConverter::importPose(AssimpImportSettings *fbxSettings) {
fbxSettings->m_resources.push_back(uuid);

if(fbxSettings->m_rootBone) {
Armature *armature = dynamic_cast<Armature *>(fbxSettings->m_rootBone->addComponent("Armature"));
Armature *armature = static_cast<Armature *>(fbxSettings->m_rootBone->addComponent("Armature"));
armature->setBindPose(resource);

for(auto r : fbxSettings->m_renders) {
Expand Down
1 change: 0 additions & 1 deletion engine/src/editor/converters/prefabconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "components/actor.h"

#include <QFile>
#include <QDebug>

#include <bson.h>
#include <json.h>
Expand Down
16 changes: 11 additions & 5 deletions engine/src/resources/prefab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Actor *Prefab::actor() const {
if(m_actor == nullptr) {
auto &children = getChildren();
if(!children.empty()) {
m_dictionary.clear();

m_actor = dynamic_cast<Actor *>(children.front());
}
}
Expand Down Expand Up @@ -67,25 +69,27 @@ Object *Prefab::protoObject(uint32_t uuid) {
return nullptr;
}
/*!
Compares with prefab and returns a list of abset \a objects in cloned list
Compares with prefab and returns a list of abset objects in \a cloned list
*/
Prefab::ConstObjectList Prefab::absentObjects(const ConstObjectList &objects) {
Object::ObjectList Prefab::absentInCloned(const ConstObjectList &cloned) {
if(m_dictionary.empty()) {
makeCache(m_actor);
}

ConstObjectList temp;
ObjectList temp;
for(auto it : m_dictionary) {
temp.push_back(it.second);
}

for(auto clone : objects) {
for(auto clone : cloned) {
uint32_t originID = clone->clonedFrom();

bool force = originID == 0 || !contains(originID);

auto it = temp.begin();
while(it != temp.end()) {
const Object *origin = *it;
if(clone->clonedFrom() == 0 || !contains(originID) || origin->uuid() == originID) {
if(force || origin->uuid() == originID) {
it = temp.erase(it);
break;
}
Expand All @@ -101,6 +105,8 @@ Prefab::ConstObjectList Prefab::absentObjects(const ConstObjectList &objects) {
void Prefab::loadUserData(const VariantMap &data) {
Resource::loadUserData(data);

m_dictionary.clear();

auto it = data.find(gActor);
if(it != data.end()) {
uint32_t uuid = uint32_t((*it).second.toInt());
Expand Down
38 changes: 15 additions & 23 deletions engine/src/resources/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Resource::~Resource() {
Increases reference count.
*/
void Resource::subscribe(ResourceUpdatedCallback callback, void *object) {
m_toSubscribe.push_back(std::make_pair(callback, object));
std::unique_lock<std::mutex> locker(m_mutex);

m_observers.push_back(std::make_pair(callback, object));

incRef();
}
Expand All @@ -68,7 +70,16 @@ void Resource::subscribe(ResourceUpdatedCallback callback, void *object) {
Decreases reference count.
*/
void Resource::unsubscribe(void *object) {
m_toUnsubscribe.push_back(object);
std::unique_lock<std::mutex> locker(m_mutex);

auto it = m_observers.begin();
while(it != m_observers.end()) {
if((it->second) == object) {
it = m_observers.erase(it);
} else {
++it;
}
}

decRef();
}
Expand Down Expand Up @@ -107,27 +118,8 @@ void Resource::setState(State state) {
Notifies subscribers about the current state of the resource.
*/
void Resource::notifyCurrentState() {
std::unique_lock<std::mutex> locker(m_mutex, std::defer_lock);

if(locker.try_lock()) {
if(!m_toSubscribe.empty()) {
for(auto it : m_toSubscribe) {
m_observers.push_back(it);
}
m_toSubscribe.clear();
}

for(auto object : m_toUnsubscribe) {
auto it = m_observers.begin();
while(it != m_observers.end()) {
if((it->second) == object) {
it = m_observers.erase(it);
} else {
++it;
}
}
}
m_toUnsubscribe.clear();
if(!m_observers.empty()) {
std::unique_lock<std::mutex> locker(m_mutex);

for(auto it : m_observers) {
(*it.first)(m_state, it.second);
Expand Down
Loading