diff --git a/src/metadata/fabric/fabric-mod-metadata-reader.ts b/src/metadata/fabric/fabric-mod-metadata-reader.ts index 147847d..318619e 100644 --- a/src/metadata/fabric/fabric-mod-metadata-reader.ts +++ b/src/metadata/fabric/fabric-mod-metadata-reader.ts @@ -1,19 +1,10 @@ -import ModMetadata from "../mod-metadata"; -import ZippedModMetadataReader from "../zipped-mod-metadata-reader"; +import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader"; import FabricModConfig from "./fabric-mod-config"; import FabricModMetadata from "./fabric-mod-metadata"; -class FabricModMetadataReader extends ZippedModMetadataReader { +class FabricModMetadataReader extends ZippedModTextMetadataReader { constructor() { - super(FabricModConfig.FILENAME); - } - - protected loadConfig(buffer: Buffer): FabricModConfig { - return JSON.parse(buffer.toString("utf8")); - } - - protected createMetadataFromConfig(config: FabricModConfig): ModMetadata { - return new FabricModMetadata(config); + super(FabricModConfig.FILENAME, x => new FabricModMetadata(x)); } } diff --git a/src/metadata/forge/forge-mod-metadata-reader.ts b/src/metadata/forge/forge-mod-metadata-reader.ts index cf3f751..3ef6ac7 100644 --- a/src/metadata/forge/forge-mod-metadata-reader.ts +++ b/src/metadata/forge/forge-mod-metadata-reader.ts @@ -1,20 +1,11 @@ -import ModMetadata from "../mod-metadata"; import toml from "toml"; -import ZippedModMetadataReader from "../zipped-mod-metadata-reader"; +import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader"; import ForgeModMetadata from "./forge-mod-metadata"; import ForgeModConfig from "./forge-mod-config"; -class ForgeModMetadataReader extends ZippedModMetadataReader { +class ForgeModMetadataReader extends ZippedModTextMetadataReader { constructor() { - super(ForgeModConfig.FILENAME); - } - - protected loadConfig(buffer: Buffer): ForgeModConfig { - return toml.parse(buffer.toString("utf8")); - } - - protected createMetadataFromConfig(config: ForgeModConfig): ModMetadata { - return new ForgeModMetadata(config); + super(ForgeModConfig.FILENAME, x => new ForgeModMetadata(x), toml.parse); } } diff --git a/src/metadata/quilt/quilt-mod-metadata-reader.ts b/src/metadata/quilt/quilt-mod-metadata-reader.ts index b934a56..3dca6de 100644 --- a/src/metadata/quilt/quilt-mod-metadata-reader.ts +++ b/src/metadata/quilt/quilt-mod-metadata-reader.ts @@ -1,19 +1,10 @@ -import ModMetadata from "../mod-metadata"; -import ZippedModMetadataReader from "../zipped-mod-metadata-reader"; +import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader"; import QuiltModConfig from "./quilt-mod-config"; import QuiltModMetadata from "./quilt-mod-metadata"; -class QuiltModMetadataReader extends ZippedModMetadataReader { +class QuiltModMetadataReader extends ZippedModTextMetadataReader { constructor() { - super(QuiltModConfig.FILENAME); - } - - protected loadConfig(buffer: Buffer): QuiltModConfig { - return JSON.parse(buffer.toString("utf8")); - } - - protected createMetadataFromConfig(config: QuiltModConfig): ModMetadata { - return new QuiltModMetadata(config); + super(QuiltModConfig.FILENAME, x => new QuiltModMetadata(x)); } } diff --git a/src/metadata/zipped-mod-metadata-reader.ts b/src/metadata/zipped-mod-metadata-reader.ts index c8a203b..143d618 100644 --- a/src/metadata/zipped-mod-metadata-reader.ts +++ b/src/metadata/zipped-mod-metadata-reader.ts @@ -2,23 +2,19 @@ import ModMetadata from "./mod-metadata"; import ModMetadataReader from "./mod-metadata-reader"; import { StreamZipAsync, async as ZipArchive } from "node-stream-zip"; -export default abstract class ZippedModMetadataReader> implements ModMetadataReader { - private configEntryName: string; +abstract class ZippedModMetadataReader> implements ModMetadataReader { + private _configEntryName: string; protected constructor(configEntryName: string) { - this.configEntryName = configEntryName; + this._configEntryName = configEntryName; } async readMetadata(modPath: string): Promise { - let zip = null; + let zip = null as StreamZipAsync; try { zip = new ZipArchive({ file: modPath }); - const buffer = await zip.entryData(this.configEntryName).catch(_ => null); - if (buffer) { - return this.createMetadataFromConfig(this.loadConfig(buffer)); - } else { - return null; - } + const buffer = await zip.entryData(this._configEntryName).catch(_ => null as Buffer); + return buffer && this.createMetadataFromConfig(this.loadConfig(buffer)); } catch { return null; } finally { @@ -30,3 +26,5 @@ export default abstract class ZippedModMetadataReader = (config: T) => ModMetadata; + +type Parser = (input: string) => T; + +abstract class ZippedModTextMetadataReader> extends ZippedModMetadataReader { + private readonly _factory: ModMetadataFactory; + private readonly _parser: Parser; + + protected constructor(configEntryName: string, factory: ModMetadataFactory, parser?: Parser) { + super(configEntryName); + this._factory = factory; + this._parser = parser ?? JSON.parse; + } + + protected loadConfig(buffer: Buffer): TConfig { + return this._parser(buffer.toString("utf8")); + } + + protected createMetadataFromConfig(config: TConfig): ModMetadata { + return this._factory(config); + } +} + +export default ZippedModTextMetadataReader;