CSEngine
Loading...
Searching...
No Matches
SResource.cpp
1//
2// Created by ounols on 19. 6. 10.
3//
4
5#include <iostream>
6#include <utility>
7#include "SResource.h"
8#include "../Manager/ResMgr.h"
9#include "../Manager/EngineCore.h"
10#include "../Manager/MemoryMgr.h"
11#include "../Util/AssetsDef.h"
12#include "../Util/Loader/XML/XML.h"
13
14
15using namespace CSE;
16
17SResource::SResource(std::string classType) : ReflectionObject(std::move(classType)) {
18 auto resMgr = CORE->GetCore(ResMgr);
19 resMgr->Register(this);
20 m_name = "Resource " + std::to_string(resMgr->GetSize());
21}
22
23SResource::SResource(const SResource* resource, bool isRegister) : SObject(isRegister),
24 ReflectionObject(resource->GetClassType()) {
25 if (isRegister) {
26 CORE->GetCore(ResMgr)->Register(this);
27 }
28 m_name = resource->m_name + " (instance)";
29}
30
31SResource::~SResource() = default;
32
33void SResource::SetName(std::string name) {
34 m_name = std::move(name);
35}
36
37void SResource::SetAbsoluteID(std::string id) {
38 m_absoluteId = std::move(id);
39}
40
41void SResource::SetResource(std::string name, bool isInit) {
42 if (m_isInited) return;
43
44 auto asset = CORE->GetCore(ResMgr)->GetAssetReference(std::move(name));
45 SetResource(asset, isInit);
46}
47
48void SResource::SetResource(AssetMgr::AssetReference* asset, bool isInit) {
49 if (asset == nullptr) return;
50 if (m_isInited) return;
51
52 asset->resource = this;
53 m_isInited = true;
54 m_absoluteId = asset->id;
55 if (!asset->hash.empty()) {
56 std::string hash = asset->hash;
57 SetHash(hash);
58 } else {
59 std::string meta = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
60 "<CSEMETA version=\"1.0.0\">\n"
61 "<hash-data hash=\"" + m_hash + "\">\n"
62 "\n</hash-data>\n</CSEMETA>";
63 SaveTxtFile(asset->name_path + ".meta", meta);
64 }
65
66 SetName(asset->name);
67 if (isInit)
68 Init(asset);
69}
70
71SResource* SResource::GetResource(std::string name) {
72 return CORE->GetCore(ResMgr)->GetSResource(std::move(name));
73}
74
75void SResource::SetHash(std::string& hash) {
76 std::string srcHash = m_hash;
77 SObject::SetHash(hash);
78 CORE->GetCore(ResMgr)->ChangeHash(srcHash, hash);
79}
80
81AssetMgr::AssetReference* SResource::GetAssetReference(std::string hash) const {
82 if (hash.empty()) hash = m_hash;
83 return CORE->GetCore(ResMgr)->GetAssetReference(std::move(hash));
84}
85
86SResource* SResource::Create(const std::string& name, const std::string& classType) {
87 {
88 SResource* res = GetResource(name);
89 if (res != nullptr) return res;
90 }
91 SResource* res = static_cast<SResource*>(ReflectionObject::NewObject(classType));
92 res->SetResource(name);
93 return res;
94}
95
96SResource* SResource::Get(std::string& name) {
97 SResource* res = GetResource(name);
98 if (res != nullptr) return res;
99 return nullptr;
100}
101
102SResource* SResource::Create(const AssetMgr::AssetReference* asset, const std::string& classType) {
103 if (asset == nullptr) return nullptr;
104 {
105 SResource* res = GetResource(asset->hash);
106 if (res != nullptr) return res;
107 }
108 SResource* res = static_cast<SResource*>(ReflectionObject::NewObject(classType));
109 res->SetResource(const_cast<AssetMgr::AssetReference*>(asset));
110 return res;
111}