@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
259 lines (258 loc) • 11.1 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 ElectronFile_1 = __importDefault(require("./ElectronFile"));
const ElectronStorage_1 = __importDefault(require("./ElectronStorage"));
const StorageUtilities_1 = __importDefault(require("./../storage/StorageUtilities"));
const FolderBase_1 = __importDefault(require("./../storage/FolderBase"));
const AppServiceProxy_1 = __importStar(require("../core/AppServiceProxy"));
const Log_1 = __importDefault(require("./../core/Log"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
class ElectronFolder extends FolderBase_1.default {
_name;
_path;
folders;
files;
_storage;
_parentFolder;
get storage() {
return this._storage;
}
get parentFolder() {
return this._parentFolder;
}
get name() {
return this._name;
}
get fullPath() {
let path = this._path;
if (!path.endsWith(ElectronStorage_1.default.folderDelimiter) && !path.endsWith(">")) {
path += ElectronStorage_1.default.folderDelimiter;
}
return path + this.name;
}
constructor(storage, parentFolder, parentPath, folderName) {
super();
this._storage = storage;
this._parentFolder = parentFolder;
this._path = parentPath;
this._name = folderName;
this.folders = {};
this.files = {};
}
async scanForChanges() {
// No-op for electron storage
}
ensureFile(name) {
const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
if (!Utilities_1.default.isUsableAsObjectKey(nameCanon)) {
Log_1.default.unsupportedToken(nameCanon);
throw new Error();
}
let candFile = this.files[nameCanon];
if (candFile == null) {
candFile = new ElectronFile_1.default(this, name);
this.files[nameCanon] = candFile;
}
return candFile;
}
_removeFile(file) {
const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name);
if (Utilities_1.default.isUsableAsObjectKey(nameCanon)) {
const candFile = this.files[nameCanon];
Log_1.default.assert(candFile === file, "Files don't match.");
this.files[nameCanon] = undefined;
this.storage.notifyFileRemoved(this.storageRelativePath + file.name);
}
}
_addExistingFile(file) {
const nameCanon = StorageUtilities_1.default.canonicalizeName(file.name);
if (Utilities_1.default.isUsableAsObjectKey(nameCanon)) {
this.files[nameCanon] = file;
}
}
ensureFolder(name) {
const nameCanon = StorageUtilities_1.default.canonicalizeName(name);
if (!Utilities_1.default.isUsableAsObjectKey(nameCanon)) {
Log_1.default.unsupportedToken(nameCanon);
throw new Error();
}
let candFolder = this.folders[nameCanon];
if (!candFolder) {
candFolder = new ElectronFolder(this._storage, this, this.fullPath, name);
this.folders[nameCanon] = candFolder;
}
return candFolder;
}
async exists() {
if (this.storage.available !== true) {
const res = await this.storage.getAvailable();
if (!res) {
return false;
}
}
if (this.storage.available === false) {
return false;
}
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsFolderExists, this.fullPath);
return result === "true";
}
async deleteThisFolder() {
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsDeleteFolder, this.fullPath);
if (result === "true") {
this.removeMeFromParent();
return true;
}
return false;
}
async deleteAllFolderContents() {
throw new Error("Deletion of all folder contents at " + this.fullPath + " is not supported.");
}
async ensureExists() {
Log_1.default.assert(this.fullPath.lastIndexOf("<") < 1, "Tokens in a folder path: " + this.fullPath);
const exists = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsFolderExists, this.fullPath);
if (exists !== "true") {
try {
await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsMkdir, this.fullPath);
}
catch (e) {
return false;
}
}
return true;
}
_addExistingFolderToParent(folder) {
const nameCanon = StorageUtilities_1.default.canonicalizeName(folder.name);
if (!Utilities_1.default.isUsableAsObjectKey(nameCanon)) {
Log_1.default.unsupportedToken(nameCanon);
throw new Error();
}
this.folders[nameCanon] = folder;
}
async moveTo(newStorageRelativePath) {
const oldFullPath = this.fullPath;
const newFolderPath = StorageUtilities_1.default.getFolderPath(newStorageRelativePath);
const newFolderName = StorageUtilities_1.default.getLeafName(newStorageRelativePath);
if (newFolderName.length < 2) {
throw new Error("New path is not correct.");
}
if (this.isSameFolder(newStorageRelativePath)) {
return false;
}
if (this._parentFolder !== null) {
const newParentFolder = await this._parentFolder.storage.ensureFolderFromStorageRelativePath(newFolderPath);
if (newParentFolder.folders[newFolderName] !== undefined) {
throw new Error("Folder exists at specified path.");
}
const newFullPath = newParentFolder.fullPath + ElectronStorage_1.default.folderDelimiter + newFolderName;
// Perform the disk rename BEFORE modifying in-memory tree.
// If the disk rename fails, the in-memory tree stays consistent.
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsRenameFolder, oldFullPath + "|" + newFullPath);
if (result !== "true") {
return false;
}
// Disk rename succeeded — now update in-memory tree
const previousStoragePath = this.storageRelativePath;
this._parentFolder._removeExistingFolderFromParent(this);
this._parentFolder = newParentFolder;
this._name = newFolderName;
newParentFolder._addExistingFolderToParent(this);
// Notify listeners about the folder move
this.notifyFolderMoved({
folder: this,
previousStoragePath: previousStoragePath,
newStoragePath: this.storageRelativePath,
});
return true;
}
this._name = newFolderName;
const newFullPath = this.fullPath;
const result = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsRenameFolder, oldFullPath + "|" + newFullPath);
return result === "true";
}
async createFile(name) {
return this.ensureFile(name);
}
async load(force) {
if (this.lastLoadedOrSaved != null && !force) {
return this.lastLoadedOrSaved;
}
// Log.debug("Reading details on folder '" + this.fullPath + "'");
const strResult = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsReaddir, this.fullPath);
if (strResult !== undefined) {
const results = JSON.parse(strResult);
if (results) {
for (const fileOrFolderName of results) {
let filePath = this.fullPath;
if (!filePath.endsWith(ElectronStorage_1.default.folderDelimiter)) {
filePath += ElectronStorage_1.default.folderDelimiter;
}
filePath += fileOrFolderName;
var stat = undefined;
let statResultStr = "";
let mtime = undefined;
try {
statResultStr = await AppServiceProxy_1.default.sendAsync(AppServiceProxy_1.AppServiceProxyCommands.fsStat, filePath);
if (statResultStr) {
stat = JSON.parse(statResultStr);
mtime = Date.parse(stat.mtime);
}
}
catch (e) {
Log_1.default.fail("Failed to parse statistics for " + fileOrFolderName + ". " + statResultStr);
}
if (stat) {
if (stat.isDirectory && !StorageUtilities_1.default.isIgnorableFolder(fileOrFolderName)) {
this.ensureFolder(fileOrFolderName);
}
else if (stat.isFile && StorageUtilities_1.default.isUsableFile(filePath)) {
const file = this.ensureFile(fileOrFolderName);
file.localPersistDateTime = mtime;
if (stat.mtime) {
file.modifiedAtLoad = new Date(stat.mtime);
}
}
}
}
}
}
this.updateLastLoadedOrSaved();
return this.lastLoadedOrSaved;
}
}
exports.default = ElectronFolder;