1#include "CustomComponent.h"
4#include "../Manager/ScriptMgr.h"
5#include "../MacroDef.h"
6#include "../Object/SScriptObject.h"
7#include "../Util/MoreComponentFunc.h"
8#include "../Util/SafeLog.h"
17CustomComponent::~CustomComponent() =
default;
20void CustomComponent::Exterminate() {
21 SAFE_DELETE(m_specialization);
22 SAFE_DELETE(m_classInstance);
26void CustomComponent::Init() {
28 if (m_specialization ==
nullptr)
return;
29 if (m_classInstance ==
nullptr) {
30 CreateClassInstance(std::vector<std::string>());
34 if (m_funcInit < 0 || m_isError)
return;
37 if(!m_classInstance->call_safe(m_funcInit)) {
39 SafeLog::Log((
"[" + m_className +
" (Init)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
41 }
catch (Sqrat::Exception e) {
43 SafeLog::Log((
"[" + m_className +
" (Init)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
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;
56 m_classInstance->call(m_funcTick, elapsedTime);
58 catch (Sqrat::Exception e) {
60 SafeLog::Log((
"[" + m_className +
" (Init)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
66void CustomComponent::RegisterScript() {
68 if (m_className.empty())
return;
69 if (m_specialization !=
nullptr) SAFE_DELETE(m_specialization);
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());
78 catch (Sqrat::Exception e) {
79 m_funcSetCSEngine = -1;
80 SafeLog::Log((
"[" + m_className +
" (SetCSEngine)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
84 if(!m_specialization->bind_safe(m_funcInit,
"Init")) {
86 SafeLog::Log((
"[" + m_className +
" (Init)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
90 catch (Sqrat::Exception e) { m_funcInit = -1; }
92 if(!m_specialization->bind_safe(m_funcTick,
"Tick")) {
94 SafeLog::Log((
"[" + m_className +
" (Tick)] " + Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
98 catch (Sqrat::Exception e) { m_funcTick = -1; }
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());
107 catch (Sqrat::Exception e) { m_funcExterminate = -1; }
112void CustomComponent::SetClassName(std::string name) {
113 auto asset = SResource::Get<SScriptObject>(std::move(name));
114 if (asset ==
nullptr)
return;
116 m_classID = asset->GetHash();
117 m_className = asset->GetScriptClassName();
120 CreateClassInstance(asset->GetVariables());
125std::string CustomComponent::SGetClassName()
const {
130bool CustomComponent::GetIsEnable()
const {
135void CustomComponent::SetIsEnable(
bool is_enable) {
136 isEnable = is_enable;
140void CustomComponent::Log(
const char* log) {
141 SafeLog::Log((std::string(log) +
'\n').c_str());
145SGameObject* CustomComponent::GetGameObject()
const {
152 if (!m_className.empty()) {
153 clone->SetClassName(m_classID);
160 if (name_str ==
"m_classID") {
161 SetClassName(ConvertSpaceStr(value[0],
true));
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());
175std::string CustomComponent::PrintValue()
const {
176 PRINT_START(
"component");
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);
186 PRINT_END(
"component");
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);
196 m_classInstance = m_specialization->NewPointer();
198 if (m_funcSetCSEngine < 0)
return;
199 m_classInstance->call(m_funcSetCSEngine,
this);
200 }
catch (Sqrat::Exception e) {
202 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
206 if(variables.empty())
return;
208 for (
const auto& val : variables) {
209 auto obj = m_classInstance->get(val.c_str());
210 auto r_obj = obj.GetObject()._type;
218 value = obj.Cast<
const SQChar*>();
222 value = obj.Cast<SQBool>() ?
"t" :
"f";
226 value = std::to_string(obj.Cast<SQFloat>());
230 value = std::to_string(obj.Cast<SQInteger>());
239 m_variables.emplace_back(val, name, value);
243void CustomComponent::BindValue(CustomComponent::VARIABLE* variable,
const char* value)
const {
244 std::string type = variable->type;
245 std::string value_str = value;
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));
252 else if(type ==
"comp") {
253 SComponent* comp = gameObject->GetSComponentByHash(value_str);
254 MoreComponentFunc::BindComponentToSQInstance(comp, variable->name, m_classInstance);
256 else if(type ==
"gobj") {
257 SGameObject* obj = SGameObject::FindByID(value_str);
258 m_classInstance->set(variable->name.c_str(), obj);
260 else if(type ==
"nut") {
261 SComponent* comp = gameObject->GetSComponentByHash(value_str);
265 m_classInstance->set(variable->name.c_str(), comp_r->GetClassInstance());
void SetValue(std::string name_str, Arguments value) override