| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- import { Distribution, Server, Module, Type, Required as HeliosRequired } from 'helios-distribution-types'
- import { MavenComponents, MavenUtil } from 'common/util/MavenUtil'
- import { join } from 'path'
- import { LoggerUtil } from 'common/logging/loggerutil'
- const logger = LoggerUtil.getLogger('DistributionFactory')
- export class HeliosDistribution {
- private mainServerIndex!: number
- public readonly servers: HeliosServer[]
- constructor(
- public readonly rawDistribution: Distribution
- ) {
- this.resolveMainServerIndex()
- this.servers = this.rawDistribution.servers.map(s => new HeliosServer(s))
- }
- private resolveMainServerIndex(): void {
- if(this.rawDistribution.servers.length > 0) {
- for(let i=0; i<this.rawDistribution.servers.length; i++) {
- if(this.mainServerIndex == null) {
- if(this.rawDistribution.servers[i].mainServer) {
- this.mainServerIndex = i
- }
- } else {
- this.rawDistribution.servers[i].mainServer = false
- }
- }
- if(this.mainServerIndex == null) {
- this.mainServerIndex = 0
- this.rawDistribution.servers[this.mainServerIndex].mainServer = true
- }
- } else {
- logger.warn('Distribution has 0 configured servers. This doesnt seem right..')
- this.mainServerIndex = 0
- }
- }
- public getMainServer(): HeliosServer | null {
- return this.mainServerIndex < this.servers.length ? this.servers[this.mainServerIndex] : null
- }
- public getServerById(id: string): HeliosServer | null {
- return this.servers.find(s => s.rawServer.id === id) || null
- }
- }
- export class HeliosServer {
- public readonly modules: HeliosModule[]
- constructor(
- public readonly rawServer: Server
- ) {
- this.modules = rawServer.modules.map(m => new HeliosModule(m, rawServer.id))
- }
- }
- export class HeliosModule {
- public readonly subModules: HeliosModule[]
- private readonly mavenComponents: Readonly<MavenComponents>
- private readonly required: Readonly<Required<HeliosRequired>>
- private readonly localPath: string
- constructor(
- public readonly rawModule: Module,
- private readonly serverId: string
- ) {
- this.mavenComponents = this.resolveMavenComponents()
- this.required = this.resolveRequired()
- this.localPath = this.resolveLocalPath()
- if(this.rawModule.subModules != null) {
- this.subModules = this.rawModule.subModules.map(m => new HeliosModule(m, serverId))
- } else {
- this.subModules = []
- }
-
- }
- private resolveMavenComponents(): MavenComponents {
- // Files need not have a maven identifier if they provide a path.
- if(this.rawModule.type === Type.File && this.rawModule.artifact.path != null) {
- return null! as MavenComponents
- }
- // Version Manifests never provide a maven identifier.
- if(this.rawModule.type === Type.VersionManifest) {
- return null! as MavenComponents
- }
- const isMavenId = MavenUtil.isMavenIdentifier(this.rawModule.id)
- if(!isMavenId) {
- if(this.rawModule.type !== Type.File) {
- throw new Error(`Module ${this.rawModule.name} (${this.rawModule.id}) of type ${this.rawModule.type} must have a valid maven identifier!`)
- } else {
- throw new Error(`Module ${this.rawModule.name} (${this.rawModule.id}) of type ${this.rawModule.type} must either declare an artifact path or have a valid maven identifier!`)
- }
- }
- try {
- return MavenUtil.getMavenComponents(this.rawModule.id)
- } catch(err) {
- throw new Error(`Failed to resolve maven components for module ${this.rawModule.name} (${this.rawModule.id}) of type ${this.rawModule.type}. Reason: ${err.message}`)
- }
-
- }
- private resolveRequired(): Required<HeliosRequired> {
- if(this.rawModule.required == null) {
- return {
- value: true,
- def: true
- }
- } else {
- return {
- value: this.rawModule.required.value ?? true,
- def: this.rawModule.required.def ?? true
- }
- }
- }
- private resolveLocalPath(): string {
- // Version Manifests have a pre-determined path.
- if(this.rawModule.type === Type.VersionManifest) {
- return join('TODO_COMMON_DIR', 'versions', this.rawModule.id, `${this.rawModule.id}.json`)
- }
- const relativePath = this.rawModule.artifact.path ?? MavenUtil.mavenComponentsAsNormalizedPath(
- this.mavenComponents.group,
- this.mavenComponents.artifact,
- this.mavenComponents.version,
- this.mavenComponents.classifier,
- this.mavenComponents.extension
- )
- switch (this.rawModule.type) {
- case Type.Library:
- case Type.Forge:
- case Type.ForgeHosted:
- case Type.LiteLoader:
- return join('TODO_COMMON_DIR', 'libraries', relativePath)
- case Type.ForgeMod:
- case Type.LiteMod:
- return join('TODO_COMMON_DIR', 'modstore', relativePath)
- case Type.File:
- default:
- return join('TODO_INSTANCE_DIR', this.serverId, relativePath)
- }
-
- }
- public hasMavenComponents(): boolean {
- return this.mavenComponents != null
- }
- public getMavenComponents(): Readonly<MavenComponents> {
- return this.mavenComponents
- }
- public getRequired(): Readonly<Required<HeliosRequired>> {
- return this.required
- }
- public getPath(): string {
- return this.localPath
- }
- public getMavenIdentifier(): string {
- return MavenUtil.mavenComponentsToIdentifier(
- this.mavenComponents.group,
- this.mavenComponents.artifact,
- this.mavenComponents.version,
- this.mavenComponents.classifier,
- this.mavenComponents.extension
- )
- }
- public getExtensionlessMavenIdentifier(): string {
- return MavenUtil.mavenComponentsToExtensionlessIdentifier(
- this.mavenComponents.group,
- this.mavenComponents.artifact,
- this.mavenComponents.version,
- this.mavenComponents.classifier
- )
- }
- public getVersionlessMavenIdentifier(): string {
- return MavenUtil.mavenComponentsToVersionlessIdentifier(
- this.mavenComponents.group,
- this.mavenComponents.artifact
- )
- }
- public hasSubModules(): boolean {
- return this.subModules.length > 0
- }
- }
|