UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

145 lines (143 loc) 4.78 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. Object.defineProperty(exports, "__esModule", { value: true }); const jszip_1 = require("jszip"); const IStorage_1 = require("./IStorage"); const ZipFolder_1 = require("./ZipFolder"); const StorageBase_1 = require("./StorageBase"); const CartoApp_1 = require("../app/CartoApp"); const StorageUtilities_1 = require("./StorageUtilities"); class ZipStorage extends StorageBase_1.default { get updatedSinceLoad() { if (this.modified === null || (this.lastLoadedOrSaved === null && this.modified === null)) { return false; } else if (this.lastLoadedOrSaved === null) { return true; } return this.modified > this.lastLoadedOrSaved; } constructor() { super(); this.modified = null; this.lastLoadedOrSaved = null; ZipStorage.zipFixup(); this._jsz = new jszip_1.default(); this.rootFolder = new ZipFolder_1.default(this, this._jsz, null, "", ""); } static zipFixup() { if (CartoApp_1.default.hostType === CartoApp_1.HostType.electronNodeJs || CartoApp_1.default.hostType === CartoApp_1.HostType.toolsNodejs) { // eslint-disable-next-line eval("jszip_1.default = jszip_1"); } } static fromJsonString(jsonData) { const zs = new ZipStorage(); const file = zs.rootFolder.ensureFile("d.json"); file.setContent(jsonData); file.saveContent(); return zs; } static fromJsObject(data) { const zs = new ZipStorage(); const file = zs.rootFolder.ensureFile("d.json"); let jsonData = undefined; jsonData = JSON.stringify(data); file.setContent(jsonData); file.saveContent(); return zs; } static async fromZipBytesToJsonObject(data) { const zs = new ZipStorage(); await zs.loadFromUint8Array(data); return await ZipStorage.toJsObject(zs); } static async toJsObject(storage) { const file = storage.rootFolder.ensureFile("d.json"); await file.loadContent(); return StorageUtilities_1.default.getJsonObject(file); } updateLastLoadedOrSaved() { this.lastLoadedOrSaved = new Date(); } async loadFromBase64(data, name) { try { await this._jsz.loadAsync(data, { base64: true, checkCRC32: true, }); } catch (e) { this.errorMessage = e.toString(); this.errorStatus = IStorage_1.StorageErrorStatus.unprocessable; } // Log.fail("Loading zip file from data " + data.length); this.name = name; this.updateLastLoadedOrSaved(); await this.rootFolder.load(true); } async loadFromUint8Array(data, name) { try { await this._jsz.loadAsync(data, { base64: false, }); } catch (e) { this.errorMessage = e.toString(); this.errorStatus = IStorage_1.StorageErrorStatus.unprocessable; } // Log.fail("Loading zip file from data " + data.length); this.name = name; this.updateLastLoadedOrSaved(); await this.rootFolder.load(true); } joinPath(pathA, pathB) { let fullPath = pathA; if (!fullPath.endsWith(StorageBase_1.default.slashFolderDelimiter)) { fullPath += StorageBase_1.default.slashFolderDelimiter; } fullPath += pathB; return fullPath; } async generateUint8ArrayAsync() { const result = await this._jsz.generateAsync({ type: "uint8array", compression: "DEFLATE", compressionOptions: { level: 9, }, }); return result; } async generateCompressedBase64Async() { const result = await this._jsz.generateAsync({ type: "base64", compression: "DEFLATE", compressionOptions: { level: 9, }, }); return result; } async generateCompressedUint8ArrayAsync() { const result = await this._jsz.generateAsync({ type: "uint8array", compression: "DEFLATE", compressionOptions: { level: 9, }, }); return result; } async generateBlobAsync() { let type = "blob"; if (CartoApp_1.default.isLocalNode) { type = "nodebuffer"; } const result = await this._jsz.generateAsync({ type: type }); return result; } } exports.default = ZipStorage; //# sourceMappingURL=../maps/storage/ZipStorage.js.map