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 m_classInstance->call(m_funcInit);
38 }
catch (Sqrat::Exception e) {
40 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
46void CustomComponent::Tick(
float elapsedTime) {
48 if (m_specialization ==
nullptr)
return;
49 if (m_classInstance ==
nullptr)
return;
50 if (m_funcTick < 0)
return;
51 if (m_isError)
return;
54 m_classInstance->call(m_funcTick, elapsedTime);
56 catch (Sqrat::Exception e) {
58 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
64void CustomComponent::RegisterScript() {
66 if (m_className.empty())
return;
67 if (m_specialization !=
nullptr) SAFE_DELETE(m_specialization);
70 m_specialization =
new sqext::SQIClass(m_className.c_str());
71 m_specialization->bind(m_funcSetCSEngine,
"SetCSEngine");
73 catch (Sqrat::Exception e) {
74 m_funcSetCSEngine = -1;
75 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
79 m_specialization->bind(m_funcInit,
"Init");
81 catch (Sqrat::Exception e) { m_funcInit = -1; }
83 m_specialization->bind(m_funcTick,
"Tick");
85 catch (Sqrat::Exception e) { m_funcTick = -1; }
88 m_specialization->bind(m_funcExterminate,
"Destroy");
90 catch (Sqrat::Exception e) { m_funcExterminate = -1; }
95void CustomComponent::SetClassName(std::string name) {
96 auto asset = SResource::Get<SScriptObject>(std::move(name));
97 if (asset ==
nullptr)
return;
99 m_classID = asset->GetHash();
100 m_className = asset->GetScriptClassName();
103 CreateClassInstance(asset->GetVariables());
108std::string CustomComponent::SGetClassName()
const {
113bool CustomComponent::GetIsEnable()
const {
118void CustomComponent::SetIsEnable(
bool is_enable) {
119 isEnable = is_enable;
123void CustomComponent::Log(
const char* log) {
124 SafeLog::Log((std::string(log) +
'\n').c_str());
128SGameObject* CustomComponent::GetGameObject()
const {
135 if (!m_className.empty()) {
136 clone->SetClassName(m_classID);
142void CustomComponent::SetValue(std::string name_str, VariableBinder::Arguments value) {
143 if (name_str ==
"m_classID") {
144 SetClassName(ConvertSpaceStr(value[0],
true));
147 else if (name_str ==
"__variable__") {
148 for(
auto& val : m_variables) {
149 if(val.name == value[0]) {
150 val.value = value[1];
152 BindValue(&val, ConvertSpaceStr(value[1],
true).c_str());
158std::string CustomComponent::PrintValue()
const {
159 PRINT_START(
"component");
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);
169 PRINT_END(
"component");
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);
179 m_classInstance = m_specialization->NewPointer();
181 if (m_funcSetCSEngine < 0)
return;
182 m_classInstance->call(m_funcSetCSEngine,
this);
183 }
catch (Sqrat::Exception e) {
185 SafeLog::Log((Sqrat::LastErrorString(Sqrat::DefaultVM::Get()) +
'\n').c_str());
189 if(variables.empty())
return;
191 for (
const auto& val : variables) {
192 auto obj = m_classInstance->get(val.c_str());
193 auto r_obj = obj.GetObject()._type;
201 value = obj.Cast<
const SQChar*>();
205 value = obj.Cast<SQBool>() ?
"t" :
"f";
209 value = std::to_string(obj.Cast<SQFloat>());
213 value = std::to_string(obj.Cast<SQInteger>());
223 m_variables.emplace_back(val, name, value);
228void CustomComponent::BindValue(CustomComponent::VARIABLE* variable,
const char* value)
const {
230 std::string type = variable->type;
231 std::string value_str = value;
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));
238 else if(type ==
"comp") {
239 SComponent* comp = gameObject->GetSComponentByHash(value_str);
240 MoreComponentFunc::BindComponentToSQInstance(comp, variable->name, m_classInstance);
242 else if(type ==
"gobj") {
243 SGameObject* obj = SGameObject::FindByID(value_str);
244 m_classInstance->set(variable->name.c_str(), obj);
246 else if(type ==
"nut") {
247 SComponent* comp = gameObject->GetSComponentByHash(value_str);
251 m_classInstance->set(variable->name.c_str(), comp_r->GetClassInstance());