-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameid.go
More file actions
405 lines (355 loc) · 12.8 KB
/
gameid.go
File metadata and controls
405 lines (355 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) 2025 Niema Moshiri and The Zaparoo Project.
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of go-gameid.
//
// go-gameid is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-gameid is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-gameid. If not, see <https://www.gnu.org/licenses/>.
// Package gameid provides game identification for various video game consoles.
// It can detect the console type from file extensions and headers, then extract
// game metadata from ROM/disc images.
package gameid
import (
"fmt"
"os"
"strings"
"github.com/ZaparooProject/go-gameid/archive"
"github.com/ZaparooProject/go-gameid/identifier"
)
// Result is an alias for identifier.Result for convenience.
type Result = identifier.Result
// Console is an alias for identifier.Console for convenience.
type Console = identifier.Console
// Re-export console constants for convenience.
const (
ConsoleGB = identifier.ConsoleGB
ConsoleGBC = identifier.ConsoleGBC
ConsoleGBA = identifier.ConsoleGBA
ConsoleGC = identifier.ConsoleGC
ConsoleGenesis = identifier.ConsoleGenesis
ConsoleN64 = identifier.ConsoleN64
ConsoleNeoGeoCD = identifier.ConsoleNeoGeoCD
ConsoleNES = identifier.ConsoleNES
ConsolePSP = identifier.ConsolePSP
ConsolePSX = identifier.ConsolePSX
ConsolePS2 = identifier.ConsolePS2
ConsoleSaturn = identifier.ConsoleSaturn
ConsoleSegaCD = identifier.ConsoleSegaCD
ConsoleSNES = identifier.ConsoleSNES
)
// AllConsoles is a list of all supported consoles.
var AllConsoles = identifier.AllConsoles
// identifiers maps console types to their identifier implementations.
var identifiers = map[identifier.Console]identifier.Identifier{
identifier.ConsoleGB: identifier.NewGBIdentifier(),
identifier.ConsoleGBC: identifier.NewGBIdentifier(), // Same as GB
identifier.ConsoleGBA: identifier.NewGBAIdentifier(),
identifier.ConsoleGC: identifier.NewGCIdentifier(),
identifier.ConsoleGenesis: identifier.NewGenesisIdentifier(),
identifier.ConsoleN64: identifier.NewN64Identifier(),
identifier.ConsoleNES: identifier.NewNESIdentifier(),
identifier.ConsoleSNES: identifier.NewSNESIdentifier(),
identifier.ConsolePSP: identifier.NewPSPIdentifier(),
identifier.ConsolePSX: identifier.NewPSXIdentifier(),
identifier.ConsolePS2: identifier.NewPS2Identifier(),
identifier.ConsoleSaturn: identifier.NewSaturnIdentifier(),
identifier.ConsoleSegaCD: identifier.NewSegaCDIdentifier(),
identifier.ConsoleNeoGeoCD: identifier.NewNeoGeoCDIdentifier(),
}
// pathIdentifiers are identifiers that need the file path rather than just a reader.
type pathIdentifier interface {
IdentifyFromPath(path string, db identifier.Database) (*identifier.Result, error)
}
// Identify detects the console type and identifies the game at the given path.
// It returns the identification result or an error if identification fails.
// If db is nil, no database lookup is performed.
//
// Archive paths are supported in two forms:
// - Explicit: /path/to/archive.zip/internal/path/game.gba
// - Auto-detect: /path/to/archive.zip (finds first game file by extension)
//
// Supported archive formats: ZIP, 7z, RAR.
// Only cartridge-based games (GB, GBC, GBA, NES, SNES, N64, Genesis) are supported in archives.
func Identify(path string, db *GameDatabase) (*Result, error) {
// Check if path references an archive
archivePath, err := archive.ParsePath(path)
if err != nil {
return nil, fmt.Errorf("parse archive path: %w", err)
}
if archivePath != nil {
return identifyFromArchive(archivePath, db)
}
console, err := DetectConsole(path)
if err != nil {
return nil, fmt.Errorf("failed to detect console: %w", err)
}
return IdentifyWithConsole(path, console, db)
}
// IdentifyWithConsole identifies the game at the given path using the specified console type.
// This is useful when the console is already known or when auto-detection fails.
func IdentifyWithConsole(path string, console Console, db *GameDatabase) (*Result, error) {
id, ok := identifiers[console]
if !ok {
return nil, identifier.ErrNotSupported{Format: string(console)}
}
// Convert database to interface (nil-safe)
var dbInterface identifier.Database
if db != nil {
dbInterface = db
}
// Check if it's a block device (physical disc)
if isBlockDevice(path) {
return identifyFromBlockDevice(path, console, id, dbInterface)
}
// Check if it's a directory (mounted disc)
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("failed to stat path: %w", err)
}
if info.IsDir() {
return identifyFromDirectory(path, console, dbInterface)
}
// Check if this identifier needs the file path (disc-based games)
if pid, ok := id.(pathIdentifier); ok {
result, pathErr := pid.IdentifyFromPath(path, dbInterface)
if pathErr != nil {
return nil, fmt.Errorf("identify from path: %w", pathErr)
}
return result, nil
}
// Open file and identify using reader
file, openErr := os.Open(path) //nolint:gosec // Path from user input is expected
if openErr != nil {
return nil, fmt.Errorf("failed to open file: %w", openErr)
}
defer func() { _ = file.Close() }()
stat, statErr := file.Stat()
if statErr != nil {
return nil, fmt.Errorf("failed to stat file: %w", statErr)
}
result, idErr := id.Identify(file, stat.Size(), dbInterface)
if idErr != nil {
return nil, fmt.Errorf("identify: %w", idErr)
}
return result, nil
}
// identifyFromDirectory identifies a game from a mounted disc directory.
func identifyFromDirectory(path string, console Console, database identifier.Database) (*Result, error) {
id, ok := identifiers[console]
if !ok {
return nil, identifier.ErrNotSupported{Format: string(console)}
}
// Check if identifier supports IdentifyFromPath (disc-based games)
if pid, ok := id.(pathIdentifier); ok {
result, err := pid.IdentifyFromPath(path, database)
if err != nil {
return nil, fmt.Errorf("identify from path: %w", err)
}
return result, nil
}
// Cartridge-based consoles don't support directories
return nil, identifier.ErrNotSupported{Format: "mounted directory for " + string(console)}
}
// IdentifyFromReader identifies a game from an io.ReaderAt.
// This is useful when the file is already open or when reading from non-file sources.
// size is the total size of the data.
func IdentifyFromReader(
reader interface {
ReadAt([]byte, int64) (int, error)
},
size int64,
console Console,
database *GameDatabase,
) (*Result, error) {
id, ok := identifiers[console]
if !ok {
return nil, identifier.ErrNotSupported{Format: string(console)}
}
var dbInterface identifier.Database
if database != nil {
dbInterface = database
}
result, err := id.Identify(reader, size, dbInterface)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}
return result, nil
}
// ParseConsole parses a console name string into a Console type.
// It is case-insensitive and accepts various common names.
func ParseConsole(name string) (Console, error) {
name = strings.ToUpper(strings.TrimSpace(name))
// Direct matches
switch name {
case "GB", "GAMEBOY":
return ConsoleGB, nil
case "GBC", "GAMEBOYCOLOR":
return ConsoleGBC, nil
case "GBA", "GAMEBOYADVANCE":
return ConsoleGBA, nil
case "GC", "GAMECUBE", "NGC":
return ConsoleGC, nil
case "GENESIS", "MEGADRIVE", "MD":
return ConsoleGenesis, nil
case "N64", "NINTENDO64":
return ConsoleN64, nil
case "NEOGEOCD", "NEOCD", "NGCD":
return ConsoleNeoGeoCD, nil
case "NES", "FAMICOM", "FC":
return ConsoleNES, nil
case "PSP", "PLAYSTATIONPORTABLE":
return ConsolePSP, nil
case "PSX", "PS1", "PLAYSTATION", "PLAYSTATION1":
return ConsolePSX, nil
case "PS2", "PLAYSTATION2":
return ConsolePS2, nil
case "SATURN", "SEGASATURN", "SS":
return ConsoleSaturn, nil
case "SEGACD", "MEGACD", "SCD", "MCD":
return ConsoleSegaCD, nil
case "SNES", "SUPERFAMICOM", "SFC":
return ConsoleSNES, nil
}
return "", identifier.ErrNotSupported{Format: name}
}
// SupportedConsoles returns a list of all supported console names.
func SupportedConsoles() []string {
result := make([]string, len(AllConsoles))
for i, c := range AllConsoles {
result[i] = string(c)
}
return result
}
// IsDiscBased returns true if the console uses disc-based media.
func IsDiscBased(console Console) bool {
switch console {
case ConsoleGC, ConsoleNeoGeoCD, ConsolePSP, ConsolePSX, ConsolePS2, ConsoleSaturn, ConsoleSegaCD:
return true
default:
return false
}
}
// IsCartridgeBased returns true if the console uses cartridge-based media.
func IsCartridgeBased(console Console) bool {
return !IsDiscBased(console)
}
// identifyFromBlockDevice identifies a game from a physical disc (block device).
//
//nolint:revive // Line length acceptable for function signature with ignored parameter
func identifyFromBlockDevice(path string, _ Console, ident identifier.Identifier, database identifier.Database) (*Result, error) {
// For disc-based consoles, use IdentifyFromPath which handles block devices
if pid, ok := ident.(pathIdentifier); ok {
result, err := pid.IdentifyFromPath(path, database)
if err != nil {
return nil, fmt.Errorf("identify from path: %w", err)
}
return result, nil
}
// Open block device directly
blockDev, err := os.Open(path) //nolint:gosec // Path from user input is expected for block device
if err != nil {
return nil, fmt.Errorf("failed to open block device: %w", err)
}
defer func() { _ = blockDev.Close() }()
// Get device size (for block devices, we need to use ioctl or read to end)
// For now, use a reasonable default size for disc identification
// Most identifiers only need the first few KB
size := int64(700 * 1024 * 1024) // 700MB typical CD size
result, err := ident.Identify(blockDev, size, database)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}
return result, nil
}
// identifyFromArchive identifies a game file inside an archive.
func identifyFromArchive(archivePath *archive.Path, db *GameDatabase) (*Result, error) {
// Open the archive
arc, err := archive.Open(archivePath.ArchivePath)
if err != nil {
return nil, fmt.Errorf("open archive: %w", err)
}
defer func() { _ = arc.Close() }()
// Determine internal path (auto-detect if not specified)
internalPath := archivePath.InternalPath
if internalPath == "" {
detected, detectErr := archive.DetectGameFile(arc)
if detectErr != nil {
return nil, fmt.Errorf("detect game file in archive: %w", detectErr)
}
internalPath = detected
}
// Detect console from the internal file's extension
console, err := DetectConsoleFromExtension(internalPath)
if err != nil {
return nil, fmt.Errorf("detect console from archive file: %w", err)
}
// Only cartridge-based games are supported in archives
if !IsCartridgeBased(console) {
return nil, archive.DiscNotSupportedError{Console: string(console)}
}
// Get the identifier for this console
id, ok := identifiers[console]
if !ok {
return nil, identifier.ErrNotSupported{Format: string(console)}
}
// Convert database to interface (nil-safe)
var dbInterface identifier.Database
if db != nil {
dbInterface = db
}
// Open the file as ReaderAt (buffered in memory)
reader, size, closer, err := arc.OpenReaderAt(internalPath)
if err != nil {
return nil, fmt.Errorf("open file in archive: %w", err)
}
defer func() { _ = closer.Close() }()
// Identify the game
result, err := id.Identify(reader, size, dbInterface)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}
return result, nil
}
// IdentifyFromArchive identifies a game from an already-opened archive.
// This is useful when you need to control archive lifecycle or identify multiple files.
//
//nolint:revive // Exported function using internal type is intentional for advanced usage
func IdentifyFromArchive(
arc archive.Archive,
internalPath string,
console Console,
db *GameDatabase,
) (*Result, error) {
// Only cartridge-based games are supported
if !IsCartridgeBased(console) {
return nil, archive.DiscNotSupportedError{Console: string(console)}
}
id, ok := identifiers[console]
if !ok {
return nil, identifier.ErrNotSupported{Format: string(console)}
}
var dbInterface identifier.Database
if db != nil {
dbInterface = db
}
reader, size, closer, err := arc.OpenReaderAt(internalPath)
if err != nil {
return nil, fmt.Errorf("open file in archive: %w", err)
}
defer func() { _ = closer.Close() }()
result, err := id.Identify(reader, size, dbInterface)
if err != nil {
return nil, fmt.Errorf("identify: %w", err)
}
return result, nil
}