5#include "SScriptObject.h"
10#include "sqrat/sqratVM.h"
11#include "../Util/AssetsDef.h"
12#include "../Util/MoreString.h"
17SScriptObject::SScriptObject() =
default;
19SScriptObject::~SScriptObject() =
default;
21void SScriptObject::Exterminate() {
26 RemakeScript(asset->name_path);
29void SScriptObject::RegisterScript(
const std::string& script) {
30 HSQUIRRELVM vm = DefaultVM::Get();
33 if (!script.empty()) {
34 Script compiledScript;
35 compiledScript.CompileString(script);
36 if (Sqrat::Error::Occurred(vm)) {
40 std::cout <<
"Compile Failed : " << Error::Message(vm) <<
'\n';
45 if (Sqrat::Error::Occurred(vm)) {
49 std::cout <<
"Run Failed : " << Error::Message(vm) <<
'\n';
53 compiledScript.Release();
57void SScriptObject::RemakeScript(
const std::string& path) {
58 std::string script_str = AssetMgr::LoadAssetFile(path);
60 GetVariables(script_str);
63 script_str = ReplaceFunction(script_str,
"GetComponent<",
">()",
"GetComponent_",
"_()");
66 script_str = ReplaceFunction(script_str,
"GetClass<",
">()",
"GetClass(\"",
"\")");
68 RegisterScript(script_str);
71void SScriptObject::GetVariables(
const std::string& str) {
73 auto split_line = split(str,
'\n');
75 bool isComment =
false;
78 for(
auto line : split_line) {
80 if(line.find(
"//") != std::string::npos)
continue;
81 if(isComment && line.find(
"*/") != std::string::npos) {
83 line = line.substr(line.find(
"*/") + 2);
85 else if(isComment || line.find(
"/*") != std::string::npos) {
86 if(isComment)
continue;
89 line = line.substr(0, line.find(
"/*"));
93 bool isBraceStart = line.find(
'{') != std::string::npos;
94 auto split_bs = split(line,
'{');
97 for(
const auto& bs : split_bs) {
99 if(level == 0 && m_className.empty()) {
100 if(line.find(
"class") != std::string::npos) {
101 int start_index = line.find(
"class") + 5;
102 int end_index = line.find(
"extends", start_index) - 5;
104 m_className = trim(line.substr(start_index, end_index));
109 bool isBraceEnd = line.find(
'}') != std::string::npos;
110 auto split_be = split(bs,
'}');
112 for(
const auto& be : split_be) {
116 auto split_end = split(be,
';');
117 for(
auto result : split_end) {
118 if(result.empty())
continue;
119 result = trim(result);
121 if(result.find(
"function") != std::string::npos)
continue;
122 int index = result.find(
'=');
123 m_variables.push_back(trim(result.substr(0, index)));
127 if(isBraceEnd) level--;
130 if(isBraceStart) level++;
137std::vector<std::string> SScriptObject::GetVariables()
const {