11import _i18n , { useI18N } from 'src/i18n' ;
22import { callRemote } from '../utils' ;
33import {
4- useInstalledMods ,
5- useGamePath ,
6- useStorage ,
7- initGamePath ,
4+ useInstalledMods ,
5+ useGamePath ,
6+ useStorage ,
7+ initGamePath ,
8+ useCurrentEverestVersion ,
89} from '../states' ;
910import { useEffect , useMemo } from 'preact/hooks' ;
1011import { createPopup } from 'src/components/Popup' ;
@@ -13,81 +14,87 @@ import { ProgressIndicator } from 'src/components/Progress';
1314
1415let lastGamePath = '' ;
1516export const createModManageContext = ( ) => {
16- const { setInstalledMods } = useInstalledMods ( ) ;
17+ const { setInstalledMods } = useInstalledMods ( ) ;
1718
18- const [ gamePath ] = useGamePath ( ) ;
19- const { storage, save } = useStorage ( ) ;
19+ const [ gamePath ] = useGamePath ( ) ;
2020
21- useEffect ( ( ) => {
22- if ( ! gamePath ) return ;
23- if ( ! storage ) return ;
21+ const { storage, save } = useStorage ( ) ;
2422
25- storage . root ??= { } ;
26- storage . root . lastGamePath = gamePath ;
27- console . log ( 'saving game path' , gamePath ) ;
28- save ( ) ;
29- } , [ gamePath , storage ] ) ;
23+ useEffect ( ( ) => {
24+ if ( ! gamePath ) return ;
25+ if ( ! storage ) return ;
3026
31- initGamePath ( ) ;
27+ storage . root ??= { } ;
28+ storage . root . lastGamePath = gamePath ;
29+ console . log ( 'saving game path' , gamePath ) ;
30+ save ( ) ;
31+ } , [ gamePath , storage ] ) ;
3232
33- const ctx = {
34- reloadMods : ( ) => {
35- return new Promise ( ( rs , rj ) => {
36- if ( ! gamePath ) {
37- console . warn ( 'game path not set' ) ;
38- rj ( 'game path not set' ) ;
39- return ;
40- }
41- callRemote ( 'get_installed_mods' , gamePath + '/Mods' , ( data : string ) => {
42- console . log ( 'mod reload finished' ) ;
43- const da = JSON . parse ( data ) ;
44- rs ( da ) ;
45- setInstalledMods ( da ) ;
46- } ) ;
47- } ) ;
48- } ,
49- gamePath,
50- modsPath : gamePath + '/Mods' ,
51- } ;
33+ initGamePath ( ) ;
34+
35+ const ctx = {
36+ reloadMods : ( ) => {
37+ return new Promise ( ( rs , rj ) => {
38+ if ( ! gamePath ) {
39+ console . warn ( 'game path not set' ) ;
40+ rj ( 'game path not set' ) ;
41+ return ;
42+ }
43+ callRemote ( 'get_installed_mods' , gamePath + '/Mods' , ( data : string ) => {
44+ console . log ( 'mod reload finished' ) ;
45+ const da = JSON . parse ( data ) ;
46+ rs ( da ) ;
47+ setInstalledMods ( da ) ;
48+ } ) ;
49+ } ) ;
50+ } ,
51+ gamePath,
52+ modsPath : gamePath + '/Mods' ,
53+ } ;
5254
53- // WHY THE FUCK useEffect doesn't trigger here
54- if ( lastGamePath !== gamePath ) {
55- lastGamePath = gamePath ;
55+ // WHY THE FUCK useEffect doesn't trigger here
56+ if ( lastGamePath !== gamePath ) {
57+ lastGamePath = gamePath ;
5658
57- if ( gamePath ) {
58- setTimeout ( ( ) => {
59- const popup = createPopup (
60- ( ) => {
61- return (
62- < div className = "loading-popup" >
63- < ProgressIndicator infinite />
64- < span > { _i18n . t ( '正在加载 Mod 列表,请稍等' ) } </ span >
65- </ div >
66- ) ;
67- } ,
68- {
69- cancelable : false ,
70- }
71- ) ;
72- ctx
73- . reloadMods ( )
74- . then ( ( ) => popup . hide ( ) )
75- . catch ( ( e ) => {
76- popup . hide ( ) ;
77- createPopup ( ( ) => {
78- return (
79- < div className = "loading-popup" >
80- < h1 > { _i18n . t ( '加载 Mod 列表失败' ) } </ h1 >
81- < p > { _i18n . t ( '请检查游戏路径是否正确,或网络连接是否正常' ) } </ p >
82- < p > { _i18n . t ( '部分功能将不可用' ) } </ p >
83- < p > { e } </ p >
84- </ div >
85- ) ;
59+ if ( gamePath ) {
60+
61+ callRemote ( "get_everest_version" , gamePath , ( ver : string ) => {
62+ if ( ver && ver . length > 2 ) {
63+ setTimeout ( ( ) => {
64+ const popup = createPopup (
65+ ( ) => {
66+ return (
67+ < div className = "loading-popup" >
68+ < ProgressIndicator infinite />
69+ < span > { _i18n . t ( '正在加载 Mod 列表,请稍等' ) } </ span >
70+ </ div >
71+ ) ;
72+ } ,
73+ {
74+ cancelable : false ,
75+ }
76+ ) ;
77+ ctx
78+ . reloadMods ( )
79+ . then ( ( ) => popup . hide ( ) )
80+ . catch ( ( e ) => {
81+ popup . hide ( ) ;
82+ createPopup ( ( ) => {
83+ return (
84+ < div className = "loading-popup" >
85+ < h1 > { _i18n . t ( '加载 Mod 列表失败' ) } </ h1 >
86+ < p > { _i18n . t ( '请检查游戏路径是否正确,或网络连接是否正常' ) } </ p >
87+ < p > { _i18n . t ( '部分功能将不可用' ) } </ p >
88+ < p > { e } </ p >
89+ </ div >
90+ ) ;
91+ } ) ;
92+ } ) ;
93+ } , 10 ) ;
94+ }
8695 } ) ;
87- } ) ;
88- } , 10 ) ;
96+ }
8997 }
90- }
9198
92- return ctx ;
99+ return ctx ;
93100} ;
0 commit comments