Add support for optional vs required modules.

Directories representing toggleable modules are now segemented into three subdirectories.

* `required` Modules that are required.
* `optionalon` Modules that are optional and enabled by default.
* `optionaloff` Modules that are optional and disabled by default.

Filter your files into these three directories. Files at the root directory will no longer be processed. Currently, forgemods and litemods are the only toggleable modules.

For existing servers, just manually create these directories and move your files inside either of them. Generating a new server will automatically create them.
This commit is contained in:
Daniel Scalzi
2020-07-05 13:51:49 -04:00
parent bac2480c1b
commit 8e611175ca
8 changed files with 253 additions and 74 deletions

View File

@@ -3,10 +3,10 @@ import { Type } from 'helios-distribution-types'
import { join } from 'path'
import { resolve } from 'url'
import { VersionSegmented } from '../../../../util/VersionSegmented'
import { ModuleStructure } from './module.struct'
import { MinecraftVersion } from '../../../../util/MinecraftVersion'
import { ToggleableModuleStructure } from './toggleablemodule.struct'
export abstract class BaseForgeModStructure extends ModuleStructure implements VersionSegmented {
export abstract class BaseForgeModStructure extends ToggleableModuleStructure implements VersionSegmented {
protected readonly EXAMPLE_MOD_ID = 'examplemod'
@@ -22,7 +22,7 @@ export abstract class BaseForgeModStructure extends ModuleStructure implements V
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async getModuleUrl(name: string, path: string, stats: Stats): Promise<string> {
return resolve(this.baseUrl, join(this.relativeRoot, name))
return resolve(this.baseUrl, join(this.relativeRoot, this.getActiveNamespace(), name))
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async getModulePath(name: string, path: string, stats: Stats): Promise<string | null> {

View File

@@ -5,9 +5,9 @@ import { join } from 'path'
import { resolve } from 'url'
import { capitalize } from '../../../../util/stringutils'
import { LiteMod } from '../../../liteloader/litemod'
import { ModuleStructure } from './module.struct'
import { ToggleableModuleStructure } from './toggleablemodule.struct'
export class LiteModStructure extends ModuleStructure {
export class LiteModStructure extends ToggleableModuleStructure {
private liteModMetadata: {[property: string]: LiteMod | undefined} = {}
@@ -28,7 +28,7 @@ export class LiteModStructure extends ModuleStructure {
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async getModuleUrl(name: string, path: string, stats: Stats): Promise<string> {
return resolve(this.baseUrl, join(this.relativeRoot, name))
return resolve(this.baseUrl, join(this.relativeRoot, this.getActiveNamespace(), name))
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async getModulePath(name: string, path: string, stats: Stats): Promise<string | null> {

View File

@@ -22,7 +22,7 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
public async getSpecModel(): Promise<Module[]> {
if (this.resolvedModels == null) {
this.resolvedModels = await this._doModuleRetrieval()
this.resolvedModels = await this._doModuleRetrieval(this.containerDirectory)
}
return this.resolvedModels
@@ -58,10 +58,6 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
id: await this.getModuleId(file, filePath),
name: await this.getModuleName(file, filePath),
type: this.type,
required: {
value: false,
def: false
},
artifact: {
size: stats.size,
MD5: createHash('md5').update(buf).digest('hex'),
@@ -75,14 +71,14 @@ export abstract class ModuleStructure extends BaseModelStructure<Module> {
return mdl
}
private async _doModuleRetrieval(): Promise<Module[]> {
protected async _doModuleRetrieval(scanDirectory: string): Promise<Module[]> {
const accumulator: Module[] = []
if (await pathExists(this.containerDirectory)) {
const files = await readdir(this.containerDirectory)
if (await pathExists(scanDirectory)) {
const files = await readdir(scanDirectory)
for (const file of files) {
const filePath = resolve(this.containerDirectory, file)
const filePath = resolve(scanDirectory, file)
const stats = await lstat(filePath)
if (stats.isFile()) {
if(this.filter == null || this.filter(file, filePath, stats)) {

View File

@@ -0,0 +1,68 @@
import { ModuleStructure } from './module.struct'
import { Type, Module } from 'helios-distribution-types'
import { Stats, mkdirs } from 'fs-extra'
import { resolve } from 'path'
export enum ToggleableNamespace {
REQUIRED = 'required',
OPTIONAL_ON = 'optionalon',
OPTIONAL_OFF = 'optionaloff'
}
export abstract class ToggleableModuleStructure extends ModuleStructure {
private activeNamespace: string | undefined
constructor(
absoluteRoot: string,
relativeRoot: string,
structRoot: string,
baseUrl: string,
protected type: Type,
protected filter?: ((name: string, path: string, stats: Stats) => boolean)
) {
super(absoluteRoot, relativeRoot, structRoot, baseUrl, type, filter)
}
public async init(): Promise<void> {
await super.init()
for(const namespace of Object.values(ToggleableNamespace)) {
await mkdirs(resolve(this.containerDirectory, namespace))
}
}
public async getSpecModel(): Promise<Module[]> {
if (this.resolvedModels == null) {
this.resolvedModels = []
for(const value of Object.values(ToggleableNamespace)) {
this.activeNamespace = value
const models = await this._doModuleRetrieval(resolve(this.containerDirectory, value))
models.forEach(this.getNamespaceMapper(value))
this.resolvedModels = this.resolvedModels.concat(models)
}
this.activeNamespace = undefined
}
return this.resolvedModels
}
protected getActiveNamespace(): string {
return this.activeNamespace || ''
}
protected getNamespaceMapper(namespace: ToggleableNamespace): (x: Module) => void {
switch(namespace) {
case ToggleableNamespace.REQUIRED:
return () => { /* do nothing */ }
case ToggleableNamespace.OPTIONAL_ON:
return (x) => x.required = { value: false }
case ToggleableNamespace.OPTIONAL_OFF:
return (x) => x.required = { value: false, def: false }
}
}
}