Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve profile import performance #1494

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/utils/ProfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import FsProvider from "../providers/generic/file/FsProvider";
import ZipProvider from "../providers/generic/zip/ZipProvider";
import ProfileInstallerProvider from "../providers/ror2/installing/ProfileInstallerProvider";
import * as PackageDb from '../r2mm/manager/PackageDexieStore';
import ProfileModList from "../r2mm/mods/ProfileModList";

export async function exportModsToCombos(exportMods: ExportMod[], community: string): Promise<ThunderstoreCombo[]> {
const tsMods = await PackageDb.getPackagesByNames(community, exportMods.map((m) => m.getName()));
Expand Down Expand Up @@ -44,7 +43,7 @@ export async function exportModsToCombos(exportMods: ExportMod[], community: str
return combos;
}

async function extractImportedProfileConfigs(
async function extractConfigsToImportedProfile(
file: string,
profileName: string,
progressCallback: (status: string) => void
Expand Down Expand Up @@ -73,12 +72,16 @@ async function extractImportedProfileConfigs(
)
}

const progress = Math.floor((index/zipEntries.length) * 100);
const progress = Math.floor(((index + 1) / zipEntries.length) * 100);
progressCallback(`Copying configs to profile: ${progress}%`);
}
}

async function installModsToProfile(
/**
* Install mods to target profile without syncing changes to mods.yml file.
* Syncing is futile, as the mods.yml is copied from the imported profile.
*/
async function installModsToImportedProfile(
comboList: ThunderstoreCombo[],
modList: ExportMod[],
profile: ImmutableProfile,
Expand All @@ -87,28 +90,18 @@ async function installModsToProfile(
const disabledMods = modList.filter((m) => !m.isEnabled()).map((m) => m.getName());

for (const [index, comboMod] of comboList.entries()) {
const manifestMod: ManifestV2 = new ManifestV2().fromThunderstoreMod(comboMod.getMod(), comboMod.getVersion());

const installError: R2Error | null = await ProfileInstallerProvider.instance.installMod(manifestMod, profile);
if (installError instanceof R2Error) {
throw installError;
}

const newModList: ManifestV2[] | R2Error = await ProfileModList.addMod(manifestMod, profile);
if (newModList instanceof R2Error) {
throw newModList;
}
const manifestMod = new ManifestV2().fromThunderstoreMod(comboMod.getMod(), comboMod.getVersion());
throwForR2Error(
await ProfileInstallerProvider.instance.installMod(manifestMod, profile)
);

if (disabledMods.includes(manifestMod.getName())) {
await ProfileModList.updateMod(manifestMod, profile, async (modToDisable: ManifestV2) => {
// Need to enable temporarily so the manager doesn't think it's re-disabling a disabled mod.
modToDisable.enable();
await ProfileInstallerProvider.instance.disableMod(modToDisable, profile);
modToDisable.disable();
});
throwForR2Error(
await ProfileInstallerProvider.instance.disableMod(manifestMod, profile)
);
}

const progress = Math.floor((index/comboList.length) * 100);
const progress = Math.floor(((index + 1) / comboList.length) * 100);
progressCallback(`Copying mods to profile: ${progress}%`);
}
}
Expand Down Expand Up @@ -153,8 +146,8 @@ export async function populateImportedProfile(
await FileUtils.recursiveRemoveDirectoryIfExists(profile.getProfilePath());
}

await installModsToProfile(comboList, exportModList, profile, progressCallback);
await extractImportedProfileConfigs(zipPath, profile.getProfileName(), progressCallback);
await installModsToImportedProfile(comboList, exportModList, profile, progressCallback);
await extractConfigsToImportedProfile(zipPath, profile.getProfileName(), progressCallback);

if (isUpdate) {
progressCallback('Applying changes to updated profile...');
Expand All @@ -178,3 +171,9 @@ export async function readProfileFile(file: string) {
}
return read;
}

function throwForR2Error(maybeError: unknown) {
if (maybeError instanceof R2Error) {
throw maybeError;
}
}
Loading