Sanitize legacy distributions on load
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const { DistributionAPI } = require('helios-core/common')
|
||||
const { DistributionAPI, HeliosDistribution } = require('helios-core/common')
|
||||
|
||||
const ConfigManager = require('./configmanager')
|
||||
|
||||
@@ -7,6 +7,80 @@ const DEFAULT_REMOTE_DISTRO_URL = 'https://cdn.mysticred.space/launcher/distribu
|
||||
let remoteDistroUrl = DEFAULT_REMOTE_DISTRO_URL
|
||||
let activeApi = createApi(remoteDistroUrl)
|
||||
|
||||
function sanitizeServer(rawServer){
|
||||
const nextServer = {
|
||||
...rawServer
|
||||
}
|
||||
|
||||
if(typeof nextServer.address !== 'string' || nextServer.address.trim().length === 0){
|
||||
nextServer.address = '127.0.0.1:25565'
|
||||
} else {
|
||||
nextServer.address = nextServer.address.trim()
|
||||
}
|
||||
|
||||
if(!Array.isArray(nextServer.modules)){
|
||||
nextServer.modules = []
|
||||
}
|
||||
|
||||
if(typeof nextServer.minecraftVersion !== 'string' || nextServer.minecraftVersion.trim().length === 0){
|
||||
nextServer.minecraftVersion = typeof nextServer.version === 'string' && nextServer.version.trim().length > 0
|
||||
? nextServer.version.trim()
|
||||
: '1.20.1'
|
||||
}
|
||||
|
||||
return nextServer
|
||||
}
|
||||
|
||||
function sanitizeDistribution(rawDistribution){
|
||||
const nextDistribution = rawDistribution == null
|
||||
? { version: '1.0.0', rss: '', servers: [] }
|
||||
: { ...rawDistribution }
|
||||
|
||||
nextDistribution.servers = Array.isArray(nextDistribution.servers)
|
||||
? nextDistribution.servers.map((server) => sanitizeServer(server))
|
||||
: []
|
||||
|
||||
return nextDistribution
|
||||
}
|
||||
|
||||
function buildDistribution(api, rawDistribution){
|
||||
const sanitizedDistribution = sanitizeDistribution(rawDistribution)
|
||||
api.rawDistribution = sanitizedDistribution
|
||||
api.distribution = new HeliosDistribution(sanitizedDistribution, api.commonDir, api.instanceDir)
|
||||
return api.distribution
|
||||
}
|
||||
|
||||
function patchDistributionApi(api){
|
||||
api.getDistribution = async function(){
|
||||
if(this.rawDistribution == null || this.distribution == null){
|
||||
const rawDistribution = await this.loadDistribution()
|
||||
return buildDistribution(this, rawDistribution)
|
||||
}
|
||||
return this.distribution
|
||||
}
|
||||
|
||||
api.getDistributionLocalLoadOnly = async function(){
|
||||
if(this.rawDistribution == null || this.distribution == null){
|
||||
const rawDistribution = await this.pullLocal()
|
||||
if(rawDistribution == null){
|
||||
throw new Error('FATAL: Unable to load distribution from local disk.')
|
||||
}
|
||||
return buildDistribution(this, rawDistribution)
|
||||
}
|
||||
return this.distribution
|
||||
}
|
||||
|
||||
api.refreshDistributionOrFallback = async function(){
|
||||
const rawDistribution = await this._loadDistributionNullable()
|
||||
if(rawDistribution == null){
|
||||
return this.distribution
|
||||
}
|
||||
return buildDistribution(this, rawDistribution)
|
||||
}
|
||||
|
||||
return api
|
||||
}
|
||||
|
||||
function createApi(url) {
|
||||
const api = new DistributionAPI(
|
||||
ConfigManager.getLauncherDirectory(),
|
||||
@@ -16,7 +90,7 @@ function createApi(url) {
|
||||
false
|
||||
)
|
||||
|
||||
return api
|
||||
return patchDistributionApi(api)
|
||||
}
|
||||
|
||||
function replaceApi(url) {
|
||||
|
||||
Reference in New Issue
Block a user