Skip to content

Commit d2b78e0

Browse files
Separate mod importing business logic from the UI component
1 parent eed26c2 commit d2b78e0

File tree

2 files changed

+91
-70
lines changed

2 files changed

+91
-70
lines changed

src/components/mixins/ProfilesMixin.vue

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import Vue from 'vue';
33
import Component from 'vue-class-component';
44
55
import sanitize from "sanitize-filename";
6+
import ExportFormat from "../../model/exports/ExportFormat";
7+
import path from "path";
8+
import Profile from "../../model/Profile";
9+
import FileUtils from "../../utils/FileUtils";
10+
import FsProvider from "../../providers/generic/file/FsProvider";
11+
import ZipProvider from "../../providers/generic/zip/ZipProvider";
12+
13+
const PROFILE_UPDATE_DIRECTORY = "_profile_update";
614
715
@Component
816
export default class ProfilesMixin extends Vue {
@@ -33,5 +41,78 @@ export default class ProfilesMixin extends Vue {
3341
return sanitize(nameToSanitize);
3442
}
3543
44+
// returns true if the importing should be continued.
45+
async prepareDirectoryForProfileImporting(eventProfileName: string, localListenerId: number, parsed: ExportFormat, isUpdate: boolean) {
46+
let fs = FsProvider.instance;
47+
if (eventProfileName == '') {
48+
return false;
49+
}
50+
51+
if (isUpdate) {
52+
if (await fs.exists(path.join(Profile.getDirectory(), PROFILE_UPDATE_DIRECTORY))) {
53+
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), PROFILE_UPDATE_DIRECTORY));
54+
await fs.rmdir(path.join(Profile.getDirectory(), PROFILE_UPDATE_DIRECTORY));
55+
}
56+
await this.$store.dispatch('profiles/setSelectedProfile', {
57+
profileName: PROFILE_UPDATE_DIRECTORY,
58+
prewarmCache: true
59+
});
60+
}
61+
62+
return parsed.getMods().length > 0;
63+
}
64+
65+
async importProfileMods(eventProfileName: string, isUpdate: boolean, parsed: ExportFormat, files: string[], downloadImportedProfileMods: Function) {
66+
let profileName: string = isUpdate ? PROFILE_UPDATE_DIRECTORY : eventProfileName;
67+
68+
setTimeout(async () => {
69+
await downloadImportedProfileMods(parsed.getMods(), async () => {
70+
if (files[0].endsWith('.r2z')) {
71+
await this.extractZippedProfileFile(files[0], profileName);
72+
}
73+
if (isUpdate) {
74+
this.activeProfileName = eventProfileName;
75+
try {
76+
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), eventProfileName));
77+
} catch (e) {
78+
console.log("Failed to empty directory:", e);
79+
}
80+
let fs = FsProvider.instance;
81+
await fs.rmdir(path.join(Profile.getDirectory(), eventProfileName));
82+
await fs.rename(path.join(Profile.getDirectory(), profileName), path.join(Profile.getDirectory(), eventProfileName));
83+
}
84+
await this.$store.dispatch('profiles/setSelectedProfile', {
85+
profileName: eventProfileName,
86+
prewarmCache: true
87+
});
88+
});
89+
}, 100);
90+
}
91+
92+
async extractZippedProfileFile(file: string, profileName: string) {
93+
const entries = await ZipProvider.instance.getEntries(file);
94+
for (const entry of entries) {
95+
if (entry.entryName.startsWith('config/') || entry.entryName.startsWith("config\\")) {
96+
await ZipProvider.instance.extractEntryTo(
97+
file,
98+
entry.entryName,
99+
path.join(
100+
Profile.getDirectory(),
101+
profileName,
102+
'BepInEx'
103+
)
104+
);
105+
} else if (entry.entryName.toLowerCase() !== "export.r2x") {
106+
await ZipProvider.instance.extractEntryTo(
107+
file,
108+
entry.entryName,
109+
path.join(
110+
Profile.getDirectory(),
111+
profileName
112+
)
113+
)
114+
}
115+
}
116+
}
36117
}
37118
</script>

