Browse Source

Fixed edge-case bug

main 1.1.1
Unbewohnte 4 years ago committed by GitHub
parent
commit
03d5e68e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      backgroundchanger.go

31
backgroundchanger.go

@ -2,6 +2,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt"
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
@ -140,15 +142,18 @@ func getBackgroundName(pathToOSUbeatmap string) (string, error) {
} }
beatmapContents := string(beatmapBytes) beatmapContents := string(beatmapBytes)
// get index of "[Events]" (this is where BG filename is stored)
eventsIndex := strings.Index(beatmapContents, "[Events]") eventsIndex := strings.Index(beatmapContents, "[Events]")
if eventsIndex == -1 { if eventsIndex == -1 {
return "", nil return "", errors.New("Could not retrieve index of \"[Events]\"")
} }
breakPeriodsIndex := strings.Index(beatmapContents, "//Break Periods") // get index of [TimingPoints] (this tag is right after the previous "[Events]" tag,
if eventsIndex == -1 { // so we can grab the whole "[Events]" tag contents)
return "", nil timingPointsIndex := strings.Index(beatmapContents, "[TimingPoints]")
if timingPointsIndex == -1 {
return "", errors.New("Could not retrieve index of \"[TimingPoints]\"")
} }
contentBetween := strings.Split(beatmapContents[eventsIndex:breakPeriodsIndex], ",") contentBetween := strings.Split(beatmapContents[eventsIndex:timingPointsIndex], ",")
for _, chunk := range contentBetween { for _, chunk := range contentBetween {
if isImage(chunk) { if isImage(chunk) {
@ -193,33 +198,35 @@ func replaceBackgrounds(beatmapFolder, replacementPicPath string) (successful, f
filename := file.Name() filename := file.Name()
if isBeatmap(filename) { if isBeatmap(filename) {
beatmap := filename
beatmapBackgroundFilename, err := getBackgroundName(filepath.Join(beatmapFolder, filename)) // getting BG filename
beatmapBackgroundFilename, err := getBackgroundName(filepath.Join(beatmapFolder, beatmap))
if err != nil { if err != nil {
log.Println(fmt.Sprintf("BEATMAP: %s: ERROR: Error getting background filename: %s", beatmap, err))
failed++ failed++
continue continue
} }
if beatmapBackgroundFilename == "" { if beatmapBackgroundFilename == "" {
log.Println("BEATMAP: ", filename, " Could not find beatmap name") log.Println(fmt.Sprintf("BEATMAP: %s Could not find beatmap`s background filename", beatmap))
failed++ failed++
continue continue
} }
backgroundPath := filepath.Join(beatmapFolder, beatmapBackgroundFilename) backgroundPath := filepath.Join(beatmapFolder, beatmapBackgroundFilename)
log.Println("BEATMAP: ", filename, " found background")
// remove old background // remove old background
err = os.Remove(backgroundPath) err = os.Remove(backgroundPath)
if err != nil { if err != nil {
failed++ failed++
log.Println("ERROR: Error removing old background : ", err, " file: ", backgroundPath) log.Println(fmt.Sprintf("BEATMAP: %v: ERROR: Error removing old background : %s", beatmap, err))
} }
// create new background file // create new background file
bgFile, err := os.Create(backgroundPath) bgFile, err := os.Create(backgroundPath)
if err != nil { if err != nil {
failed++ failed++
log.Println("ERROR: Error creating new background file : ", err) log.Println(fmt.Sprintf("BEATMAP: %s: ERROR: Error creating new background file : %s", beatmap, err))
continue continue
} }
defer bgFile.Close() defer bgFile.Close()
@ -227,7 +234,9 @@ func replaceBackgrounds(beatmapFolder, replacementPicPath string) (successful, f
// copy the contents of a given image to the newly created bg file // copy the contents of a given image to the newly created bg file
err = copyFile(replacementPicPath, backgroundPath) err = copyFile(replacementPicPath, backgroundPath)
if err != nil { if err != nil {
log.Println(fmt.Sprintf("BEATMAP: %s: ERROR: Error copying file: %s", beatmap, err))
failed++ failed++
continue
} }
successful++ successful++
} }
@ -308,7 +317,7 @@ func main() {
} }
log.Printf("Found %d song folders", len(songPaths)) log.Printf("Found %d song folders", len(songPaths))
// check if there are less jobs than workers // check if there is less job than workers
if int(settings.MaxWorkers) > len(songPaths) { if int(settings.MaxWorkers) > len(songPaths) {
settings.MaxWorkers = uint(len(songPaths)) settings.MaxWorkers = uint(len(songPaths))
} }

Loading…
Cancel
Save