Unbewohnte
4 years ago
committed by
GitHub
14 changed files with 231 additions and 126 deletions
@ -0,0 +1,43 @@ |
|||||||
|
package manager |
||||||
|
|
||||||
|
import ( |
||||||
|
"errors" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
"strings" |
||||||
|
|
||||||
|
"github.com/Unbewohnte/OBM/util" |
||||||
|
) |
||||||
|
|
||||||
|
// parses given .osu file and returns the filename of its background
|
||||||
|
// NOTE: Osu! beatmap (as whole) can have multiple backgrounds for each .osu file
|
||||||
|
// the perfect example : https://osu.ppy.sh/beatmapsets/43701#osu/137122
|
||||||
|
// this is why this functions asks for a certain difficulty (.osu filename) to be sure
|
||||||
|
// to return the correct background name
|
||||||
|
func (BEATMAP *Beatmap) GetBackgroundName(diff string) (string, error) { |
||||||
|
beatmapBytes, err := os.ReadFile(filepath.Join(BEATMAP.Path, diff)) |
||||||
|
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 |
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
package manager |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
|
||||||
|
"github.com/Unbewohnte/OBM/logger" |
||||||
|
) |
||||||
|
|
||||||
|
// parses each difficulty for background info, removes found backgrounds
|
||||||
|
func (BEATMAP *Beatmap) RemoveBackgrounds() (successful, failed uint) { |
||||||
|
// looping through each .osu file of a beatmap
|
||||||
|
for _, diff := range BEATMAP.Diffs { |
||||||
|
background, err := BEATMAP.GetBackgroundName(diff) |
||||||
|
if err != nil || background == "" { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Error getting background filename: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
// remove background
|
||||||
|
err = os.Remove(filepath.Join(BEATMAP.Path, background)) |
||||||
|
if err != nil { |
||||||
|
// background file does not exist (success ???)
|
||||||
|
successful++ |
||||||
|
continue |
||||||
|
} |
||||||
|
successful++ |
||||||
|
} |
||||||
|
return successful, failed |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
package manager |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"os" |
||||||
|
"path/filepath" |
||||||
|
|
||||||
|
"github.com/Unbewohnte/OBM/logger" |
||||||
|
"github.com/Unbewohnte/OBM/util" |
||||||
|
) |
||||||
|
|
||||||
|
// parses each beatmap`s .osu file for background info;
|
||||||
|
// removes original background and replaces it with copied version of given image
|
||||||
|
func (BEATMAP *Beatmap) ReplaceBackgrounds(replacementImgPath string) (successful, failed uint) { |
||||||
|
// looping through each .osu file of a beatmap
|
||||||
|
for _, diff := range BEATMAP.Diffs { |
||||||
|
background, err := BEATMAP.GetBackgroundName(diff) |
||||||
|
if err != nil || background == "" { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Error getting background filename: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
// remove old bg (if there is no background file - no need to worry)
|
||||||
|
os.Remove(filepath.Join(BEATMAP.Path, background)) |
||||||
|
|
||||||
|
// copy given picture, thus replacing background
|
||||||
|
err = util.CopyFile(replacementImgPath, filepath.Join(BEATMAP.Path, background)) |
||||||
|
if err != nil { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Could not copy: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
successful++ |
||||||
|
} |
||||||
|
return successful, failed |
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
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 (BEATMAP *Beatmap) RetrieveBackgrounds(retrievementPath string) (successful, failed uint) { |
||||||
|
// looping through each .osu file of a beatmap
|
||||||
|
for _, diff := range BEATMAP.Diffs { |
||||||
|
background, err := BEATMAP.GetBackgroundName(diff) |
||||||
|
if err != nil || background == "" { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Error getting background filename: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
// check if the background does exist
|
||||||
|
exists := util.DoesExist(filepath.Join(BEATMAP.Path, background)) |
||||||
|
if !exists { |
||||||
|
// if not - we cannot copy it, so moving to the next diff
|
||||||
|
logger.LogWarning(fmt.Sprintf("BEATMAP: %s: Background does not exist", diff)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
// creating directory that represents current beatmap
|
||||||
|
dstPath := filepath.Join(retrievementPath, BEATMAP.Name) |
||||||
|
|
||||||
|
err = os.MkdirAll(dstPath, os.ModePerm) |
||||||
|
if err != nil { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Error creating a directory: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
|
||||||
|
// copy background to the `retrievementPath`
|
||||||
|
err = util.CopyFile(filepath.Join(BEATMAP.Path, background), filepath.Join(dstPath, background)) |
||||||
|
if err != nil { |
||||||
|
logger.LogError(false, fmt.Sprintf("BEATMAP: %s: Could not copy: %s", diff, err)) |
||||||
|
failed++ |
||||||
|
continue |
||||||
|
} |
||||||
|
successful++ |
||||||
|
} |
||||||
|
return successful, failed |
||||||
|
} |
@ -0,0 +1,20 @@ |
|||||||
|
package manager |
||||||
|
|
||||||
|
import "strings" |
||||||
|
|
||||||
|
// Search tries to locate instances of beatmaps with the provided name (or part of the name);
|
||||||
|
// returns a slice of found beatmaps and a number of searched beatmaps
|
||||||
|
func Search(beatmaps []Beatmap, name string) ([]Beatmap, uint64) { |
||||||
|
var instances []Beatmap |
||||||
|
var searched uint64 = 0 |
||||||
|
|
||||||
|
// to make the search case-insensitive
|
||||||
|
name = strings.ToLower(name) |
||||||
|
for _, beatmap := range beatmaps { |
||||||
|
if strings.Contains(strings.ToLower(beatmap.Name), name) { |
||||||
|
instances = append(instances, beatmap) |
||||||
|
} |
||||||
|
searched++ |
||||||
|
} |
||||||
|
return instances, searched |
||||||
|
} |
Loading…
Reference in new issue