@@ -13,7 +13,7 @@ const protectRuntime = () => {
13
13
if ( sillyProtection ) return
14
14
sillyProtection = true
15
15
const sensetiveKeys = new Set ( [ 'authenticatedAccounts' , 'serversList' , 'username' ] )
16
- window . localStorage = new Proxy ( window . localStorage , {
16
+ const proxy = new Proxy ( window . localStorage , {
17
17
get ( target , prop ) {
18
18
if ( typeof prop === 'string' ) {
19
19
if ( sensetiveKeys . has ( prop ) ) {
@@ -69,6 +69,11 @@ const protectRuntime = () => {
69
69
return Reflect . deleteProperty ( target , prop )
70
70
}
71
71
} )
72
+ Object . defineProperty ( window , 'localStorage' , {
73
+ value : proxy ,
74
+ writable : false ,
75
+ configurable : false ,
76
+ } )
72
77
}
73
78
74
79
// #region Database
@@ -198,6 +203,7 @@ window.mcraft = {
198
203
}
199
204
200
205
const activateMod = async ( mod : ClientMod , reason : string ) => {
206
+ if ( mod . enabled === false ) return false
201
207
protectRuntime ( )
202
208
console . debug ( `Activating mod ${ mod . name } (${ reason } )...` )
203
209
window . loadedMods ??= { }
@@ -280,24 +286,35 @@ const installOrUpdateMod = async (repo: Repository, mod: ClientModDefinition, ac
280
286
}
281
287
if ( mod . stylesGlobal ) {
282
288
await progress ?. executeWithMessage (
283
- `Installing ${ mod . name } styles` ,
289
+ `Downloading ${ mod . name } styles` ,
284
290
async ( ) => {
285
291
mod . stylesGlobal = await fetchData ( [ 'global.css' ] ) as any
286
292
}
287
293
)
288
294
}
289
295
if ( mod . scriptMainUnstable ) {
290
296
await progress ?. executeWithMessage (
291
- `Installing ${ mod . name } script` ,
297
+ `Downloading ${ mod . name } script` ,
292
298
async ( ) => {
293
299
mod . scriptMainUnstable = await fetchData ( [ 'mainUnstable.js' ] ) as any
294
300
}
295
301
)
296
302
}
303
+ if ( mod . serverPlugin ) {
304
+ if ( mod . name . endsWith ( '.disabled' ) ) throw new Error ( `Mod name ${ mod . name } can't end with .disabled` )
305
+ await progress ?. executeWithMessage (
306
+ `Downloading ${ mod . name } server plugin` ,
307
+ async ( ) => {
308
+ mod . serverPlugin = await fetchData ( [ 'serverPlugin.js' ] ) as any
309
+ }
310
+ )
311
+ }
297
312
if ( activate ) {
298
- const result = await activateMod ( mod as ClientMod , 'install' )
299
- if ( ! result ) {
313
+ // todo try to de-activate mod if it's already loaded
314
+ if ( window . loadedMods ?. [ mod . name ] ) {
300
315
modsWaitingReloadStatus [ mod . name ] = true
316
+ } else {
317
+ await activateMod ( mod as ClientMod , 'install' )
301
318
}
302
319
}
303
320
await savePlugin ( mod as ClientMod )
@@ -324,7 +341,7 @@ const checkRepositoryUpdates = async (repo: Repository) => {
324
341
325
342
}
326
343
327
- const fetchRepository = async ( urlOriginal : string , url : string , hasMirrors = false ) => {
344
+ export const fetchRepository = async ( urlOriginal : string , url : string , hasMirrors = false ) => {
328
345
const fetchUrl = normalizeRepoUrl ( url ) . replace ( / \/ $ / , '' ) + '/mcraft-repo.json'
329
346
try {
330
347
const response = await fetch ( fetchUrl ) . then ( async res => res . json ( ) )
@@ -393,18 +410,21 @@ export const uninstallModAction = async (name: string) => {
393
410
delete modsErrors [ name ]
394
411
}
395
412
396
- export const setEnabledModAction = async ( name : string , enabled : boolean ) => {
413
+ export const setEnabledModAction = async ( name : string , newEnabled : boolean ) => {
397
414
const mod = await getPlugin ( name )
398
415
if ( ! mod ) throw new Error ( `Mod ${ name } not found` )
399
- if ( enabled ) {
400
- if ( window . loadedMods ?. [ mod . name ] ) {
401
- mod . enabled = true
402
- } else {
416
+ if ( newEnabled ) {
417
+ mod . enabled = true
418
+ if ( ! window . loadedMods ?. [ mod . name ] ) {
403
419
await activateMod ( mod , 'manual' )
404
420
}
405
421
} else {
406
422
// todo deactivate mod
407
423
mod . enabled = false
424
+ if ( window . loadedMods ?. [ mod . name ] ?. deactivate ) {
425
+ window . loadedMods [ mod . name ] . deactivate ( )
426
+ delete window . loadedMods [ mod . name ]
427
+ }
408
428
}
409
429
await savePlugin ( mod )
410
430
}
@@ -420,7 +440,9 @@ export const getAllModsDisplayList = async () => {
420
440
const mapMods = ( mapMods : ClientMod [ ] ) => mapMods . map ( mod => ( {
421
441
...mod ,
422
442
installed : installedMods . some ( m => m . name === mod . name ) ,
423
- enabled : ! ! window . loadedMods ?. [ mod . name ]
443
+ activated : ! ! window . loadedMods ?. [ mod . name ] ,
444
+ installedVersion : installedMods . find ( m => m . name === mod . name ) ?. version ,
445
+ canBeActivated : mod . scriptMainUnstable || mod . stylesGlobal ,
424
446
} ) )
425
447
return {
426
448
repos : repos . map ( repo => ( {
@@ -433,7 +455,7 @@ export const getAllModsDisplayList = async () => {
433
455
434
456
export const removeRepositoryAction = async ( url : string ) => {
435
457
// todo remove mods
436
- const choice = await showOptionsModal ( 'Remove repository? Installed mods won\' be automatically removed.' , [ 'Yes' ] )
458
+ const choice = await showOptionsModal ( 'Remove repository? Installed mods wont be automatically removed.' , [ 'Yes' ] )
437
459
if ( ! choice ) return
438
460
await deleteRepository ( url )
439
461
modsReactiveUpdater . counter ++
@@ -458,4 +480,21 @@ export const addRepositoryAction = async () => {
458
480
await fetchRepository ( url , url )
459
481
}
460
482
461
- // export const getAllMods = () => {}
483
+ export const getServerPlugin = async ( plugin : string ) => {
484
+ const mod = await getPlugin ( plugin )
485
+ if ( ! mod ) return null
486
+ if ( mod . serverPlugin ) {
487
+ return {
488
+ content : mod . serverPlugin ,
489
+ version : mod . version
490
+ }
491
+ }
492
+ return null
493
+ }
494
+
495
+ export const getAvailableServerPlugins = async ( ) => {
496
+ const mods = await getAllMods ( )
497
+ return mods . filter ( mod => mod . serverPlugin )
498
+ }
499
+
500
+ window . inspectInstalledMods = getAllMods
0 commit comments