CSEngine
Loading...
Searching...
No Matches
ScriptMgr.cpp
1#include "ScriptMgr.h"
2
3#include <sstream>
4#include "sqrat.h"
5#include "sqrat/sqratVM.h"
6
7#include "../Util/AssetsDef.h"
8#include "../Util/Render/SMaterial.h"
9#include "../Component/LightComponent.h"
10#include "../Object/SScriptObject.h"
11#include "../Component/RenderComponent.h"
12#include "EngineCore.h"
13
14using namespace Sqrat;
15using namespace CSE;
16
17const char* CSEngineScript = " \n\
18class CSEngineScript { \n\
19//CSEngineComponent \n\
20CSEngine = null; \n\
21gameObject = null; \n\
22gameobject = null; \n\
23 \n\
24function SetCSEngine(component) { \n\
25 if(CSEngine != null) return; \n\
26 CSEngine = component; \n\
27 gameobject = CSEngine.GetGameObject(); \n\
28 gameObject = CSEngine.GetGameObject(); \n\
29} \n\
30function IsEnable(){ \n\
31return CSEngine.IsEnable(); \n\
32} \n\
33 \n\
34function SetEnable(enable) { \n\
35 CSEngine.SetEnable(enable); \n\
36} \n\
37 \n\
38function GetTransform() { \n\
39 return gameobject.GetTransform(); \n\
40} \n\
41function Log(log){ \n\
42CSEngine.Log(log); \n\
43} \n\
44 \n\
45 \n\
46}";
47
48#define COMPONENT_DEF(CLASSNAME) SQRComponentDef<CLASSNAME>(_SC(#CLASSNAME))
49#define COMPONENT_DEF_WITH_SQNAME(CLASSNAME, SQNAME) SQRComponentDef<CLASSNAME>(_SC(#SQNAME))
50
51ScriptMgr::ScriptMgr() = default;
52
53
54ScriptMgr::~ScriptMgr() {
55 ReleaseSqratObject();
56 sq_close(DefaultVM::Get());
57}
58
59
60void ScriptMgr::Init() {
61 HSQUIRRELVM vm;
62// Sqrat::SqratVM vm = Sqrat::SqratVM();
63 vm = sq_open(1024);
64 Sqrat::DefaultVM::Set(vm);
65// Sqrat::DefaultVM::Set(vm.GetVM());
66 sq_pushroottable(vm);
67 sqstd_register_mathlib(vm);
68 sq_pop(vm, 1);
69 //SquirrelVM::Init();
70 DefineClasses();
71 ReadScriptList();
72}
73
74
75void ScriptMgr::RegisterScript(const std::string& script) {
76
77 HSQUIRRELVM vm = DefaultVM::Get();
78
79 //register script
80 if (!script.empty()) {
81 Script compiledScript;
82 compiledScript.CompileString(script);
83 if (Sqrat::Error::Occurred(vm)) {
84 SafeLog::Log((_SC("Compile Failed: ") + Error::Message(vm)).c_str());
85 }
86
87 compiledScript.Run();
88 if (Sqrat::Error::Occurred(vm)) {
89 SafeLog::Log((_SC("Run Failed: ") + Error::Message(vm)).c_str());
90 }
91
92 compiledScript.Release();
93 }
94
95}
96
97SQInteger GetCustomComponentFunc(HSQUIRRELVM v) {
98 SQInteger args = sq_gettop(v);
99
100 if (args != 2) return 1;
101
102 SGameObject* game_object = Var<SGameObject*>(v, 1).value;
103 char* classname_str = Var<char*>(v, 2).value;
104 auto customComponent = game_object->GetCustomComponent(classname_str);
105
106 if (customComponent._type == OT_NULL) {
107 return 1;
108 }
109
110 sq_pushobject(v, customComponent);
111
112 return 1;
113
114
115}
116
117
118void ScriptMgr::DefineClasses(HSQUIRRELVM vm) {
119
120
121
122 //GameObject
123 SQRClassDef<SGameObject>(_SC("GameObject"), vm)
124 .Func(_SC("Find"), &SGameObject::Find)
125 .Func(_SC("GetTransform"), &SGameObject::GetTransform)
126 .Func(_SC("IsEnable"), &SGameObject::GetIsEnable)
127 .Func(_SC("SetEnable"), &SGameObject::SetIsEnable)
128 .Func(_SC("SetName"), &SGameObject::SetName)
129 .Func(_SC("GetName"), &SGameObject::GetName)
130 .SquirrelFunc(_SC("GetClass"), GetCustomComponentFunc);
131 //Components
132
143 SQRClassDef<TransformInterface>(_SC("Transform"), vm)
144 .Var(_SC("position"), &TransformInterface::m_position)
145 .Var(_SC("rotation"), &TransformInterface::m_rotation)
146 .Var(_SC("scale"), &TransformInterface::m_scale);
147
148
149 COMPONENT_DEF_WITH_SQNAME(CustomComponent, CSEngineComponent)
150 .Func(_SC("GetGameObject"), &CustomComponent::GetGameObject)
151 .Func(_SC("IsEnable"), &CustomComponent::GetIsEnable)
152 .Func(_SC("SetEnable"), &CustomComponent::SetIsEnable)
153 .Func(_SC("Log"), &CustomComponent::Log);
154
155 COMPONENT_DEF(RenderComponent)
156 .Func(_SC("GetMaterial"), &RenderComponent::GetMaterial)
157 .Func(_SC("SetEnable"), &RenderComponent::SetIsEnable)
158 .Func(_SC("GetEnable"), &RenderComponent::GetIsEnable);
159
160
161 COMPONENT_DEF(LightComponent)
162 .Func(_SC("SetLightType"), &LightComponent::SetLightType)
163 .Func(_SC("SetColor"), &LightComponent::SetColor)
164 .Func(_SC("GetColor"), &LightComponent::GetColor)
165 .Func(_SC("SetDirection"), &LightComponent::SetDirection)
166 .Func(_SC("GetDirection"), &LightComponent::GetDirection);
167
168 COMPONENT_DEF(CameraComponent)
169 .Func(_SC("SetTarget"), &CameraComponent::SetTarget)
170 .Func(_SC("SetTargetVector"), &CameraComponent::SetTargetVector)
171 .Func(_SC("SetUp"), &CameraComponent::SetUp)
172 .Func(_SC("SetOrtho"), &CameraComponent::SetOrtho)
173 .Func(_SC("SetZDepthRange"), &CameraComponent::SetZDepthRange)
174 .Func(_SC("SetPerspective"), &CameraComponent::SetPerspective);
175
176
177 //Util
178
179 SQRClassDef<vec2>(_SC("vec2"))
180 .Var(_SC("x"), &vec2::x)
181 .Var(_SC("y"), &vec2::y)
182 .Func(_SC("Set"), &vec2::Set);
183
184 SQRClassDef<vec3>(_SC("vec3"))
185 .Var(_SC("x"), &vec3::x)
186 .Var(_SC("y"), &vec3::y)
187 .Var(_SC("z"), &vec3::z)
188 .Func(_SC("Cross"), &vec3::Cross)
189 .Func(_SC("Dot"), &vec3::Dot)
190 .Func(_SC("Set"), &vec3::Set);
191
192 SQRClassDef<vec4>(_SC("vec4"))
193 .Var(_SC("x"), &vec4::x)
194 .Var(_SC("y"), &vec4::y)
195 .Var(_SC("z"), &vec4::z)
196 .Var(_SC("w"), &vec4::w)
197 .Func(_SC("Set"), &vec4::Set);
198
199 SQRClassDef<Quaternion>(_SC("Quaternion"))
200 .Var(_SC("x"), &Quaternion::x)
201 .Var(_SC("y"), &Quaternion::y)
202 .Var(_SC("z"), &Quaternion::z)
203 .Var(_SC("w"), &Quaternion::w)
204 .Func(_SC("Set"), &Quaternion::Set)
205 .Func(_SC("Clone"), &Quaternion::Clone)
206 .StaticFunc(_SC("AngleAxis"), &Quaternion::AngleAxis)
207 .Func(_SC("Rotate"), &Quaternion::Rotate)
208 .Func(_SC("Slerp"), &Quaternion::Slerp)
209 .Func(_SC("ToEulerAngle"), &Quaternion::ToEulerAngle);
210
211 SQRClassDef<SMaterial>(_SC("Material"))
212 .Func(_SC("SetInt"), &SMaterial::SetInt)
213 .Func(_SC("SetFloat"), &SMaterial::SetFloat)
214 .Func(_SC("SetVec3"), &SMaterial::SetVec3)
215 .Func(_SC("SetTexture"), &SMaterial::SetTexture);
216}
217
218
219void ScriptMgr::ReleaseSqratObject() {
220
221 for (auto obj : m_objects) {
222 obj->Release();
223 SAFE_DELETE(obj);
224 }
225
226}
227
228
229void ScriptMgr::ReadScriptList() {
230 auto assets = CORE->GetCore(ResMgr)->GetAssetReferences(AssetMgr::TYPE::SCRIPT);
231
232 //compile base script class
233 RegisterScript(CSEngineScript);
234
235 for (const auto& asset : assets) {
236 SResource::Create<SScriptObject>(asset);
237 }
238
239}