AppKernel.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { ANDROID, 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 iosAuth from '@scripts/platform/IOS/IOSAuth';
  20. import iap from '@scripts/platform/IAP/IAP';
  21. import * as i18n from '../i18n';
  22. export class AppKernel {
  23. private static instance: AppKernel | null = null;
  24. static initialize(): AppKernel {
  25. if (!this.instance) {
  26. this.instance = new AppKernel();
  27. }
  28. return this.instance;
  29. }
  30. static get Instance(): AppKernel {
  31. if (!this.instance) throw new Error('AppKernel has not been initialized');
  32. return this.instance;
  33. }
  34. static get events(): EventManager<AppEventMap> {
  35. return this.Instance.events;
  36. }
  37. static get resources(): ResourceManager {
  38. return this.Instance.resources;
  39. }
  40. static get redpoint(): RedpointManager {
  41. return this.Instance.redpoint;
  42. }
  43. static get ui(): UIManager {
  44. return this.Instance.ui;
  45. }
  46. static get scenes(): SceneNavigator {
  47. return this.Instance.scenes;
  48. }
  49. static get sound(): SoundManager {
  50. return this.Instance.sound;
  51. }
  52. static get version(): VersionService {
  53. return this.Instance.version;
  54. }
  55. static get hotUpdate(): HotUpdateService {
  56. return this.Instance.hotUpdate;
  57. }
  58. static get platform(): PlatformBridge {
  59. return this.Instance.platform;
  60. }
  61. static get stores(): typeof stores {
  62. return this.Instance.stores;
  63. }
  64. static get services(): typeof services {
  65. return this.Instance.services;
  66. }
  67. static get appLobby(): typeof services.appLobby {
  68. return this.services.appLobby;
  69. }
  70. static get fb(): typeof fb {
  71. return this.Instance.fb;
  72. }
  73. static get google(): typeof google {
  74. return this.Instance.google;
  75. }
  76. static get i18n(): typeof i18n { return i18n; }
  77. static get iap(): typeof iap { return iap; }
  78. static get iosAuth(): typeof iosAuth { return iosAuth; }
  79. readonly events = new EventManager<AppEventMap>();
  80. readonly redpoint = new RedpointManager();
  81. readonly resources = new ResourceManager();
  82. readonly sound = new SoundManager(this.resources);
  83. readonly ui = new UIManager(this.resources);
  84. readonly scenes = new SceneNavigator(this.resources);
  85. readonly version = new VersionService();
  86. readonly hotUpdate = new HotUpdateService();
  87. readonly platform: PlatformBridge;
  88. readonly stores = stores;
  89. readonly services = services;
  90. readonly fb = fb;
  91. readonly google = google;
  92. readonly i18n = i18n;
  93. readonly iap = iap;
  94. readonly iosAuth = iosAuth;
  95. private constructor() {
  96. this.platform = this.createPlatformBridge();
  97. if (IOS && this.platform instanceof IOSPlatformBridge) {
  98. this.platform.requestTrackingAuthorization();
  99. }
  100. this.fb.init();
  101. this.google.init();
  102. }
  103. private createPlatformBridge(): PlatformBridge {
  104. if (ANDROID) return new AndroidPlatformBridge();
  105. if (IOS) return new IOSPlatformBridge();
  106. return new H5PlatformBridge();
  107. }
  108. }