Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ScriptSystemWrapper Class Reference

Wraps an AngelScript object that is an implementation of ScriptSystem. More...

#include <ScriptSystemWrapper.h>

Public Member Functions

DLLEXPORT ScriptSystemWrapper (const std::string &name, asIScriptObject *impl)
 
DLLEXPORT ~ScriptSystemWrapper ()
 
 ScriptSystemWrapper (const ScriptSystemWrapper &other)=delete
 
ScriptSystemWrapperoperator= (const ScriptSystemWrapper &other)=delete
 
DLLEXPORT asIScriptObject * GetASImplementationObject ()
 Returns the ImplementationObject increasing refcount. More...
 
DLLEXPORT void Init (GameWorld *world)
 
DLLEXPORT void Release ()
 
DLLEXPORT void Run ()
 
DLLEXPORT void CreateAndDestroyNodes ()
 
DLLEXPORT void Clear ()
 
DLLEXPORT void Suspend ()
 
DLLEXPORT void Resume ()
 

Public Attributes

const std::string Name
 

Protected Member Functions

DLLEXPORT bool _CallMethodOnUs (const std::string &methodname)
 Helper for reducing copy pasting between the functions that don't need extra parameters. More...
 
DLLEXPORT void _ReleaseCachedFunctions ()
 

Detailed Description

Wraps an AngelScript object that is an implementation of ScriptSystem.

Definition at line 44 of file ScriptSystemWrapper.h.

Constructor & Destructor Documentation

◆ ScriptSystemWrapper() [1/2]

DLLEXPORT ScriptSystemWrapper::ScriptSystemWrapper ( const std::string &  name,
asIScriptObject *  impl 
)
Note
Doesn't increase reference on impl so caller needs to make sure that it is increased

Definition at line 12 of file ScriptSystemWrapper.cpp.

13  :
14  Name(name),
15  ImplementationObject(impl)
16 {
17  if(!ImplementationObject)
18  throw InvalidArgument("ScriptSystemWrapper not given an angelscript object");
19 }

◆ ~ScriptSystemWrapper()

DLLEXPORT ScriptSystemWrapper::~ScriptSystemWrapper ( )

Definition at line 21 of file ScriptSystemWrapper.cpp.

22 {
24 
25  if(ImplementationObject) {
26  LOG_ERROR("ScriptSystemWrapper: Release has not been called before destructor");
27  ImplementationObject->Release();
28  }
29 }
#define LOG_ERROR(x)
Definition: Define.h:84

◆ ScriptSystemWrapper() [2/2]

Leviathan::ScriptSystemWrapper::ScriptSystemWrapper ( const ScriptSystemWrapper other)
delete

Member Function Documentation

◆ _CallMethodOnUs()

DLLEXPORT bool Leviathan::ScriptSystemWrapper::_CallMethodOnUs ( const std::string &  methodname)
protected

Helper for reducing copy pasting between the functions that don't need extra parameters.

◆ _ReleaseCachedFunctions()

DLLEXPORT void ScriptSystemWrapper::_ReleaseCachedFunctions ( )
protected

Must be called to not leak the cached function pointers (RunMethod, CreateAndDestroyNodesMethod)

Definition at line 206 of file ScriptSystemWrapper.cpp.

207 {
208 
209  if(RunMethod) {
210  RunMethod->Release();
211  RunMethod = nullptr;
212  }
213 
214  if(CreateAndDestroyNodesMethod) {
215  CreateAndDestroyNodesMethod->Release();
216  CreateAndDestroyNodesMethod = nullptr;
217  }
218 }

◆ Clear()

DLLEXPORT void ScriptSystemWrapper::Clear ( )

Definition at line 143 of file ScriptSystemWrapper.cpp.

