1
1
import Pako from "pako"
2
- import { clean } from "@/util/Utility"
3
2
import { Data , downloadData } from "@/aps/APSDataManagement"
4
3
import { globalAddToast } from "@/components/GlobalUIControls"
5
4
import { mirabuf } from "@/proto/mirabuf"
@@ -32,9 +31,9 @@ export interface MirabufRemoteInfo {
32
31
33
32
type MapCache = { [ id : MirabufCacheID ] : MirabufCacheInfo }
34
33
35
- const robotsDirName = "Robots "
36
- const fieldsDirName = "Fields "
37
- const piecesDirName = "Pieces "
34
+ const robotsDirName = "robot "
35
+ const fieldsDirName = "field "
36
+ const piecesDirName = "piece "
38
37
const root = await navigator . storage . getDirectory ( )
39
38
const getDirectoryHandle = ( dirName : string ) => root . getDirectoryHandle ( dirName , { create : true } )
40
39
@@ -90,11 +89,18 @@ export const canOPFS = await (async () => {
90
89
fieldFolderHandle . removeEntry ( key )
91
90
}
92
91
92
+ const pieceFolderHandle = dirHandleMap [ MiraType . PIECE ]
93
+ for await ( const key of pieceFolderHandle . keys ( ) ) {
94
+ pieceFolderHandle . removeEntry ( key )
95
+ }
96
+
93
97
window . localStorage . setItem ( robotsDirName , "{}" )
94
98
window . localStorage . setItem ( fieldsDirName , "{}" )
95
99
window . localStorage . setItem ( piecesDirName , "{}" )
96
100
97
- ; [ ...Object . values ( backUpMap ) ] . forEach ( clean )
101
+ backUpMap [ MiraType . FIELD ] = { }
102
+ backUpMap [ MiraType . PIECE ] = { }
103
+ backUpMap [ MiraType . ROBOT ] = { }
98
104
99
105
return false
100
106
}
@@ -162,9 +168,10 @@ class MirabufCachingService {
162
168
if ( ! resp . ok ) throw new Error ( `${ resp . status } ${ resp . statusText } ` )
163
169
164
170
const miraBuff = await resp . arrayBuffer ( )
171
+ miraType ??= this . assemblyFromBuffer ( miraBuff ) . dynamic ? MiraType . ROBOT : MiraType . FIELD
165
172
166
173
World . analyticsSystem ?. event ( "Remote Download" , {
167
- type : miraType === MiraType . ROBOT ? "robot" : miraType === MiraType . FIELD ? "field" : "piece" ,
174
+ type : dirNameMap [ miraType ] ,
168
175
fileSize : miraBuff . byteLength ,
169
176
} )
170
177
@@ -180,7 +187,7 @@ class MirabufCachingService {
180
187
return {
181
188
id : Date . now ( ) . toString ( ) ,
182
189
// There isn't a way to know set this to game piece correctly, since you must parse the assembly to know
183
- miraType : miraType ?? ( this . assemblyFromBuffer ( miraBuff ) . dynamic ? MiraType . ROBOT : MiraType . FIELD ) ,
190
+ miraType,
184
191
cacheKey : fetchLocation ,
185
192
buffer : miraBuff ,
186
193
name : name ,
@@ -211,7 +218,7 @@ class MirabufCachingService {
211
218
}
212
219
213
220
World . analyticsSystem ?. event ( "APS Download" , {
214
- type : dirNameMap [ miraType ] . toLowerCase ( ) ,
221
+ type : dirNameMap [ miraType ] ,
215
222
fileSize : miraBuff . byteLength ,
216
223
} )
217
224
@@ -389,7 +396,7 @@ class MirabufCachingService {
389
396
const assembly = this . assemblyFromBuffer ( buff )
390
397
World . analyticsSystem ?. event ( "Cache Get" , {
391
398
key : id ,
392
- type : miraType == MiraType . ROBOT ? "robot" : miraType == MiraType . FIELD ? "field" : "piece" ,
399
+ type : dirNameMap [ miraType ] ,
393
400
assemblyName : assembly . info ! . name ! ,
394
401
fileSize : buff . byteLength ,
395
402
} )
@@ -429,7 +436,7 @@ class MirabufCachingService {
429
436
430
437
World . analyticsSystem ?. event ( "Cache Remove" , {
431
438
key : key ,
432
- type : miraType == MiraType . ROBOT ? "robot" : miraType == MiraType . FIELD ? "field" : "piece" ,
439
+ type : dirNameMap [ miraType ] ,
433
440
} )
434
441
return true
435
442
} catch ( e ) {
@@ -444,21 +451,20 @@ class MirabufCachingService {
444
451
*/
445
452
public static async removeAll ( ) {
446
453
if ( canOPFS ) {
447
- const robotFolderHandle = dirHandleMap [ MiraType . ROBOT ]
448
- for await ( const key of robotFolderHandle . keys ( ) ) {
449
- robotFolderHandle . removeEntry ( key )
450
- }
451
-
452
- const fieldFolderHandle = dirHandleMap [ MiraType . FIELD ]
453
- for await ( const key of fieldFolderHandle . keys ( ) ) {
454
- fieldFolderHandle . removeEntry ( key )
455
- }
454
+ Object . values ( dirHandleMap ) . forEach ( async dirHandle => {
455
+ for await ( const key of dirHandle . keys ( ) ) {
456
+ dirHandle . removeEntry ( key )
457
+ }
458
+ } )
456
459
}
457
460
458
461
window . localStorage . setItem ( robotsDirName , "{}" )
459
462
window . localStorage . setItem ( fieldsDirName , "{}" )
463
+ window . localStorage . setItem ( piecesDirName , "{}" )
460
464
461
- ; [ ...Object . values ( backUpMap ) ] . forEach ( clean )
465
+ backUpMap [ MiraType . FIELD ] = { }
466
+ backUpMap [ MiraType . PIECE ] = { }
467
+ backUpMap [ MiraType . ROBOT ] = { }
462
468
}
463
469
464
470
/**
@@ -497,7 +503,7 @@ class MirabufCachingService {
497
503
498
504
World . analyticsSystem ?. event ( "Devtool Cache Persist" , {
499
505
key : id ,
500
- type : miraType == MiraType . ROBOT ? "robot" : "field" ,
506
+ type : dirNameMap [ miraType ] ,
501
507
assemblyName : assembly . info ?. name ?? "unknown" ,
502
508
fileSize : updatedBuffer . byteLength ,
503
509
} )
@@ -540,7 +546,7 @@ class MirabufCachingService {
540
546
World . analyticsSystem ?. event ( "Cache Store" , {
541
547
name : name ?? "-" ,
542
548
key : key ,
543
- type : miraType == MiraType . ROBOT ? "robot" : miraType == MiraType . FIELD ? "field" : "piece" ,
549
+ type : dirNameMap [ miraType ] ,
544
550
fileSize : miraBuff . byteLength ,
545
551
} )
546
552
0 commit comments