CSEngine
Loading...
Searching...
No Matches
SScriptObject.cpp
1//
2// Created by ounols on 19. 10. 27.
3//
4
5#include "SScriptObject.h"
6
7
8#include <iostream>
9#include "sqrat.h"
10#include "sqrat/sqratVM.h"
11#include "../Util/AssetsDef.h"
12#include "../Util/MoreString.h"
13
14using namespace CSE;
15using namespace Sqrat;
16
17SScriptObject::SScriptObject() = default;
18
19SScriptObject::~SScriptObject() = default;
20
21void SScriptObject::Exterminate() {
22
23}
24
25void SScriptObject::Init(const AssetMgr::AssetReference* asset) {
26 RemakeScript(asset->name_path);
27}
28
29void SScriptObject::RegisterScript(const std::string& script) {
30 HSQUIRRELVM vm = DefaultVM::Get();
31
32 //register script
33 if (!script.empty()) {
34 Script compiledScript;
35 compiledScript.CompileString(script);
36 if (Sqrat::Error::Occurred(vm)) {
37#ifdef __ANDROID__
38// LOGE("Compile Failed : %s", Error::Message(vm).c_str());
39#else
40 std::cout << "Compile Failed : " << Error::Message(vm) << '\n';
41#endif
42 }
43
44 compiledScript.Run();
45 if (Sqrat::Error::Occurred(vm)) {
46#ifdef __ANDROID__
47// LOGE("Run Failed : %s", Error::Message(vm).c_str());
48#else
49 std::cout << "Run Failed : " << Error::Message(vm) << '\n';
50#endif
51 }
52
53 compiledScript.Release();
54 }
55}
56
57void SScriptObject::RemakeScript(const std::string& path) {
58 std::string script_str = AssetMgr::LoadAssetFile(path);
59
60 GetVariables(script_str);
61
62 //replace GetComponent function
63 script_str = ReplaceFunction(script_str, "GetComponent<", ">()", "GetComponent_", "_()");
64
65 //replace GetCustomComponent(GetClass) function
66 script_str = ReplaceFunction(script_str, "GetClass<", ">()", "GetClass(\"", "\")");
67
68 RegisterScript(script_str);
69}
70
71void SScriptObject::GetVariables(const std::string& str) {
72
73 auto split_line = split(str, '\n');
74 int level = -1;
75 bool isComment = false;
76
77 //split line
78 for(auto line : split_line) {
79 line = trim(line);
80 if(line.find("//") != std::string::npos) continue;
81 if(isComment && line.find("*/") != std::string::npos) {
82 isComment = false;
83 line = line.substr(line.find("*/") + 2);
84 }
85 else if(isComment || line.find("/*") != std::string::npos) {
86 if(isComment) continue;
87
88 isComment = true;
89 line = line.substr(0, line.find("/*"));
90 }
91
92 //split start of brace
93 bool isBraceStart = line.find('{') != std::string::npos;
94 auto split_bs = split(line, '{');
95
96
97 for(const auto& bs : split_bs) {
98
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;
103
104 m_className = trim(line.substr(start_index, end_index));
105 }
106 }
107
108 //split end of brace
109 bool isBraceEnd = line.find('}') != std::string::npos;
110 auto split_be = split(bs, '}');
111
112 for(const auto& be : split_be) {
113
114 if(level == 1) {
115 //split semicolon
116 auto split_end = split(be, ';');
117 for(auto result : split_end) {
118 if(result.empty()) continue;
119 result = trim(result);
120
121 if(result.find("function") != std::string::npos) continue;
122 int index = result.find('=');
123 m_variables.push_back(trim(result.substr(0, index)));
124 }
125 }
126
127 if(isBraceEnd) level--;
128 }
129
130 if(isBraceStart) level++;
131 }
132
133 }
134
135}
136
137std::vector<std::string> SScriptObject::GetVariables() const {
138 return m_variables;
139}