Compare commits

...

4 Commits

  1. 3
      .gitignore
  2. 2
      Makefile
  3. 16
      flags.go
  4. 3
      go.mod
  5. 64
      src/OBM.go
  6. 3
      src/go.mod
  7. 0
      src/logger/logger.go
  8. 11
      src/manager/beatmap.go
  9. 8
      src/manager/parse.go
  10. 2
      src/manager/remove.go
  11. 4
      src/manager/replace.go
  12. 4
      src/manager/retrieve.go
  13. 0
      src/manager/search.go
  14. 15
      src/settings/settings.go
  15. 0
      src/settings/structure.go
  16. 0
      src/util/background.go
  17. 0
      src/util/checks.go
  18. 0
      src/util/copy.go
  19. 52
      worker.go

3
.gitignore vendored

@ -0,0 +1,3 @@
settings.json
OBM
logs

2
Makefile

@ -0,0 +1,2 @@
all:
cd src && go build && mv OBM ..

16
flags.go

@ -1,16 +0,0 @@
package main
import (
"flag"
)
var (
orderMessage string = "Order: Replace -> Retrieve -> Remove\n"
)
var (
cmdlnBeatmap = flag.String("beatmap", "", `Specifies a certain beatmap.
If set to non "" - the program will search for given name and perform the magic
provided in settings if successful`)
showOrder = flag.Bool("showOrder", false, "Prints an order in which functions are performed on a beatmap")
)

3
go.mod

@ -1,3 +0,0 @@
module github.com/Unbewohnte/OBM
go 1.16

64
OBM.go → src/OBM.go

@ -7,12 +7,68 @@ import (
"sync"
"time"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/manager"
"github.com/Unbewohnte/OBM/settings"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/logger"
"unbewohnte/OBM/manager"
"unbewohnte/OBM/settings"
"unbewohnte/OBM/util"
)
const orderMessage string = "Order: Replace -> Retrieve -> Remove\n"
var (
cmdlnBeatmap = flag.String("beatmap", "", `Specifies a certain beatmap.
If set to non "" - the program will search for given name and perform the magic
provided in settings if successful`)
showOrder = flag.Bool("showOrder", false, "Prints an order in which functions are performed on a beatmap")
)
// a basic implementation of a concurrent worker
func worker(jobs <-chan job, results chan result, WG *sync.WaitGroup) {
defer WG.Done()
for job := range jobs {
var successful, failed uint = 0, 0
// the order is: Replace->Retrieve->Remove (if all 3 options are enabled)
if job.replacementImagePath != "" {
s, f := job.beatmap.ReplaceBackgrounds(job.replacementImagePath)
successful += s
failed += f
}
if job.retrievementPath != "" {
s, f := job.beatmap.RetrieveBackgrounds(job.retrievementPath)
successful += s
failed += f
}
if job.remove == true {
s, f := job.beatmap.RemoveBackgrounds()
successful += s
failed += f
}
results <- result{
beatmapName: job.beatmap.Name,
numberOfDiffs: uint(len(job.beatmap.Diffs)),
successful: successful,
failed: failed,
}
}
}
// the `starter` that `glues` workers and jobs together
func workerPool(jobs chan job, results chan result, numOfWorkers int, WG *sync.WaitGroup) {
// check if there are less jobs than workers
if numOfWorkers > len(jobs) {
numOfWorkers = len(jobs)
}
// replacing backgrounds for each beatmap concurrently
for i := 0; i < numOfWorkers; i++ {
WG.Add(1)
go worker(jobs, results, WG)
}
}
type result struct {
beatmapName string
numberOfDiffs uint

3
src/go.mod

@ -0,0 +1,3 @@
module unbewohnte/OBM
go 1.16

0
logger/logger.go → src/logger/logger.go

11
manager/beatmap.go → src/manager/beatmap.go

@ -1,12 +1,11 @@
package manager
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/util"
)
// the main beatmap struct, contains necessary data for functions
@ -22,10 +21,10 @@ func getSongsDir(baseOsuDir string) (string, error) {
stat, err := os.Stat(songsDir)
if err != nil {
return "", errors.New(fmt.Sprintf("Could not process the given path : %s", err))
return "", fmt.Errorf("could not process the given path : %s", err)
}
if !stat.IsDir() {
return "", errors.New("Given Osu! directory is not a directory !")
return "", fmt.Errorf("given Osu! directory is not a directory")
}
return songsDir, nil
@ -35,7 +34,7 @@ func getSongsDir(baseOsuDir string) (string, error) {
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))
return nil, fmt.Errorf("could not read a directory : %s", err)
}
var diffs []string
@ -65,7 +64,7 @@ func GetBeatmaps(baseOsuDir string) ([]Beatmap, error) {
}
contents, err := os.ReadDir(songsDir)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not read a directory : %s", err))
return nil, fmt.Errorf("could not read a directory : %s", err)
}
var beatmaps []Beatmap