src/components/profiles-modals/ImportProfileModal.vue

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { ModalCard } from "../all";
44
import R2Error from "../../model/errors/R2Error";
55
import { ProfileImportExport } from "../../r2mm/mods/ProfileImportExport";
66
import ExportMod from "../../model/exports/ExportMod";
7-
import path from "path";
87
import Profile from "../../model/Profile";
9-
import FileUtils from "../../utils/FileUtils";
108
import FsProvider from "../../providers/generic/file/FsProvider";
119
import ThunderstoreDownloaderProvider from "../../providers/ror2/downloading/ThunderstoreDownloaderProvider";
1210
import StatusEnum from "../../model/enums/StatusEnum";
@@ -150,9 +148,16 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
150148
const profileContent = this.profileImportContent;
151149
const localListenerId = this.listenerId + 1;
152150
this.listenerId = localListenerId;
153-
document.addEventListener('created-profile', ((event: CustomEvent) =>
154-
this.profileCreatedCallback(event, localListenerId, profileContent!, [this.profileImportFilePath!])
155-
) as EventListener, {once: true});
151+
document.addEventListener('created-profile', ((event: CustomEvent) => {
152+
if (this.listenerId === localListenerId) {
153+
const isUpdate: boolean = this.importUpdateSelection === 'UPDATE';
154+
if (this.prepareDirectoryForProfileImporting(event.detail, localListenerId, profileContent!, isUpdate)) {
155+
this.isProfileBeingImported = true;
156+
this.importProfileMods(event.detail, isUpdate, profileContent!, [this.profileImportFilePath!], this.downloadImportedProfileMods);
157+
this.closeModal();
158+
}
159+
}
160+
}) as EventListener, {once: true});
156161
this.newProfileName = profileContent!.getProfileName();
157162
this.activeStep = 'ADDING_PROFILE';
158163
}
@@ -186,71 +191,6 @@ export default class ImportProfileModal extends mixins(ProfilesMixin) {
186191
return read;
187192
}
188193
189-
profileCreatedCallback(event: CustomEvent, localListenerId: number, parsed: ExportFormat, files: string[]) {
190-
if (this.listenerId === localListenerId) {
191-
(async () => {
192-
let profileName: string = event.detail;
193-
if (profileName !== '') {
194-
if (this.importUpdateSelection === 'UPDATE') {
195-
profileName = "_profile_update";
196-
if (await fs.exists(path.join(Profile.getDirectory(), profileName))) {
197-
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), profileName));
198-
await fs.rmdir(path.join(Profile.getDirectory(), profileName));
199-
}
200-
await this.$store.dispatch('profiles/setSelectedProfile', { profileName: profileName, prewarmCache: true });
201-
}
202-
if (parsed.getMods().length > 0) {
203-
this.isProfileBeingImported = true;
204-
setTimeout(async () => {
205-
await this.downloadImportedProfileMods(parsed.getMods(), async () => {
206-
if (files[0].endsWith('.r2z')) {
207-
await this.extractZippedProfileFile(files[0], profileName);
208-
}
209-
if (this.importUpdateSelection === 'UPDATE') {
210-
this.activeProfileName = event.detail;
211-
try {
212-
await FileUtils.emptyDirectory(path.join(Profile.getDirectory(), event.detail));
213-
} catch (e) {
214-
console.log("Failed to empty directory:", e);
215-
}
216-
await fs.rmdir(path.join(Profile.getDirectory(), event.detail));
217-
await fs.rename(path.join(Profile.getDirectory(), profileName), path.join(Profile.getDirectory(), event.detail));
218-
}
219-
await this.$store.dispatch('profiles/setSelectedProfile', { profileName: event.detail, prewarmCache: true });
220-
this.closeModal();
221-
});
222-
}, 100);
223-
}
224-
}
225-
})();
226-
}
227-
}
228-
229-
async extractZippedProfileFile(file: string, profileName: string) {
230-
const entries = await ZipProvider.instance.getEntries(file);
231-
for (const entry of entries) {
232-
if (entry.entryName.startsWith('config/') || entry.entryName.startsWith("config\\")) {
233-
await ZipProvider.instance.extractEntryTo(
234-
file,
235-
entry.entryName,
236-
path.join(
237-
Profile.getDirectory(),
238-
profileName,
239-
'BepInEx'
240-
)
241-
);
242-
} else if (entry.entryName.toLowerCase() !== "export.r2x") {
243-
await ZipProvider.instance.extractEntryTo(
244-
file,
245-
entry.entryName,
246-
path.join(
247-
Profile.getDirectory(),
248-
profileName
249-
)
250-
)
251-
}
252-
}
253-
}
254194
255195
async downloadImportedProfileMods(modList: ExportMod[], callback?: () => void) {
256196
const settings = this.$store.getters['settings'];

0 commit comments

Comments
 (0)