Merge used profile mods onto a single record for each mod by name, choosing newest

This commit is contained in:
Dev 2023-10-10 17:01:29 +01:00
parent 76d8b944d5
commit f67c4293d8
2 changed files with 41 additions and 1 deletions

View File

@ -185,6 +185,11 @@ export class LauncherController
{
const profile = this.profileHelper.getFullProfile(sessionId);
return profile?.aki?.mods ?? [];
if (profile?.aki?.mods)
{
return this.preAkiModLoader.getProfileModsGroupedByModName(profile?.aki?.mods);
}
return [];
}
}

View File

@ -3,6 +3,7 @@ import os from "os";
import path from "path";
import semver from "semver";
import { DependencyContainer, inject, injectable } from "tsyringe";
import { ModDetails } from "../models/eft/profile/IAkiProfile";
import { ConfigTypes } from "../models/enums/ConfigTypes";
import { IPreAkiLoadMod } from "../models/external/IPreAkiLoadMod";
import { IPreAkiLoadModAsync } from "../models/external/IPreAkiLoadModAsync";
@ -72,6 +73,40 @@ export class PreAkiModLoader implements IModLoader
return this.imported;
}
public getProfileModsGroupedByModName(profileMods: ModDetails[]): ModDetails[]
{
// Group all mods used by profile by name
const modsGroupedByName: Record<string, ModDetails[]> = {};
for (const mod of profileMods)
{
if (!modsGroupedByName[mod.name])
{
modsGroupedByName[mod.name] = [];
}
modsGroupedByName[mod.name].push(mod);
}
// Find the highest versioned mod and add to results array
const result = [];
for (const modName in modsGroupedByName)
{
const modDatas = modsGroupedByName[modName];
const modVersions = modDatas.map(x => x.version);
const highestVersion = semver.maxSatisfying(modVersions, "*");
const chosenVersion = modDatas.find(x => x.name === modName && x.version === highestVersion);
if (!chosenVersion)
{
continue;
}
result.push(chosenVersion);
}
return result;
}
public getModPath(mod: string): string
{
return `${this.basepath}${mod}/`;