8
manager/parse.go → src/manager/parse.go

@ -1,12 +1,12 @@
package manager
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/util"
)
// parses given .osu file and returns the filename of its background
@ -24,13 +24,13 @@ func (BEATMAP *Beatmap) GetBackgroundName(diff string) (string, error) {
// 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]\"")
return "", fmt.Errorf("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]\"")
return "", fmt.Errorf("could not retrieve index of \"[TimingPoints]\"")
}
contentBetween := strings.Split(beatmapContents[eventsIndex:timingPointsIndex], ",")

2
manager/remove.go → src/manager/remove.go

@ -5,7 +5,7 @@ import (
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/logger"
"unbewohnte/OBM/logger"
)
// parses each difficulty for background info, removes found backgrounds

4
manager/replace.go → src/manager/replace.go

@ -5,8 +5,8 @@ import (
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/logger"
"unbewohnte/OBM/util"
)
// parses each beatmap`s .osu file for background info;

4
manager/retrieve.go → src/manager/retrieve.go

@ -5,8 +5,8 @@ import (
"os"
"path/filepath"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/logger"
"unbewohnte/OBM/util"
)
// retrieves backgrounds from given beatmap folder (same as in `ReplaceBackgrounds`) and copies them to the retrievement path

0
manager/search.go → src/manager/search.go

15
settings/settings.go → src/settings/settings.go

@ -2,12 +2,11 @@ package settings
import (
"encoding/json"
"errors"
"fmt"
"os"
"github.com/Unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util"
"unbewohnte/OBM/logger"
"unbewohnte/OBM/util"
)
const (
@ -18,7 +17,7 @@ const (
func DoesExist() (bool, error) {
files, err := os.ReadDir(".")
if err != nil {
return false, errors.New(fmt.Sprintf("Unable to read current directory %s", err))
return false, fmt.Errorf("wasn`t able to read current directory %s", err)
}
for _, file := range files {
@ -42,8 +41,9 @@ func Create() error {
file, err := os.Create(settingsFilename)
if err != nil {
return errors.New(fmt.Sprintf("Unable to create settings file : %s", err))
return fmt.Errorf("could not create settings file : %s", err)
}
defer file.Close()
// marshaling default settings
settingsJson, err := json.MarshalIndent(Settings{
@ -67,11 +67,10 @@ func Create() error {
Workers: 100,
}, "", " ")
if err != nil {
return errors.New(fmt.Sprintf("Could not marshal settings into file : %s", err))
return fmt.Errorf("could not marshal settings into file : %s", err)
}
file.Write(settingsJson)
file.Close()
return nil
}
@ -90,7 +89,7 @@ func Get() Settings {
}
// if all features are disabled
if !settings.BackgroundReplacement.Enabled && !settings.BackgroundRetrievement.Enabled {
if !settings.BackgroundReplacement.Enabled && !settings.BackgroundRetrievement.Enabled && !settings.BackgroundRemovement.Enabled {
logger.LogInfo("No features enabled. Exiting...")
os.Exit(0)
}

0
settings/structure.go → src/settings/structure.go

0
util/background.go → src/util/background.go

0
util/checks.go → src/util/checks.go

0
util/copy.go → src/util/copy.go

52
worker.go

@ -1,52 +0,0 @@
package main
import (
"sync"
)
// a basic implementation of a concurrent worker
func worker(jobs <-chan job, results chan result, WG *sync.WaitGroup) {
defer WG.Done()
for job := range jobs {
var successful, failed uint = 0, 0
// the order is: Replace->Retrieve->Remove (if all 3 options are enabled)
if job.replacementImagePath != "" {
s, f := job.beatmap.ReplaceBackgrounds(job.replacementImagePath)
successful += s
failed += f
}
if job.retrievementPath != "" {
s, f := job.beatmap.RetrieveBackgrounds(job.retrievementPath)
successful += s
failed += f
}
if job.remove == true {
s, f := job.beatmap.RemoveBackgrounds()
successful += s
failed += f
}
results <- result{
beatmapName: job.beatmap.Name,
numberOfDiffs: uint(len(job.beatmap.Diffs)),
successful: successful,
failed: failed,
}
}
}
// the `starter` that `glues` workers and jobs together
func workerPool(jobs chan job, results chan result, numOfWorkers int, WG *sync.WaitGroup) {
// check if there are less jobs than workers
if numOfWorkers > len(jobs) {
numOfWorkers = len(jobs)
}
// replacing backgrounds for each beatmap concurrently
for i := 0; i < numOfWorkers; i++ {
WG.Add(1)
go worker(jobs, results, WG)
}
}
Loading…
Cancel
Save