144 {
145  asIScriptFunction* func = ImplementationObject->GetObjectType()->GetMethodByName("Clear");
146 
147  if(!func) {
148 
149  LOG_ERROR("Script system(" + Name + "): failed to find Clear method on as object");
150  return;
151  }
152 
153  ScriptRunningSetup setup;
154  auto result =
155  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
156  ->RunScriptMethod<void>(setup, func, ImplementationObject);
157 
158  if(result.Result != SCRIPT_RUN_RESULT::Success) {
159 
160  LOG_ERROR("Script system(" + Name + "): failed to call Clear");
161  return;
162  }
163 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ CreateAndDestroyNodes()

DLLEXPORT void ScriptSystemWrapper::CreateAndDestroyNodes ( )

Definition at line 114 of file ScriptSystemWrapper.cpp.

115 {
116  // For performance reasons this might be good to store
117  if(!CreateAndDestroyNodesMethod) {
118 
119  CreateAndDestroyNodesMethod =
120  ImplementationObject->GetObjectType()->GetMethodByName("CreateAndDestroyNodes");
121  CreateAndDestroyNodesMethod->AddRef();
122  }
123 
124  if(!CreateAndDestroyNodesMethod) {
125 
126  LOG_ERROR("Script system(" + Name +
127  "): failed to find CreateAndDestroyNodes method on as object");
128  return;
129  }
130 
131  ScriptRunningSetup setup;
132  auto result =
133  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
134  ->RunScriptMethod<void>(setup, CreateAndDestroyNodesMethod, ImplementationObject);
135 
136  if(result.Result != SCRIPT_RUN_RESULT::Success) {
137 
138  LOG_ERROR("Script system(" + Name + "): failed to call CreateAndDestroyNodes");
139  return;
140  }
141 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ GetASImplementationObject()

DLLEXPORT asIScriptObject * ScriptSystemWrapper::GetASImplementationObject ( )

Returns the ImplementationObject increasing refcount.

Definition at line 31 of file ScriptSystemWrapper.cpp.

32 {
33  if(ImplementationObject)
34  ImplementationObject->AddRef();
35  return ImplementationObject;
36 }

◆ Init()

DLLEXPORT void ScriptSystemWrapper::Init ( GameWorld world)

Definition at line 38 of file ScriptSystemWrapper.cpp.

39 {
40  asIScriptFunction* func = ImplementationObject->GetObjectType()->GetMethodByName("Init");
41 
42  if(!func) {
43 
44  LOG_ERROR("Script system(" + Name + "): failed to find Init method on as object");
45  return;
46  }
47 
48  ScriptRunningSetup setup;
49  auto result =
50  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
51  ->RunScriptMethod<void>(setup, func, ImplementationObject, world);
52 
53  if(result.Result != SCRIPT_RUN_RESULT::Success) {
54 
55  LOG_ERROR("Script system(" + Name + "): failed to call Init");
56  return;
57  }
58 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ operator=()

ScriptSystemWrapper& Leviathan::ScriptSystemWrapper::operator= ( const ScriptSystemWrapper other)
delete

◆ Release()

DLLEXPORT void ScriptSystemWrapper::Release ( )
Note
This also clears ImplementationObject ptr.

Definition at line 59 of file ScriptSystemWrapper.cpp.

60 {
61  asIScriptFunction* func =
62  ImplementationObject->GetObjectType()->GetMethodByName("Release");
63 
64  if(!func) {
65 
66  LOG_ERROR("Script system(" + Name + "): failed to find Release method on as object");
67  return;
68  }
69 
70  ScriptRunningSetup setup;
71  auto result =
72  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
73  ->RunScriptMethod<void>(setup, func, ImplementationObject);
74 
75  if(result.Result != SCRIPT_RUN_RESULT::Success) {
76 
77  LOG_ERROR("Script system(" + Name + "): failed to call Release");
78  return;
79  }
80 
82 
83  ImplementationObject->Release();
84  ImplementationObject = nullptr;
85 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ Resume()

DLLEXPORT void ScriptSystemWrapper::Resume ( )

Definition at line 186 of file ScriptSystemWrapper.cpp.

187 {
188  asIScriptFunction* func = ImplementationObject->GetObjectType()->GetMethodByName("Resume");
189 
190  // This is optional
191  if(!func)
192  return;
193 
194  ScriptRunningSetup setup;
195  auto result =
196  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
197  ->RunScriptMethod<void>(setup, func, ImplementationObject);
198 
199  if(result.Result != SCRIPT_RUN_RESULT::Success) {
200 
201  LOG_ERROR("Script system(" + Name + "): failed to call Resume");
202  return;
203  }
204 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ Run()

DLLEXPORT void ScriptSystemWrapper::Run ( )

Definition at line 87 of file ScriptSystemWrapper.cpp.

88 {
89  // For performance reasons this might be good to store
90  if(!RunMethod) {
91 
92  RunMethod = ImplementationObject->GetObjectType()->GetMethodByName("Run");
93  RunMethod->AddRef();
94  }
95 
96  if(!RunMethod) {
97 
98  LOG_ERROR("Script system(" + Name + "): failed to find Run method on as object");
99  return;
100  }
101 
102  ScriptRunningSetup setup;
103  auto result =
104  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
105  ->RunScriptMethod<void>(setup, RunMethod, ImplementationObject);
106 
107  if(result.Result != SCRIPT_RUN_RESULT::Success) {
108 
109  LOG_ERROR("Script system(" + Name + "): failed to call Run");
110  return;
111  }
112 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

◆ Suspend()

DLLEXPORT void ScriptSystemWrapper::Suspend ( )

Definition at line 165 of file ScriptSystemWrapper.cpp.

166 {
167  asIScriptFunction* func =
168  ImplementationObject->GetObjectType()->GetMethodByName("Suspend");
169 
170  // This is optional
171  if(!func)
172  return;
173 
174  ScriptRunningSetup setup;
175  auto result =
176  static_cast<ScriptExecutor*>(ImplementationObject->GetEngine()->GetUserData())
177  ->RunScriptMethod<void>(setup, func, ImplementationObject);
178 
179  if(result.Result != SCRIPT_RUN_RESULT::Success) {
180 
181  LOG_ERROR("Script system(" + Name + "): failed to call Suspend");
182  return;
183  }
184 }
#define LOG_ERROR(x)
Definition: Define.h:84
Handles ScriptModule creation and AngelScript code execution.

Member Data Documentation

◆ Name

const std::string Leviathan::ScriptSystemWrapper::Name

Definition at line 75 of file ScriptSystemWrapper.h.


The documentation for this class was generated from the following files: