CSEngine
Loading...
Searching...
No Matches
XMLParser.cpp
1#include "XMLParser.h"
2#include <string>
3
4#include "../../MoreComponentFunc.h"
5#include "../../../Manager/GameObjectMgr.h"
6#include "../../Render/SFrameBuffer.h"
7#include "../../Render/SMaterial.h"
8#include "../../Render/STexture.h"
9
10using namespace CSE;
11
12void XMLParser::parse(std::vector<std::string> values, void* dst, SType type) {
13 if (type == SType::STRING) dst = (void*) values[0].c_str();
14 else if (type == SType::BOOL) *static_cast<bool*>(dst) = parseBool(values[0].c_str());
15 else if (type == SType::FLOAT) *static_cast<float*>(dst) = parseFloat(values[0].c_str());
16 else if (type == SType::INT) *static_cast<int*>(dst) = parseInt(values[0].c_str());
17 // else if(type == "arr") dst = (void*)parseInt(values[0].c_str());
18
19 else if (type == SType::RESOURCE) dst = (void*) parseResources<SResource>(values[0].c_str());
20 else if (type == SType::MATERIAL) dst = (void*) parseMaterial(values[0].c_str());
21 else if (type == SType::TEXTURE) {
22 dst = (void*) parseTexture(values[0].c_str());
23 if (dst == nullptr) dst = (void*) parseFrameBuffer(values[0].c_str());
24 } else if (type == SType::COMPONENT) dst = (void*) parseComponent(values[0].c_str());
25 else if (type == SType::GAME_OBJECT) dst = (void*) parseGameObject(values[0].c_str());
26
27 else if (type == SType::VEC2) *static_cast<vec2*>(dst) = parseVec2(values);
28 else if (type == SType::VEC3) *static_cast<vec3*>(dst) = parseVec3(values);
29 else if (type == SType::VEC4) *static_cast<vec4*>(dst) = parseVec4(values);
30
31// else if (type == SType::MAT2) *static_cast<mat2*>(dst) = parseMat2(values);
32// else if (type == SType::MAT3) *static_cast<mat3*>(dst) = parseMat3(values);
33// else if (type == SType::MAT4) *static_cast<mat4*>(dst) = parseMat4(values);
34}
35
36int XMLParser::parseInt(const char* value) {
37 return std::stoi(value);
38}
39
40float XMLParser::parseFloat(const char* value) {
41 return std::stof(value);
42}
43
44bool XMLParser::parseBool(const char* value) {
45 return strncmp(value, "t", 1);
46}
47
48vec2 XMLParser::parseVec2(std::vector<std::string> values) {
49 return {std::stof(values[0]), std::stof(values[1])};
50}
51
52vec3 XMLParser::parseVec3(std::vector<std::string> values) {
53 return {std::stof(values[0]), std::stof(values[1]), std::stof(values[2])};
54}
55
56vec4 XMLParser::parseVec4(std::vector<std::string> values) {
57 return {std::stof(values[0]), std::stof(values[1]), std::stof(values[2]), std::stof(values[3])};
58}
59
60STexture* XMLParser::parseTexture(const char* value) {
61 const auto& asset = CORE->GetCore(ResMgr)->GetAssetReference(value);
62 if (asset == nullptr || asset->type == AssetMgr::FRAMEBUFFER) {
63 const auto value_split = split(value, '?');
64 if (value_split.size() <= 1)
65 return SResource::Create<STexture>(value);
66
67 const auto& frameBuffer = SResource::Create<SFrameBuffer>(value_split[0]);
68 return frameBuffer->GetTexture(value);
69 }
70 return SResource::Create<STexture>(value);
71}
72
73SFrameBuffer* XMLParser::parseFrameBuffer(const char* value) {
74 std::string value_result = value;
75 const auto split_index = value_result.rfind('?');
76 if (split_index != std::string::npos)
77 value_result = value_result.substr(0, split_index - 1);
78
79 const auto& asset = CORE->GetCore(ResMgr)->GetAssetReference(value_result);
80 if (asset->type != AssetMgr::FRAMEBUFFER) {
81 return nullptr;
82 }
83 return SResource::Create<SFrameBuffer>(asset);
84}
85
86SMaterial* XMLParser::parseMaterial(const char* value) {
87 return SResource::Create<SMaterial>(value);
88}
89
90SComponent* XMLParser::parseComponent(const char* value) {
91 SComponent* comp = CORE->GetCore(GameObjectMgr)->FindComponentByID(value);
92 return comp;
93}
94
95SGameObject* XMLParser::parseGameObject(const char* value) {
96 SGameObject* obj = CORE->GetCore(GameObjectMgr)->FindByID(ConvertSpaceStr(value, true));
97 return obj;
98}
99
100SType XMLParser::GetType(std::string type) {
101 static const std::unordered_map<std::string, SType> typeMap = {
102 {"str", SType::STRING},
103 {"bool", SType::BOOL},
104 {"float", SType::FLOAT},
105 {"int", SType::INT},
106 {"res", SType::RESOURCE},
107 {"tex", SType::TEXTURE},
108 {"mat", SType::MATERIAL},
109 {"comp", SType::COMPONENT},
110 {"gobj", SType::GAME_OBJECT},
111 {"vec2", SType::VEC2},
112 {"vec3", SType::VEC3},
113 {"vec4", SType::VEC4},
114 {"mat2", SType::MAT2},
115 {"mat3", SType::MAT3},
116 {"mat4", SType::MAT4}
117 };
118
119 auto it = typeMap.find(type);
120 if (it != typeMap.end()) {
121 return it->second;
122 } else {
123 return SType::UNKNOWN;
124 }
125}
126
127SType XMLParser::GetType(unsigned int type) {
128 switch (type) {
129 case GL_BOOL: return SType::BOOL;
130 case GL_FLOAT: return SType::FLOAT;
131 case GL_INT: return SType::INT;
132 case GL_SAMPLER_2D: return SType::TEXTURE;
133 case GL_FLOAT_VEC2: return SType::VEC2;
134 case GL_FLOAT_VEC3: return SType::VEC3;
135 case GL_FLOAT_VEC4: return SType::VEC4;
136 case GL_FLOAT_MAT2: return SType::MAT2;
137 case GL_FLOAT_MAT3: return SType::MAT3;
138 case GL_FLOAT_MAT4: return SType::MAT4;
139 default: return SType::UNKNOWN;
140 }
141}
142
143std::string XMLParser::ToString(SType type) {
144 switch (type) {
145 case SType::STRING: return "str";
146 case SType::BOOL: return "bool";
147 case SType::FLOAT: return "float";
148 case SType::INT: return "int";
149 case SType::RESOURCE: return "res";
150 case SType::TEXTURE: return "tex";
151 case SType::MATERIAL: return "mat";
152 case SType::COMPONENT: return "comp";
153 case SType::GAME_OBJECT: return "gobj";
154 case SType::VEC2: return "vec2";
155 case SType::VEC3: return "vec3";
156 case SType::VEC4: return "vec4";
157 case SType::MAT2: return "mat2";
158 case SType::MAT3: return "mat3";
159 case SType::MAT4: return "mat4";
160 default: return "str";
161 }
162}