Leviathan  0.8.0.0
Leviathan game engine
ScriptModule.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2018 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "AccessMask.h"
7 
8 #include "Common/ThreadSafe.h"
10 
11 #include "add_on/scriptbuilder/scriptbuilder.h"
12 #include "angelscript.h"
13 
14 #include "boost/thread/mutex.hpp"
15 
16 #define SCRIPTMODULE_LISTENFORFILECHANGES
17 
18 namespace Leviathan {
19 
21 
29 };
30 
31 
34  ValidListenerData(asIScriptFunction* funcptr, std::string* name, std::string* metadataend);
35  ValidListenerData(asIScriptFunction* funcptr, std::string* name, std::string* metadataend,
36  std::string* generictypename);
38 
39  asIScriptFunction* FuncPtr;
40  std::unique_ptr<std::string> ListenerName;
41  std::unique_ptr<std::string> RestOfMeta;
42  std::unique_ptr<std::string> GenericTypeName;
43 };
44 
47 
49  DLLEXPORT ScriptSourceFileData(const std::string& file, int line, const std::string& code);
50 
51 
52  std::string SourceFile;
53  int StartLine;
54 
57  std::shared_ptr<std::string> SourceCode;
58 };
59 
60 
61 // \brief Holds everything related to "one" script needed to run it an build it
62 class ScriptModule : public ThreadSafe, public std::enable_shared_from_this<ScriptModule> {
63  // friend to be able to delete static objects //
64  friend ScriptExecutor;
65 
66 public:
68  asIScriptEngine* engine, const std::string& name, int id, const std::string& source);
70 
74  DLLEXPORT inline void SetAccessMask(AccessFlags access)
75  {
76  AccessMask = access;
77  }
78 
82  {
83  AccessMask |= newaccess;
84  }
85 
88  DLLEXPORT asIScriptModule* GetModule(Lock& guard);
89 
90  DLLEXPORT inline asIScriptModule* GetModule()
91  {
92  GUARD_LOCK();
93  return GetModule(guard);
94  }
95 
96  DLLEXPORT std::shared_ptr<ScriptScript> GetScriptInstance();
97 
98 
99  DLLEXPORT inline std::string GetName()
100  {
101  return Name;
102  }
103 
104 
106  DLLEXPORT void Release();
107 
109  DLLEXPORT const std::string& GetModuleName() const;
110 
111  DLLEXPORT inline int GetID()
112  {
113  return ID;
114  }
115 
116  DLLEXPORT const std::string& GetSource() const
117  {
118  return Source;
119  }
120 
121  DLLEXPORT inline const std::string& GetIncompleteSourceCode()
122  {
123  return ObjectFileLoadedScriptCode;
124  }
125 
126 
128 
130  const std::string& listenername, const std::string* generictype = NULL);
131 
133  std::vector<std::shared_ptr<ValidListenerData>>& receiver);
134 
136  const std::string& listenername, const std::string* generictype = NULL);
137 
138  DLLEXPORT std::string GetInfoString();
139 
140  DLLEXPORT inline void SetBuildState(const SCRIPTBUILDSTATE& state)
141  {
142  ScriptState = state;
143  }
144 
147  DLLEXPORT size_t GetScriptSegmentCount() const;
148 
153  DLLEXPORT std::shared_ptr<ScriptSourceFileData> GetScriptSegment(size_t index) const;
154 
155 
160  const std::string& file, int line, const std::string& code)
161  {
162  return AddScriptSegment(std::make_shared<ScriptSourceFileData>(file, line, code));
163  }
164 
166  DLLEXPORT bool AddScriptSegment(std::shared_ptr<ScriptSourceFileData> data);
167 
168 
171  DLLEXPORT bool AddScriptSegmentFromFile(const std::string& file);
172 
173 
179 
181  DLLEXPORT void SetAsInvalid();
182 
183 
185 
186  // static map that contains listener names //
187  static const std::map<std::string, int> ListenerNameType;
188 
189  // static include resolver for scripts //
191  const char* include, const char* from, CScriptBuilder* builder, void* userParam);
192 
194  DLLEXPORT static std::string ResolvePathToScriptFile(const std::string& inputfilename,
195  const std::string& relativepath, bool checkworkdirrelative = true);
196 
200  DLLEXPORT bool OnAddedToBridge(std::shared_ptr<ScriptArgumentsProviderBridge> bridge);
201 
202 private:
203  ScriptModule(const ScriptModule& other) {}
204 
205  void _FillParameterDataObject(
206  int typeofas, int* paramtypeid, std::string* paramdecl, int* datablocktype);
207 
208  void _BuildListenerList(Lock& guard);
209 
212  void _ProcessMetadataForFunc(asIScriptFunction* func, asIScriptModule* mod);
213 
214  std::map<std::string, std::shared_ptr<ValidListenerData>>::iterator _GetIteratorOfListener(
215  Lock& guard, const std::string& listenername, const std::string* generictype = NULL);
216 
218  void _BuildTheModule(Lock& guard);
219 
220 #ifdef SCRIPTMODULE_LISTENFORFILECHANGES
221 
224  void _StartMonitoringFiles(Lock& guard);
225 
229  void _StopFileMonitoring(Lock& guard);
230 
231 
234  void _AddFileToMonitorIfNotAlready(const std::string& file);
235 
236 #endif // SCRIPTMODULE_LISTENFORFILECHANGES
237 
238 
239  // ------------------------------------ //
240 
241  std::string Name;
242  std::string ModuleName;
243  std::string Source;
244  std::string ObjectFileLoadedScriptCode;
245  int ID;
246 
247 
249  bool ListenerDataBuilt;
250 
252  AccessFlags AccessMask = DefaultAccessFlags;
253 
255  CScriptBuilder* ScriptBuilder;
256 
257 
259  std::vector<std::shared_ptr<ScriptSourceFileData>> ScriptSourceSegments;
260 
261 
263  asIScriptModule* ASModule = nullptr;
264 
265 
267  std::map<std::string, std::shared_ptr<ValidListenerData>> FoundListenerFunctions;
268 
269 
271  static int LatestAssigned;
272 
273 
276  static Mutex ModuleBuildingMutex;
277 
280  std::shared_ptr<ScriptArgumentsProviderBridge> ArgsBridge;
281 
282  // Data for file listening //
283 #ifdef SCRIPTMODULE_LISTENFORFILECHANGES
284 
287  std::vector<int> FileListeners;
288 
289 
292  struct AutomonitoredFile {
293 
294  AutomonitoredFile(const std::string& file) : File(new std::string(file)), Added(false)
295  {}
296 
297  std::unique_ptr<std::string> File;
298  bool Added;
299  };
300 
302  std::vector<std::unique_ptr<AutomonitoredFile>> AlreadyMonitoredFiles;
303 
304 
306  void _FileChanged(const std::string& file, ResourceFolderListener& caller);
307 
308 #endif // SCRIPTMODULE_LISTENFORFILECHANGES
309 };
310 
311 } // namespace Leviathan
DLLEXPORT std::string GetName()
Definition: ScriptModule.h:99
some data that is stored when a listener is found
Definition: ScriptModule.h:33
std::unique_ptr< std::string > RestOfMeta
Definition: ScriptModule.h:41
DLLEXPORT ScriptSourceFileData(const std::string &file, int line, const std::string &code)
static const std::map< std::string, int > ListenerNameType
Definition: ScriptModule.h:187
Allows the inherited object to be locked.
Definition: ThreadSafe.h:137
DLLEXPORT FORCE_INLINE bool AddScriptSegment(const std::string &file, int line, const std::string &code)
Adds a new script section.
Definition: ScriptModule.h:159
Represents a section of script source file.
Definition: ScriptModule.h:46
Handles ScriptModule creation and AngelScript code execution.
uint32_t AccessFlags
Definition: AccessMask.h:54
asIScriptFunction * FuncPtr
Definition: ScriptModule.h:39
#define FORCE_INLINE
Definition: Include.h:95
DLLEXPORT const std::string & GetIncompleteSourceCode()
Definition: ScriptModule.h:121
DLLEXPORT void PrintFunctionsInModule()
DLLEXPORT bool ReLoadModuleCode()
Rebuilds the module and tries to restore data.
DLLEXPORT size_t GetScriptSegmentCount() const
Gets the number of code segments.
static DLLEXPORT int ScriptModuleIncludeCallback(const char *include, const char *from, CScriptBuilder *builder, void *userParam)
DLLEXPORT const std::string & GetSource() const
Definition: ScriptModule.h:116
DLLEXPORT void Release()
Releases the internal resources.
std::unique_ptr< std::string > ListenerName
Definition: ScriptModule.h:40
DLLEXPORT void SetAsInvalid()
Sets the module as invalid to avoid usage.
std::shared_ptr< std::string > SourceCode
Definition: ScriptModule.h:57
ValidListenerData(asIScriptFunction *funcptr, std::string *name, std::string *metadataend)
constexpr AccessFlags DefaultAccessFlags
Definition: AccessMask.h:55
DLLEXPORT void GetListOfListeners(std::vector< std::shared_ptr< ValidListenerData >> &receiver)
std::mutex Mutex
Definition: ThreadSafe.h:16
DLLEXPORT std::shared_ptr< ScriptSourceFileData > GetScriptSegment(size_t index) const
Gets the data associated with a code segment.
DLLEXPORT std::shared_ptr< ScriptScript > GetScriptInstance()
DLLEXPORT bool OnAddedToBridge(std::shared_ptr< ScriptArgumentsProviderBridge > bridge)
Call when this module is added to a bridge.
static DLLEXPORT std::string ResolvePathToScriptFile(const std::string &inputfilename, const std::string &relativepath, bool checkworkdirrelative=true)
Finds a path to source file or returns an empty string.
DLLEXPORT const std::string & GetModuleName() const
Gets the name of the internal AngelScript module.
std::unique_ptr< std::string > GenericTypeName
Definition: ScriptModule.h:42
DLLEXPORT void SetAccessMask(AccessFlags access)
Sets the access mask to be used for this script.
Definition: ScriptModule.h:74
DLLEXPORT std::string GetListeningFunctionName(const std::string &listenername, const std::string *generictype=NULL)
DLLEXPORT void SetBuildState(const SCRIPTBUILDSTATE &state)
Definition: ScriptModule.h:140
DLLEXPORT ScriptModule(asIScriptEngine *engine, const std::string &name, int id, const std::string &source)
#define DLLEXPORT
Definition: Include.h:84
DLLEXPORT void DeleteThisModule()
DLLEXPORT bool DoesListenersContainSpecificListener(const std::string &listenername, const std::string *generictype=NULL)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
DLLEXPORT int GetID()
Definition: ScriptModule.h:111
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
DLLEXPORT asIScriptModule * GetModule()
Definition: ScriptModule.h:90
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
DLLEXPORT void AddAccessRight(AccessFlags newaccess)
Adds flags to the AccessMask.
Definition: ScriptModule.h:81
DLLEXPORT std::string GetInfoString()
DLLEXPORT bool AddScriptSegmentFromFile(const std::string &file)
Adds an entire file as a script segment.