@minecraft/creator-tools
Version:
Minecraft Creator Tools command line and libraries.
167 lines (166 loc) • 6.01 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities"));
const Utilities_1 = __importDefault(require("../core/Utilities"));
class Lang {
_file;
_containerName;
_language;
_isLoaded = false;
tokens = {};
_onLoaded = new ste_events_1.EventDispatcher();
get isLoaded() {
return this._isLoaded;
}
get file() {
return this._file;
}
set file(newFile) {
this._file = newFile;
}
get onLoaded() {
return this._onLoaded.asEvent();
}
get containerName() {
return this._containerName;
}
get language() {
return this._language;
}
getLocKeys() {
return Object.keys(this.tokens);
}
static async ensureOnFile(file, loadHandler) {
let lang;
if (file.manager === undefined) {
lang = new Lang();
lang.file = file;
file.manager = lang;
}
if (file.manager !== undefined && file.manager instanceof Lang) {
lang = file.manager;
if (!lang.isLoaded) {
if (loadHandler) {
lang.onLoaded.subscribe(loadHandler);
}
await lang.load();
}
}
return lang;
}
persist() {
if (this._file === undefined) {
return false;
}
let content = this._file.content;
if (content === undefined || content === null || content instanceof Uint8Array) {
content = "";
}
for (const tokName in this.tokens) {
const tok = this.tokens[tokName];
if (tok && tok.isModified) {
const tokStart = content.indexOf(tokName + "=");
if (tokStart >= 0) {
const tokEndR = content.indexOf("\r", tokStart + tokName.length + 1);
let tokEnd = content.indexOf("\n", tokStart + tokName.length + 1);
if (tokEndR > tokStart && tokEndR === tokEnd - 1) {
tokEnd = tokEndR;
}
if (tokEnd < 0) {
tokEnd = content.length;
}
content = content.substring(0, tokStart + tokName.length + 1) + tok.value + content.substring(tokEnd);
}
else {
// Try to insert a new token into areas that are similarly named.
const periodInName = tokName.lastIndexOf(".");
let wasInserted = false;
if (periodInName >= 0) {
const findSimilar = content.lastIndexOf("\n" + tokName.substring(0, periodInName + 1));
if (findSimilar >= 0) {
content =
content.substring(0, findSimilar + 1) +
tokName +
"=" +
tok.value +
"\n" +
content.substring(findSimilar + 1);
wasInserted = true;
}
}
if (!wasInserted) {
content += "\n" + tokName + "=" + tok.value;
}
}
tok.isModified = false;
}
}
return this._file.setContent(content);
}
async save() {
if (this._file === undefined) {
return;
}
if (this.persist()) {
await this._file.saveContent(false);
}
}
async load() {
if (this._file === undefined || this._isLoaded) {
return;
}
if (!this._file.isContentLoaded) {
await this._file.loadContent();
}
if (this._file.content === null || this._file.content instanceof Uint8Array) {
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
return;
}
const content = this._file.content;
this._language = StorageUtilities_1.default.getBaseFromName(this._file.name);
let dir = this._file.parentFolder;
if (dir) {
if (dir.name === "texts" && dir.parentFolder) {
dir = dir.parentFolder;
}
if (dir.name === "" && !dir.parentFolder && dir.storage.name) {
this._containerName = dir.storage.name;
}
else {
this._containerName = dir.name;
}
}
const lines = content.split("\n");
for (let line of lines) {
line = line.trim();
const lastEqual = line.indexOf("=");
if (lastEqual > 0 && lastEqual < line.length - 1) {
const tokenName = line.substring(0, lastEqual);
let tokenVal = line.substring(lastEqual + 1);
let comment = undefined;
let lastHash = tokenVal.indexOf("#");
if (lastHash >= 0) {
comment = tokenVal.substring(lastHash + 1);
tokenVal = tokenVal.substring(0, lastHash).trim();
}
if (Utilities_1.default.isUsableAsObjectKey(tokenName)) {
this.tokens[tokenName] = {
value: tokenVal,
comment: comment,
isModified: false,
};
}
}
}
this._isLoaded = true;
this._onLoaded.dispatch(this, this);
}
}
exports.default = Lang;