diff --git a/src/instance-import.ts b/src/instance-import.ts index e83dc2e..2dec216 100644 --- a/src/instance-import.ts +++ b/src/instance-import.ts @@ -36,7 +36,7 @@ export interface ImportScan { export function parseIni(text: string): Record { const values: Record = Object.create(null); for (const line of text.replace(/^\uFEFF/, '').split(/\r?\n/)) { - const match = /^\s*([^#;=\[\]][^=]*?)\s*=\s*(.*?)\s*$/.exec(line); + const match = /^\s*([^#;:=\[\]]+?)\s*[=:]\s*(.*?)\s*$/.exec(line); if (match) { let value = match[2]; if (value.startsWith('"') && value.endsWith('"')) { @@ -61,6 +61,9 @@ async function json(file: string): Promise { throw new Error('Invalid instance metadata.'); return value; } +function forgeLoaderVersion(version: string, minecraft: string): string { + return version.replace(new RegExp(`^${minecraft.replaceAll('.', '\\.')}[-]`), ''); +} export function loaderInfo( libraries: Json[], minecraft: string, @@ -70,6 +73,8 @@ export function loaderInfo( ['net.fabricmc:fabric-loader:', 'fabric'], ['net.neoforged:neoforge:', 'neoforge'], ['net.minecraftforge:forge:', 'forge'], + ['net.minecraftforge:fmlloader:', 'forge'], + ['net.minecraftforge:fmlearlydisplay:', 'forge'], ] as const) { const found = [...libraries] .reverse() @@ -78,15 +83,69 @@ export function loaderInfo( const version = found.name.slice(group.length).split(':')[0]; return { loader, - loaderVersion: - loader === 'forge' - ? version.replace(new RegExp(`^${minecraft.replaceAll('.', '\\.')}[-]`), '') - : version, + loaderVersion: loader === 'forge' ? forgeLoaderVersion(version, minecraft) : version, }; } } return { loader: 'vanilla' }; } +const patchLoaders: Record = { + fabric: 'fabric', + quilt: 'quilt', + forge: 'forge', + neoforge: 'neoforge', +}; +function patchLoaderInfo( + chain: Json[], + minecraft: string, +): { loader: Loader; loaderVersion?: string } | undefined { + for (const profile of chain) { + if (!Array.isArray(profile.patches)) continue; + for (const patch of profile.patches) { + const loader = typeof patch?.id === 'string' ? patchLoaders[patch.id] : undefined; + if (!loader) continue; + return { + loader, + loaderVersion: + typeof patch.version === 'string' + ? loader === 'forge' + ? forgeLoaderVersion(patch.version, minecraft) + : patch.version + : undefined, + }; + } + } +} +function pclLoaderInfo( + pcl: Record, +): { loader: Loader; loaderVersion?: string } | undefined { + for (const [key, loader] of [ + ['VersionNeoForge', 'neoforge'], + ['VersionForge', 'forge'], + ['VersionFabric', 'fabric'], + ['VersionQuilt', 'quilt'], + ] as const) { + if (pcl[key] && !/^(false|0)$/i.test(pcl[key])) return { loader, loaderVersion: pcl[key] }; + } +} +function fmlVersion(profiles: Json[], argument: string): string | undefined { + for (const profile of profiles) { + const args = profile.arguments?.game; + if (!Array.isArray(args)) continue; + const index = args.indexOf(argument); + if (index >= 0 && typeof args[index + 1] === 'string') return args[index + 1]; + } +} +function argumentLoaderInfo(chain: Json[]): { loader: Loader; loaderVersion?: string } | undefined { + const profiles = [ + ...chain, + ...chain.flatMap((profile) => (Array.isArray(profile.patches) ? profile.patches : [])), + ]; + const neoforge = fmlVersion(profiles, '--fml.neoForgeVersion'); + if (neoforge) return { loader: 'neoforge', loaderVersion: neoforge }; + const forge = fmlVersion(profiles, '--fml.forgeVersion'); + if (forge) return { loader: 'forge', loaderVersion: forge }; +} function gameVersion(chain: Json[]): string { for (const profile of chain) { for (const field of ['clientVersion', '_minecraftVersion', 'minecraftVersion']) @@ -131,10 +190,24 @@ async function standard(root: string, id: string, override?: Json): Promise normalizeProfile(p).libraries || []); - const info = loaderInfo(libraries, minecraft); const settings = await json(path.join(folder, 'hmclversion.cfg')); const pcl = parseIni(await text(path.join(folder, 'PCL', 'Setup.ini'))); + const libraries = [...chain].reverse().flatMap((p) => normalizeProfile(p).libraries || []); + const detected = [ + patchLoaderInfo(chain, minecraft), + argumentLoaderInfo(chain), + loaderInfo(libraries, minecraft), + pclLoaderInfo(pcl), + ]; + const info: { loader: Loader; loaderVersion?: string } = detected.find( + (item) => item && item.loader !== 'vanilla', + ) ?? { loader: 'vanilla' }; + if (!info.loaderVersion) { + const version = detected.find( + (item) => item?.loader === info.loader && item.loaderVersion, + )?.loaderVersion; + if (version) info.loaderVersion = version; + } const isolated = await Promise.all( ['mods', 'saves', 'options.txt', 'config'].map((p) => exists(path.join(folder, p))), ); diff --git a/src/version-profile.ts b/src/version-profile.ts index 7cf6918..5bd4c45 100644 --- a/src/version-profile.ts +++ b/src/version-profile.ts @@ -40,13 +40,9 @@ export function normalizeProfile(profile: Profile): Profile { let result: Profile = profile.root === true && !profile.inheritsFrom ? {} : { ...profile }; delete result.patches; for (const patch of patches) { - if ( - !patch || - typeof patch !== 'object' || - Array.isArray(patch) || - patch.patches?.length || - patch.inheritsFrom - ) + // PCL loader patches may name their base game in inheritsFrom; their contents are already + // merged into the owning profile and the version chain, so only real nesting is rejected. + if (!patch || typeof patch !== 'object' || Array.isArray(patch) || patch.patches?.length) throw new Error('Unsupported nested instance patch.'); const arguments_ = { game: [...(result.arguments?.game || []), ...(patch.arguments?.game || [])], diff --git a/test/instance-import.test.ts b/test/instance-import.test.ts index 657e0c9..2434041 100644 --- a/test/instance-import.test.ts +++ b/test/instance-import.test.ts @@ -203,6 +203,65 @@ test('HMCL patch metadata accepts flat vanilla profiles and preserves library an } }); +test('PCL Forge and NeoForge versions report loader versions from patches, arguments, libraries and Setup.ini', async () => { + const root = await temp(); + try { + const versions = path.join(root, 'versions'); + await writeJson(path.join(versions, 'PCL Neo', 'PCL Neo.json'), { + ...profile('PCL Neo', { root: true }), + patches: [ + { ...profile('game'), version: '1.21.1', priority: 0 }, + { id: 'neoforge', version: '21.1.233', priority: 1000, inheritsFrom: '1.21.1' }, + ], + }); + await writeJson(path.join(versions, 'PCL Forge', 'PCL Forge.json'), { + ...profile('PCL Forge', { root: true }), + patches: [ + { ...profile('game'), version: '1.20.1', priority: 0 }, + { id: 'forge', version: '1.20.1-47.4.0', priority: 1000, inheritsFrom: '1.20.1' }, + ], + }); + await writeJson( + path.join(versions, 'Arguments Neo', 'Arguments Neo.json'), + profile('Arguments Neo', { + clientVersion: '1.21.1', + arguments: { game: ['--fml.neoForgeVersion', '21.1.242'], jvm: [] }, + libraries: [{ name: 'net.neoforged.fancymodloader:loader:4.0.43' }], + }), + ); + await writeJson( + path.join(versions, 'Setup Neo', 'Setup Neo.json'), + profile('Setup Neo', { + clientVersion: '1.21.1', + libraries: [{ name: 'net.neoforged.fancymodloader:loader:4.0.42' }], + }), + ); + await put(root, 'versions/Setup Neo/PCL/Setup.ini', 'VersionNeoForge:21.1.233\n'); + await writeJson( + path.join(versions, 'Legacy Forge', 'Legacy Forge.json'), + profile('Legacy Forge', { + clientVersion: '1.20.1', + libraries: [{ name: 'net.minecraftforge:fmlloader:1.20.1-47.4.6' }], + }), + ); + const found = await scanInstances(root); + const candidate = (name: string) => found.candidates.find((item) => item.name === name)!; + assert.equal(found.candidates.length, 5); + assert.equal(candidate('PCL Neo').loader, 'neoforge'); + assert.equal(candidate('PCL Neo').loaderVersion, '21.1.233'); + assert.equal(candidate('PCL Forge').loader, 'forge'); + assert.equal(candidate('PCL Forge').loaderVersion, '47.4.0'); + assert.equal(candidate('Arguments Neo').loader, 'neoforge'); + assert.equal(candidate('Arguments Neo').loaderVersion, '21.1.242'); + assert.equal(candidate('Setup Neo').loader, 'neoforge'); + assert.equal(candidate('Setup Neo').loaderVersion, '21.1.233'); + assert.equal(candidate('Legacy Forge').loader, 'forge'); + assert.equal(candidate('Legacy Forge').loaderVersion, '47.4.6'); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } +}); + test('new instances use portable Prism/MultiMC metadata, readable unique names, and system game roots', async () => { const root = await temp(); try {