2021-07-25 14:12:40 +00:00
/ *
2022-02-21 21:53:49 +00:00
* Copyright 2022 James Lyne
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
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* 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 .
* /
2021-07-25 01:02:42 +00:00
import { LiveAtlasGlobalConfig , LiveAtlasServerDefinition } from "@/index" ;
2021-05-27 00:41:58 +00:00
import ConfigurationError from "@/errors/ConfigurationError" ;
import { DynmapUrlConfig } from "@/dynmap" ;
2021-12-09 18:26:57 +00:00
import { useStore } from "@/store" ;
2022-01-21 20:20:01 +00:00
import MapProvider from "@/providers/MapProvider" ;
import DynmapMapProvider from "@/providers/DynmapMapProvider" ;
2021-12-09 18:26:57 +00:00
2022-02-26 14:57:16 +00:00
const expectedConfigVersion = 1 ,
registeredProviders : Map < string , new ( config : any ) = > MapProvider > = new Map ( ) ,
serverProviders : Map < string , MapProvider > = new Map ( ) ;
/ * *
* Registers the given { @link MapProvider } with the given id
* Server entries in { @link LiveAtlasGlobalConfig } with the given id will use the given MapProvider
* @param { string } id The id
* @param { new ( config : any ) = > MapProvider } provider The MapProvider
* /
2022-01-21 20:20:01 +00:00
export const registerMapProvider = ( id : string , provider : new ( config : any ) = > MapProvider ) = > {
if ( registeredProviders . has ( id ) ) {
throw new TypeError ( ` ${ id } is already registered ` ) ;
}
registeredProviders . set ( id , provider ) ;
}
2022-02-26 14:57:16 +00:00
/ * *
* Gets the MapProvider for the given server
* @param { string } server Name of the server
* @returns The MapProvider , if one exists
* /
2022-01-21 20:20:01 +00:00
export const getServerMapProvider = ( server : string ) : MapProvider | undefined = > {
return serverProviders . get ( server ) ;
}
2022-02-26 14:57:16 +00:00
/ * *
* Attempts to load server definitions from the provided config object
* @param { Object } config Config object to load server definitions from
* @returns Map of loaded servers
* @see { @link loadConfig }
* @private
* /
2022-01-21 20:20:01 +00:00
const loadLiveAtlasConfig = ( config : any ) : Map < string , LiveAtlasServerDefinition > = > {
2021-05-27 00:41:58 +00:00
const check = '\nCheck your server configuration in index.html is correct.' ,
result = new Map < string , LiveAtlasServerDefinition > ( ) ;
if ( ! Object . keys ( config ) . length ) {
throw new ConfigurationError ( ` No servers defined. ${ check } ` ) ;
}
for ( const server in config ) {
if ( ! Object . hasOwnProperty . call ( config , server ) ) {
continue ;
}
const serverConfig = config [ server ] ;
2022-01-21 20:20:01 +00:00
let foundProvider = false ;
for ( const mapProvider of registeredProviders ) {
if ( serverConfig && Object . hasOwnProperty . call ( serverConfig , mapProvider [ 0 ] ) ) {
try {
serverProviders . set ( server , new mapProvider [ 1 ] ( serverConfig [ mapProvider [ 0 ] ] ) ) ;
} catch ( e : any ) {
e . message = ` Server " ${ server } ": ${ e . message } . ${ check } ` ;
throw e ;
}
result . set ( server , serverConfig ) ;
foundProvider = true ;
2021-07-29 17:06:19 +00:00
}
2022-01-21 20:20:01 +00:00
}
2021-07-29 17:06:19 +00:00
2022-01-21 20:20:01 +00:00
if ( ! foundProvider ) {
throw new ConfigurationError ( ` Server " ${ server } ": No configuration found for any supported map type. ${ check } ` ) ;
2021-05-27 00:41:58 +00:00
}
2022-01-21 20:20:01 +00:00
serverConfig . id = server ;
2021-05-27 00:41:58 +00:00
}
return result ;
} ;
2022-02-26 14:57:16 +00:00
/ * *
* Attempts to load a Dynmap server definition from the given object
* @param { Object } config Config object to load from
* @see { @link loadConfig }
* @private
* /
2022-01-21 20:20:01 +00:00
const loadDefaultConfig = ( config : DynmapUrlConfig ) : Map < string , LiveAtlasServerDefinition > = > {
2021-05-27 00:41:58 +00:00
const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.' ;
2021-07-25 01:02:42 +00:00
const result = new Map < string , LiveAtlasServerDefinition > ( ) ;
2021-05-27 00:41:58 +00:00
result . set ( 'dynmap' , {
id : 'dynmap' ,
label : 'dynmap' ,
dynmap : config
} ) ;
2022-01-21 20:20:01 +00:00
try {
serverProviders . set ( 'dynmap' , new DynmapMapProvider ( config ) ) ;
} catch ( e : any ) {
e . message = ` ${ e . message } . ${ check } ` ;
throw e ;
}
2021-05-27 00:41:58 +00:00
return result ;
} ;
2022-02-26 14:57:16 +00:00
/ * *
* Attempts to load server definitions from the provided config object
* If no servers definitions are present in the object , an attempt will be made to load a Dynmap server definition from
* a global dynmap config object defined by standalone / config . js
* @param { Object } config Config object to load server definitions from
* @returns Map of loaded servers
* @private
* /
2022-01-21 20:20:01 +00:00
export const loadConfig = ( config : LiveAtlasGlobalConfig ) : Map < string , LiveAtlasServerDefinition > = > {
2021-07-20 20:12:08 +00:00
if ( ! config ) {
2021-12-09 18:26:20 +00:00
throw new ConfigurationError ( ` No configuration found. \ nCheck for any syntax errors in your configuration in index.html. Your browser console may contain additional information. ` ) ;
2021-05-27 00:41:58 +00:00
}
2021-12-09 18:26:57 +00:00
if ( config . version !== expectedConfigVersion ) {
throw new ConfigurationError ( ` Configuration version mismatch. \ nUse a fresh copy of index.html from your current LiveAtlas version ( ${ useStore ( ) . state . version } ) and reapply any customisations. ` ) ;
}
2021-07-22 21:49:38 +00:00
if ( typeof config . servers !== 'undefined' ) {
2022-01-21 20:20:01 +00:00
return loadLiveAtlasConfig ( config . servers ) ;
2021-05-27 00:41:58 +00:00
}
2022-01-21 20:20:01 +00:00
return loadDefaultConfig ( window . config ? . url || null ) ;
2021-05-27 00:41:58 +00:00
} ;
2021-07-20 20:12:08 +00:00