Browse Source

Delete paths.go

main
Unbewohnte 4 years ago committed by GitHub
parent
commit
587962840a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 82
      manager/paths.go

82
manager/paths.go

@ -1,82 +0,0 @@
package manager
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/util"
)
// 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
}
// checks for .osu files in given path and returns all found instances
func getDiffs(path string) ([]string, error) {
files, err := os.ReadDir(path)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not read a directory : %s", err))
}
var diffs []string
for _, file := range files {
filename := file.Name()
if util.IsBeatmap(filename) {
diffs = append(diffs, filename)
}
}
return diffs, nil
}
// constructs a `Beatmap` struct and returns it
func newBeatmap(name, path string, diffs []string) Beatmap {
return Beatmap{
Name: name,
Path: path,
Diffs: diffs,
}
}
// returns an array of beatmaps from given base Osu! directory
func GetBeatmaps(baseOsuDir string) ([]Beatmap, 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 beatmaps []Beatmap
// looping through all folders in yourOsuDir/Songs/ directory
for _, file := range contents {
if file.IsDir() {
// retrieving all necessary data for creating a new instance of a beatmap
beatmapName := file.Name()
pathToBeatmap := filepath.Join(songsDir, beatmapName)
diffs, err := getDiffs(pathToBeatmap)
if err != nil {
continue
}
newBeatmap := newBeatmap(beatmapName, pathToBeatmap, diffs)
beatmaps = append(beatmaps, newBeatmap)
}
}
return beatmaps, nil
}
Loading…
Cancel
Save