CSEngine
Loading...
Searching...
No Matches
CustomComponent.cpp
1#include "CustomComponent.h"
2
3#include <utility>
4#include "../Manager/ScriptMgr.h"
5#include "../MacroDef.h"
6#include "../Object/SScriptObject.h"
7#include "../Util/MoreComponentFunc.h"
8#include "../Util/SafeLog.h"
9
10using namespace CSE;
11
12
13COMPONENT_CONSTRUCTOR(CustomComponent) {
14}
15
16
17CustomComponent::~CustomComponent() = default;
18
19
20void CustomComponent::Exterminate() {
21 SAFE_DELETE(m_specialization);
22 SAFE_DELETE(m_classInstance);
23}
24
25
26void CustomComponent::Init() {
27
28 if (m_specialization == nullptr) return;
29 if (m_classInstance == nullptr) {
30 CreateClassInstance(std::vector<std::string>());
31 }
32
33
34 if (m_funcInit < 0 || m_isError) return;
35
36 try {
37 m_classInstance->call(m_funcInit);
38 } catch (Sqrat::Exception e) {
39 m_isError = true;
40 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) + '\n').c_str());
41 }
42
43}
44
45
46void CustomComponent::Tick(float elapsedTime) {
47
48 if (m_specialization == nullptr) return;
49 if (m_classInstance == nullptr) return;
50 if (m_funcTick < 0) return;
51 if (m_isError) return;
52
53 try {
54 m_classInstance->call(m_funcTick, elapsedTime);
55 }
56 catch (Sqrat::Exception e) {
57 m_isError = true;
58 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) + '\n').c_str());
59 }
60
61}
62
63
64void CustomComponent::RegisterScript() {
65
66 if (m_className.empty()) return;
67 if (m_specialization != nullptr) SAFE_DELETE(m_specialization);
68
69 try {
70 m_specialization = new sqext::SQIClass(m_className.c_str());
71 m_specialization->bind(m_funcSetCSEngine, "SetCSEngine");
72 }
73 catch (Sqrat::Exception e) {
74 m_funcSetCSEngine = -1;
75 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) + '\n').c_str());
76 }
77
78 try {
79 m_specialization->bind(m_funcInit, "Init");
80 }
81 catch (Sqrat::Exception e) { m_funcInit = -1; }
82 try {
83 m_specialization->bind(m_funcTick, "Tick");
84 }
85 catch (Sqrat::Exception e) { m_funcTick = -1; }
86
87 try {
88 m_specialization->bind(m_funcExterminate, "Destroy");
89 }
90 catch (Sqrat::Exception e) { m_funcExterminate = -1; }
91
92}
93
94
95void CustomComponent::SetClassName(std::string name) {
96 auto asset = SResource::Get<SScriptObject>(std::move(name));
97 if (asset == nullptr) return;
98
99 m_classID = asset->GetHash();
100 m_className = asset->GetScriptClassName();
101
102 RegisterScript();
103 CreateClassInstance(asset->GetVariables());
104// Init();
105}
106
107
108std::string CustomComponent::SGetClassName() const {
109 return m_className;
110}
111
112
113bool CustomComponent::GetIsEnable() const {
114 return isEnable;
115}
116
117
118void CustomComponent::SetIsEnable(bool is_enable) {
119 isEnable = is_enable;
120}
121
122
123void CustomComponent::Log(const char* log) {
124 SafeLog::Log((std::string(log) + '\n').c_str());
125}
126
127
128SGameObject* CustomComponent::GetGameObject() const {
129 return gameObject;
130}
131
132SComponent* CustomComponent::Clone(SGameObject* object) {
133 INIT_COMPONENT_CLONE(CustomComponent, clone);
134
135 if (!m_className.empty()) {
136 clone->SetClassName(m_classID);
137 }
138
139 return clone;
140}
141
142void CustomComponent::SetValue(std::string name_str, VariableBinder::Arguments value) {
143 if (name_str == "m_classID") {
144 SetClassName(ConvertSpaceStr(value[0], true));
145 }
146 //variable : 0.name , 1.value, 2. type
147 else if (name_str == "__variable__") {
148 for(auto& val : m_variables) {
149 if(val.name == value[0]) {
150 val.value = value[1];
151 val.type = value[2];
152 BindValue(&val, ConvertSpaceStr(value[1], true).c_str());
153 }
154 }
155 }
156}
157
158std::string CustomComponent::PrintValue() const {
159 PRINT_START("component");
160
161 PRINT_VALUE(m_classID, ConvertSpaceStr(m_classID));
162 for(auto& val : m_variables) {
163 std::string value = val.value;
164 if(value.empty()) value = "null";
165 PRINT_VALUE(__variable__, val.name, ' ', ConvertSpaceStr(value) , ' ', val.type);
166 }
167
168
169 PRINT_END("component");
170}
171
172void CustomComponent::CreateClassInstance(const std::vector<std::string>& variables) {
173 if (m_specialization == nullptr) return;
174 if (m_classInstance != nullptr) {
175 SAFE_DELETE(m_classInstance);
176 }
177
178 try {
179 m_classInstance = m_specialization->NewPointer();
180
181 if (m_funcSetCSEngine < 0) return;
182 m_classInstance->call(m_funcSetCSEngine, this);
183 } catch (Sqrat::Exception e) {
184 m_isError = true;
185 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) + '\n').c_str());
186 return;
187 }
188
189 if(variables.empty()) return;
190
191 for (const auto& val : variables) {
192 auto obj = m_classInstance->get(val.c_str());
193 auto r_obj = obj.GetObject()._type;
194
195 const char* name;
196 std::string value;
197
198 switch (r_obj) {
199 case OT_STRING:
200 name = "str";
201 value = obj.Cast<const SQChar*>();
202 break;
203 case OT_BOOL:
204 name = "bool";
205 value = obj.Cast<SQBool>() ? "t" : "f";
206 break;
207 case OT_FLOAT:
208 name = "float";
209 value = std::to_string(obj.Cast<SQFloat>());
210 break;
211 case OT_INTEGER:
212 name = "int";
213 value = std::to_string(obj.Cast<SQInteger>());
214 break;
215 case OT_ARRAY:
216 name = "arr";
217 break;
218 default:
219 name = "null";
220 break;
221 }
222
223 m_variables.emplace_back(val, name, value);
224 }
225
226}
227
228void CustomComponent::BindValue(CustomComponent::VARIABLE* variable, const char* value) const {
229
230 std::string type = variable->type;
231 std::string value_str = value;
232
233 if(type == "str") m_classInstance->set(variable->name.c_str(), value);
234 else if(type == "bool") m_classInstance->set(variable->name.c_str(), value_str == "t");
235 else if(type == "float") m_classInstance->set(variable->name.c_str(), std::stof(value_str));
236 else if(type == "int") m_classInstance->set(variable->name.c_str(), std::stoi(value_str));
237// else if(type == "arr") m_classInstance->set(variable->name.c_str(), std::stof(valueStr));
238 else if(type == "comp") {
239 SComponent* comp = gameObject->GetSComponentByHash(value_str);
240 MoreComponentFunc::BindComponentToSQInstance(comp, variable->name, m_classInstance);
241 }
242 else if(type == "gobj") {
243 SGameObject* obj = SGameObject::FindByID(value_str);
244 m_classInstance->set(variable->name.c_str(), obj);
245 }
246 else if(type == "nut") {
247 SComponent* comp = gameObject->GetSComponentByHash(value_str);
248
249 if(dynamic_cast<CustomComponent*>(comp) != nullptr) {
250 auto comp_r = static_cast<CustomComponent*>(comp);
251 m_classInstance->set(variable->name.c_str(), comp_r->GetClassInstance());
252 }
253 }
254
255}