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" "sync"
"time" "time"
"github.com/Unbewohnte/OBM/logger" "unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/manager" "unbewohnte/OBM/manager"
"github.com/Unbewohnte/OBM/settings" "unbewohnte/OBM/settings"
"github.com/Unbewohnte/OBM/util" "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 { type result struct {
beatmapName string beatmapName string
numberOfDiffs uint 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 package manager
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"github.com/Unbewohnte/OBM/util" "unbewohnte/OBM/util"
) )
// the main beatmap struct, contains necessary data for functions // the main beatmap struct, contains necessary data for functions
@ -22,10 +21,10 @@ func getSongsDir(baseOsuDir string) (string, error) {
stat, err := os.Stat(songsDir) stat, err := os.Stat(songsDir)
if err != nil { 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() { 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 return songsDir, nil
@ -35,7 +34,7 @@ func getSongsDir(baseOsuDir string) (string, error) {
func getDiffs(path string) ([]string, error) { func getDiffs(path string) ([]string, error) {
files, err := os.ReadDir(path) files, err := os.ReadDir(path)
if err != nil { 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 var diffs []string
@ -65,7 +64,7 @@ func GetBeatmaps(baseOsuDir string) ([]Beatmap, error) {
} }
contents, err := os.ReadDir(songsDir) contents, err := os.ReadDir(songsDir)
if err != nil { 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 var beatmaps []Beatmap

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

@ -1,12 +1,12 @@
package manager package manager
import ( import (
"errors" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/Unbewohnte/OBM/util" "unbewohnte/OBM/util"
) )
// parses given .osu file and returns the filename of its background // 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) // get index of "[Events]" (this is where BG filename is stored)
eventsIndex := strings.Index(beatmapContents, "[Events]") eventsIndex := strings.Index(beatmapContents, "[Events]")
if eventsIndex == -1 { 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, // get index of [TimingPoints] (this tag is right after the previous "[Events]" tag,
// so we can grab the whole "[Events]" tag contents) // so we can grab the whole "[Events]" tag contents)
timingPointsIndex := strings.Index(beatmapContents, "[TimingPoints]") timingPointsIndex := strings.Index(beatmapContents, "[TimingPoints]")
if timingPointsIndex == -1 { 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], ",") contentBetween := strings.Split(beatmapContents[eventsIndex:timingPointsIndex], ",")

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

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

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

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

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

@ -5,8 +5,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/Unbewohnte/OBM/logger" "unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util" "unbewohnte/OBM/util"
) )
// retrieves backgrounds from given beatmap folder (same as in `ReplaceBackgrounds`) and copies them to the retrievement path // 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 ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"os" "os"
"github.com/Unbewohnte/OBM/logger" "unbewohnte/OBM/logger"
"github.com/Unbewohnte/OBM/util" "unbewohnte/OBM/util"
) )
const ( const (
@ -18,7 +17,7 @@ const (
func DoesExist() (bool, error) { func DoesExist() (bool, error) {
files, err := os.ReadDir(".") files, err := os.ReadDir(".")
if err != nil { 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 { for _, file := range files {
@ -42,8 +41,9 @@ func Create() error {
file, err := os.Create(settingsFilename) file, err := os.Create(settingsFilename)
if err != nil { 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 // marshaling default settings
settingsJson, err := json.MarshalIndent(Settings{ settingsJson, err := json.MarshalIndent(Settings{
@ -67,11 +67,10 @@ func Create() error {
Workers: 100, Workers: 100,
}, "", " ") }, "", " ")
if err != nil { 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.Write(settingsJson)
file.Close()
return nil return nil
} }
@ -90,7 +89,7 @@ func Get() Settings {
} }
// if all features are disabled // 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...") logger.LogInfo("No features enabled. Exiting...")
os.Exit(0) 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