Leviathan  0.8.0.0
Leviathan game engine
GameModule.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 "Common/ThreadSafe.h"
8 #include "Script/ScriptModule.h"
9 // Required for template based script running
10 #include "Script/ScriptExecutor.h"
11 
12 namespace Leviathan {
13 
14 class GameModuleLoader;
15 
18 class GameModule : public ReferenceCounted, public EventableScriptObject {
19  friend GameModuleLoader;
20  // This is to make MakeShared work, but it should only be called from GameModuleLoader
21  friend ReferenceCounted;
22 
23 protected:
24  DLLEXPORT GameModule(const std::string& filepath, const std::string& ownername,
25  GameModuleLoader* loadedthrough);
26 
28  DLLEXPORT bool Init();
29 
30 public:
32 
38  DLLEXPORT void ReleaseScript();
39 
41  DLLEXPORT inline auto GetScriptModule()
42  {
43  return ScriptMain;
44  }
45 
46  // Used to actually use the module //
47 
51  template<typename ReturnT, class... Args>
53  ScriptRunningSetup& setup, bool passself, Args&&... args)
54  {
55  if(!Scripting)
57 
58  if(passself) {
59  return ScriptExecutor::Get()->RunScript<ReturnT>(
60  Scripting->GetModuleSafe(), setup, this, std::forward<Args>(args)...);
61  } else {
62  return ScriptExecutor::Get()->RunScript<ReturnT>(
63  Scripting->GetModuleSafe(), setup, std::forward<Args>(args)...);
64  }
65  }
66 
67 
69  DLLEXPORT std::string GetDescription(bool full);
70 
72  DLLEXPORT const auto& GetName() const
73  {
74  return Name;
75  }
76 
78  DLLEXPORT const auto& GetExportFiles() const
79  {
80  return ExportFiles;
81  }
82 
85  {
86  return IsCurrentlyInitializing;
87  }
88 
90 
91 protected:
93  ScriptRunningSetup& sargs, Event* event, GenericEvent* event2) override;
94 
95 private:
96  AccessFlags ExtraAccess = 0;
97 
98  std::string OwnerName;
99  std::string LoadedFromFile;
100 
101  std::shared_ptr<ScriptModule> ScriptMain;
102 
103  // Properties loaded from the file //
104  std::string Name;
105  std::string Version;
106  std::vector<std::string> SourceFiles;
107  std::vector<std::string> ImportModules;
108  std::vector<std::string> ExportFiles;
109 
110 
114  GameModuleLoader* Loader;
115 
117  bool IsCurrentlyInitializing = false;
118 
120  std::vector<GameModule::pointer> LoadedImportedModules;
121 };
122 
123 } // namespace Leviathan
124 
125 #ifdef LEAK_INTO_GLOBAL
127 #endif
ScriptRunResult< ReturnT > RunScript(const std::shared_ptr< ScriptModule > &module, ScriptRunningSetup &parameters, Args &&... args)
Runs a function in a script.
Class that represents a statically defined event.
Definition: Event.h:138
DLLEXPORT bool IsInitializing() const
Used to detect circular dependencies by GameModuleLoader.
Definition: GameModule.h:84
Definition: lz4.c:260
DLLEXPORT std::string GetDescription(bool full)
Definition: GameModule.cpp:303
DLLEXPORT auto GetScriptModule()
Definition: GameModule.h:41
DLLEXPORT const auto & GetExportFiles() const
Definition: GameModule.h:78
REFERENCE_COUNTED_PTR_TYPE(GameModule)
uint32_t AccessFlags
Definition: AccessMask.h:54
ScriptRunResult< ReturnT > ExecuteOnModule(ScriptRunningSetup &setup, bool passself, Args &&... args)
Definition: GameModule.h:52
Holds a result of the new script run method.
static DLLEXPORT ScriptExecutor * Get()
virtual ScriptRunResult< int > _DoCallWithParams(ScriptRunningSetup &sargs, Event *event, GenericEvent *event2) override
Definition: GameModule.cpp:310
DLLEXPORT void ReleaseScript()
Releases the script.
Definition: GameModule.cpp:285
Class that represents a dynamically defined event.
Definition: Event.h:173
DLLEXPORT const auto & GetName() const
Definition: GameModule.h:72
DLLEXPORT bool Init()
Makes the scripts usable.
Definition: GameModule.cpp:171
DLLEXPORT GameModule(const std::string &filepath, const std::string &ownername, GameModuleLoader *loadedthrough)
Definition: GameModule.cpp:13
#define DLLEXPORT
Definition: Include.h:84
Represents a scriptable part of a program.
Definition: GameModule.h:18
DLLEXPORT ~GameModule()
Definition: GameModule.cpp:154
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
Manages loading GameModule objects to make sure that each is loaded only once.