Skip to content

Commit

Permalink
Refactor code and added multidisc game handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ncirocco committed May 14, 2020
1 parent 31c7205 commit 643f866
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
/output
main
main.exe
/bin/*
/build/*
/ISOs
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build

build:
go build -o bin/psioHelperLinux main.go && GOOS=windows GOARCH=386 go build -o bin/psioHelper.exe main.go && GOOS=darwin GOARCH=amd64 go build -o bin/psioHelperMac main.go
go build -o build/psioHelperLinux main.go && GOOS=windows GOARCH=386 go build -o build/psioHelper.exe main.go && GOOS=darwin GOARCH=amd64 go build -o build/psioHelperMac main.go
32 changes: 32 additions & 0 deletions bin/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bin

import (
"fmt"
"log"
"path/filepath"

"github.com/ncirocco/psio-helper/files"
binmerge "github.com/ncirocco/psx-bin-merge"
)

//Merge merges the bin files in the origin directory and outputs them in the destination directory
func Merge(originDir string, destinationDir string) error {
cueFiles, err := files.GetFilesByExtension(originDir, files.CueExtension)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Attempting to merge bins for %d found CUE sheets\n\n", len(cueFiles))

for _, cue := range cueFiles {
fmt.Printf("Merging bin for %s\n", filepath.Base(cue))
err := binmerge.Merge(cue, destinationDir)
if err != nil {
fmt.Printf("Error processing %s. Message: %s\n", cue, err)
}
}

fmt.Print("\n\n")

return nil
}
Empty file added build/.gitkeep
Empty file.
Binary file added build/psioHelperLinux
Binary file not shown.
Binary file added build/psioHelperMac
Binary file not shown.
37 changes: 37 additions & 0 deletions cu2/cu2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cu2

import (
"fmt"
"log"
"os"
"path/filepath"

cuetocu2 "github.com/ncirocco/cue-to-cu2"
"github.com/ncirocco/psio-helper/files"
)

//Generate creates the cu2 files for all the found cue in the given directory
func Generate(dir string, removeCue bool) error {
cueFiles, err := files.GetFilesByExtension(dir, files.CueExtension)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Attempting to generate CU2 sheets for %d found CUE sheets\n\n", len(cueFiles))

for _, cue := range cueFiles {
fmt.Printf("Generating cu2 for %s\n", filepath.Base(cue))
err := cuetocu2.Generate(cue, filepath.Dir(cue))
if err != nil {
fmt.Printf("Error processing %s. Message: %s\n", cue, err)
}

if removeCue {
os.Remove(cue)
}
}

fmt.Print("\n\n")

return nil
}
88 changes: 88 additions & 0 deletions files/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package files

import (
"fmt"
"os"
"path/filepath"
"strings"
)

//Cu2Extension cu2
const Cu2Extension string = "cu2"

//CueExtension cue
const CueExtension string = "cue"

//BinExtension bin
const BinExtension string = "bin"

const fileNameMaxLength int = 60

//GetFilesByExtension returns a list of the existing files with the given extension in the given directory
func GetFilesByExtension(path string, extension string) ([]string, error) {
var files []string
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if strings.ToLower(filepath.Ext(path)) == "."+extension {
files = append(files, path)
}

return nil
})

return files, err
}

// HasFilesWithExtension returns true if the directory has files with the given extension
func HasFilesWithExtension(path string, extension string) (bool, error) {
var files []string
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if strings.ToLower(filepath.Ext(path)) == "."+extension {
files = append(files, path)
}

return nil
})

return len(files) > 0, err
}

// TrimNames shortens the file names to the PSIO limit of 60 chars.
func TrimNames(dir string) error {
err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

ext := filepath.Ext(path)
if (ext != "."+BinExtension && ext != "."+CueExtension && ext != "."+Cu2Extension) || len(filepath.Base(path)) <= fileNameMaxLength {
return nil
}

fmt.Printf("File name for %s is longer than 60, trimming it.\n", filepath.Base(path))

newName := filepath.Join(filepath.Dir(path), filepath.Base(path)[:fileNameMaxLength-len(ext)]+ext)

if _, err := os.Stat(newName); err == nil {
fmt.Printf("can't rename %s. please rename it manually", filepath.Base(path))

return nil
}

os.Rename(path, newName)

return nil
})

return err
}
68 changes: 68 additions & 0 deletions images/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package images

import (
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"

"github.com/ncirocco/psio-helper/files"
psxserialnumber "github.com/ncirocco/psx-serial-number"
)

const imagesEndpoint string = "https://ncirocco.github.io/PSIO-Library/images/covers_by_id/%s.bmp"

// GetImages downloads the images for the given directory
func GetImages(dir string) error {
bins, err := files.GetFilesByExtension(dir, files.BinExtension)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Attempting to download covers for %d found discs\n\n", len(bins))

for _, bin := range bins {
fmt.Printf("Downloading image for %s\n", filepath.Base(bin))
serial, err := psxserialnumber.GetSerial(bin)
if err != nil {
fmt.Println(err)
continue
}

err = downloadFile(serial, filepath.Dir(bin))
if err != nil {
fmt.Printf("%s for %s - serial %s\n", err, bin, serial)
continue
}
}

fmt.Print("\n\n")

return nil
}

func downloadFile(code string, dir string) error {
resp, err := http.Get(fmt.Sprintf(imagesEndpoint, strings.ToLower(code)))
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return errors.New("Image not found")
}

out, err := os.Create(filepath.Join(dir, path.Base(resp.Request.URL.String())))
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}
Loading

0 comments on commit 643f866

Please sign in to comment.