AppKernel.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { ANDROID, HTML5, IOS } from 'cc/env';
  2. import type { AppEventMap } from './AppEvents';
  3. import { EventManager } from '../framework/event/EventManager';
  4. import { ResourceManager } from '../framework/resource/ResourceManager';
  5. import { RedpointManager } from '../framework/redpoint/RedpointManager';
  6. import { SceneNavigator } from '../framework/scene/SceneNavigator';
  7. import { SoundManager } from '../framework/sound/SoundManager';
  8. import { UIManager } from '../framework/ui/UIManager';
  9. import { AndroidPlatformBridge } from '../platform/AndroidPlatformBridge';
  10. import { H5PlatformBridge } from '../platform/H5PlatformBridge';
  11. import { IOSPlatformBridge } from '../platform/IOSPlatformBridge';
  12. import { PlatformBridge } from '../platform/PlatformBridge';
  13. import { HotUpdateService } from '../services/hotupdate/HotUpdateService';
  14. import { VersionService } from '../services/version/VersionService';
  15. import * as stores from '../store';
  16. import * as services from '../services';
  17. import fb from '@scripts/platform/FaceBook/Facebook';
  18. import google from '@scripts/platform/Google/Google';
  19. import * as i18n from '../i18n';
  20. export class AppKernel {
  21. private static instance: AppKernel | null = null;
  22. static initialize(): AppKernel {
  23. if (!this.instance) {
  24. this.instance = new AppKernel();
  25. }
  26. return this.instance;
  27. }
  28. static get Instance(): AppKernel {
  29. if (!this.instance) throw new Error('AppKernel has not been initialized');
  30. return this.instance;
  31. }
  32. static get events(): EventManager<AppEventMap> {
  33. return this.Instance.events;
  34. }
  35. static get resources(): ResourceManager {
  36. return this.Instance.resources;
  37. }
  38. static get redpoint(): RedpointManager {
  39. return this.Instance.redpoint;
  40. }
  41. static get ui(): UIManager {
  42. return this.Instance.ui;
  43. }
  44. static get scenes(): SceneNavigator {
  45. return this.Instance.scenes;
  46. }
  47. static get sound(): SoundManager {
  48. return this.Instance.sound;
  49. }
  50. static get version(): VersionService {
  51. return this.Instance.version;
  52. }
  53. static get hotUpdate(): HotUpdateService {
  54. return this.Instance.hotUpdate;
  55. }
  56. static get platform(): PlatformBridge {
  57. return this.Instance.platform;
  58. }
  59. static get stores(): typeof stores {
  60. return this.Instance.stores;
  61. }
  62. static get services(): typeof services {
  63. return this.Instance.services;
  64. }
  65. static get appLobby(): typeof services.appLobby {
  66. return this.services.appLobby;
  67. }
  68. static get fb(): typeof fb {
  69. return this.Instance.fb;
  70. }
  71. static get google(): typeof google {
  72. return this.Instance.google;
  73. }
  74. static get i18n(): typeof i18n { return i18n; }
  75. readonly events = new EventManager<AppEventMap>();
  76. readonly redpoint = new RedpointManager();
  77. readonly resources = new ResourceManager();
  78. readonly sound = new SoundManager(this.resources);
  79. readonly ui = new UIManager(this.resources);
  80. readonly scenes = new SceneNavigator(this.resources);
  81. readonly version = new VersionService();
  82. readonly hotUpdate = new HotUpdateService();
  83. readonly platform: PlatformBridge;
  84. readonly stores = stores;
  85. readonly services = services;
  86. readonly fb = fb;
  87. readonly google = google;
  88. readonly i18n = i18n;
  89. private constructor() {
  90. this.platform = this.createPlatformBridge();
  91. if (IOS && this.platform instanceof IOSPlatformBridge) {
  92. this.platform.requestTrackingAuthorization();
  93. }
  94. this.fb.init();
  95. this.google.init();
  96. }
  97. private createPlatformBridge(): PlatformBridge {
  98. if (ANDROID) return new AndroidPlatformBridge();
  99. if (IOS) return new IOSPlatformBridge();
  100. return new H5PlatformBridge();
  101. }
  102. }