UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

142 lines (141 loc) 5.51 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const FileBase_1 = __importDefault(require("./FileBase")); const StorageUtilities_1 = __importStar(require("./StorageUtilities")); const Log_1 = __importDefault(require("../core/Log")); class ZipFile extends FileBase_1.default { _name; _parentFolder; _jszipo; get name() { return this._name; } get fullPath() { return StorageUtilities_1.default.ensureEndsWithDelimiter(this._parentFolder.fullPath) + this.name; } get parentFolder() { return this._parentFolder; } get isContentLoaded() { return this.lastLoadedOrSaved != null || this.modified != null; } updateZipNativeFile(thisFileObject) { this._jszipo = thisFileObject; } constructor(parentFolder, fileName, thisFileObject) { super(); this._jszipo = thisFileObject; this._parentFolder = parentFolder; this._name = fileName; this.modified = null; this.lastLoadedOrSaved = null; } async scanForChanges() { // No-op for zip storage } async deleteThisFile(skipRemoveFromParent) { throw new Error("Not implemented."); } async moveTo(newStorageRelativePath) { throw new Error("Not implemented."); } async loadContent(force, forceEncoding) { if (force || !this.lastLoadedOrSaved) { if (this._jszipo !== null) { const type = forceEncoding ?? StorageUtilities_1.default.getEncodingByFileName(this.name); if (type === StorageUtilities_1.EncodingType.ByteBuffer) { try { this._content = await this._jszipo.async("uint8array"); } catch (e) { this.errorStateMessage = e.toString(); } /*, (metadata) => { Log.verbose("Extracting " + this.storageRelativePath + " (" + metadata.percent.toFixed(2) + ")%"); });*/ Log_1.default.assert(this._content !== null, "Unexpectedly could not load content."); } else { try { this._content = await this._jszipo.async("string"); } catch (e) { this.errorStateMessage = e.toString(); } } } this.lastLoadedOrSaved = new Date(); } return this.lastLoadedOrSaved; } setContent(newContent, updateType, sourceId) { const areEqual = StorageUtilities_1.default.contentsAreEqual(this._content, newContent); if (areEqual) { return false; } if (!this.lastLoadedOrSaved) { this.lastLoadedOrSaved = new Date(); this.lastLoadedOrSaved = new Date(this.lastLoadedOrSaved.getTime() - 1); } let oldContent = this._content; this._content = newContent; this.contentWasModified(oldContent, updateType, sourceId); this._parentFolder.storage.modified = this.modified; return true; } async saveContent(force) { if (this.parentFolder.storage.readOnly) { throw new Error("Can't save read-only file."); } if (this.needsSave || force === true) { this.lastLoadedOrSaved = new Date(); if (this.content !== null) { // Log.debug("Saving '" + this.content.length + "' content to '" + this.name + "' within zip"); this._parentFolder.storage.modified = this.modified; this._parentFolder.zip.file(this.name, this.content); } } if (this.lastLoadedOrSaved === null) { this.lastLoadedOrSaved = new Date(); } return this.lastLoadedOrSaved; } } exports.default = ZipFile;