Browse Source

Add files via upload

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

70
backgroundchanger.go

@ -70,7 +70,7 @@ func createSettingsFile() {
file.Write(jsonEncodedSettings) file.Write(jsonEncodedSettings)
file.Close() file.Close()
log.Println("Successfully created new settings file") log.Println("Successfully created new settingsFile")
} }
// filepath.Joins the main osu directory with its songs folder // filepath.Joins the main osu directory with its songs folder
@ -113,50 +113,69 @@ func isBeatmap(filename string) bool {
return false return false
} }
func isImage(filename string) bool {
var imageExtentions []string = []string{"jpeg", "jpg", "png", "JPEG", "JPG", "PNG"}
for _, extention := range imageExtentions {
if strings.Contains(filename, extention) {
return true
}
}
return false
}
// parses .osu file and returns the filename of its background // parses .osu file and returns the filename of its background
func getBackgroundName(pathToOSUbeatmap string) string { func getBackgroundName(pathToOSUbeatmap string) (string, error) {
beatmapBytes, err := os.ReadFile(pathToOSUbeatmap) beatmapBytes, err := os.ReadFile(pathToOSUbeatmap)
if err != nil { if err != nil {
log.Println("ERROR: Error reading beatmap file : ", err) return "", err
} }
beatmapContents := string(beatmapBytes) beatmapContents := string(beatmapBytes)
eventsIndex := strings.Index(beatmapContents, "[Events]") eventsIndex := strings.Index(beatmapContents, "[Events]")
if eventsIndex == -1 { if eventsIndex == -1 {
return "" return "", nil
} }
breakPeriodsIndex := strings.Index(beatmapContents, "//Break Periods") breakPeriodsIndex := strings.Index(beatmapContents, "//Break Periods")
if eventsIndex == -1 { if eventsIndex == -1 {
return "" return "", nil
} }
beatmapBackground := strings.Split(beatmapContents[eventsIndex:breakPeriodsIndex], ",")[2] contentBetween := strings.Split(beatmapContents[eventsIndex:breakPeriodsIndex], ",")
return beatmapBackground for _, chunk := range contentBetween {
if isImage(chunk) {
return chunk[1 : len(chunk)-1], nil
}
}
return "", nil
} }
// opens given files, copies one into another // opens given files, copies one into another
func copyFile(src, dst string) { func copyFile(src, dst string) error {
srcFile, err := os.Open(src) srcFile, err := os.Open(src)
if err != nil { if err != nil {
log.Println("ERROR: ", err) log.Println("ERROR: ", err)
return err
} }
defer srcFile.Close() defer srcFile.Close()
dstFile, err := os.OpenFile(dst, os.O_WRONLY, os.ModePerm) dstFile, err := os.OpenFile(dst, os.O_WRONLY, os.ModePerm)
if err != nil { if err != nil {
log.Println("ERROR: ", err) log.Println("ERROR: ", err)
return err
} }
defer dstFile.Close() defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile) _, err = io.Copy(dstFile, srcFile)
if err != nil { if err != nil {
log.Println("ERROR: Error copying files : ", err) log.Println("ERROR: Error copying files : ", err)
return err
} }
return nil
} }
// reads contents of given dir; searches for .osu files; parses them for background info; // reads contents of given dir; searches for .osu files; parses them for background info;
// removes original background and replaces it with copied version of given image // removes original background and replaces it with copied version of given image
func replaceBackgrounds(beatmapFolder, replacementPicPath string) { func replaceBackgrounds(beatmapFolder, replacementPicPath string) (successful, failed uint64) {
files, err := os.ReadDir(beatmapFolder) files, err := os.ReadDir(beatmapFolder)
if err != nil { if err != nil {
log.Fatal("ERROR: Wrong path : ", err) log.Fatal("ERROR: Wrong path : ", err)
@ -166,29 +185,46 @@ func replaceBackgrounds(beatmapFolder, replacementPicPath string) {
if isBeatmap(filename) { if isBeatmap(filename) {
beatmapBackgroundFilename := strings.Split(getBackgroundName(filepath.Join(beatmapFolder, filename)), "\"")[1] beatmapBackgroundFilename, err := getBackgroundName(filepath.Join(beatmapFolder, filename))
if err != nil {
failed++
continue
}
if beatmapBackgroundFilename == "" { if beatmapBackgroundFilename == "" {
log.Println("BEATMAP: ", filename, " Could not find beatmap name")
failed++
continue continue
} }
backgroundPath := filepath.Join(beatmapFolder, beatmapBackgroundFilename) backgroundPath := filepath.Join(beatmapFolder, beatmapBackgroundFilename)
log.Println(backgroundPath) log.Println("BEATMAP: ", filename, " found background")
// remove old background // remove old background
os.Remove(backgroundPath) err = os.Remove(backgroundPath)
if err != nil {
failed++
log.Println("ERROR: Error removing old background : ", err, " file: ", backgroundPath)
}
// 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++
log.Println("ERROR: Error creating new background file : ", err) log.Println("ERROR: Error creating new background file : ", err)
continue
} }
bgFile.Close() defer bgFile.Close()
// 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
copyFile(replacementPicPath, backgroundPath) err = copyFile(replacementPicPath, backgroundPath)
if err != nil {
failed++
}
successful++
} }
} }
return successful, failed
} }
// creates a complete black image file // creates a complete black image file
@ -252,8 +288,12 @@ func main() {
log.Printf("Found %d song folders", len(songPaths)) log.Printf("Found %d song folders", len(songPaths))
// replacing backgrounds for each beatmap // replacing backgrounds for each beatmap
var successful, failed uint64 = 0, 0
for _, songPath := range songPaths { for _, songPath := range songPaths {
replaceBackgrounds(songPath, replacementImage) s, f := replaceBackgrounds(songPath, replacementImage)
successful += s
failed += f
} }
log.Printf("\n\nDONE. %d successful; %d failed", successful, failed)
} }

Loading…
Cancel
Save