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  void start();
167 
176  double update(std::function<void(double)>& updateFunction);
177 
182  ResourceManager* getResourceManager() { return mResourceManager.get(); }
183 
187  const ModuleManager& getModuleManager() const { return *mModuleManager; }
188 
192  uint32 getTicks() const;
193 
197  double getElapsedTime() const;
198 
202  SteadyTimeStamp getStartTime() const;
203 
207  float getFramerate() const { return mFramerate; }
208 
214  Service* getService(const rtti::TypeInfo& type);
215 
221  const Service* getService(const rtti::TypeInfo& type) const;
222 
228  Service* getService(const std::string& type);
229 
239  template <typename T>
240  T* getService();
241 
251  template <typename T>
252  const T* getService() const;
253 
260  template <typename T>
261  const T& getExtension() const;
262 
269  template <typename T>
270  T& getExtension();
271 
275  template <typename T>
276  bool hasExtension() const;
277 
285  bool findProjectFilePath(const std::string& filename, std::string& foundFilePath) const;
286 
291  const nap::ProjectInfo* getProjectInfo() const;
292 
300  bool writeConfigFile(const std::string& path, utility::ErrorState& errorState, bool linkToProjectInfo = true);
301 
306  std::vector<const ServiceConfiguration*> getServiceConfigs() const;
307 
311  void setupPlatformSpecificEnvironment();
312 
326  class NAPAPI Services final
327  {
328  public:
332  ~Services();
333 
337  bool initialized() { return mInitialized; }
338 
339  private:
340  friend class Core;
346  Services(Core& core, utility::ErrorState& error);
347  nap::Core& mCore;
348  bool mInitialized = false;
349  };
350 
351  private:
357  bool findProjectInfoFile(std::string& foundFilePath) const;
358 
366  bool createServices(const nap::ProjectInfo& projectInfo, utility::ErrorState& errorState);
367 
376  bool addService(const rtti::TypeInfo& type, ServiceConfiguration* configuration, std::vector<Service*>& outServices, utility::ErrorState& errorState);
377 
386  bool loadServiceConfigurations(nap::utility::ErrorState& err);
387 
397  bool loadServiceConfiguration(const std::string& filename, rtti::DeserializeResult& deserialize_result, utility::ErrorState& errorState);
398 
404  void preResourcesLoaded();
405 
411  void postResourcesLoaded();
412 
416  void calculateFramerate(double deltaTime);
417 
425  bool loadProjectInfo(std::string projectFilename, ProjectInfo::EContext context, nap::utility::ErrorState& error);
426 
432  nap::ServiceConfiguration* findServiceConfig(rtti::TypeInfo serviceType) const;
433 
440  bool addServiceConfig(std::unique_ptr<nap::ServiceConfiguration> serviceConfig, utility::ErrorState& error);
441 
442  // Manages all the loaded modules
443  std::unique_ptr<ModuleManager> mModuleManager = nullptr;
444 
445  // Manages all the objects in core
446  std::unique_ptr<ResourceManager> mResourceManager = nullptr;
447 
448  // Holds on to the current project's configuration
449  std::unique_ptr<nap::ProjectInfo> mProjectInfo = nullptr;
450 
451  // Sorted service nodes, set after init
452  std::vector<std::unique_ptr<Service>> mServices;
453 
454  // All service configurations
455  std::unordered_map<rtti::TypeInfo, std::unique_ptr<ServiceConfiguration>> mServiceConfigs;
456 
457  // Interface associated with this instance of core.
458  std::unique_ptr<CoreExtension> mExtension = nullptr;
459 
460  // Frame reference point
461  SteadyTimeStamp mTimeStamp;
462 
463  // Core timer
464  SteadyTimer mTimer;
465 
466  // Current framerate
467  float mFramerate = 0.0f;
468 
469  // Used to calculate framerate over time
470  std::array<double, 20> mTicks = { 0 };
471  double mTicksum = 0;
472  uint32 mTickIdx = 0;
473 
474  // If the engine is initialized
475  bool mInitialized = false;
476 
477  // Called before resources are loaded
478  nap::Slot<> mPreResourcesLoadedSlot = { [&]() -> void { preResourcesLoaded(); } };
479 
480  // Called after resources are loaded
481  nap::Slot<> mPostResourcesLoadedSlot = { [&]() -> void { postResourcesLoaded(); } };
482  };
483 }
484 
486 // Template definitions
488 
492 template <typename T>
494 {
495  return static_cast<T*>(getService(RTTI_OF(T)));
496 }
497 
498 
502 template <typename T>
503 const T* nap::Core::getService() const
504 {
505  return static_cast<const T*>(getService(RTTI_OF(T)));
506 }
507 
508 
512 template <typename T>
513 const T& nap::Core::getExtension() const
514 {
515  T* core_ext = rtti_cast<T>(mExtension.get());
516  assert(core_ext != nullptr);
517  return *core_ext;
518 }
519 
520 
524 template <typename T>
526 {
527  T* core_ext = rtti_cast<T>(mExtension.get());
528  assert(core_ext != nullptr);
529  return *core_ext;
530 }
531 
532 
536 template <typename T>
538 {
539  return rtti_cast<T>(mExtension.get()) != nullptr;
540 }
nap::Core::getResourceManager
ResourceManager * getResourceManager()
Definition: core.h:182
nap::Core::Services::initialized
bool initialized()
Definition: core.h:337
nap::Slot
Definition: signalslot.h:18
nap::utility::ErrorState
Definition: errorstate.h:19
nap::Core::getService
T * getService()
Definition: core.h:493
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:187
nap::rtti::DeserializeResult
Definition: deserializeresult.h:40
nap::Service
Definition: templateservice.h:8
nap::Core::getExtension
const T & getExtension() const
Definition: core.h:513
nap::Core::Services
Definition: core.h:326
nap::ModuleManager
Definition: modulemanager.h:25
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:207
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:141
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:537
nap::SteadyTimeStamp
std::chrono::time_point< SteadyClock > SteadyTimeStamp
Point in time associated with the SteadyClock.
Definition: datetime.h:30