Compare commits
3 Commits
79d9d300cb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
4b82c9210f
|
|||
|
d3c277eb7f
|
|||
|
42a1080981
|
12
README.md
Normal file
12
README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# OPL-CLI
|
||||||
|
|
||||||
|
Manage your [OPL](https://github.com/ps2homebrew/Open-PS2-Loader) library from the command line.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
There are 4 subcommands provided:
|
||||||
|
|
||||||
|
- `scan` Scan PS2 ISOs and determine the game ID and name.
|
||||||
|
- `rename` Rename the ISO based on the scanned name.
|
||||||
|
- `art` Download game art like cover and screenshots for the game.
|
||||||
|
- `cfg` Download base configuration for the game.
|
||||||
@@ -30,19 +30,24 @@ type args struct {
|
|||||||
Rename *RenameCmd `arg:"subcommand:rename"`
|
Rename *RenameCmd `arg:"subcommand:rename"`
|
||||||
Art *ArtCmd `arg:"subcommand:art"`
|
Art *ArtCmd `arg:"subcommand:art"`
|
||||||
Cfg *CfgCmd `arg:"subcommand:cfg"`
|
Cfg *CfgCmd `arg:"subcommand:cfg"`
|
||||||
|
GameListLocation string `args:"--gamelist" default:"./ps2-gameslist.txt" help:"Location of game list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
var args args
|
var args args
|
||||||
p := arg.MustParse(&args)
|
p := arg.MustParse(&args)
|
||||||
|
|
||||||
|
config := oplcli.Config{
|
||||||
|
GameListLocation: args.GameListLocation,
|
||||||
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case args.Scan != nil:
|
case args.Scan != nil:
|
||||||
oplcli.Scan(args.Scan.Files, oplcli.Config{})
|
oplcli.Scan(args.Scan.Files, config)
|
||||||
case args.Rename != nil:
|
case args.Rename != nil:
|
||||||
oplcli.Rename(args.Rename.Files, oplcli.Config{})
|
oplcli.Rename(args.Rename.Files, config)
|
||||||
case args.Art != nil:
|
case args.Art != nil:
|
||||||
oplcli.DownloadGameArt(args.Art.Files, args.Art.OutputDirectory, oplcli.Config{})
|
oplcli.DownloadGameArt(args.Art.Files, args.Art.OutputDirectory, config)
|
||||||
case args.Cfg != nil:
|
case args.Cfg != nil:
|
||||||
if args.Cfg.Override && args.Cfg.Append {
|
if args.Cfg.Override && args.Cfg.Append {
|
||||||
p.FailSubcommand("Can only append or override cfg files", "cfg")
|
p.FailSubcommand("Can only append or override cfg files", "cfg")
|
||||||
@@ -58,7 +63,7 @@ func Run() {
|
|||||||
mode = oplcli.ModeOverride
|
mode = oplcli.ModeOverride
|
||||||
}
|
}
|
||||||
|
|
||||||
oplcli.DownloadCfg(args.Cfg.Files, args.Cfg.OutputDirectory, mode, oplcli.Config{})
|
oplcli.DownloadCfg(args.Cfg.Files, args.Cfg.OutputDirectory, mode, config)
|
||||||
default:
|
default:
|
||||||
p.Fail("Must specify command")
|
p.Fail("Must specify command")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package oplcli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -38,12 +39,21 @@ func getArtUrlForGameFromGithub(gameID string) artUrls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadArtForGame(gameID string, targetDir string) error {
|
func downloadArtForGame(gameID string, targetDir string) {
|
||||||
|
|
||||||
urls := getArtUrlForGameFromGithub(gameID)
|
urls := getArtUrlForGameFromGithub(gameID)
|
||||||
|
|
||||||
dl := func(url, kind string) (bool, error) {
|
totalDownloads := 0
|
||||||
return downloadFile(url, path.Join(targetDir, fmt.Sprintf("%s_%s.png", gameID, kind)))
|
|
||||||
|
dl := func(url, kind string) {
|
||||||
|
file := path.Join(targetDir, fmt.Sprintf("%s_%s.png", gameID, kind))
|
||||||
|
found, err := downloadFile(url, file)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("Failed to download art file", "gameID", gameID, "url", url)
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
slog.Debug("Downloaded art file", "gameID", gameID, "kind", kind, "target", file)
|
||||||
|
totalDownloads++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dl(urls.cover, "COV")
|
dl(urls.cover, "COV")
|
||||||
@@ -54,7 +64,11 @@ func downloadArtForGame(gameID string, targetDir string) error {
|
|||||||
dl(urls.screenshot2, "SRC2")
|
dl(urls.screenshot2, "SRC2")
|
||||||
dl(urls.spine, "LAB")
|
dl(urls.spine, "LAB")
|
||||||
|
|
||||||
return nil
|
if totalDownloads == 0 {
|
||||||
|
slog.Error("No art files found", "gameID", gameID)
|
||||||
|
} else {
|
||||||
|
slog.Info("Finished downloading art files", "gameID", gameID, "total", totalDownloads)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadFile(url, target string) (bool, error) {
|
func downloadFile(url, target string) (bool, error) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package oplcli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@@ -23,18 +24,21 @@ func downloadCfgForGame(gameID string, output string, mode FileMode) error {
|
|||||||
|
|
||||||
file, err := openFileBasedOnMode(filePath, mode)
|
file, err := openFileBasedOnMode(filePath, mode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("Failed to open cfg file", "file", filePath, "gameID", gameID, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("Failed to download cfg file", "file", filePath, "gameID", gameID, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
_, err = io.Copy(file, resp.Body)
|
_, err = io.Copy(file, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("Failed to write cfg file", "file", filePath, "gameID", gameID, "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package oplcli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -11,7 +12,8 @@ import (
|
|||||||
func scanGameFileForID(filename string) (string, error) {
|
func scanGameFileForID(filename string) (string, error) {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to open file: %w", err)
|
slog.Error("Failed to open file", "file", filename, "err", err)
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
@@ -20,11 +22,13 @@ func scanGameFileForID(filename string) (string, error) {
|
|||||||
|
|
||||||
n, err := file.Read(buffer)
|
n, err := file.Read(buffer)
|
||||||
if err != nil && n == 0 {
|
if err != nil && n == 0 {
|
||||||
return "", fmt.Errorf("failed to read file: %w", err)
|
slog.Error("Failed to read file", "file", file, "err", err)
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
id, found := scanBufferForID(buffer[:n])
|
id, found := scanBufferForID(buffer[:n])
|
||||||
if !found {
|
if !found {
|
||||||
|
slog.Error("No PS2 ID found in the file", "file", file)
|
||||||
return "", fmt.Errorf("no PS2 game ID found in file")
|
return "", fmt.Errorf("no PS2 game ID found in file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package oplcli
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -16,15 +17,16 @@ func loadGamelist(filepath string) (map[string]string, error) {
|
|||||||
|
|
||||||
gameList := make(map[string]string)
|
gameList := make(map[string]string)
|
||||||
|
|
||||||
|
lineNum := 0
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
lineNum++
|
||||||
|
|
||||||
substrings := strings.SplitN(line, " ", 2)
|
substrings := strings.SplitN(line, " ", 2)
|
||||||
|
|
||||||
if len(substrings) != 2 {
|
if len(substrings) != 2 {
|
||||||
// TODO: something in the gamelist file is not formatted right
|
slog.Warn("Line in gamelist is malformed", "line", lineNum)
|
||||||
fmt.Printf("Malformed: %s\n", line)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,77 +1,78 @@
|
|||||||
package oplcli
|
package oplcli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
GameListLocation string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Scan(files []string, config Config) {
|
func Scan(files []string, config Config) {
|
||||||
gameList, err := loadGamelist("./ps2-gameslist.txt")
|
gameList, err := loadGamelist(config.GameListLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load game list: %v", err)
|
slog.Error("Failed to load game list", "file", config.GameListLocation)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Printf("Scanning file: %s\n", file)
|
slog.Info("Scanning file", "file", file)
|
||||||
gameID, err := scanGameFileForID(file)
|
gameID, err := scanGameFileForID(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to scan game file: %s", err)
|
slog.Error("Failed to scan game file", "file", file)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Found game ID: %s\n", gameID)
|
slog.Info("Found game ID", "id", gameID)
|
||||||
|
|
||||||
gameName := gameList[gameID]
|
gameName := gameList[gameID]
|
||||||
fmt.Printf("Found game name: %s\n", gameName)
|
slog.Info("Found game name", "name", gameName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Rename(files []string, config Config) {
|
func Rename(files []string, config Config) {
|
||||||
gameList, err := loadGamelist("./ps2-gameslist.txt")
|
gameList, err := loadGamelist(config.GameListLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to load game list: %v", err)
|
slog.Error("Failed to load game list", "file", config.GameListLocation)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Printf("Scanning file: %s\n", file)
|
slog.Info("Scanning file", "file", file)
|
||||||
|
|
||||||
gameID, err := scanGameFileForID(file)
|
gameID, err := scanGameFileForID(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to scan game file: %s", err)
|
slog.Error("Failed to scan game file", "file", file)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Found game ID: %s\n", gameID)
|
slog.Info("Found game ID", "id", gameID)
|
||||||
|
|
||||||
gameName := gameList[gameID]
|
gameName := gameList[gameID]
|
||||||
fmt.Printf("Found game name: %s\n", gameName)
|
slog.Info("Found game name", "name", gameName)
|
||||||
|
|
||||||
newPath, err := renameGameFile(file, gameName)
|
newPath, err := renameGameFile(file, gameName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to rename file: %v", err)
|
slog.Error("Failed to reanme file", "file", file, "gameName", gameName, "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Renamed %s to %s", file, newPath)
|
slog.Info("Renamed file", "file", file, "to", newPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DownloadGameArt(files []string, output string, config Config) {
|
func DownloadGameArt(files []string, output string, config Config) {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Printf("Scanning file: %s\n", file)
|
slog.Info("Scanning file", "file", file)
|
||||||
|
|
||||||
gameID, err := scanGameFileForID(file)
|
gameID, err := scanGameFileForID(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to scan game file: %s", err)
|
slog.Error("Failed to scan game file", "file", file)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Found game ID: %s\n", gameID)
|
slog.Info("Found game ID", "id", gameID)
|
||||||
|
|
||||||
downloadArtForGame(gameID, output)
|
downloadArtForGame(gameID, output)
|
||||||
}
|
}
|
||||||
@@ -79,15 +80,15 @@ func DownloadGameArt(files []string, output string, config Config) {
|
|||||||
|
|
||||||
func DownloadCfg(files []string, output string, mode FileMode, config Config) {
|
func DownloadCfg(files []string, output string, mode FileMode, config Config) {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
fmt.Printf("Scanning file: %s\n", file)
|
slog.Info("Scanning file", "file", file)
|
||||||
|
|
||||||
gameID, err := scanGameFileForID(file)
|
gameID, err := scanGameFileForID(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to scan game file: %s", err)
|
slog.Error("Failed to scan game file", "file", file)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Found game ID: %s\n", gameID)
|
slog.Info("Found game ID", "id", gameID)
|
||||||
|
|
||||||
downloadCfgForGame(gameID, output, mode)
|
downloadCfgForGame(gameID, output, mode)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user