123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import { native, sys } from "cc";
- export const ConfigKeys = {
- GameSaveData: "CarGameSaveData", // 游戏存档
- }
- // 全局变量名
- export const DataKeys = {
- status: "status", // 存放游戏状态,start,stop,pause
- }
- export class GlobalData {
- private static _instance: GlobalData;
- static get instance() {
- if (this._instance) {
- return this._instance;
- }
- this._instance = new GlobalData();
- this._instance.init()
- return this._instance;
- }
- private _data: {[key: string]: any} = {}
- private get data() {
- return this._data
- }
- public setData(key: string, value: any){
- this._data[key] = value
- }
- public getData(key: string){
- if(this._data[key] === undefined){
- return null
- }
- return this._data[key]
- }
- private _configData: {[key: string]: any} = {}
- private get configData() {
- return this._configData
- }
- public setConfigData(key: string, value: any){
- this._configData[key] = value
- this.save()
- }
- public getConfigData(key: string){
- if(this._configData[key] === undefined){
- return null
- }
- return this._configData[key]
- }
- path: string = ''
- init(){
- this.path = this.getConfigPath()
- let content;
- if (sys.isNative) {
- var valueObject = native.fileUtils.getValueMapFromFile(this.path);
- content = valueObject["CarConfigData"];
- } else {
- content = sys.localStorage.getItem("CarConfigData");
- }
- if (content){
- this._configData = JSON.parse(content) || {}
- }else {
- this._configData = {}
- }
-
- }
-
- public save () {
- // 写入文件
- var str = JSON.stringify(this._configData);
- // // 加密代码
- // if (cc.game.config["encript"]) {
- // str = new Xxtea("upgradeHeroAbility").xxteaEncrypt(str);
- // }
- // let zipStr = '@' + Util.encrypt(str);
- let zipStr = str;
-
- if (!sys.isNative) {
- var ls = sys.localStorage;
- ls.setItem("CarConfigData", zipStr);
- return;
- }
- var valueObj: any = {};
- valueObj["CarConfigData"] = zipStr;
- //@ts-ignore
- native.fileUtils.writeToFile(valueObj, this.path);
- }
-
- getConfigPath () {
- var platform: any = sys.platform;
- var path: any = "";
- if (platform === sys.OS.WINDOWS) {
- path = "src/conf";
- } else if (platform === sys.OS.LINUX) {
- path = "./conf";
- } else {
- if (sys.isNative) {
- path = native.fileUtils.getWritablePath();
- path = path + "conf";
- } else {
- path = "src/conf";
- }
- }
- return path;
- }
- }
|