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[] = "app.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 
296  bool writeConfigFile(const std::string& path, utility::ErrorState& errorState, bool linkToProjectInfo = true);
297 
302  std::vector<const ServiceConfiguration*> getServiceConfigs() const;
303 
307  void setupPlatformSpecificEnvironment();
308 
322  class NAPAPI Services final
323  {
324  public:
328  ~Services();
329 
333  bool initialized() { return mInitialized; }
334 
335  private:
336  friend class Core;
342  Services(Core& core, utility::ErrorState& error);
343  nap::Core& mCore;
344  bool mInitialized = false;
345  };
346 
347  private:
353  bool findProjectInfoFile(std::string& foundFilePath) const;
354 
362  bool createServices(const nap::ProjectInfo& projectInfo, utility::ErrorState& errorState);
363 
372  bool addService(const rtti::TypeInfo& type, ServiceConfiguration* configuration, std::vector<Service*>& outServices, utility::ErrorState& errorState);
373 
382  bool loadServiceConfigurations(nap::utility::ErrorState& err);
383 
393  bool loadServiceConfiguration(const std::string& filename, rtti::DeserializeResult& deserialize_result, utility::ErrorState& errorState);
394 
400  void preResourcesLoaded();
401 
407  void postResourcesLoaded();
408 
412  void calculateFramerate(double deltaTime);
413 
418  void setupPythonEnvironment();
419 
427  bool loadProjectInfo(std::string projectFilename, ProjectInfo::EContext context, nap::utility::ErrorState& error);
428 
434  nap::ServiceConfiguration* findServiceConfig(rtti::TypeInfo serviceType) const;
435 
442  bool addServiceConfig(std::unique_ptr<nap::ServiceConfiguration> serviceConfig);
443 
444  // Manages all the loaded modules
445  std::unique_ptr<ModuleManager> mModuleManager = nullptr;
446 
447  // Manages all the objects in core
448  std::unique_ptr<ResourceManager> mResourceManager = nullptr;
449 
450  // Holds on to the current project's configuration
451  std::unique_ptr<nap::ProjectInfo> mProjectInfo = nullptr;
452 
453  // Sorted service nodes, set after init
454  std::vector<std::unique_ptr<Service>> mServices;
455 
456  // All service configurations
457  std::unordered_map<rtti::TypeInfo, std::unique_ptr<ServiceConfiguration>> mServiceConfigs;
458 
459  // Interface associated with this instance of core.
460  std::unique_ptr<CoreExtension> mExtension = nullptr;
461 
462  // Frame reference point
463  SteadyTimeStamp mTimeStamp;
464 
465  // Core timer
466  SteadyTimer mTimer;
467 
468  // Current framerate
469  float mFramerate = 0.0f;
470 
471  // Used to calculate framerate over time
472  std::array<double, 20> mTicks = { 0 };
473  double mTicksum = 0;
474  uint32 mTickIdx = 0;
475 
476  // If the engine is initialized
477  bool mInitialized = false;
478 
479  // Called before resources are loaded
480  nap::Slot<> mPreResourcesLoadedSlot = { [&]() -> void { preResourcesLoaded(); } };
481 
482  // Called after resources are loaded
483  nap::Slot<> mPostResourcesLoadedSlot = { [&]() -> void { postResourcesLoaded(); } };
484  };
485 }
486 
488 // Template definitions
490 
494 template <typename T>
496 {
497  return static_cast<T*>(getService(RTTI_OF(T)));
498 }
499 
500 
504 template <typename T>
505 const T* nap::Core::getService() const
506 {
507  return static_cast<const T*>(getService(RTTI_OF(T)));
508 }
509 
510 
514 template <typename T>
515 const T& nap::Core::getExtension() const
516 {
517  T* core_ext = rtti_cast<T>(mExtension.get());
518  assert(core_ext != nullptr);
519  return *core_ext;
520 }
521 
522 
526 template <typename T>
528 {
529  return rtti_cast<T>(mExtension.get()) != nullptr;
530 }
nap::Core::getResourceManager
ResourceManager * getResourceManager()
Definition: core.h:187
nap::Core::Services::initialized
bool initialized()
Definition: core.h:333
nap::Slot
Slot.
Definition: signalslot.h:21
nap::utility::ErrorState
Definition: errorstate.h:19
nap::Core::getService
T * getService()
Definition: core.h:495
nap::ProjectInfo::EContext
EContext
Definition: projectinfo.h:62
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: templateservice.h:8
nap::Core::getExtension
const T & getExtension() const
Definition: core.h:515
nap::Core::Services
Definition: core.h:322
nap::ModuleManager
Definition: modulemanager.h:86
nap::ServiceConfigMap
std::unordered_map< rtti::TypeInfo, ServiceConfiguration * > ServiceConfigMap
Definition: core.h:37
nap
Definition: templateapp.h:17
nap::Core::getFramerate
float getFramerate() const
Definition: core.h:212
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::Core::ServicesHandle
std::unique_ptr< Services > ServicesHandle
Definition: core.h:89
nap::ProjectInfo
Definition: projectinfo.h:53
nap::ResourceManager
Definition: resourcemanager.h:49
nap::Core::hasExtension
bool hasExtension() const
Definition: core.h:527
nap::SteadyTimeStamp
std::chrono::time_point< SteadyClock > SteadyTimeStamp
Point in time associated with the SteadyClock.
Definition: datetime.h:30