【工具箱】egret读取自己的配置文件

推荐类库:JSZip

/**
 * Created by d8q8 on 2015/5/25.
 * @class JSZip
 * @constructor
 **/
interface JSZip {
    file(path:string): JSZipObject;
    file(path:RegExp): JSZipObject[];
    file(path:string, data:any, options?:JSZipFileOptions): JSZip;
    folder(name:string): JSZip;
    folder(name:RegExp): JSZipObject[];
    filter(predicate:(relativePath:string, file:JSZipObject) => boolean): JSZipObject[];
    remove(path:string): JSZip;
    generate(options?:JSZipGeneratorOptions): any;
    load(data:any, options:JSZipLoadOptions): JSZip;
}

interface JSZipObject {
    name: string;
    dir: boolean;
    date: Date;
    comment: string;
    options: JSZipObjectOptions;

    asText(): string;
    asBinary(): string;
    asArrayBuffer(): ArrayBuffer;
    asUint8Array(): Uint8Array;
    asNodeBuffer(): Buffer;
}

interface JSZipFileOptions extends JSZipObjectOptions {
    compressionOptions?:Object;
    comment?: string;
    optimizedBinaryString?: boolean;
    createFolders?: boolean;
    unixPermissions?:number;
    dosPermissions?:number;
}

interface JSZipObjectOptions {
    base64?: boolean;
    binary?: boolean;
    dir?: boolean;
    date?: Date;
    compression?: string;
}

interface JSZipGeneratorOptions {
    base64?: boolean;
    compression?: string;
    compressionOptions?:Object;
    type?: string;
    comment?: string;
    mimeType?:string;
    platform?:string;
}

interface JSZipLoadOptions {
    base64?: boolean;
    checkCRC32?: boolean;
    optimizedBinaryString?: boolean;
    createFolders?: boolean;
}

interface JSZipSupport {
    arraybuffer: boolean;
    uint8array: boolean;
    blob: boolean;
    nodebuffer: boolean;
}

interface Buffer {
    data?:any;
    encoding?:string;
}

interface DEFLATE {
    compress(input:string, compressionOptions:{level:number}): Uint8Array;
    compress(input:number[], compressionOptions:{level:number}): Uint8Array;
    compress(input:Uint8Array, compressionOptions:{level:number}): Uint8Array;

    uncompress(input:string): Uint8Array;
    uncompress(input:number[]): Uint8Array;
    uncompress(input:Uint8Array): Uint8Array;
}

declare var JSZip:{
    (): JSZip;
    (data:any, options?:JSZipLoadOptions): JSZip;
    new (): JSZip;
    new (data:any, options?:JSZipLoadOptions): JSZip;

    prototype: JSZip;
    support: JSZipSupport;
    compressions: {
        DEFLATE: DEFLATE;
    }
}

declare module "jszip" {
    export = JSZip;
}

interface FileSaver {
    (data:Blob, filename:string): void
}

declare var saveAs:FileSaver;

StdConfig

class StdConfig {
    public constructor() {

    }
    private static _zip: JSZip;//配置压缩包缓存
    private static _cfgls: Object = new Object();

        public static parseCfg(cfg: string): string {    
        return StdConfig._zip.file(cfg).asText();    
    }        

        public static async parseStd(byte: egret.ByteArray) {
        StdConfig._hasInit = true;
        StdConfig._zip = new JSZip(byte.buffer);
                
                StdConfig.parseFirstPayConfig("fristpayrebate");
                //... ...
        }    

        private static _getFirstPayById: (id) => StdFirstPay;
        private static parseFirstPayConfig(cfg: string) {
          cfg = StdConfig.parseCfg(cfg);
          let payAward: any = JSON.parse(cfg);
          StdConfig._getFirstPayById = function (id): StdFirstPay {
              if (!payAward) {
                  throw new Error("缺少首充奖励配置");
              }
              return payAward[id - 1];
        }
    }                    
}

猜你喜欢

转载自www.cnblogs.com/harrickheng/p/11329805.html