lawless/client/assets/scripts/core/ConfigManager.ts

47 行
1.2 KiB
TypeScript

import { _decorator, Component } from 'cc';
const { ccclass, property } = _decorator;
/**
* ConfigManager 读取本地 JSON/Bundle 配置,并在登录后拉取服务端远程配置。
*/
@ccclass('ConfigManager')
export class ConfigManager extends Component {
@property
public remoteConfigUrl: string = '/api/v1/configs';
private _local: Map<string, any> = new Map();
private _remote: Map<string, any> = new Map();
onLoad() {
this._loadLocalConfig();
}
private _loadLocalConfig(): void {
// TODO: 从本地 resources/config 加载基础配置
this._local.set('ui', {});
}
/**
* 从服务端拉取 Nacos 热更配置
*/
public async fetchRemote(): Promise<void> {
// TODO: 调用 HTTP 接口或 Nacos 配置接口
this._remote.set('event', {});
}
public get<T = any>(key: string, defaultValue?: T): T | undefined {
if (this._remote.has(key)) {
return this._remote.get(key);
}
if (this._local.has(key)) {
return this._local.get(key);
}
return defaultValue;
}
public setRemote(key: string, value: any): void {
this._remote.set(key, value);
}
}