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