Compare commits
3 Commits
8e83c99620
...
08d2d528c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
08d2d528c7
|
|||
|
a9588b079a
|
|||
|
d826646a5a
|
@@ -9,8 +9,13 @@ type ScanCmd struct {
|
|||||||
File string `arg:"positional"`
|
File string `arg:"positional"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RenameCmd struct {
|
||||||
|
File string `arg:"positional"`
|
||||||
|
}
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
Scan *ScanCmd `arg:"subcommand:scan"`
|
Scan *ScanCmd `arg:"subcommand:scan"`
|
||||||
|
Rename *RenameCmd `arg:"subcommand:rename"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
@@ -20,5 +25,7 @@ func Run() {
|
|||||||
switch {
|
switch {
|
||||||
case args.Scan != nil:
|
case args.Scan != nil:
|
||||||
oplcli.Scan(args.Scan.File, oplcli.Config{})
|
oplcli.Scan(args.Scan.File, oplcli.Config{})
|
||||||
|
case args.Rename != nil:
|
||||||
|
oplcli.Rename(args.Rename.File, oplcli.Config{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
122
internal/gamefile_test.go
Normal file
122
internal/gamefile_test.go
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package oplcli
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestParseCases(t *testing.T) {
|
||||||
|
var testData = []struct {
|
||||||
|
name string
|
||||||
|
content []byte
|
||||||
|
expected string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "SLUS with underscore and dot",
|
||||||
|
content: []byte("Some binary data\x00\x00SLUS_123.45\x00\x00more data"),
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLUS with hyphen",
|
||||||
|
content: []byte("\x00\x00\x00SLUS-12345\x00\x00\x00"),
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLES format",
|
||||||
|
content: []byte("Header data\x00SLES_987.65\x00footer"),
|
||||||
|
expected: "SLES-98765",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SLPS format",
|
||||||
|
content: []byte("\xFF\xFESLPS_456.78\x00"),
|
||||||
|
expected: "SLPS-45678",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "SCUS format",
|
||||||
|
content: []byte("SCUS-97512"),
|
||||||
|
expected: "SCUS-97512",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No separator format",
|
||||||
|
content: []byte("\x00\x00SLUS12345\x00\x00"),
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "With spaces",
|
||||||
|
content: []byte("SLUS 123.45"),
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "No game ID found",
|
||||||
|
content: []byte("Just some random data without any game ID"),
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Invalid game ID format",
|
||||||
|
content: []byte("SLUS-ABC.DE"),
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testData {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
gameID, found := scanBufferForID(tt.content)
|
||||||
|
|
||||||
|
if tt.expected == "" && found {
|
||||||
|
t.Errorf("Found game id where none should be found: %s", gameID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.expected == "" && !found {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt.expected != gameID {
|
||||||
|
t.Errorf("Expected %s got %s", tt.expected, gameID)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalize(t *testing.T) {
|
||||||
|
testData := []struct {
|
||||||
|
input string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "SLUS-12345",
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "SLES_987.65",
|
||||||
|
expected: "SLES-98765",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "SLPS_456.78",
|
||||||
|
expected: "SLPS-45678",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "scus-97512",
|
||||||
|
expected: "SCUS-97512",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "SLUS12345",
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "SLUS123.45",
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "SLUs123.45",
|
||||||
|
expected: "SLUS-12345",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range testData {
|
||||||
|
t.Run(tt.input, func(t *testing.T) {
|
||||||
|
normalized := normalizeGameID(tt.input)
|
||||||
|
|
||||||
|
if normalized != tt.expected {
|
||||||
|
t.Errorf("Expected %s got %s", tt.expected, normalized)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,8 +22,38 @@ func Scan(file string, config Config) {
|
|||||||
gameList, err := loadGamelist("./ps2-gameslist.txt")
|
gameList, err := loadGamelist("./ps2-gameslist.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load game list: %v", err)
|
fmt.Printf("Failed to load game list: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
gameName := gameList[gameID]
|
gameName := gameList[gameID]
|
||||||
fmt.Printf("Found game name: %s\n", gameName)
|
fmt.Printf("Found game name: %s\n", gameName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Rename(file string, config Config) {
|
||||||
|
fmt.Printf("Scanning file: %s\n", file)
|
||||||
|
|
||||||
|
gameID, err := scanGameFileForID(file)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to scan game file: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Found game ID: %s\n", gameID)
|
||||||
|
|
||||||
|
gameList, err := loadGamelist("./ps2-gameslist.txt")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to load game list: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
gameName := gameList[gameID]
|
||||||
|
fmt.Printf("Found game name: %s\n", gameName)
|
||||||
|
|
||||||
|
newPath, err := renameGameFile(file, gameName)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Failed to rename file: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Renamed %s to %s", file, newPath)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user