initial commit
This commit is contained in:
70
internal/gamefile.go
Normal file
70
internal/gamefile.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package oplcli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func scanGameFileForID(filename string) (string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
const bufferSize = 1024 * 1024 // 1MB
|
||||
buffer := make([]byte, bufferSize)
|
||||
|
||||
n, err := file.Read(buffer)
|
||||
if err != nil && n == 0 {
|
||||
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
|
||||
}
|
||||
|
||||
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 s
|
||||
}
|
||||
|
||||
func renameGameFile(filePath, gameName string) (string, error) {
|
||||
ext := filepath.Ext(filePath)
|
||||
dir := filepath.Dir(filePath)
|
||||
|
||||
newPath := filepath.Join(dir, gameName+ext)
|
||||
|
||||
if filePath == newPath {
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
// Do not overwrite existing files
|
||||
if _, err := os.Stat(newPath); err == nil {
|
||||
return "", fmt.Errorf("file already exists: %s", newPath)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return "", fmt.Errorf("failed to check destination: %w", err)
|
||||
}
|
||||
|
||||
// rename file
|
||||
err := os.Rename(filePath, newPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to rename file: %w", err)
|
||||
}
|
||||
|
||||
return newPath, nil
|
||||
}
|
||||
|
||||
38
internal/gamelist.go
Normal file
38
internal/gamelist.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package oplcli
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func loadGamelist(filepath string) (map[string]string, error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
gameList := make(map[string]string)
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
substrings := strings.SplitN(line, " ", 2)
|
||||
|
||||
if len(substrings) != 2 {
|
||||
// TODO: something in the gamelist file is not formatted right
|
||||
fmt.Printf("Malformed: %s\n", line)
|
||||
continue
|
||||
}
|
||||
|
||||
key := substrings[0]
|
||||
value := substrings[1]
|
||||
|
||||
gameList[key] = value
|
||||
}
|
||||
|
||||
return gameList, nil
|
||||
}
|
||||
29
internal/oplcli.go
Normal file
29
internal/oplcli.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package oplcli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
}
|
||||
|
||||
func Scan(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)
|
||||
}
|
||||
|
||||
gameName := gameList[gameID]
|
||||
fmt.Printf("Found game name: %s\n", gameName)
|
||||
}
|
||||
Reference in New Issue
Block a user