LiveAtlas/src/util.ts

455 lines
15 KiB
TypeScript
Raw Normal View History

2020-12-16 16:54:41 +00:00
/*
2022-02-21 21:53:49 +00:00
* Copyright 2022 James Lyne
2020-12-16 16:54:41 +00:00
*
2021-07-25 14:12:40 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2020-12-16 16:54:41 +00:00
*
2021-07-25 14:12:40 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2020-12-16 16:54:41 +00:00
*
2021-07-25 14:12:40 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2020-12-16 16:54:41 +00:00
*/
import {useStore} from "@/store";
import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition";
import {
Coordinate,
LiveAtlasBounds,
LiveAtlasDimension,
2022-02-21 21:50:31 +00:00
LiveAtlasGlobalMessageConfig,
LiveAtlasLocation,
2022-02-26 14:57:16 +00:00
LiveAtlasMessageConfig, LiveAtlasParsedUrl,
} from "@/index";
import {notify} from "@kyvg/vue3-notification";
import {globalMessages, serverMessages} from "../messages";
2022-01-14 14:38:58 +00:00
const documentRange = document.createRange(),
brToSpaceRegex = /<br \/>/g;
export const titleColoursRegex = /§[0-9a-f]/ig;
export const netherWorldNameRegex = /[_\s]?nether([\s_]|$)/i;
export const endWorldNameRegex = /(^|[_\s])end([\s_]|$)/i;
2022-02-26 14:57:16 +00:00
/**
* Calculates 24 hour time of day and the current day from the given server time
* @param {number} serverTime Server time in ticks
* @returns The equivalent 24 hour time, current day and whether it is currently day or night
*/
2021-01-26 20:37:29 +00:00
export const getMinecraftTime = (serverTime: number) => {
const day = serverTime >= 0 && serverTime < 13700;
2021-01-26 20:37:29 +00:00
return {
serverTime: serverTime,
days: Math.floor((serverTime + 8000) / 24000),
2021-01-26 20:37:29 +00:00
// Assuming it is day at 6:00
hours: (Math.floor(serverTime / 1000) + 6) % 24,
minutes: Math.floor(((serverTime / 1000) % 1) * 60),
seconds: Math.floor(((((serverTime / 1000) % 1) * 60) % 1) * 60),
2021-01-26 20:37:29 +00:00
day: day,
night: !day
};
}
2022-02-26 14:57:16 +00:00
/**
* Parses the given {@link URL} into a {@link LiveAtlasParsedUrl}, if the URL matches any known URL formats
* @param {URL} url The URL to parse
* @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided URL matched any known URL formats,
* otherwise null
*/
export const parseUrl = (url: URL): LiveAtlasParsedUrl | null => {
const query = new URLSearchParams(url.search),
hash = url.hash.replace('#', '');
2021-09-12 19:37:59 +00:00
return hash ? parseMapHash(hash) : parseMapSearchParams(query);
2021-01-26 20:37:29 +00:00
}
2022-02-26 14:57:16 +00:00
/**
* Parses the given hash into a {@link LiveAtlasParsedUrl}, if the hash matches the LiveAtlas URL hash format
* @param {string} hash The hash to parse
* @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided hash matched the LiveAtlas URL
* hash format, otherwise null
*/
export const parseMapHash = (hash: string): LiveAtlasParsedUrl | null => {
2022-02-27 19:28:37 +00:00
let world, map, location, zoom;
hash = hash.replace('#', '');
if(hash[0] === '/' && hash.split('/').length === 7) { //Overviewer URL format
const parts = hash.split('/');
zoom = undefined; //FIXME: Not sure how to handle negative values atm
world = parts[5];
map = parts[6];
location = [
parts[1],
parts[2],
parts[3]
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item));
} else { //LiveAtlas URL format
const parts = hash.split(';');
world = parts[0] || undefined;
map = parts[1] || undefined;
2021-09-12 19:37:59 +00:00
location = (parts[2] || '').split(',')
2022-02-27 19:28:37 +00:00
.map(item => parseFloat(item))
.filter(item => !isNaN(item) && isFinite(item));
2021-01-26 20:37:29 +00:00
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
2022-02-27 19:28:37 +00:00
}
2021-09-12 19:37:59 +00:00
return validateParsedUrl({
2022-02-27 19:28:37 +00:00
world: world ? decodeURIComponent(world) : undefined,
map: map ? decodeURIComponent(map) : undefined,
2021-09-12 19:37:59 +00:00
location: location.length === 3 ? {
2021-01-26 20:37:29 +00:00
x: location[0],
y: location[1],
z: location[2],
} : undefined,
zoom,
legacy: false,
2021-09-12 19:37:59 +00:00
});
2021-01-26 20:37:29 +00:00
}
2022-02-26 14:57:16 +00:00
/**
* Parses the given {@link URLSearchParams} into a {@link LiveAtlasParsedUrl}, if it matches any known query string formats
* @param {URLSearchParams} query The URLSearchParams to parse
* @returns {LiveAtlasParsedUrl | null} A LiveAtlasParsedUrl if the provided hash matched the LiveAtlas URL
* hash format, otherwise null
*/
export const parseMapSearchParams = (query: URLSearchParams): LiveAtlasParsedUrl | null => {
2022-02-27 19:28:37 +00:00
let world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined,
map = query.has('worldname') ? query.get('mapname') || undefined : undefined; //Dynmap only
const location = [
query.get('x') || '',
query.get('y') || '64',
query.get('z') || ''
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)),
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
world = world ? decodeURIComponent(world) : undefined;
map = map ? decodeURIComponent(map) : undefined;
2021-01-26 20:37:29 +00:00
2021-09-12 19:37:59 +00:00
return validateParsedUrl({
2021-01-26 20:37:29 +00:00
world,
map,
2021-09-12 19:37:59 +00:00
location: location.length === 3 ? {
2021-01-26 20:37:29 +00:00
x: location[0],
y: location[1],
z: location[2],
} : undefined,
zoom,
legacy: true,
2021-09-12 19:37:59 +00:00
});
}
2022-02-26 14:57:16 +00:00
/**
* Validates the given {@link LiveAtlasParsedUrl} to ensure all required properties are present and have valid values
* @param {LiveAtlasParsedUrl} parsed The parsed URL to validate
* @return {LiveAtlasParsedUrl | null} The parsed URL, possibly modified to ensure validity, or null if it is invalid
* and cannot be fixed
* @see {@link parseMapSearchParams}
* @see {@link parseMapHash}
* @private
*/
2021-09-12 19:37:59 +00:00
const validateParsedUrl = (parsed: any) => {
if(typeof parsed.zoom !== 'undefined' && (isNaN(parsed.zoom) || parsed.zoom < 0 || !isFinite(parsed.zoom))) {
parsed.zoom = undefined;
}
2021-09-12 19:37:59 +00:00
if(!parsed.world) {
return null;
}
return parsed;
2021-05-19 23:01:19 +00:00
}
2022-02-26 14:57:16 +00:00
/**
* Generates a LiveAtlas formatted URL hash representing the given {@link LiveAtlasMapDefinition}map, {@link Coordinate}
* location and zoom level
* @param {LiveAtlasMapDefinition} map The map
* @param {Coordinate} location The location
* @param {number} zoom The zoom level
* @return {string} The URL hash (including the #), or an empty string if a valid hash cannot be constructed
*/
export const getUrlForLocation = (map: LiveAtlasMapDefinition, location: Coordinate, zoom: number): string => {
2021-05-24 15:40:03 +00:00
const x = Math.round(location.x),
2022-01-14 19:53:37 +00:00
y = Math.round(location.y),
2021-05-24 15:40:03 +00:00
z = Math.round(location.z),
locationString = `${x},${y},${z}`;
if(!map) {
2021-05-24 15:40:03 +00:00
return '';
}
return `#${map.world.name};${map.name};${locationString};${zoom}`;
2021-05-26 23:50:34 +00:00
}
2022-02-26 14:57:16 +00:00
/**
* Focuses the first html element which matches the given selector, if any
* @param {string} selector The selector string
*/
export const focus = (selector: string) => {
const element = document.querySelector(selector);
if(element) {
(element as HTMLElement).focus();
}
2021-07-19 15:40:30 +00:00
}
const decodeTextarea = document.createElement('textarea');
2022-02-26 14:57:16 +00:00
/**
* Decodes HTML entities in the given string using a <textarea>
* @param {string} text The text to decode HTML entities in
* @returns {string} The given text with any HTML entities decoded
*/
export const decodeHTMLEntities = (text: string) => {
decodeTextarea.innerHTML = text;
return decodeTextarea.textContent || '';
}
2022-02-26 14:57:16 +00:00
/**
* Strips HTML from the given string using a contextual {@link DocumentFragment} and converts <br>s to spaces
* @param {string} text The text to strip HTML from
* @returns {string} The given text with HTML stripped
*/
2022-01-14 14:38:43 +00:00
export const stripHTML = (text: string) => {
return documentRange.createContextualFragment(text.replace(brToSpaceRegex,'&nbsp;')).textContent || '';
}
2022-02-26 14:57:16 +00:00
/**
* Default success callback function for VueClipboard, will display a notification with the configured copy success
* message
*/
export const clipboardSuccess = () => () => notify(useStore().state.messages.copyToClipboardSuccess);
2022-02-26 14:57:16 +00:00
/**
* Default error callback function for VueClipboard, will display a notification with the configured copy error
* message
*/
export const clipboardError = () => (e: Error) => {
notify({ type: 'error', text: useStore().state.messages.copyToClipboardError });
console.error('Error copying to clipboard', e);
};
2022-02-26 14:57:16 +00:00
/**
* Creates a {@link LiveAtlasMessageConfig} from the provided config object. The provided object will be checked for all
* expected LiveAtlasMessageConfig messages, with fallback "Missing message" messages being used when a message is
* missing from the provided object.
* @param {Object} config Config object containing messages to include in the final LiveAtlasMessageConfig. Should
* contain a complete or subset of keys from LiveAtlasMessageConfig, additional keys will be ignored.
*/
export const getMessages = (config: any = {}) => {
return Object.assign(_getMessages(globalMessages, config),
_getMessages(serverMessages, config)) as LiveAtlasMessageConfig;
}
2022-02-26 14:57:16 +00:00
/**
* Creates a {@link LiveAtlasGlobalMessageConfig} from the provided config object. The provided object will be checked
* for all expected LiveAtlasGlobalMessageConfig messages, with fallback "Missing message" messages being used
* when a message is missing from the provided object.
* @param {Object} config Config object containing messages to include in the final LiveAtlasGlobalMessageConfig.
* Should contain a complete or subset of keys from LiveAtlasGlobalMessageConfig, additional keys will be ignored.
*/
export const getGlobalMessages = (config: any = {}) => {
return _getMessages(globalMessages, config) as LiveAtlasGlobalMessageConfig;
}
2022-02-26 14:57:16 +00:00
/**
* Creates an object containing the keys present in the messageKeys object and the values present in the config object.
*
* For each key, the config object is checked for a corresponding value. A fallback "Missing message" value is used if
* config object does not contain a value.
* @param {Object} messageKeys The object to take the keys from
* @param {Object} config The object to take the values from, if present
* @see {@link getMessages}
* @see {@link getGlobalMessages}
* @private
*/
const _getMessages = (messageKeys: any, config: any = {}) => {
const messages: any = {};
for(const key of messageKeys) {
messages[key] = config[key] || `Missing message: ${key}`;
}
return messages as LiveAtlasGlobalMessageConfig;
}
/**
* Determines the bounds required to enclose the given separate arrays of x, y and z coordinates
* All arrays are expected to be the same length
* @param {number[]} x X coordinates
* @param {number[]} y Y coordinates
* @param {number[]} z Z coordinates
* @returns {LiveAtlasBounds} The calculated bounds
*/
2022-01-16 21:18:48 +00:00
export const getBounds = (x: number[], y: number[], z: number[]): LiveAtlasBounds => {
return {
min: {x: Math.min.apply(null, x), y: Math.min.apply(null, y), z: Math.min.apply(null, z)},
max: {x: Math.max.apply(null, x), y: Math.max.apply(null, y), z: Math.max.apply(null, z)},
};
}
/**
2022-02-26 14:57:16 +00:00
* Determines the bounds required to enclose the given array of {@link Coordinate}s
* Multiple dimension arrays are accepted and will be handled recursively
* @param {Coordinate[]} points Points to determine the bounds for
* @returns {LiveAtlasBounds} The calculated bounds
*/
2022-01-16 21:18:48 +00:00
export const getBoundsFromPoints = (points: Coordinate[]): LiveAtlasBounds => {
const bounds = {
max: {x: -Infinity, y: -Infinity, z: -Infinity},
min: {x: Infinity, y: Infinity, z: Infinity},
2022-01-16 21:18:48 +00:00
}
const handlePoint = (point: any) => {
if(Array.isArray(point)) {
point.map(handlePoint);
} else {
bounds.max.x = Math.max(point.x, bounds.max.x);
bounds.max.y = Math.max(point.y, bounds.max.y);
bounds.max.z = Math.max(point.z, bounds.max.z);
bounds.min.x = Math.min(point.x, bounds.min.x);
bounds.min.y = Math.min(point.y, bounds.min.y);
bounds.min.z = Math.min(point.z, bounds.min.z);
}
}
points.map(handlePoint);
2022-01-16 21:18:48 +00:00
return bounds;
}
/**
2022-02-26 14:57:16 +00:00
* Determines the center point of the given {@link LiveAtlasBounds}
* @param {LiveAtlasBounds} bounds The bounds to find the center point for
* @return {LiveAtlasLocation} The center point
*/
2022-01-16 21:18:48 +00:00
export const getMiddle = (bounds: LiveAtlasBounds): LiveAtlasLocation => {
return {
2022-01-16 21:18:48 +00:00
x: bounds.min.x + ((bounds.max.x - bounds.min.x) / 2),
y: bounds.min.y + ((bounds.max.y - bounds.min.y) / 2),
z: bounds.min.z + ((bounds.max.z - bounds.min.z) / 2),
};
}
2022-02-21 21:50:31 +00:00
/**
2022-02-26 14:57:16 +00:00
* Creates an "allow-scripts" sandboxed <iframe>
* @see {@link runSandboxed}
2022-02-27 21:36:46 +00:00
* @returns {Window} A promise that resolves to the iframe's contentWindow
*/
2022-02-27 21:36:46 +00:00
const createIframeSandbox = async (): Promise<Window> => {
2022-02-21 21:50:31 +00:00
const frame = document.createElement('iframe');
frame.hidden = true;
frame.sandbox.add('allow-scripts');
frame.srcdoc = `<script>window.addEventListener("message", function(e) {
if(!e.data?.key) {
console.warn('Ignoring postmessage without key');
return;
}
try {
e.source.postMessage({
key: e.data.key,
success: true,
result: Function('', "'use strict';" + e.data.code)(),
}, e.origin);
} catch(ex) {
e.source.postMessage({
key: e.data.key,
success: false,
error: ex
}, e.origin);
}
})</script>`;
window.addEventListener('message', e => {
if(e.origin !== "null" || e.source !== frame.contentWindow) {
console.warn('Ignoring postmessage with invalid source');
return;
}
if(!e.data?.key) {
console.warn('Ignoring postmessage without key');
return;
}
if(!sandboxSuccessCallbacks.has(e.data.key)) {
console.warn('Ignoring postmessage with invalid key');
return;
}
if(e.data.success) {
sandboxSuccessCallbacks.get(e.data.key)!.call(this, e.data.result);
} else {
sandboxErrorCallbacks.get(e.data.key)!.call(this, e.data.error);
}
});
2022-02-27 21:36:46 +00:00
return new Promise(resolve => {
document.body.appendChild(frame);
frame.onload = () => {
resolve(frame.contentWindow as Window);
};
});
2022-02-21 21:50:31 +00:00
}
2022-02-27 21:36:46 +00:00
let sandboxWindow: Window | null = null;
2022-02-21 21:50:31 +00:00
const sandboxSuccessCallbacks: Map<number, (result?: any) => void> = new Map();
const sandboxErrorCallbacks: Map<number, (reason?: any) => void> = new Map();
/**
* Runs the given untrusted JavaScript code inside an "allow-scripts" sandboxed <iframe>
* The executing code cannot access or interfere with LiveAtlas state,
* but can still make requests and access many JS APIs
* @param {string} code The code to run
* @returns {Promise<any>} A promise that will resolve with the return value of the executed JS,
* or will reject with any Errors that occurred during execution.
*/
2022-02-21 21:50:31 +00:00
export const runSandboxed = async (code: string) => {
2022-02-27 21:36:46 +00:00
if(!sandboxWindow) {
sandboxWindow = await createIframeSandbox();
}
2022-02-21 21:50:31 +00:00
return new Promise((resolve, reject) => {
const key = Math.random();
sandboxSuccessCallbacks.set(key, resolve);
sandboxErrorCallbacks.set(key, reject);
sandboxWindow!.postMessage({
key,
code,
}, '*');
});
}
/**
* Attempts to guess the dimension of the given world name
* The world name is checked against vanilla nether/end world names and regexes covering
* common nether/end world naming conventions
* If none of the above match, the world is assumed to be overworld
* @param {string} worldName Name of the world to guess
* @returns {LiveAtlasDimension} The guessed dimension
*/
export const guessWorldDimension = (worldName: string) => {
let dimension: LiveAtlasDimension = 'overworld';
if (netherWorldNameRegex.test(worldName) || (worldName == 'DIM-1')) {
dimension = 'nether';
} else if (endWorldNameRegex.test(worldName) || (worldName == 'DIM1')) {
dimension = 'end';
}
return dimension;
}