improved game scanning

This commit is contained in:
2026-01-05 14:49:32 +01:00
parent d826646a5a
commit a9588b079a

View File

@@ -23,23 +23,39 @@ func scanGameFileForID(filename string) (string, error) {
return "", fmt.Errorf("failed to read file: %w", err) return "", fmt.Errorf("failed to read file: %w", err)
} }
pattern := regexp.MustCompile(`(SLUS|SLES|SLPS|SCUS|SCES|SCPS)[_-]?\d{3}[\._]\d{2}`) id, found := scanBufferForID(buffer[:n])
if !found {
match := pattern.Find(buffer[:n]) return "", fmt.Errorf("no PS2 game ID found in file")
if match != nil {
gameID := string(match)
return normalizeGameID(gameID), nil
} }
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 { func normalizeGameID(id string) string {
s := strings.ReplaceAll(id, "_", "-") re := regexp.MustCompile(`(?i)^([A-Z]{4})[^0-9]*([0-9]{3})[^0-9]*([0-9]{2})$`)
s = strings.ReplaceAll(s, ".", "") id = strings.TrimSpace(id)
s = strings.ToUpper(s)
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) { func renameGameFile(filePath, gameName string) (string, error) {
@@ -67,4 +83,3 @@ func renameGameFile(filePath, gameName string) (string, error) {
return newPath, nil return newPath, nil
} }