NAP
core.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "modulemanager.h"
9 #include "resourcemanager.h"
10 #include "service.h"
11 #include "coreextension.h"
12 #include "projectinfo.h"
13 #include "timer.h"
14 
15 // External Includes
16 #include <rtti/factory.h>
17 #include <rtti/rtti.h>
18 #include <rtti/deserializeresult.h>
19 #include <unordered_set>
20 #include <utility/dllexport.h>
21 #include <unordered_map>
22 #include <vector>
23 
24 // Default name to use when writing the file that contains all the settings for the NAP services.
25 inline constexpr char DEFAULT_SERVICE_CONFIG_FILENAME[] = "config.json";
26 inline constexpr char PROJECT_INFO_FILENAME[] = "project.json";
27 
28 // Build configuration eg. "Clang-Debug-x86_64"
29 #define STRINGIZE(x) #x
30 #define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
31 inline constexpr char sBuildConf[] = STRINGIZE_VALUE_OF(NAP_BUILD_CONF);
32 inline constexpr char sBuildType[] = STRINGIZE_VALUE_OF(NAP_BUILD_TYPE);
33 inline constexpr char sBuildArch[] = STRINGIZE_VALUE_OF(NAP_BUILD_ARCH);
34 
35 namespace nap
36 {
37  using ServiceConfigMap = std::unordered_map<rtti::TypeInfo, ServiceConfiguration*>;
38 
82  class NAPAPI Core final
83  {
84  RTTI_ENABLE()
85  public:
86  // Unique services handle.
87  // Ensures services are destroyed after being initialized.
88  class Services;
89  using ServicesHandle = std::unique_ptr<Services>;
90 
94  Core();
95 
102  Core(std::unique_ptr<CoreExtension> coreExtension);
103 
107  virtual ~Core();
108 
125  bool initializeEngine(utility::ErrorState& error);
126 
145  bool initializeEngine(const std::string& projectInfofile, ProjectInfo::EContext context, utility::ErrorState& error);
146 
155  Core::ServicesHandle initializeServices(utility::ErrorState& errorState);
156 
161  bool isInitialized() const;
162 
166  bool initializePython(utility::ErrorState& error);
167 
171  void start();
172 
181  double update(std::function<void(double)>& updateFunction);
182 
187  ResourceManager* getResourceManager() { return mResourceManager.get(); }
188 
192  const ModuleManager& getModuleManager() const { return *mModuleManager; }
193 
197  uint32 getTicks() const;
198 
202  double getElapsedTime() const;
203 
207  SteadyTimeStamp getStartTime() const;
208 
212  float getFramerate() const { return mFramerate; }
213 
219  Service* getService(const rtti::TypeInfo& type);
220 
226  const Service* getService(const rtti::TypeInfo& type) const;
227 
233  Service* getService(const std::string& type);
234 
244  template <typename T>
245  T* getService();
246 
256  template <typename T>
257  const T* getService() const;
258 
265  template <typename T>
266  const T& getExtension() const;
267 
271  template <typename T>
272  bool hasExtension() const;
273 
281  bool findProjectFilePath(const std::string& filename, std::string& foundFilePath) const;
282 
287  const nap::ProjectInfo* getProjectInfo() const;
288 
295  bool loadPathMapping(nap::ProjectInfo& projectInfo, nap::utility::ErrorState& err);
296 
304  bool writeConfigFile(const std::string& path, utility::ErrorState& errorState, bool linkToProjectInfo = true);
305 
310  std::vector<const ServiceConfiguration*> getServiceConfigs() const;
311 
315  void setupPlatformSpecificEnvironment();
316 
330  class NAPAPI Services final
331  {
332  public:
336  ~Services();
337 
341  bool initialized() { return mInitialized; }
342 
343  private:
344  friend class Core;
350  Services(Core& core, utility::ErrorState& error);
351  nap::Core& mCore;
352  bool mInitialized = false;
353  };
354 
355  private:
361  bool findProjectInfoFile(std::string& foundFilePath) const;
362 
370  bool createServices(const nap::ProjectInfo& projectInfo, utility::ErrorState& errorState);
371 
380  bool addService(const rtti::TypeInfo& type, ServiceConfiguration* configuration, std::vector<Service*>& outServices, utility::ErrorState& errorState);
381 
390  bool loadServiceConfigurations(nap::utility::ErrorState& err);
391 
401  bool loadServiceConfiguration(const std::string& filename, rtti::DeserializeResult& deserialize_result, utility::ErrorState& errorState);
402 
408  void preResourcesLoaded();
409 
415  void postResourcesLoaded();
416 
420  void calculateFramerate(double deltaTime);
421 
426  void setupPythonEnvironment();
427 
435  bool loadProjectInfo(std::string projectFilename, ProjectInfo::EContext context, nap::utility::ErrorState& error);
436 
442  nap::ServiceConfiguration* findServiceConfig(rtti::TypeInfo serviceType) const;
443 
450  bool addServiceConfig(std::unique_ptr<nap::ServiceConfiguration> serviceConfig);
451 
452  // Manages all the loaded modules
453  std::unique_ptr<ModuleManager> mModuleManager = nullptr;
454 
455  // Manages all the objects in core
456  std::unique_ptr<ResourceManager> mResourceManager = nullptr;
457 
458  // Holds on to the current project's configuration
459  std::unique_ptr<nap::ProjectInfo> mProjectInfo = nullptr;
460 
461  // Sorted service nodes, set after init
462  std::vector<std::unique_ptr<Service>> mServices;
463 
464  // All service configurations
465  std::unordered_map<rtti::TypeInfo, std::unique_ptr<ServiceConfiguration>> mServiceConfigs;
466 
467  // Interface associated with this instance of core.
468  std::unique_ptr<CoreExtension> mExtension = nullptr;
469 
470  // Frame reference point
471  SteadyTimeStamp mTimeStamp;
472 
473  // Core timer
474  SteadyTimer mTimer;
475 
476  // Current framerate
477  float mFramerate = 0.0f;
478 
479  // Used to calculate framerate over time
480  std::array<double, 20> mTicks = { 0 };
481  double mTicksum = 0;
482  uint32 mTickIdx = 0;
483 
484  // If the engine is initialized
485  bool mInitialized = false;
486 
487  // Called before resources are loaded
488  nap::Slot<> mPreResourcesLoadedSlot = { [&]() -> void { preResourcesLoaded(); } };
489 
490  // Called after resources are loaded
491  nap::Slot<> mPostResourcesLoadedSlot = { [&]() -> void { postResourcesLoaded(); } };
492  };
493 }
494 
496 // Template definitions
498 
502 template <typename T>
504 {
505  return static_cast<T*>(getService(RTTI_OF(T)));
506 }
507 
508 
512 template <typename T>
513 const T* nap::Core::getService() const
514 {
515  return static_cast<const T*>(getService(RTTI_OF(T)));
516 }
517 
518 
522 template <typename T>
523 const T& nap::Core::getExtension() const
524 {
525  T* core_ext = rtti_cast<T>(mExtension.get());
526  assert(core_ext != nullptr);
527  return *core_ext;
528 }
529 
530 
534 template <typename T>
536 {
537  return rtti_cast<T>(mExtension.get()) != nullptr;
538 }
nap::Core::getResourceManager
ResourceManager * getResourceManager()
Definition: core.h:187
nap::Core::Services::initialized
bool initialized()
Definition: core.h:341
nap::Slot
Slot.
Definition: signalslot.h:21
nap::utility::ErrorState
Definition: errorstate.h:19
nap::Core::getService
T * getService()
Definition: core.h:503
nap::ProjectInfo::EContext
EContext
Definition: projectinfo.h:61
nap::uint32
uint32_t uint32
Definition: numeric.h:20
nap::ServiceConfiguration
Definition: service.h:28
nap::Timer< SteadyClock >
nap::Core
Definition: core.h:82
nap::Core::getModuleManager
const ModuleManager & getModuleManager() const
Definition: core.h:192
nap::rtti::DeserializeResult
Definition: deserializeresult.h:40
nap::Service
Definition: service.h:52
nap::Core::getExtension
const T & getExtension() const
Definition: core.h:523
nap::Core::Services
Definition: core.h:330
nap::ModuleManager
Definition: modulemanager.h:86
nap::ServiceConfigMap
std::unordered_map< rtti::TypeInfo, ServiceConfiguration * > ServiceConfigMap
Definition: core.h:37
nap
Definition: assert.h:10
nap::Core::getFramerate
float getFramerate() const
Definition: core.h:212
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:139
nap::Core::ServicesHandle
std::unique_ptr< Services > ServicesHandle
Definition: core.h:89
nap::ProjectInfo
Definition: projectinfo.h:52
nap::ResourceManager
Definition: resourcemanager.h:49
nap::Core::hasExtension
bool hasExtension() const
Definition: core.h:535
nap::SteadyTimeStamp
std::chrono::time_point< SteadyClock > SteadyTimeStamp
Point in time associated with the SteadyClock.
Definition: datetime.h:30