Skip to content

Commit

Permalink
Introduce ProfilesMixin and clean up some of Profiles.vue's code
Browse files Browse the repository at this point in the history
  • Loading branch information
VilppeRiskidev committed Jun 24, 2024
1 parent 0a2652f commit 6cd313b
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 686 deletions.
141 changes: 141 additions & 0 deletions src/components/mixins/ProfilesMixin.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<script lang='ts'>
import Vue from 'vue';
import Component from 'vue-class-component';
import ZipProvider from "../../providers/generic/zip/ZipProvider";
import path from "path";
import Profile from "../../model/Profile";
import ThunderstoreMod from "../../model/ThunderstoreMod";
import ThunderstoreVersion from "../../model/ThunderstoreVersion";
import R2Error from "../../model/errors/R2Error";
import ManifestV2 from "../../model/ManifestV2";
import ProfileInstallerProvider from "../../providers/ror2/installing/ProfileInstallerProvider";
import ProfileModList from "../../r2mm/mods/ProfileModList";
import sanitize from "sanitize-filename";
import ManagerInformation from "../../_managerinf/ManagerInformation";
import FsProvider from "../../providers/generic/file/FsProvider";
import * as yaml from "yaml";
import ExportFormat from "../../model/exports/ExportFormat";
import ExportMod from "../../model/exports/ExportMod";
import VersionNumber from "../../model/VersionNumber";
@Component
export default class ProfilesMixin extends Vue {
get profileList(): string[] {
return this.$store.state.profiles.profileList;
}
get appName(): string {
return ManagerInformation.APP_NAME;
}
get activeProfileName(): string {
return this.$store.getters['profile/activeProfileName'];
}
get activeProfile(): Profile {
return this.$store.getters['profile/activeProfile'];
}
doesProfileExist(nameToCheck: string): boolean {
if ((nameToCheck.match(new RegExp('^([a-zA-Z0-9])(\\s|[a-zA-Z0-9]|_|-|[.])*$'))) === null) {
return true;
}
const safe: string = this.makeProfileNameSafe(nameToCheck);
return (this.profileList.some(function (profile: string) {
return profile.toLowerCase() === safe.toLowerCase()
}));
}
makeProfileNameSafe(nameToSanitize: string): string {
return sanitize(nameToSanitize);
}
async setSelectedProfile(profileName: string, prewarmCache = true) {
try {
await this.$store.dispatch('profiles/setSelectedProfile', { profileName: profileName, prewarmCache: prewarmCache });
} catch (e) {
const err = R2Error.fromThrownValue(e, 'Error while selecting profile');
this.$store.commit('error/handleError', err);
}
}
async updateProfileList() {
try {
await this.$store.dispatch('profiles/updateProfileList');
} catch (e) {
const err = R2Error.fromThrownValue(e, 'Error whilst updating ProfileList');
this.$store.commit('error/handleError', err);
}
}
async extractZippedProfileFile(file: string, profileName: string) {
const entries = await ZipProvider.instance.getEntries(file);
for (const entry of entries) {
if (entry.entryName.startsWith('config/') || entry.entryName.startsWith("config\\")) {
await ZipProvider.instance.extractEntryTo(
file,
entry.entryName,
path.join(
Profile.getDirectory(),
profileName,
'BepInEx'
)
);
} else if (entry.entryName.toLowerCase() !== "export.r2x") {
await ZipProvider.instance.extractEntryTo(
file,
entry.entryName,
path.join(
Profile.getDirectory(),
profileName
)
)
}
}
}
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')) {
read = (await FsProvider.instance.readFile(file)).toString();
} else if (file.endsWith('.r2z')) {
const result: Buffer | null = await ZipProvider.instance.readFile(file, "export.r2x");
if (result === null) {
return null;
}
read = result.toString();
}
return read;
}
async parseYamlToExportFormat(read: string) {
const parsedYaml = await yaml.parse(read);
return new ExportFormat(
parsedYaml.profileName,
parsedYaml.mods.map((mod: any) => {
const enabled = mod.enabled === undefined || mod.enabled;
return new ExportMod(
mod.name,
new VersionNumber(
`${mod.version.major}.${mod.version.minor}.${mod.version.patch}`
),
enabled
);
})
);
}
}
</script>
27 changes: 6 additions & 21 deletions src/components/profiles-modals/CreateProfileModal.vue
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
import { Component } from 'vue-property-decorator';
import { ModalCard } from "../all";
import R2Error from "../../model/errors/R2Error";
import sanitize from "sanitize-filename";
import ProfilesMixin from "../../components/mixins/ProfilesMixin.vue";
@Component({
components: {ModalCard}
})
export default class CreateProfileModal extends Vue {
export default class CreateProfileModal extends ProfilesMixin {
private newProfileName = '';
get isOpen(): boolean {
return this.$store.state.modals.isCreateProfileModalOpen;
}
closeModal() {
this.newProfileName = '';
this.$store.commit('closeCreateProfileModal');
}
get profileList(): string[] {
return this.$store.state.profiles.profileList;
}
makeProfileNameSafe(nameToSanitize: string): string {
return sanitize(nameToSanitize);
}
doesProfileExist(nameToCheck: string): boolean {
if ((nameToCheck.match(new RegExp('^([a-zA-Z0-9])(\\s|[a-zA-Z0-9]|_|-|[.])*$'))) === null) {
return true;
}
const safe: string = this.makeProfileNameSafe(nameToCheck);
return (this.profileList.some(function (profile: string) {
return profile.toLowerCase() === safe.toLowerCase()
}));
}
// User confirmed creation of a new profile with a name that didn't exist before.
async createProfile() {
const safeName = this.makeProfileNameSafe(this.newProfileName);
Expand Down
9 changes: 3 additions & 6 deletions src/components/profiles-modals/DeleteProfileModal.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';
import { Component } from 'vue-property-decorator';
import { ModalCard } from "../all";
import R2Error from "../../model/errors/R2Error";
import ProfilesMixin from "../../components/mixins/ProfilesMixin.vue";
@Component({
components: {ModalCard}
})
export default class DeleteProfileModal extends Vue {
export default class DeleteProfileModal extends ProfilesMixin {
get isOpen(): boolean {
return this.$store.state.modals.isDeleteProfileModalOpen;
}
get profileList(): string[] {
return this.$store.state.profiles.profileList;
}
closeDeleteProfileModal() {
this.$store.commit('closeDeleteProfileModal');
}
Expand Down
Loading

0 comments on commit 6cd313b

Please sign in to comment.