-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathConcerningPackageComposable.ts
More file actions
93 lines (76 loc) · 3 KB
/
Copy pathConcerningPackageComposable.ts
File metadata and controls
93 lines (76 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Definitions are outside the composable function so that calculations are shared.
* We do not need to recalculate on a per-usage basis.
*/
import { getStore } from '@r2/providers/generic/store/StoreProvider';
import { computed, onMounted, ref, watch } from 'vue';
import ManifestV2 from '@r2/model/ManifestV2';
import ThunderstoreMod from '@r2/model/ThunderstoreMod';
import VersionNumber from '@r2/model/VersionNumber';
import * as PackageDb from "@r2/r2mm/manager/PackageDexieStore";
type ConcerningPackage = {
fullName: string;
latestVersion: string;
}
const store = getStore<any>();
const activeGame = computed(() => store.state.activeGame);
const localModList = computed<ManifestV2[]>(() => store.state.profile.modList);
const onlineModList = computed<Map<string, ConcerningPackage>>(() => {
const mods: ThunderstoreMod[] = store.state.tsMods.mods;
return new Map<string, ConcerningPackage>(mods.map(value => [value.getFullName(), {
fullName: value.getFullName(),
latestVersion: value.getLatestVersion(),
}]));
});
const allConcerningPackages = ref<ManifestV2[]>([]);
const activeConcerningPackages = ref<ManifestV2[]>([]);
async function updateConcerningPackages() {
const game = activeGame.value;
const localMods = localModList.value;
const concerningPackages: ManifestV2[] = [];
const modsToCheck: ManifestV2[] = [];
for (const mod of localMods) {
if (!mod.isOnlineSource()) {
continue;
}
if (!onlineModList.value.has(mod.getName())) {
concerningPackages.push(mod);
} else {
modsToCheck.push(mod);
}
}
const versionNumbersBatch = await PackageDb.getPackageVersionNumbersBatch(
game.internalFolderName,
modsToCheck.map(mod => mod.getName())
);
for (const mod of modsToCheck) {
const versions = versionNumbersBatch.get(mod.getName());
const dnf = !versions || !versions.includes(mod.getVersionNumber().toString());
if (dnf) {
concerningPackages.push(mod);
}
}
allConcerningPackages.value = concerningPackages;
activeConcerningPackages.value = concerningPackages.filter(value => !value.isTrustedPackage());
}
watch([activeGame, localModList], async () => {
await updateConcerningPackages();
});
export function useConcerningPackageComposable() {
onMounted(async () => {
await updateConcerningPackages();
});
const hasConcerningPackages = computed<boolean>(() => activeConcerningPackages.value.length > 0);
function isConcerningPackage(mod: ManifestV2) {
return activeConcerningPackages.value.findIndex(value => value.getName() === mod.getName()) >= 0;
}
function wasConcerningPackage(mod: ManifestV2) {
return allConcerningPackages.value.findIndex(value => value.getName() === mod.getName()) >= 0;
}
return {
concerningPackages: activeConcerningPackages,
hasConcerningPackages,
isConcerningPackage,
wasConcerningPackage,
}
}