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
|
|
|
*/
|
|
|
|
|
2020-12-11 15:28:51 +00:00
|
|
|
import {useStore} from "@/store";
|
2021-07-23 19:32:15 +00:00
|
|
|
import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition";
|
2022-01-12 15:21:01 +00:00
|
|
|
import {
|
2022-01-14 18:35:59 +00:00
|
|
|
Coordinate,
|
2022-02-26 14:41:23 +00:00
|
|
|
LiveAtlasBounds,
|
|
|
|
LiveAtlasDimension,
|
2022-02-21 21:50:31 +00:00
|
|
|
LiveAtlasGlobalMessageConfig,
|
|
|
|
LiveAtlasLocation,
|
2022-01-12 15:21:01 +00:00
|
|
|
LiveAtlasMessageConfig,
|
|
|
|
} from "@/index";
|
2022-01-10 22:16:05 +00:00
|
|
|
import {notify} from "@kyvg/vue3-notification";
|
2022-01-12 15:21:01 +00:00
|
|
|
import {globalMessages, serverMessages} from "../messages";
|
2021-01-26 21:07:56 +00:00
|
|
|
|
2022-01-14 14:38:58 +00:00
|
|
|
const documentRange = document.createRange(),
|
2022-02-26 14:41:23 +00:00
|
|
|
brToSpaceRegex = /<br \/>/g;
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-07-24 00:15:52 +00:00
|
|
|
export const titleColoursRegex = /§[0-9a-f]/ig;
|
2022-02-26 13:52:02 +00:00
|
|
|
export const netherWorldNameRegex = /[_\s]?nether([\s_]|$)/i;
|
|
|
|
export const endWorldNameRegex = /(^|[_\s])end([\s_]|$)/i;
|
2021-07-24 00:15:52 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
export const getMinecraftTime = (serverTime: number) => {
|
|
|
|
const day = serverTime >= 0 && serverTime < 13700;
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
return {
|
|
|
|
serverTime: serverTime,
|
|
|
|
days: Math.floor((serverTime + 8000) / 24000),
|
2020-12-10 02:21:42 +00:00
|
|
|
|
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),
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
day: day,
|
|
|
|
night: !day
|
|
|
|
};
|
|
|
|
}
|
2020-12-10 02:21:42 +00:00
|
|
|
|
2021-09-10 14:29:27 +00:00
|
|
|
export const parseUrl = (url: URL) => {
|
|
|
|
const query = new URLSearchParams(url.search),
|
|
|
|
hash = url.hash.replace('#', '');
|
2020-12-21 18:40:28 +00:00
|
|
|
|
2021-09-12 19:37:59 +00:00
|
|
|
return hash ? parseMapHash(hash) : parseMapSearchParams(query);
|
2021-01-26 20:37:29 +00:00
|
|
|
}
|
2020-12-13 02:50:17 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
export const parseMapHash = (hash: string) => {
|
|
|
|
const parts = hash.replace('#', '').split(';');
|
2020-12-21 18:40:28 +00:00
|
|
|
|
2021-01-26 20:37:29 +00:00
|
|
|
const world = parts[0] || undefined,
|
2021-09-12 19:37:59 +00:00
|
|
|
map = parts[1] || undefined,
|
|
|
|
location = (parts[2] || '').split(',')
|
2021-01-26 20:37:29 +00:00
|
|
|
.map(item => parseFloat(item))
|
|
|
|
.filter(item => !isNaN(item) && isFinite(item)),
|
|
|
|
zoom = typeof parts[3] !== 'undefined' ? parseInt(parts[3]) : undefined;
|
2020-12-13 02:50:17 +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: false,
|
2021-09-12 19:37:59 +00:00
|
|
|
});
|
2021-01-26 20:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const parseMapSearchParams = (query: URLSearchParams) => {
|
2021-09-07 22:08:44 +00:00
|
|
|
const world = query.get('worldname') /* Dynmap */ || query.get('world') /* Pl3xmap */ || undefined,
|
2021-09-12 19:37:59 +00:00
|
|
|
map = query.has('worldname') ? query.get('mapname') || undefined : undefined, //Dynmap only
|
|
|
|
location = [
|
2021-01-26 20:37:29 +00:00
|
|
|
query.get('x') || '',
|
2021-09-07 22:08:44 +00:00
|
|
|
query.get('y') || '64',
|
2021-01-26 20:37:29 +00:00
|
|
|
query.get('z') || ''
|
|
|
|
].map(item => parseFloat(item)).filter(item => !isNaN(item) && isFinite(item)),
|
|
|
|
zoom = query.has('zoom') ? parseInt(query.get('zoom') as string) : undefined;
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const validateParsedUrl = (parsed: any) => {
|
|
|
|
if(typeof parsed.zoom !== 'undefined' && (isNaN(parsed.zoom) || parsed.zoom < 0 || !isFinite(parsed.zoom))) {
|
|
|
|
parsed.zoom = undefined;
|
2020-12-10 02:21:42 +00:00
|
|
|
}
|
2021-09-12 19:37:59 +00:00
|
|
|
|
|
|
|
if(!parsed.world) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
2021-05-19 23:01:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 18:35:59 +00:00
|
|
|
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}`;
|
|
|
|
|
2021-07-23 19:32:15 +00:00
|
|
|
if(!map) {
|
2021-05-24 15:40:03 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-07-23 19:32:15 +00:00
|
|
|
return `#${map.world.name};${map.name};${locationString};${zoom}`;
|
2021-05-26 23:50:34 +00:00
|
|
|
}
|
2021-05-28 23:08:43 +00:00
|
|
|
|
|
|
|
export const focus = (selector: string) => {
|
|
|
|
const element = document.querySelector(selector);
|
|
|
|
|
|
|
|
if(element) {
|
|
|
|
(element as HTMLElement).focus();
|
|
|
|
}
|
2021-07-19 15:40:30 +00:00
|
|
|
}
|
2022-01-10 22:16:05 +00:00
|
|
|
|
2022-01-11 17:52:37 +00:00
|
|
|
const decodeTextarea = document.createElement('textarea');
|
|
|
|
|
|
|
|
export const decodeHTMLEntities = (text: string) => {
|
|
|
|
decodeTextarea.innerHTML = text;
|
|
|
|
return decodeTextarea.textContent || '';
|
|
|
|
}
|
|
|
|
|
2022-01-14 14:38:43 +00:00
|
|
|
export const stripHTML = (text: string) => {
|
|
|
|
return documentRange.createContextualFragment(text.replace(brToSpaceRegex,' ')).textContent || '';
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:16:05 +00:00
|
|
|
export const clipboardSuccess = () => () => notify(useStore().state.messages.copyToClipboardSuccess);
|
|
|
|
|
|
|
|
export const clipboardError = () => (e: Error) => {
|
|
|
|
notify({ type: 'error', text: useStore().state.messages.copyToClipboardError });
|
|
|
|
console.error('Error copying to clipboard', e);
|
|
|
|
};
|
2022-01-12 15:21:01 +00:00
|
|
|
|
|
|
|
export const getMessages = (config: any = {}) => {
|
|
|
|
return Object.assign(_getMessages(globalMessages, config),
|
|
|
|
_getMessages(serverMessages, config)) as LiveAtlasMessageConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getGlobalMessages = (config: any = {}) => {
|
|
|
|
return _getMessages(globalMessages, config) as LiveAtlasGlobalMessageConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-01-14 18:35:59 +00:00
|
|
|
|
2022-02-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* 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-01-14 18:35:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* Determines the bounds required to enclose the given array of {@see 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 = {
|
2022-01-31 22:36:25 +00:00
|
|
|
max: {x: -Infinity, y: -Infinity, z: -Infinity},
|
|
|
|
min: {x: Infinity, y: Infinity, z: Infinity},
|
2022-01-16 21:18:48 +00:00
|
|
|
}
|
2022-01-14 18:35:59 +00:00
|
|
|
|
2022-01-31 22:36:25 +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);
|
|
|
|
}
|
2022-01-14 18:35:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 22:36:25 +00:00
|
|
|
points.map(handlePoint);
|
|
|
|
|
2022-01-16 21:18:48 +00:00
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* Determines the center point of the given {@see 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 => {
|
2022-01-14 18:35:59 +00:00
|
|
|
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-01-14 18:35:59 +00:00
|
|
|
}
|
2022-02-21 21:50:31 +00:00
|
|
|
|
2022-02-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* Creates an "allow-scripts" sandboxed <iframe> to be used by {@see runSandboxed}
|
|
|
|
* @returns {Window} The iframe's contentWindow
|
|
|
|
*/
|
2022-02-21 21:50:31 +00:00
|
|
|
const createIframeSandbox = () => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.body.appendChild(frame);
|
|
|
|
return frame.contentWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sandboxWindow: Window | null = createIframeSandbox();
|
|
|
|
const sandboxSuccessCallbacks: Map<number, (result?: any) => void> = new Map();
|
|
|
|
const sandboxErrorCallbacks: Map<number, (reason?: any) => void> = new Map();
|
|
|
|
|
2022-02-26 13:52:02 +00:00
|
|
|
/**
|
|
|
|
* 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) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const key = Math.random();
|
|
|
|
|
|
|
|
sandboxSuccessCallbacks.set(key, resolve);
|
|
|
|
sandboxErrorCallbacks.set(key, reject);
|
|
|
|
|
|
|
|
sandboxWindow!.postMessage({
|
|
|
|
key,
|
|
|
|
code,
|
|
|
|
}, '*');
|
|
|
|
});
|
|
|
|
}
|
2022-02-26 13:52:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|