Browse Source

Delete manager directory

main
Unbewohnte 3 years ago committed by GitHub
parent
commit
75b2731c51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      manager/beatmap.go
  2. 45
      manager/paths.go
  3. 71
      manager/replacer.go
  4. 73
      manager/retriever.go

38
manager/beatmap.go

@ -1,38 +0,0 @@
package manager
import (
"errors"
"os"
"strings"
"github.com/Unbewohnte/OBM/util"
)
// parses given .osu file and returns the filename of its background
func GetBackgroundName(pathTobeatmap string) (string, error) {
beatmapBytes, err := os.ReadFile(pathTobeatmap)
if err != nil {
return "", err
}
beatmapContents := string(beatmapBytes)
// get index of "[Events]" (this is where BG filename is stored)
eventsIndex := strings.Index(beatmapContents, "[Events]")
if eventsIndex == -1 {
return "", errors.New("Could not retrieve index of \"[Events]\"")
}
// get index of [TimingPoints] (this tag is right after the previous "[Events]" tag,
// so we can grab the whole "[Events]" tag contents)
timingPointsIndex := strings.Index(beatmapContents, "[TimingPoints]")
if timingPointsIndex == -1 {
return "", errors.New("Could not retrieve index of \"[TimingPoints]\"")
}
contentBetween := strings.Split(beatmapContents[eventsIndex:timingPointsIndex], ",")
for _, chunk := range contentBetween {
if util.IsImage(chunk) {
return strings.Split(chunk, "\"")[1], nil
}
}
return "", nil
}

45
manager/paths.go

@ -1,45 +0,0 @@
package manager
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// filepath.Joins the main osu directory with its songs folder
func getSongsDir(baseOsuDir string) (string, error) {
songsDir := filepath.Join(baseOsuDir, "Songs")
stat, err := os.Stat(songsDir)
if err != nil {
return "", errors.New(fmt.Sprintf("Could not process the given path : %s", err))
}
if !stat.IsDir() {
return "", errors.New("Given Osu! directory is not a directory !")
}
return songsDir, nil
}
// returns an array of full filepaths to each beatmap from given base Osu! directory
func GetBeatmapFolderPaths(baseOsuDir string) ([]string, error) {
songsDir, err := getSongsDir(baseOsuDir)
if err != nil {
return nil, err
}
contents, err := os.ReadDir(songsDir)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not read a directory : %s", err))
}
var beatmapFolderPaths []string
for _, file := range contents {
if file.IsDir() {
path := filepath.Join(songsDir, file.Name())
beatmapFolderPaths = append(beatmapFolderPaths, path)
}
}
return beatmapFolderPaths, nil
}

71
manager/replacer.go

@ -1,71 +0,0 @@
package manager
import (
"fmt"
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util"
)
// 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
func ReplaceBackgrounds(beatmapFolder, replacementPicPath string) (successful, failed uint64) {
files, err := os.ReadDir(beatmapFolder)
if err != nil {
logger.LogError(true, fmt.Sprintf("Could not read directory : %s", err))
}
for _, file := range files {
filename := file.Name()
// if not a beatmap - skip
if !util.IsBeatmap(filename) {
continue
}
beatmap := filename
// getting BG filename
beatmapBackgroundFilename, err := GetBackgroundName(filepath.Join(beatmapFolder, beatmap))
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Error getting background filename: %s", beatmap, err))
failed++
continue
}
if beatmapBackgroundFilename == "" {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s Could not find background filename in this beatmap file", beatmap))
failed++
continue
}
pathToBackground := filepath.Join(beatmapFolder, beatmapBackgroundFilename)
// remove old background
err = os.Remove(pathToBackground)
if err != nil {
failed++
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not remove old background : %s", beatmap, err))
}
// create new background file
bgFile, err := os.Create(pathToBackground)
if err != nil {
failed++
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not create new background file : %s", beatmap, err))
continue
}
bgFile.Close()
// copy the contents of a given image to the newly created bg file
err = util.CopyFile(replacementPicPath, pathToBackground)
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not copy file: %s", beatmap, err))
failed++
continue
}
successful++
}
return successful, failed
}

73
manager/retriever.go

@ -1,73 +0,0 @@
package manager
import (
"fmt"
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util"
)
// retrieves backgrounds from given beatmap folder (same as in `ReplaceBackgrounds`) and copies them to the retrievement path
func RetrieveBackgrounds(beatmapFolder, retrievementPath string) (successful, failed uint64) {
files, err := os.ReadDir(beatmapFolder)
if err != nil {
logger.LogError(true, fmt.Sprintf("Could not read directory : %s", err))
}
for _, file := range files {
filename := file.Name()
// if not a beatmap - skip
if !util.IsBeatmap(filename) {
continue
}
beatmap := filename
// getting BG filename
beatmapBackgroundFilename, err := GetBackgroundName(filepath.Join(beatmapFolder, beatmap))
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Error getting background filename: %s", beatmap, err))
failed++
continue
}
if beatmapBackgroundFilename == "" {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s Could not find background filename in this beatmap file", beatmap))
failed++
continue
}
pathToBackground := filepath.Join(beatmapFolder, beatmapBackgroundFilename)
// creating a directory with the name of current beatmap folder in the retrievement path
dstPath := filepath.Join(retrievementPath, filepath.Base(beatmapFolder))
err = os.MkdirAll(dstPath, os.ModePerm)
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not create a directory (%s) for copying", beatmap, dstPath))
continue
}
// creating a copy file
fullPathToCopy := filepath.Join(dstPath, beatmapBackgroundFilename)
dstFile, err := os.Create(fullPathToCopy)
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not create a copy file", beatmap))
failed++
continue
}
dstFile.Close()
// copy the background file to the retrievement path
err = util.CopyFile(pathToBackground, fullPathToCopy)
if err != nil {
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Could not copy file: %s", beatmap, err))
failed++
continue
}
successful++
}
return successful, failed
}
Loading…
Cancel
Save