UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

111 lines (110 loc) 5.21 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 StorageUtilities_1 = __importDefault(require("../storage/StorageUtilities")); const Log_1 = __importDefault(require("./Log")); const md5 = __importStar(require("js-md5")); const Utilities_1 = __importDefault(require("./Utilities")); class HashUtilities { /** * Generates hash catalog entries for a file (both complete file hash and property hashes for JSON files) * @param file The file to generate hashes for * @param filePath The file path to store in the catalog entry * @param hashCatalog The hash catalog to populate * @param onError Optional error callback for handling errors */ static async addHashesForFile(hashCatalog, file, filePath) { try { // Complete file hash with filename included await file.loadContent(); if (file.content !== null && file.content !== undefined) { const fileHash = await file.getHash(); if (fileHash) { hashCatalog[fileHash] = { fileName: file.name, propertyName: "", filePath: filePath, }; } } // Property hashes for JSON files if (file.name.endsWith(".json")) { await file.loadContent(); if (file.isString && typeof file.content === "string") { try { // Check for BOM and handle it properly const bytes = StorageUtilities_1.default.getContentsAsBinary(file); const hasBOM = StorageUtilities_1.default.hasUTF8ByteOrderMark(bytes); let cleanContent = file.content; if (hasBOM && bytes) { // Convert back to string without BOM const withoutBOM = bytes.slice(3); // Remove the 3-byte UTF-8 BOM cleanContent = new TextDecoder("utf-8").decode(withoutBOM); } // Strip comments from JSON content and parse const fixedContent = Utilities_1.default.fixJsonContent(cleanContent); const jsonContent = JSON.parse(fixedContent); // Process each top-level property for (const key in jsonContent) { const value = jsonContent[key]; if (value !== null && !key.includes(":") && typeof value === "object") { const stringifiedObj = JSON.stringify(value); const stringifiedObjAndKey = stringifiedObj + key; const propertyHash = md5.md5(stringifiedObjAndKey).toLowerCase(); hashCatalog[propertyHash] = { fileName: file.name, propertyName: key, filePath: filePath, }; } } } catch (error) { Log_1.default.verbose(`Skipping property hash generation for ${file.fullPath}: ${error}`); } } } } catch (error) { Log_1.default.verbose(`Skipping property hash generation for ${file.fullPath}: ${error}`); } } } exports.default = HashUtilities;