Skip to content

Commit

Permalink
Merge pull request #1454 from ebkr/separate-install-mod-from-component
Browse files Browse the repository at this point in the history
Separate installModAfterDownload function from UI component
  • Loading branch information
anttimaki authored Oct 1, 2024
2 parents b1352c5 + 7bc0283 commit 680aa41
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/components/profiles-modals/ImportProfileModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import ExportMod from "../../model/exports/ExportMod";
import ManifestV2 from "../../model/ManifestV2";
import Profile from "../../model/Profile";
import ThunderstoreCombo from "../../model/ThunderstoreCombo";
import ThunderstoreMod from "../../model/ThunderstoreMod";
import ThunderstoreVersion from "../../model/ThunderstoreVersion";
import FsProvider from "../../providers/generic/file/FsProvider";
import ZipProvider from "../../providers/generic/zip/ZipProvider";
import ThunderstoreDownloaderProvider from "../../providers/ror2/downloading/ThunderstoreDownloaderProvider";
Expand Down Expand Up @@ -158,21 +156,6 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
this.activeStep = 'ADDING_PROFILE';
}
async installModAfterDownload(mod: ThunderstoreMod, version: ThunderstoreVersion): Promise<R2Error | ManifestV2> {
const manifestMod: ManifestV2 = new ManifestV2().fromThunderstoreMod(mod, version);
const installError: R2Error | null = await ProfileInstallerProvider.instance.installMod(manifestMod, this.activeProfile);
if (!(installError instanceof R2Error)) {
const newModList: ManifestV2[] | R2Error = await ProfileModList.addMod(manifestMod, this.activeProfile);
if (newModList instanceof R2Error) {
return newModList;
}
return manifestMod;
} else {
// (mod failed to be placed in /{profile} directory)
return installError;
}
}
async readProfileFile(file: string) {
let read = '';
if (file.endsWith('.r2x')) {
Expand Down Expand Up @@ -267,16 +250,20 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
if (!keepIterating) {
return;
}
const installResult: R2Error | ManifestV2 = await this.installModAfterDownload(comboMod.getMod(), comboMod.getVersion());
if (installResult instanceof R2Error) {
this.$store.commit('error/handleError', installResult);
let installedMod: ManifestV2;
try {
installedMod = await ProfileUtils.installModAfterDownload(comboMod.getMod(), comboMod.getVersion(), this.activeProfile);
} catch (e) {
this.$store.commit('error/handleError', e);
keepIterating = false;
this.isProfileBeingImported = false;
return;
}
for (const imported of modList) {
if (imported.getName() == comboMod.getMod().getFullName() && !imported.isEnabled()) {
await ProfileModList.updateMod(installResult, this.activeProfile, async (modToDisable: ManifestV2) => {
await ProfileModList.updateMod(installedMod, this.activeProfile, 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, this.activeProfile);
Expand Down
21 changes: 21 additions & 0 deletions src/utils/ProfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import path from "path";

import * as yaml from "yaml";

import R2Error from "../model/errors/R2Error";
import ExportFormat from "../model/exports/ExportFormat";
import ExportMod from "../model/exports/ExportMod";
import ManifestV2 from "../model/ManifestV2";
import Profile from "../model/Profile";
import ThunderstoreMod from "../model/ThunderstoreMod";
import ThunderstoreVersion from "../model/ThunderstoreVersion";
import VersionNumber from "../model/VersionNumber";
import ZipProvider from "../providers/generic/zip/ZipProvider";
import ProfileInstallerProvider from "../providers/ror2/installing/ProfileInstallerProvider";
import ProfileModList from "../r2mm/mods/ProfileModList";

export async function extractZippedProfileFile(file: string, profileName: string) {
const entries = await ZipProvider.instance.getEntries(file);
Expand Down Expand Up @@ -34,6 +40,21 @@ export async function extractZippedProfileFile(file: string, profileName: string
}
}

export async function installModAfterDownload(mod: ThunderstoreMod, version: ThunderstoreVersion, profile: Profile): Promise<ManifestV2> {
const manifestMod: ManifestV2 = new ManifestV2().fromThunderstoreMod(mod, version);
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;
}

return manifestMod;
}

export async function parseYamlToExportFormat(yamlContent: string) {
const parsedYaml = await yaml.parse(yamlContent);
return new ExportFormat(
Expand Down

0 comments on commit 680aa41

Please sign in to comment.