2021-07-25 14:12:40 +00:00
/ *
* Copyright 2021 James Lyne
*
* 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" ;
const expectedConfigVersion = 1 ;
2021-05-27 00:41:58 +00:00
const validateLiveAtlasConfiguration = ( config : any ) : Map < string , LiveAtlasServerDefinition > = > {
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 ] ;
if ( ! serverConfig || serverConfig . constructor !== Object || ! Object . keys ( serverConfig ) . length ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Configuration missing. ${ check } ` ) ;
}
serverConfig . id = server ;
2021-07-29 17:06:19 +00:00
2022-01-12 01:06:18 +00:00
//Squaremap and Pl3xmap are currently interchangeable
if ( typeof serverConfig . squaremap !== 'undefined' ) {
serverConfig . pl3xmap = serverConfig . squaremap ;
}
2021-07-29 17:06:19 +00:00
if ( typeof serverConfig . pl3xmap !== 'undefined' ) {
serverConfig . type = 'pl3xmap' ;
2021-09-28 12:33:05 +00:00
// Ensure trailing /
if ( serverConfig . pl3xmap . slice ( - 1 ) !== '/' ) {
serverConfig . pl3xmap = ` ${ serverConfig . pl3xmap } / ` ;
}
2021-07-29 17:06:19 +00:00
} else if ( typeof serverConfig . dynmap !== 'undefined' ) {
if ( ! serverConfig . dynmap || serverConfig . dynmap . constructor !== Object ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap configuration object missing. ${ check } ` ) ;
}
if ( ! serverConfig . dynmap . configuration ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap configuration URL missing. ${ check } ` ) ;
}
if ( ! serverConfig . dynmap . update ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap update URL missing. ${ check } ` ) ;
}
if ( ! serverConfig . dynmap . markers ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap markers URL missing. ${ check } ` ) ;
}
if ( ! serverConfig . dynmap . tiles ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap tiles URL missing. ${ check } ` ) ;
}
if ( ! serverConfig . dynmap . sendmessage ) {
throw new ConfigurationError ( ` Server ' ${ server } ': Dynmap sendmessage URL missing. ${ check } ` ) ;
}
serverConfig . type = 'dynmap' ;
} else {
throw new ConfigurationError ( ` Server ' ${ server } ': No Dynmap or Pl3xmap configuration defined. ${ check } ` ) ;
2021-05-27 00:41:58 +00:00
}
result . set ( server , serverConfig ) ;
}
return result ;
} ;
2021-07-25 01:02:42 +00:00
const validateDynmapConfiguration = ( 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.' ;
if ( ! config ) {
throw new ConfigurationError ( ` Dynmap configuration is missing. ${ check } ` ) ;
}
if ( ! config . configuration ) {
throw new ConfigurationError ( ` Dynmap configuration URL is missing. ${ check } ` ) ;
}
if ( ! config . update ) {
throw new ConfigurationError ( ` Dynmap update URL is missing. ${ check } ` ) ;
}
if ( ! config . markers ) {
throw new ConfigurationError ( ` Dynmap markers URL is missing. ${ check } ` ) ;
}
if ( ! config . tiles ) {
throw new ConfigurationError ( ` Dynmap tiles URL is missing. ${ check } ` ) ;
}
if ( ! config . sendmessage ) {
throw new ConfigurationError ( ` Dynmap sendmessage URL is missing. ${ check } ` ) ;
}
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' ,
type : 'dynmap' ,
dynmap : config
} ) ;
return result ;
} ;
2021-07-20 20:12:08 +00:00
export const getServerDefinitions = ( config : LiveAtlasGlobalConfig ) : Map < string , LiveAtlasServerDefinition > = > {
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' ) {
return validateLiveAtlasConfiguration ( config . servers ) ;
2021-05-27 00:41:58 +00:00
}
return validateDynmapConfiguration ( window . config ? . url || null ) ;
} ;
2021-07-20 20:12:08 +00:00