@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
197 lines (196 loc) • 8.36 kB
JavaScript
"use strict";
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 ElectronStorage_1 = __importDefault(require("./ElectronStorage"));
const FileBase_1 = __importDefault(require("../storage/FileBase"));
const AppServiceProxy_1 = __importStar(require("../core/AppServiceProxy"));
const StorageUtilities_1 = __importStar(require("../storage/StorageUtilities"));
const Log_1 = __importDefault(require("../core/Log"));
class ElectronFile extends FileBase_1.default {
_name;
_parentFolder;
localPersistDateTime;
get name() {
return this._name;
}
get fullPath() {
let path = this._parentFolder.fullPath;
if (!path.endsWith(ElectronStorage_1.default.folderDelimiter) && !path.endsWith(">")) {
path += ElectronStorage_1.default.folderDelimiter;
}
return path + this.name;
}
get parentFolder() {
return this._parentFolder;
}
get isContentLoaded() {
return this.lastLoadedOrSaved != null || this.modified != null;
}
constructor(parentFolder, folderName) {
super();
this._parentFolder = parentFolder;
this._name = folderName;
}
async scanForChanges() {
var stat = undefined;
let statResultStr = "";
let mtime = undefined;
try {
statResultStr = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsStat, this.fullPath);
if (statResultStr) {
stat = JSON.parse(statResultStr);
mtime = Date.parse(stat.mtime);
}
}
catch (e) {
Log_1.default.fail("Failed to parse statistics for " + this.fullPath + ". " + statResultStr);
}
if (this.localPersistDateTime && stat && mtime && mtime > this.localPersistDateTime) {
await this.reloadAfterExternalUpdate();
}
}
async exists() {
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsExists, this.fullPath);
return result === "true";
}
async loadContent(force) {
if (force || this.lastLoadedOrSaved == null) {
const encoding = StorageUtilities_1.default.getEncodingByFileName(this._name);
if (encoding === StorageUtilities_1.EncodingType.ByteBuffer) {
// Log.debug("ElecF loading '" + this.fullPath + "' as binary.");
const byteResult = await AppServiceProxy_1.default.sendAsyncBinary(AppServiceProxy_1.AppServiceProxyCommands.fsReadFile, this.fullPath);
if (byteResult instanceof ArrayBuffer) {
this._content = new Uint8Array(byteResult);
}
else if (byteResult === undefined) {
this._content = null;
}
else {
this._content = byteResult;
}
}
else {
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsReadUtf8File, this.fullPath);
if (result === undefined) {
this._content = null;
}
else {
this._content = result;
}
// Log.debug("ElecF loading '" + this.fullPath + "' as text - " + (result ? result.length + " chars." : " empty"));
}
this.lastLoadedOrSaved = new Date();
}
return this.lastLoadedOrSaved;
}
setContent(newContent, updateType, sourceId) {
const areEqual = StorageUtilities_1.default.contentsAreEqual(this._content, newContent);
if (newContent === null) {
Log_1.default.fail("Setting null content for " + this.storageRelativePath);
}
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);
return true;
}
async saveContent() {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (this.content != null) {
this.parentFolder.ensureExists();
const encoding = StorageUtilities_1.default.getEncodingByFileName(this._name);
if (encoding === StorageUtilities_1.EncodingType.ByteBuffer) {
// Log.verbose("Saving '" + this.fullPath + "' as binary. size: " + this.content.length);
await AppServiceProxy_1.default.sendBinaryAsync(AppServiceProxy_1.AppServiceProxyCommands.fsWriteFile, {
path: this.fullPath,
content: this.content,
});
}
else {
// Log.verbose("Saving '" + this.fullPath + "' as text. size: " + this.content.length);
await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsWriteUtf8File, {
path: this.fullPath,
content: this.content,
});
}
}
this.lastLoadedOrSaved = new Date();
return this.lastLoadedOrSaved;
}
async deleteThisFile(skipRemoveFromParent) {
if (this.parentFolder.storage.readOnly) {
throw new Error("Can't save read-only file.");
}
if (skipRemoveFromParent !== true) {
this._parentFolder._removeFile(this);
}
this._recycleItem(this.fullPath);
return true;
}
async moveTo(newStorageRelativePath) {
const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
const newFileName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
if (newFileName.length < 2) {
throw new Error("New path is not correct.");
}
const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
if (newParentFolder.files[newFileName] !== undefined) {
throw new Error("File exists at specified path.");
}
await this.loadContent(false);
const originalPath = this.fullPath;
this._name = newFileName;
this._parentFolder = newParentFolder;
this.modified = new Date();
newParentFolder._addExistingFile(this);
this._recycleItem(originalPath);
return true;
}
async _recycleItem(path) {
AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.shellRecycleItem, path);
}
}
exports.default = ElectronFile;