feat: groundwork for multi-profile support
This commit is contained in:
121
src/stores.ts
121
src/stores.ts
@ -1,18 +1,18 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { listen } from '@tauri-apps/api/event';
|
||||
import { Package, Profile } from './types';
|
||||
import { pkgKey } from './util';
|
||||
import { open } from '@tauri-apps/plugin-dialog';
|
||||
import { Game, Package, Profile, ProfileMeta } from './types';
|
||||
import { changePrimaryColor, pkgKey } from './util';
|
||||
|
||||
type InstallStatus = {
|
||||
pkg: string;
|
||||
};
|
||||
|
||||
export const usePkgStore = defineStore('pkg', {
|
||||
state: (): { pkg: { [key: string]: Package }; prf: Profile | null } => {
|
||||
state: (): { pkg: { [key: string]: Package } } => {
|
||||
return {
|
||||
pkg: {},
|
||||
prf: null,
|
||||
};
|
||||
},
|
||||
getters: {
|
||||
@ -22,10 +22,6 @@ export const usePkgStore = defineStore('pkg', {
|
||||
all: (state) => Object.values(state),
|
||||
allLocal: (state) => Object.values(state.pkg).filter((p) => p.loc),
|
||||
allRemote: (state) => Object.values(state.pkg).filter((p) => p.rmt),
|
||||
profile: (state) => state.prf,
|
||||
isEnabled: (state) => (pkg: Package | undefined) =>
|
||||
pkg !== undefined && state.prf?.mods.includes(pkgKey(pkg)),
|
||||
cfg: (state) => (key: string) => state.prf?.cfg[key],
|
||||
},
|
||||
actions: {
|
||||
setupListeners() {
|
||||
@ -38,7 +34,6 @@ export const usePkgStore = defineStore('pkg', {
|
||||
listen<InstallStatus>('install-end', async (ev) => {
|
||||
const key = ev.payload.pkg;
|
||||
await this.reload(key);
|
||||
await this.reloadProfile();
|
||||
this.pkg[key].js.busy = false;
|
||||
});
|
||||
},
|
||||
@ -73,36 +68,108 @@ export const usePkgStore = defineStore('pkg', {
|
||||
Object.assign(this.pkg[key], pkg);
|
||||
},
|
||||
|
||||
async initProfile(exePath: string) {
|
||||
this.prf = await invoke('init_profile', { exePath });
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
await invoke('save_profile');
|
||||
},
|
||||
|
||||
async reloadProfile() {
|
||||
this.prf = await invoke('get_current_profile');
|
||||
},
|
||||
|
||||
async fetch() {
|
||||
await invoke('fetch_listings');
|
||||
await this.reloadAll();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
async toggle(pkg: Package | undefined, enable: boolean) {
|
||||
export const usePrfStore = defineStore('prf', {
|
||||
state: (): { prf: Profile | null; list: ProfileMeta[] } => {
|
||||
return {
|
||||
prf: null,
|
||||
list: [],
|
||||
};
|
||||
},
|
||||
getters: {
|
||||
current: (state) => state.prf,
|
||||
isPkgEnabled: (state) => (pkg: Package | undefined) =>
|
||||
pkg !== undefined && state.prf?.data.mods.includes(pkgKey(pkg)),
|
||||
cfg: (state) => (key: string) => state.prf?.data.cfg[key],
|
||||
},
|
||||
actions: {
|
||||
setupListeners() {
|
||||
listen<InstallStatus>('install-end', async () => {
|
||||
await this.reload();
|
||||
});
|
||||
},
|
||||
|
||||
async prompt() {
|
||||
const exePath = await open({
|
||||
multiple: false,
|
||||
directory: false,
|
||||
filters: [
|
||||
{
|
||||
name: 'mu3.exe or chusanApp.exe',
|
||||
extensions: ['exe'],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (exePath !== null) {
|
||||
await this.create(exePath);
|
||||
}
|
||||
},
|
||||
|
||||
async create(exePath: string) {
|
||||
try {
|
||||
await invoke('init_profile', { exePath });
|
||||
await this.reload();
|
||||
await this.reloadList();
|
||||
} catch (e) {
|
||||
this.prf = null;
|
||||
}
|
||||
|
||||
if (this.prf !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
},
|
||||
|
||||
async switchTo(game: Game, name: string) {
|
||||
await invoke('load_profile', { game, name });
|
||||
await this.reload();
|
||||
if (this.prf !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
},
|
||||
|
||||
async save() {
|
||||
await invoke('save_current_profile');
|
||||
},
|
||||
|
||||
async reload() {
|
||||
this.prf = await invoke('get_current_profile');
|
||||
if (this.prf !== null) {
|
||||
changePrimaryColor(this.prf.game);
|
||||
}
|
||||
},
|
||||
|
||||
async reloadList() {
|
||||
const raw = (await invoke('list_profiles')) as [Game, string][];
|
||||
|
||||
this.list = raw.map(([game, name]) => {
|
||||
return {
|
||||
game,
|
||||
name,
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
async togglePkg(pkg: Package | undefined, enable: boolean) {
|
||||
if (pkg === undefined) {
|
||||
return;
|
||||
}
|
||||
await invoke('toggle_package', { key: pkgKey(pkg), enable });
|
||||
await this.reloadProfile();
|
||||
await this.saveProfile();
|
||||
await this.reload();
|
||||
await this.save();
|
||||
},
|
||||
|
||||
async set_cfg(key: string, value: string | boolean | number) {
|
||||
async setCfg(key: string, value: string | boolean | number) {
|
||||
await invoke('set_cfg', { key, value });
|
||||
await this.reloadProfile();
|
||||
await this.saveProfile();
|
||||
await this.reload();
|
||||
await this.save();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
Reference in New Issue
Block a user