From a9588b079adb4042ba566056549002821dc92f4c Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 5 Jan 2026 14:49:32 +0100 Subject: [PATCH] improved game scanning --- internal/gamefile.go | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/internal/gamefile.go b/internal/gamefile.go index 1d0a4cd..999b45c 100644 --- a/internal/gamefile.go +++ b/internal/gamefile.go @@ -23,23 +23,39 @@ func scanGameFileForID(filename string) (string, error) { return "", fmt.Errorf("failed to read file: %w", err) } - pattern := regexp.MustCompile(`(SLUS|SLES|SLPS|SCUS|SCES|SCPS)[_-]?\d{3}[\._]\d{2}`) - - match := pattern.Find(buffer[:n]) - if match != nil { - gameID := string(match) - return normalizeGameID(gameID), nil + id, found := scanBufferForID(buffer[:n]) + if !found { + return "", fmt.Errorf("no PS2 game ID found in file") } - return "", fmt.Errorf("no PS2 game ID found in file") + return id, nil +} + +func scanBufferForID(buffer []byte) (string, bool) { + pattern := regexp.MustCompile(`(SLUS|SLES|SLPS|SCUS|SCES|SCPS)[_\-\s]?\d{3}[\._\-\s]?\d{2}`) + + match := pattern.Find(buffer) + if match != nil { + gameID := string(match) + return normalizeGameID(gameID), true + } + + return "", false } func normalizeGameID(id string) string { - s := strings.ReplaceAll(id, "_", "-") - s = strings.ReplaceAll(s, ".", "") - s = strings.ToUpper(s) + re := regexp.MustCompile(`(?i)^([A-Z]{4})[^0-9]*([0-9]{3})[^0-9]*([0-9]{2})$`) + id = strings.TrimSpace(id) - return s + matches := re.FindStringSubmatch(id) + if len(matches) != 4 { + return "" + } + + prefix := strings.ToUpper(matches[1]) + number := matches[2] + matches[3] + + return prefix + "-" + number } func renameGameFile(filePath, gameName string) (string, error) { @@ -67,4 +83,3 @@ func renameGameFile(filePath, gameName string) (string, error) { return newPath, nil } -