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)
}
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")
}
func normalizeGameID(id string) string {
s := strings.ReplaceAll(id, "_", "-")
s = strings.ReplaceAll(s, ".", "")
s = strings.ToUpper(s)
return id, nil
}
return s
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 {
re := regexp.MustCompile(`(?i)^([A-Z]{4})[^0-9]*([0-9]{3})[^0-9]*([0-9]{2})$`)
id = strings.TrimSpace(id)
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
}