improved game scanning
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user