AppKernel.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 { SceneNavigator } from '../framework/scene/SceneNavigator';
  6. import { UIManager } from '../framework/ui/UIManager';
  7. import { AndroidPlatformBridge } from '../platform/AndroidPlatformBridge';
  8. import { H5PlatformBridge } from '../platform/H5PlatformBridge';
  9. import { IOSPlatformBridge } from '../platform/IOSPlatformBridge';
  10. import { PlatformBridge } from '../platform/PlatformBridge';
  11. import { HotUpdateService } from '../services/hotupdate/HotUpdateService';
  12. import { VersionService } from '../services/version/VersionService';
  13. import * as stores from '../store';
  14. import * as services from '../services';
  15. import fb from '@scripts/platform/FaceBook/Facebook';
  16. import google from '@scripts/platform/Google/Google';
  17. export class AppKernel {
  18. private static instance: AppKernel | null = null;
  19. static initialize(): AppKernel {
  20. if (!this.instance) {
  21. this.instance = new AppKernel();
  22. }
  23. return this.instance;
  24. }
  25. static get Instance(): AppKernel {
  26. if (!this.instance) throw new Error('AppKernel has not been initialized');
  27. return this.instance;
  28. }
  29. static get events(): EventManager<AppEventMap> {
  30. return this.Instance.events;
  31. }
  32. static get resources(): ResourceManager {
  33. return this.Instance.resources;
  34. }
  35. static get ui(): UIManager {
  36. return this.Instance.ui;
  37. }
  38. static get scenes(): SceneNavigator {
  39. return this.Instance.scenes;
  40. }
  41. static get version(): VersionService {
  42. return this.Instance.version;
  43. }
  44. static get hotUpdate(): HotUpdateService {
  45. return this.Instance.hotUpdate;
  46. }
  47. static get platform(): PlatformBridge {
  48. return this.Instance.platform;
  49. }
  50. static get stores(): typeof stores {
  51. return this.Instance.stores;
  52. }
  53. static get services(): typeof services {
  54. return this.Instance.services;
  55. }
  56. static get fb(): typeof fb {
  57. return this.Instance.fb;
  58. }
  59. static get google(): typeof google {
  60. return this.Instance.google;
  61. }
  62. readonly events = new EventManager<AppEventMap>();
  63. readonly resources = new ResourceManager();
  64. readonly ui = new UIManager(this.resources);
  65. readonly scenes = new SceneNavigator(this.resources);
  66. readonly version = new VersionService();
  67. readonly hotUpdate = new HotUpdateService();
  68. readonly platform: PlatformBridge;
  69. readonly stores = stores;
  70. readonly services = services;
  71. readonly fb = fb;
  72. readonly google = google;
  73. private constructor() {
  74. this.platform = this.createPlatformBridge();
  75. this.fb.init();
  76. this.google.init();
  77. }
  78. private createPlatformBridge(): PlatformBridge {
  79. if (ANDROID) return new AndroidPlatformBridge();
  80. if (IOS) return new IOSPlatformBridge();
  81. return new H5PlatformBridge();
  82. }
  83. }