| 1 | import 'dart:io'; |
| 2 | import 'dart:isolate'; |
| 3 | import 'dart:typed_data'; |
| 4 | |
| 5 | import 'package:gba_core/gba_core.dart'; |
| 6 | |
| 7 | /// A game in the library. |
| 8 | final class GameEntry { |
| 9 | const GameEntry({ |
| 10 | required this.romPath, |
| 11 | required this.fileName, |
| 12 | required this.title, |
| 13 | required this.gameCode, |
| 14 | required this.hasSave, |
| 15 | }); |
| 16 | |
| 17 | final String romPath; |
| 18 | final String fileName; |
| 19 | final String title; |
| 20 | final String gameCode; |
| 21 | final bool hasSave; |
| 22 | } |
| 23 | |
| 24 | /// Thrown when a file cannot be added to the library; [message] is for the |
| 25 | /// user. |
| 26 | final class LibraryException implements |
| 27 | const LibraryException(this |
| 28 | |
| 29 | final String message; |
| 30 | |
| 31 | @override |
| 32 | String toString() => message |
| 33 | } |
| 34 | |
| 35 | /// The user's games, their saves and an optional BIOS, kept in the app's |
| 36 | /// own storage: |
| 37 | /// |
| 38 | /// * `roms/` — copies of imported ROM files; |
| 39 | /// * `saves/<ROM name>.sav` — each game's save; |
| 40 | /// * `bios/gba_bios.bin` — a BIOS the user imported, if any. |
| 41 | final class GameLibrary { |
| 42 | GameLibrary(Directory root) |
| 43 | : _roms = Directory(' |
| 44 | _saves = Directory('${root |
| 45 | _bios = File('${root |
| 46 | |
| 47 | final Directory _roms; |
| 48 | final Directory _saves; |
| 49 | final File _bios; |
| 50 | |
| 51 | /// The imported BIOS, or null to use the built-in stand-in. |
| 52 | String? get biosPath => _bios |
| 53 | |
| 54 | /// Where [game]'s save is stored. |
| 55 | String savePathFor(GameEntry |
| 56 | '${_saves.path} |
| 57 | |
| 58 | /// The games in the library, by title. |
| 59 | Future<List<GameEntry |
| 60 | if (!await _roms.exists |
| 61 | final games = <GameEntry |
| 62 | await for (final entity in |
| 63 | if (entity is! File) |
| 64 | final entry = await _describe |
| 65 | if (entry != null) games.add |
| 66 | } |
| 67 | games.sort( |
| 68 | (a, b) => a.title.toLowerCase |
| 69 | ); |
| 70 | return games; |
| 71 | } |
| 72 | |
| 73 | /// Copies the ROM at [sourcePath] into the library under [fileName] |
| 74 | /// after checking that it is a GBA ROM (off the UI isolate: ROMs can be |
| 75 | /// 32 MB). Replaces a game with the same file name. |
| 76 | Future<GameEntry> importRom |
| 77 | final problem = await Isolate |
| 78 | try { |
| 79 | Cartridge.fromBytes(File |
| 80 | return null; |
| 81 | } on InvalidRomException catch (error) { |
| 82 | return error.message; |
| 83 | } on FileSystemException catch (error) { |
| 84 | return 'The file could not be read (${error. |
| 85 | } |
| 86 | }); |
| 87 | if (problem != null) throw LibraryException |
| 88 | await _roms.create(recursive: |
| 89 | final safeName = fileName.replaceAll |
| 90 | final target = File( |
| 91 | await File(sourcePath).copy |
| 92 | return (await _describe(target)) |
| 93 | } |
| 94 | |
| 95 | /// Removes [game] and, if [deleteSave] |
| 96 | Future<void> delete |
| 97 | await File(game.romPath). |
| 98 | final save = File( |
| 99 | if (deleteSave && await save.exists |
| 100 | } |
| 101 | |
| 102 | /// Uses the BIOS image at [sourcePath] from now on. |
| 103 | Future<void> importBios |
| 104 | final length = await File |
| 105 | if (length != GbaMemory.biosSize) { |
| 106 | throw LibraryException( |
| 107 | 'A GBA BIOS file is exactly ${GbaMemory.biosSize} |
| 108 | 'is $length bytes.', |
| 109 | ); |
| 110 | } |
| 111 | await _bios.parent.create(recursive |
| 112 | await File(sourcePath).copy |
| 113 | } |
| 114 | |
| 115 | /// Goes back to the built-in BIOS stand-in. |
| 116 | Future<void> removeBios |
| 117 | if (await _bios.exists()) |
| 118 | } |
| 119 | |
| 120 | /// Reads just the header, so listing stays fast with large ROMs. |
| 121 | Future<GameEntry?> _describe |
| 122 | final RandomAccessFile handle; |
| 123 | try { |
| 124 | handle = await file.open() |
| 125 | } on FileSystemException { |
| 126 | return null; |
| 127 | } |
| 128 | try { |
| 129 | final header = Uint8List( |
| 130 | final read = await handle. |
| 131 | if (read < RomHeader.size) |
| 132 | final parsed = RomHeader. |
| 133 | final fileName = file.uri. |
| 134 | final title = parsed.title. |
| 135 | final entry = GameEntry( |
| 136 | romPath: file.path, |
| 137 | fileName: fileName, |
| 138 | title: title.isEmpty || title.contains |
| 139 | ? _withoutExtension(fileName) |
| 140 | : title, |
| 141 | gameCode: parsed.gameCode.trim() |
| 142 | hasSave: false, |
| 143 | ); |
| 144 | return GameEntry( |
| 145 | romPath: entry.romPath, |
| 146 | fileName: entry.fileName, |
| 147 | title: entry.title, |
| 148 | gameCode: entry.gameCode, |
| 149 | hasSave: await File(savePathFor |
| 150 | ); |
| 151 | } finally { |
| 152 | await handle.close(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static String _withoutExtension |
| 157 | final dot = name.lastIndexOf |
| 158 | return dot > 0 ? |
| 159 | } |
| 160 | } |