CSEngine
Loading...
Searching...
No Matches
SPrefab.cpp
1//
2// Created by ounols on 19. 2. 10.
3//
4
5#include "../Manager/SCloneFactory.h"
6#include "../Manager/ResMgr.h"
7#include "SPrefab.h"
8#include "../Util/Loader/DAE/DAELoader.h"
9#include "../Util/AssetsDef.h"
10#include "SGameObjectFromSPrefab.h"
11
12using namespace CSE;
13
14SPrefab::SPrefab() = default;
15
16SPrefab::~SPrefab() = default;
17
18void SPrefab::Exterminate() {
19}
20
21SGameObject* SPrefab::Clone(const vec3& position, SGameObject* parent) {
22 SGameObject* clone = SCloneFactory::Clone(m_root, parent);
23
24 clone->GetTransform()->m_position = vec3(position);
25 clone->SetIsPrefab(false);
26 clone->SetUndestroyable(false);
27
28 return clone;
29}
30
31
32SGameObject* SPrefab::Clone(const vec3& position, const vec3& scale, Quaternion rotation, SGameObject* parent) {
33 SGameObject* clone = SCloneFactory::Clone(m_root, parent);
34
35 clone->GetTransform()->m_position = vec3(position);
36 clone->GetTransform()->m_scale = vec3(scale);
37 clone->GetTransform()->m_rotation = Quaternion(rotation);
38 clone->SetIsPrefab(false);
39 clone->SetUndestroyable(false);
40
41 return clone;
42}
43
44bool SPrefab::SetGameObject(SGameObject* obj) {
45 if (m_root != nullptr) return false;
46
47 m_root = obj;
48 m_root->SetUndestroyable(true);
49 m_root->SetIsPrefab(true);
50 m_root->SetResourceID(GetHash(), true);
51
52 return true;
53}
54
55void SPrefab::Init(const AssetMgr::AssetReference* asset) {
56 AssetMgr::TYPE type = asset->type;
57 std::string path = asset->name_path;
58
59 switch (type) {
60 case AssetMgr::DAE:
61 DAELoader::GeneratePrefab(path.c_str(), nullptr, nullptr, nullptr, this);
62 break;
63 }
64
65 std::string hashRaw = AssetMgr::LoadAssetFile(path + ".meta");
66 if(!hashRaw.empty()) {
67 const XNode* root = XFILE().loadBuffer(hashRaw);
68 const auto& hashData = root->getNode("hash-data");
69 std::string hash = hashData.getAttribute("hash").value;
70 SetHash(hash);
71 const auto& hashChildren = hashData.children;
72
73 if(hashData.children.size() <= 0) {
74 hashRaw = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
75 "<CSEMETA version=\"1.0.0\">\n"
76 "<hash-data hash=\"" + m_hash + "\">\n"
77 + m_root->GenerateMeta() +
78 "\n</hash-data>\n</CSEMETA>";
79 SaveTxtFile(path + ".meta", hashRaw);
80 }
81 else {
82 for(const auto& child : hashChildren) {
83 const auto& id = child.getAttribute("id").value;
84 std::string hash = child.value;
85 const auto& obj = static_cast<SGameObjectFromSPrefab*>(m_root->FindLocalByID(id));
86 if(obj == nullptr) continue;
87 obj->SetHash(hash);
88 obj->SetRefHash(hash);
89 }
90 }
91 SAFE_DELETE(root);
92 }
93 GenerateResourceID();
94}
95
96void SPrefab::GenerateResourceID(SGameObject* obj) {
97 if (obj == nullptr) {
98 obj = m_root;
99 obj->SetResourceID(GetHash());
100 } else {
101 std::string resultID = obj->GetID();
102 resultID = resultID.substr(resultID.find('/') + 1);
103 obj->SetResourceID(std::string(GetHash()) + "*" + resultID);
104 }
105
106 for (auto child : obj->GetChildren()) {
107 GenerateResourceID(child);
108 }
109}
Definition XML.h:77
Definition XML.h:43