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() {
18 auto resMgr = CORE->GetCore(ResMgr);
19 resMgr->Register(this);
20 m_name = "Resource " + std::to_string(resMgr->GetSize());
21}
22
23SResource::SResource(bool isRegister) {
24 m_name = "Unregister Resource";
25}
26
27SResource::SResource(const SResource* resource, bool isRegister) : SObject(isRegister) {
28 if(isRegister) {
29 CORE->GetCore(ResMgr)->Register(this);
30 }
31 m_name = resource->m_name + " (instance)";
32}
33
34SResource::~SResource() = default;
35
36void SResource::SetName(std::string name) {
37 m_name = std::move(name);
38}
39
40void SResource::SetAbsoluteID(std::string id) {
41 m_absoluteId = std::move(id);
42}
43
44void SResource::SetResource(std::string name, bool isInit) {
45 if (m_isInited) return;
46
47 auto asset = CORE->GetCore(ResMgr)->GetAssetReference(std::move(name));
48
49 SetResource(asset, isInit);
50}
51
52void SResource::SetResource(const AssetMgr::AssetReference* asset, bool isInit) {
53 if (asset == nullptr) return;
54 if (m_isInited) return;
55
56 m_isInited = true;
57 m_absoluteId = asset->id;
58 if(!asset->hash.empty()) {
59 std::string hash = asset->hash;
60 SetHash(hash);
61 }
62 else {
63 std::string meta = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
64 "<CSEMETA version=\"1.0.0\">\n"
65 "<hash-data hash=\"" + m_hash + "\">\n"
66 "\n</hash-data>\n</CSEMETA>";
67 SaveTxtFile(asset->name_path + ".meta", meta);
68 }
69
70 SetName(asset->name);
71 if (isInit)
72 Init(asset);
73}
74
75SResource* SResource::GetResource(std::string name) {
76 return CORE->GetCore(ResMgr)->GetSResource(std::move(name));
77}
78
79void SResource::SetHash(std::string& hash) {
80 std::string srcHash = m_hash;
81 SObject::SetHash(hash);
82 CORE->GetCore(ResMgr)->ChangeHash(srcHash, hash);
83}
84
85AssetMgr::AssetReference* SResource::GetAssetReference(std::string hash) const {
86 if(hash.empty()) hash = m_hash;
87 return CORE->GetCore(ResMgr)->GetAssetReference(std::move(hash));
88}