/
mod.ts
79 lines (76 loc) · 2.47 KB
/
mod.ts
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
/* eslint-disable no-param-reassign */
import { createModel } from '@rematch/core';
import { RootModel } from '../rootModelType';
import { initializeMod } from '../../utils/mod';
export enum MOD_LOADING_STATE {
notStarted = 'notStarted',
prepare = 'prepare',
loading = 'loading',
ready = 'ready',
failed = 'failed',
}
const initialState = {
modList: [],
modLoadingState: MOD_LOADING_STATE.notStarted,
mapList: [],
};
export type IState = {
modList: string[];
modLoadingState: MOD_LOADING_STATE;
mapList: { modName: string; mapName: string }[];
};
export const mod = createModel<RootModel>()({
state: initialState as IState,
reducers: {
setModList(state: IState, newModList: string[]) {
state.modList = newModList;
return state;
},
setModLoadingState(state: IState, newLoadingState: MOD_LOADING_STATE) {
state.modLoadingState = newLoadingState;
return state;
},
setMapList(state: IState, newMapList: { modName: string; mapName: string }[]) {
state.mapList = newMapList;
return state;
},
},
effects: (dispatch) => ({
async loadModList() {
try {
dispatch.mod.setModLoadingState(MOD_LOADING_STATE.prepare);
const modList = await window.mod.loadModList();
dispatch.mod.setModList(modList);
} catch (error) {
dispatch.mod.setModLoadingState(MOD_LOADING_STATE.failed);
console.error(error);
}
},
async initializeMods(_, rootState) {
try {
dispatch.mod.setModLoadingState(MOD_LOADING_STATE.loading);
const loadModTasks: Promise<void>[] = rootState.mod.modList.map(async (modName) => {
const scriptContent = await window.mod.loadModInitializationScript({ modName });
initializeMod(modName, scriptContent);
});
await Promise.all(loadModTasks);
dispatch.mod.setModLoadingState(MOD_LOADING_STATE.ready);
} catch (error) {
dispatch.mod.setModLoadingState(MOD_LOADING_STATE.failed);
console.error(error);
}
},
async loadMapList(_, rootState) {
try {
let mapList: { modName: string; mapName: string }[] = [];
for (const modName of rootState.mod.modList) {
const mapListOfMod = await window.mod.loadModMapList({ modName });
mapList = [...mapList, ...mapListOfMod.map((mapName) => ({ modName, mapName }))];
}
dispatch.mod.setMapList(mapList);
} catch (error) {
console.error(error);
}
},
}),
});