Skip to content

Commit

Permalink
Replace ThunderstorePackages.PACKAGES and ModBridge with Vuex methods
Browse files Browse the repository at this point in the history
Implement cached "ManifestV2 to ThunderstoreMod" conversion in
TsModsModule in a way that's compatible with what ModBridgre did
previously. This allows us to drop ModBridge, and the duplicate
in-memory list of PACKAGES.
  • Loading branch information
anttimaki committed Mar 13, 2024
1 parent 2baf6b1 commit 85ea2aa
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 155 deletions.
17 changes: 6 additions & 11 deletions src/components/views/DownloadModModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ import StatusEnum from '../../model/enums/StatusEnum';
import ThunderstoreCombo from '../../model/ThunderstoreCombo';
import ProfileInstallerProvider from '../../providers/ror2/installing/ProfileInstallerProvider';
import ProfileModList from '../../r2mm/mods/ProfileModList';
import ModBridge from '../../r2mm/mods/ModBridge';
import Profile from '../../model/Profile';
import { Progress } from '../all';
import Game from '../../model/game/Game';
Expand Down Expand Up @@ -242,27 +241,23 @@ let assignId = 0;
this.downloadHandler(refSelectedThunderstoreMod, version);
}
// TODO: rethink how this method and provider's downloadLatestOfAll()
// access the active game, local mod list and TS mod list.
async downloadLatest() {
this.closeModal();
const localMods = await ProfileModList.getModList(this.contextProfile!);
if (localMods instanceof R2Error) {
this.downloadingMod = false;
this.$store.commit('error/handleError', localMods);
return;
}
const outdatedMods = localMods.filter(mod => !ModBridge.isCachedLatestVersion(mod));
const modsWithUpdates: ThunderstoreCombo[] = this.$store.getters['profile/modsWithUpdates'];
const currentAssignId = assignId++;
const progressObject = {
progress: 0,
initialMods: outdatedMods.map(value => `${value.getName()} (${value.getVersionNumber().toString()})`),
initialMods: modsWithUpdates.map(value => `${value.getMod().getName()} (${value.getVersion().toString()})`),
modName: '',
assignId: currentAssignId,
failed: false,
};
this.downloadObject = progressObject;
DownloadModModal.allVersions.push([currentAssignId, this.downloadObject]);
this.downloadingMod = true;
ThunderstoreDownloaderProvider.instance.downloadLatestOfAll(this.activeGame, outdatedMods, this.thunderstorePackages, (progress: number, modName: string, status: number, err: R2Error | null) => {
ThunderstoreDownloaderProvider.instance.downloadLatestOfAll(this.activeGame, modsWithUpdates, this.thunderstorePackages, (progress: number, modName: string, status: number, err: R2Error | null) => {
const assignIndex = DownloadModModal.allVersions.findIndex(([number, val]) => number === currentAssignId);
if (status === StatusEnum.FAILURE) {
if (err !== null) {
Expand All @@ -277,7 +272,7 @@ let assignId = 0;
const obj = {
progress: progress,
modName: modName,
initialMods: outdatedMods.map(value => `${value.getName()} (${value.getVersionNumber().toString()})`),
initialMods: modsWithUpdates.map(value => `${value.getMod().getName()} (${value.getVersion().getVersionNumber().toString()})`),
assignId: currentAssignId,
failed: false,
}
Expand Down
3 changes: 1 addition & 2 deletions src/components/views/LocalModList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import ManifestV2 from '../../model/ManifestV2';
import ProfileModList from '../../r2mm/mods/ProfileModList';
import R2Error from '../../model/errors/R2Error';
import ManagerSettings from '../../r2mm/manager/ManagerSettings';
import ModBridge from '../../r2mm/mods/ModBridge';
import DependencyListDisplayType from '../../model/enums/DependencyListDisplayType';
import Dependants from '../../r2mm/mods/Dependants';
import ProfileInstallerProvider from '../../providers/ror2/installing/ProfileInstallerProvider';
Expand Down Expand Up @@ -255,7 +254,7 @@ import SearchAndSort from './LocalModList/SearchAndSort.vue';
updateMod(mod: ManifestV2) {
this.selectedManifestMod = mod;
const tsMod = ModBridge.getCachedThunderstoreModFromMod(mod);
const tsMod = this.$store.getters.tsMods.tsMod(mod);
if (tsMod instanceof ThunderstoreMod) {
this.$store.commit("openDownloadModModal", tsMod);
Expand Down
8 changes: 4 additions & 4 deletions src/components/views/LocalModList/LocalModCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ExpandableCard, Link } from '../../all';
import DonateButton from '../../buttons/DonateButton.vue';
import R2Error from '../../../model/errors/R2Error';
import ManifestV2 from '../../../model/ManifestV2';
import ThunderstoreMod from '../../../model/ThunderstoreMod';
import { LogSeverity } from '../../../providers/ror2/logging/LoggerProvider';
import Dependants from '../../../r2mm/mods/Dependants';
import ModBridge from '../../../r2mm/mods/ModBridge';
@Component({
components: {
Expand Down Expand Up @@ -41,15 +41,15 @@ export default class LocalModCard extends Vue {
}
get isLatestVersion() {
return ModBridge.isCachedLatestVersion(this.mod);
return this.$store.getters.tsMods.isLatestVersion(this.mod);
}
get localModList(): ManifestV2[] {
return this.$store.state.profile.modList;
}
get tsMod() {
return ModBridge.getCachedThunderstoreModFromMod(this.mod);
get tsMod(): ThunderstoreMod | undefined {
return this.$store.getters.tsMods.tsMod(this.mod);
}
@Watch("localModList")
Expand Down
8 changes: 8 additions & 0 deletions src/model/ThunderstoreMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export default class ThunderstoreMod extends ThunderstoreVersion implements Reac
this.versions = versions;
}

public getLatestVersion(): ThunderstoreVersion {
return this.getVersions().reduce(reduceToNewestVersion);
}

public getRating(): number {
return this.rating;
}
Expand Down Expand Up @@ -172,3 +176,7 @@ export default class ThunderstoreMod extends ThunderstoreVersion implements Reac
this.donationLink = url;
}
}

function reduceToNewestVersion(v1: ThunderstoreVersion, v2: ThunderstoreVersion) {
return v1.getVersionNumber().isNewerThan(v2.getVersionNumber()) ? v1 : v2;
};
13 changes: 3 additions & 10 deletions src/providers/ror2/downloading/ThunderstoreDownloaderProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,16 @@ export default abstract class ThunderstoreDownloaderProvider {
*/
public abstract buildDependencySetUsingLatest(mod: ThunderstoreVersion, allMods: ThunderstoreMod[], builder: ThunderstoreCombo[]): ThunderstoreCombo[];

/**
* Allows a list of mods passed in to be converted to the latest version of their equivalent upload.
*
* @param mods
* @param allMods
*/
public abstract getLatestOfAllToUpdate(mods: ManifestV2[], allMods: ThunderstoreMod[]): ThunderstoreCombo[];

/**
* A top-level method to download the latest version of all mods passed in, including their dependencies.
*
* @param mods An array of ManifestV2 objects to be updated.
* @param game Currently selected game
* @param modsWithUpdate An array of ThunderstoreCombo objects to be updated.
* @param allMods An array of all mods available from the Thunderstore API.
* @param callback Callback to show the current state of the downloads.
* @param completedCallback Callback to perform final actions against. Only called if {@param callback} has not returned a failed status.
*/
public abstract downloadLatestOfAll(game: Game, mods: ManifestV2[], allMods: ThunderstoreMod[],
public abstract downloadLatestOfAll(game: Game, modsWithUpdate: ThunderstoreCombo[], allMods: ThunderstoreMod[],
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void,
completedCallback: (modList: ThunderstoreCombo[]) => void): void;

Expand Down
19 changes: 4 additions & 15 deletions src/r2mm/data/ThunderstorePackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import ThunderstoreMod from '../../model/ThunderstoreMod';
import Game from '../../model/game/Game';
import ApiResponse from '../../model/api/ApiResponse';
import ConnectionProvider from '../../providers/generic/connection/ConnectionProvider';
import ModBridge from '../mods/ModBridge';
import * as PackageDb from '../manager/PackageDexieStore';

export default class ThunderstorePackages {

public static PACKAGES: ThunderstoreMod[] = [];
public static PACKAGES_MAP: Map<String, ThunderstoreMod> = new Map();
// TODO: would IndexedDB or Vuex be more suitable place for exclusions?
public static EXCLUSIONS: string[] = [];
Expand All @@ -33,25 +31,16 @@ export default class ThunderstorePackages {

// TODO: see if this can be hooked into the progress bar in Splash screen.
await PackageDb.updateFromApiResponse(gameName, packages);
}

// The stuff below will become obsolete once the mod list is
// accessed from Vuex, but it's needed for now to not break
// existing code.
ThunderstorePackages.PACKAGES = response.data
.map(ThunderstoreMod.parseFromThunderstoreData)
.filter((mod) => !ThunderstorePackages.EXCLUSIONS.includes(mod.getFullName()));

ModBridge.clearCache();

ThunderstorePackages.PACKAGES_MAP = ThunderstorePackages.PACKAGES.reduce((map, pkg) => {
public static getDeprecatedPackageMap(packages: ThunderstoreMod[]): Map<string, boolean> {
ThunderstorePackages.PACKAGES_MAP = packages.reduce((map, pkg) => {
map.set(pkg.getFullName(), pkg);
return map;
}, new Map<String, ThunderstoreMod>());
}

public static getDeprecatedPackageMap(): Map<string, boolean> {
const result = new Map<string, boolean>();
this.PACKAGES.forEach(pkg => {
packages.forEach(pkg => {
this.populateDeprecatedPackageMapForModChain(pkg, result);
});
return result;
Expand Down
26 changes: 5 additions & 21 deletions src/r2mm/downloading/BetterThunderstoreDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import * as path from 'path';
import FsProvider from '../../providers/generic/file/FsProvider';
import FileWriteError from '../../model/errors/FileWriteError';
import Profile from '../../model/Profile';
import ThunderstorePackages from '../data/ThunderstorePackages';
import ExportMod from '../../model/exports/ExportMod';
import ManagerSettings from '../manager/ManagerSettings';
import ManifestV2 from '../../model/ManifestV2';
import ModBridge from '../mods/ModBridge';
import * as PackageDb from '../manager/PackageDexieStore';
import ThunderstoreDownloaderProvider from '../../providers/ror2/downloading/ThunderstoreDownloaderProvider';
import ManagerInformation from '../../_managerinf/ManagerInformation';
import Game from '../../model/game/Game';
Expand Down Expand Up @@ -92,26 +90,12 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
})
}

public getLatestOfAllToUpdate(mods: ManifestV2[], allMods: ThunderstoreMod[]): ThunderstoreCombo[] {
return mods.filter(mod => !ModBridge.isCachedLatestVersion(mod))
.map(mod => ModBridge.getCachedThunderstoreModFromMod(mod))
.filter(value => value != undefined)
.map(mod => {
const latestVersion = mod!.getVersions().sort((a, b) => a.getVersionNumber().compareToDescending(b.getVersionNumber()))[0];
const combo = new ThunderstoreCombo();
combo.setMod(mod!);
combo.setVersion(latestVersion);
return combo;
})
}

public async downloadLatestOfAll(game: Game, mods: ManifestV2[], allMods: ThunderstoreMod[],
public async downloadLatestOfAll(game: Game, modsWithUpdates: ThunderstoreCombo[], allMods: ThunderstoreMod[],
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void,
completedCallback: (modList: ThunderstoreCombo[]) => void) {

const dependenciesToUpdate: ThunderstoreCombo[] = this.getLatestOfAllToUpdate(mods, allMods);
const dependencies: ThunderstoreCombo[] = [...dependenciesToUpdate];
dependenciesToUpdate.forEach(value => {
const dependencies: ThunderstoreCombo[] = [...modsWithUpdates];
modsWithUpdates.forEach(value => {
this.buildDependencySetUsingLatest(value.getVersion(), allMods, dependencies);
});

Expand Down Expand Up @@ -203,7 +187,7 @@ export default class BetterThunderstoreDownloader extends ThunderstoreDownloader
public async downloadImportedMods(game: Game, modList: ExportMod[],
callback: (progress: number, modName: string, status: number, err: R2Error | null) => void,
completedCallback: (mods: ThunderstoreCombo[]) => void) {
const tsMods: ThunderstoreMod[] = ThunderstorePackages.PACKAGES;
const tsMods = await PackageDb.getPackagesAsThunderstoreMods(game.internalFolderName);
const comboList: ThunderstoreCombo[] = [];
for (const importMod of modList) {
for (const mod of tsMods) {
Expand Down
82 changes: 0 additions & 82 deletions src/r2mm/mods/ModBridge.ts

This file was deleted.

Loading

0 comments on commit 85ea2aa

Please sign in to comment.