@@ -15,12 +15,17 @@ import { GameNotFoundError, type LibraryProvider } from "./provider";
1515import { logger } from "../logging" ;
1616import type { GameModel } from "~/prisma/client/models" ;
1717
18+ export function createGameImportTaskId ( libraryId : string , libraryPath : string ) {
19+ return btoa ( `import:${ libraryId } :${ libraryPath } ` ) ;
20+ }
21+
22+ export function createVersionImportTaskId ( gameId : string , versionName : string ) {
23+ return btoa ( `import:${ gameId } :${ versionName } ` ) ;
24+ }
25+
1826class LibraryManager {
1927 private libraries : Map < string , LibraryProvider < unknown > > = new Map ( ) ;
2028
21- private gameImportLocks : Map < string , Array < string > > = new Map ( ) ; // Library ID to Library Path
22- private versionImportLocks : Map < string , Array < string > > = new Map ( ) ; // Game ID to Version Name
23-
2429 addLibrary ( library : LibraryProvider < unknown > ) {
2530 this . libraries . set ( library . id ( ) , library ) ;
2631 }
@@ -58,12 +63,10 @@ class LibraryManager {
5863
5964 for ( const [ id , library ] of this . libraries . entries ( ) ) {
6065 const providerGames = await library . listGames ( ) ;
61- const locks = this . gameImportLocks . get ( id ) ?? [ ] ;
6266 const providerUnimportedGames = providerGames . filter (
6367 ( libraryPath ) =>
64- instanceGames [ id ] &&
65- ! instanceGames [ id ] [ libraryPath ] &&
66- ! locks . includes ( libraryPath ) ,
68+ ! instanceGames [ id ] ?. [ libraryPath ] &&
69+ ! taskHandler . hasTask ( createGameImportTaskId ( id , libraryPath ) ) ,
6770 ) ;
6871 unimportedGames [ id ] = providerUnimportedGames ;
6972 }
@@ -93,7 +96,7 @@ class LibraryManager {
9396 const unimportedVersions = versions . filter (
9497 ( e ) =>
9598 game . versions . findIndex ( ( v ) => v . versionName == e ) == - 1 &&
96- ! ( this . versionImportLocks . get ( game . id ) ?? [ ] ) . includes ( e ) ,
99+ ! taskHandler . hasTask ( createVersionImportTaskId ( game . id , e ) ) ,
97100 ) ;
98101 return unimportedVersions ;
99102 } catch ( e ) {
@@ -177,7 +180,8 @@ class LibraryManager {
177180 for ( const filename of files ) {
178181 const basename = path . basename ( filename ) ;
179182 const dotLocation = filename . lastIndexOf ( "." ) ;
180- const ext = dotLocation == - 1 ? "" : filename . slice ( dotLocation ) ;
183+ const ext =
184+ dotLocation == - 1 ? "" : filename . slice ( dotLocation ) . toLowerCase ( ) ;
181185 for ( const [ platform , checkExts ] of Object . entries ( fileExts ) ) {
182186 for ( const checkExt of checkExts ) {
183187 if ( checkExt != ext ) continue ;
@@ -215,70 +219,6 @@ class LibraryManager {
215219 }
216220 */
217221
218- /**
219- * Locks the game so you can't be imported
220- * @param libraryId
221- * @param libraryPath
222- */
223- async lockGame ( libraryId : string , libraryPath : string ) {
224- let games = this . gameImportLocks . get ( libraryId ) ;
225- if ( ! games ) this . gameImportLocks . set ( libraryId , ( games = [ ] ) ) ;
226-
227- if ( ! games . includes ( libraryPath ) ) games . push ( libraryPath ) ;
228-
229- this . gameImportLocks . set ( libraryId , games ) ;
230- }
231-
232- /**
233- * Unlocks the game, call once imported
234- * @param libraryId
235- * @param libraryPath
236- */
237- async unlockGame ( libraryId : string , libraryPath : string ) {
238- let games = this . gameImportLocks . get ( libraryId ) ;
239- if ( ! games ) this . gameImportLocks . set ( libraryId , ( games = [ ] ) ) ;
240-
241- if ( games . includes ( libraryPath ) )
242- games . splice (
243- games . findIndex ( ( e ) => e === libraryPath ) ,
244- 1 ,
245- ) ;
246-
247- this . gameImportLocks . set ( libraryId , games ) ;
248- }
249-
250- /**
251- * Locks a version so it can't be imported
252- * @param gameId
253- * @param versionName
254- */
255- async lockVersion ( gameId : string , versionName : string ) {
256- let versions = this . versionImportLocks . get ( gameId ) ;
257- if ( ! versions ) this . versionImportLocks . set ( gameId , ( versions = [ ] ) ) ;
258-
259- if ( ! versions . includes ( versionName ) ) versions . push ( versionName ) ;
260-
261- this . versionImportLocks . set ( gameId , versions ) ;
262- }
263-
264- /**
265- * Unlocks the version, call once imported
266- * @param libraryId
267- * @param libraryPath
268- */
269- async unlockVersion ( gameId : string , versionName : string ) {
270- let versions = this . versionImportLocks . get ( gameId ) ;
271- if ( ! versions ) this . versionImportLocks . set ( gameId , ( versions = [ ] ) ) ;
272-
273- if ( versions . includes ( gameId ) )
274- versions . splice (
275- versions . findIndex ( ( e ) => e === versionName ) ,
276- 1 ,
277- ) ;
278-
279- this . versionImportLocks . set ( gameId , versions ) ;
280- }
281-
282222 async importVersion (
283223 gameId : string ,
284224 versionName : string ,
@@ -295,7 +235,7 @@ class LibraryManager {
295235 umuId : string ;
296236 } ,
297237 ) {
298- const taskId = `import: ${ gameId } : ${ versionName } ` ;
238+ const taskId = createVersionImportTaskId ( gameId , versionName ) ;
299239
300240 const platform = parsePlatform ( metadata . platform ) ;
301241 if ( ! platform ) return undefined ;
@@ -309,8 +249,6 @@ class LibraryManager {
309249 const library = this . libraries . get ( game . libraryId ) ;
310250 if ( ! library ) return undefined ;
311251
312- await this . lockVersion ( gameId , versionName ) ;
313-
314252 taskHandler . create ( {
315253 id : taskId ,
316254 taskGroup : "import:game" ,
@@ -387,9 +325,6 @@ class LibraryManager {
387325
388326 progress ( 100 ) ;
389327 } ,
390- async finally ( ) {
391- await libraryManager . unlockVersion ( gameId , versionName ) ;
392- } ,
393328 } ) ;
394329
395330 return taskId ;
@@ -403,7 +338,7 @@ class LibraryManager {
403338 ) {
404339 const library = this . libraries . get ( libraryId ) ;
405340 if ( ! library ) return undefined ;
406- return library . peekFile ( game , version , filename ) ;
341+ return await library . peekFile ( game , version , filename ) ;
407342 }
408343
409344 async readFile (
@@ -415,7 +350,7 @@ class LibraryManager {
415350 ) {
416351 const library = this . libraries . get ( libraryId ) ;
417352 if ( ! library ) return undefined ;
418- return library . readFile ( game , version , filename , options ) ;
353+ return await library . readFile ( game , version , filename , options ) ;
419354 }
420355}
421356
0 commit comments