|
|
@ -1,11 +1,7 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"errors" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"image" |
|
|
|
|
|
|
|
"image/color" |
|
|
|
|
|
|
|
"image/png" |
|
|
|
|
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
@ -20,41 +16,40 @@ var ( |
|
|
|
WG sync.WaitGroup |
|
|
|
WG sync.WaitGroup |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
// creates a complete black image file
|
|
|
|
type job struct { |
|
|
|
func createBlackBG(width, height int) error { |
|
|
|
songPath string |
|
|
|
bgfile, err := os.Create("blackBG.png") |
|
|
|
pathToImage string |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
return errors.New(fmt.Sprintf("Could not create black background file : %s", err)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
image := image.NewRGBA(image.Rect(0, 0, width, height)) |
|
|
|
|
|
|
|
bounds := image.Bounds() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for y := 0; y < bounds.Max.Y; y++ { |
|
|
|
type result struct { |
|
|
|
for x := 0; x < bounds.Max.X; x++ { |
|
|
|
successful uint64 |
|
|
|
image.Set(x, y, color.Black) |
|
|
|
failed uint64 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 { |
|
|
|
|
|
|
|
s, f := manager.ReplaceBackgrounds(job.songPath, job.pathToImage) |
|
|
|
|
|
|
|
results <- result{ |
|
|
|
|
|
|
|
successful: s, |
|
|
|
|
|
|
|
failed: f, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
err = png.Encode(bgfile, image) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return errors.New(fmt.Sprintf("Could not encode an image : %s", err)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err = bgfile.Close() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return errors.New(fmt.Sprintf("Could not close the background file : %s", err)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// a basic implementation of a concurrent worker
|
|
|
|
func workerPool(jobs chan job, results chan result, numOfWorkers int, WG *sync.WaitGroup) { |
|
|
|
func worker(paths <-chan string, replacementImage string, successful, failed *uint64, WG *sync.WaitGroup) { |
|
|
|
// check if there are less jobs than workers
|
|
|
|
defer WG.Done() |
|
|
|
if numOfWorkers > len(jobs) { |
|
|
|
for songPath := range paths { |
|
|
|
numOfWorkers = len(jobs) |
|
|
|
s, f := manager.ReplaceBackgrounds(songPath, replacementImage) |
|
|
|
|
|
|
|
*successful += s |
|
|
|
|
|
|
|
*failed += f |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// replacing backgrounds for each beatmap concurrently
|
|
|
|
|
|
|
|
for i := 0; i < numOfWorkers; i++ { |
|
|
|
|
|
|
|
WG.Add(1) |
|
|
|
|
|
|
|
go worker(jobs, results, WG) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
func init() { |
|
|
@ -67,19 +62,19 @@ func init() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// settings file does not exist, so create it and exit
|
|
|
|
// settings file does not exist, so create it and exit (assuming that this is the first run)
|
|
|
|
settings.CreateSettingsFile() |
|
|
|
settings.CreateSettingsFile() |
|
|
|
os.Exit(0) |
|
|
|
os.Exit(0) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
func main() { |
|
|
|
startingTime := time.Now().UTC() |
|
|
|
startingTime := time.Now() |
|
|
|
|
|
|
|
|
|
|
|
settings := settings.GetSettings() |
|
|
|
settings := settings.GetSettings() |
|
|
|
|
|
|
|
|
|
|
|
// process the given settings
|
|
|
|
// processing given settings
|
|
|
|
if settings.CreateBlackBGImage { |
|
|
|
if settings.CreateBlackBGImage { |
|
|
|
err := createBlackBG(1920, 1080) |
|
|
|
err := manager.CreateBlackBG(1920, 1080) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
logger.LogInfo("Successfully created black background") |
|
|
|
logger.LogInfo("Successfully created black background") |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -102,32 +97,34 @@ func main() { |
|
|
|
logger.LogError(true, fmt.Sprintf("Error reading osu songs directory : %s", err.Error())) |
|
|
|
logger.LogError(true, fmt.Sprintf("Error reading osu songs directory : %s", err.Error())) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// storing all paths to each beatmap
|
|
|
|
// creating jobs for workers
|
|
|
|
songPaths := make(chan string, len(osuSongsDirContents)) |
|
|
|
jobs := make(chan job, len(osuSongsDirContents)) |
|
|
|
for _, songDir := range osuSongsDirContents { |
|
|
|
for _, songDir := range osuSongsDirContents { |
|
|
|
if songDir.IsDir() { |
|
|
|
if songDir.IsDir() { |
|
|
|
songPaths <- filepath.Join(osuSongsDir, songDir.Name()) |
|
|
|
jobs <- job{ |
|
|
|
|
|
|
|
songPath: filepath.Join(osuSongsDir, songDir.Name()), |
|
|
|
|
|
|
|
pathToImage: settings.ReplacementImagePath, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
logger.LogInfo(fmt.Sprintf("Found %d song folders", len(songPaths))) |
|
|
|
close(jobs) |
|
|
|
|
|
|
|
logger.LogInfo(fmt.Sprintf("Found %d song folders", len(jobs))) |
|
|
|
// check if there is less job than workers
|
|
|
|
|
|
|
|
if settings.Workers > len(songPaths) { |
|
|
|
|
|
|
|
settings.Workers = len(songPaths) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// replacing backgrounds for each beatmap concurrently
|
|
|
|
results := make(chan result, len(jobs)) |
|
|
|
var successful, failed uint64 = 0, 0 |
|
|
|
workerPool(jobs, results, settings.Workers, &WG) |
|
|
|
for i := 0; i < int(settings.Workers); i++ { |
|
|
|
|
|
|
|
WG.Add(1) |
|
|
|
|
|
|
|
go worker(songPaths, settings.ReplacementImagePath, &successful, &failed, &WG) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close(songPaths) |
|
|
|
|
|
|
|
WG.Wait() |
|
|
|
WG.Wait() |
|
|
|
|
|
|
|
close(results) |
|
|
|
|
|
|
|
|
|
|
|
endTime := time.Now().UTC() |
|
|
|
// extracting results and logging the last message
|
|
|
|
|
|
|
|
var successful, failed uint64 = 0, 0 |
|
|
|
|
|
|
|
for result := range results { |
|
|
|
|
|
|
|
successful += result.successful |
|
|
|
|
|
|
|
failed += result.failed |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
total := successful + failed |
|
|
|
|
|
|
|
|
|
|
|
logger.LogInfo(fmt.Sprintf("\n\nDONE in %v . %d successful; %d failed", endTime.Sub(startingTime), successful, failed)) |
|
|
|
logger.LogInfo(fmt.Sprintf("DONE in %v. %d successful (%d%%/100%%); %d failed (%d%%/100%%)", |
|
|
|
|
|
|
|
time.Since(startingTime), successful, successful/total*100, failed, failed/total*100)) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|