Skip to content

Commit

Permalink
🎉 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aplulu committed Aug 14, 2021
0 parents commit 63cfe1f
Show file tree
Hide file tree
Showing 38 changed files with 538 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .commit_template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


#### Emoji Prefix ####
# ✨ :sparkles: Introducing new features.
# ⚡️ :zap: General update.
# 🐞 :beetle: Bug Fix.
# 🚑 :ambulance: Critical Bug Fix.
# 💄 :lipstick: Updating the UI and style files.
# ♻️ :recycle: Refactoring code.
# 🎨 :art: Improving structure / format of the code.
# 🐎 :racehorse: Improving performance.
# 📝 :pencil: Writing docs.
# 💚 :green_heart: Fixing CI Build.
# ✅ :white_check_mark: Updating tests.
# 🔥 :fire: Removing code or files.
# 🐳 :whale: Work about Docker.
# 🔧 :wrench: Changing configuration files.
# 🎉 :tada: Initial commit.

#### Format ####
# :emoji: Subject
#
# Message
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
.idea
obj
bin
*.user
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Aplulu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# RankedPlaylistGenerator

A Beat Saber Mod that generates a playlist of ScoreSaber ranked beat maps by difficulty.

# Requirements

* Beat Saber 1.16.4
* BSIPA 4.1.4
* BeatSaberMarkupLanguage 1.4.5
* SiraUtil 2.4.0
* BeatSaberPlaylistsLib 1.4.0

# Installation

Unzip the contents in [RankedPlaylistGenerator.zip](https://github.com/aplulu/ranked-playlist-generator/releases/latest/download/RankedPlaylistGenerator.zip) and place it in your Beat Saber installation directory. (e.g. `C:\Program Files (x86)\Steam\steamapps\common\Beat Saber`)

# Usage

Click the RankedPlaylist button that appears in the Mod menu.
16 changes: 16 additions & 0 deletions RankedPlaylistGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RankedPlaylistGenerator", "RankedPlaylistGenerator\RankedPlaylistGenerator.csproj", "{CC59A6C5-1CA6-4A95-85DF-08363B69A419}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CC59A6C5-1CA6-4A95-85DF-08363B69A419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC59A6C5-1CA6-4A95-85DF-08363B69A419}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC59A6C5-1CA6-4A95-85DF-08363B69A419}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC59A6C5-1CA6-4A95-85DF-08363B69A419}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions RankedPlaylistGenerator/Installers/AppInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using RankedPlaylistGenerator.Managers;
using Zenject;

namespace RankedPlaylistGenerator.Installers
{
public class AppInstaller: Installer
{
public override void InstallBindings()
{
Container.Bind<PlaylistGenerator>().AsSingle();
}
}
}
13 changes: 13 additions & 0 deletions RankedPlaylistGenerator/Installers/MenuInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using RankedPlaylistGenerator.Managers;
using Zenject;

namespace RankedPlaylistGenerator.Installers
{
public class MenuInstaller: Installer
{
public override void InstallBindings()
{
Container.BindInterfacesTo<MenuButtonManager>().AsSingle();
}
}
}
59 changes: 59 additions & 0 deletions RankedPlaylistGenerator/Managers/MenuButtonManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Threading.Tasks;
using BeatSaberMarkupLanguage;
using BeatSaberMarkupLanguage.MenuButtons;
using SiraUtil.Tools;
using Zenject;

namespace RankedPlaylistGenerator.Managers
{
internal class MenuButtonManager: IInitializable, IDisposable
{
private readonly SiraLog _log = null;
private readonly MenuButton _menuButton = null;

[Inject]
public MenuButtonManager(SiraLog log, PlaylistGenerator playlistGenerator)
{
_log = log;
var name = "Ranked Playlist";
_menuButton = new MenuButton(name, "Generate ScoreSaber Ranked Playlists", () =>
{
Task.Run(async () =>
{
_menuButton.Text = "Generating...";
try
{
await playlistGenerator.Generate();
_menuButton.Text = "Generated!";
}
catch (Exception e)
{
_log.Logger.Error("Generate Error: " + e.Message);
_menuButton.Text = "ERROR";
}
finally
{
await Task.Delay(3000);
_menuButton.Text = name;
}
});
}, true);
}

public void Initialize()
{

MenuButtons.instance.RegisterButton(_menuButton);
}

public void Dispose()
{
if (BSMLParser.IsSingletonAvailable && MenuButtons.IsSingletonAvailable)
{
MenuButtons.instance.UnregisterButton(_menuButton);
}
}
}
}
123 changes: 123 additions & 0 deletions RankedPlaylistGenerator/Managers/PlaylistGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Security.Policy;
using System.Threading.Tasks;
using BeatSaberPlaylistsLib;
using BeatSaberPlaylistsLib.Blist;
using BeatSaberPlaylistsLib.Legacy;
using BeatSaberPlaylistsLib.Types;
using Newtonsoft.Json;
using RankedPlaylistGenerator.Models;
using RankedPlaylistGenerator.Utils;
using SiraUtil.Tools;
using UnityEngine.Networking;

namespace RankedPlaylistGenerator.Managers
{
public class PlaylistGenerator
{
private readonly string _beatStarURL = "https://cdn.wes.cloud/beatstar/bssb/v2-all.json.gz";

private readonly SiraLog _log = null;

public PlaylistGenerator(SiraLog log)
{
_log = log;
}

public async Task Generate()
{
_log.Logger.Info("Generate");
var entries = await downloadBeatStar();

var playlistManager = PlaylistManager.DefaultManager;

var songByStar = new Dictionary<int, Dictionary<string, IPlaylistSong>>();
foreach (var kv in entries)
{
foreach (var diff in kv.Value.diffs)
{
if (diff.star == 0)
{
continue;
}

var star = (int)Math.Truncate(diff.star);
if (!songByStar.ContainsKey(star))
{
songByStar[star] = new Dictionary<string, IPlaylistSong >();
}

IPlaylistSong song = null;
if (songByStar[star].ContainsKey(kv.Key))
{
song = songByStar[star][kv.Key];
}
else
{
song = new LegacyPlaylistSong();
song.Hash = kv.Key;
song.Difficulties = new List<Difficulty>();
songByStar[star][kv.Key] = song;
}

var difficulty = new Difficulty
{
Characteristic =
((BeatStarCharacteristicName) Enum.ToObject(typeof(BeatStarCharacteristicName), diff.type))
.ToString(),
Name = diff.diff == "Expert+" ? "ExpertPlus" : diff.diff
};

song.Difficulties?.Add(difficulty);

}
}

foreach (var kv in songByStar)
{
var fileName = $"ranked_star_{kv.Key:00}";
var title = $"Ranked Songs ★{kv.Key}";
_log.Logger.Info($"Writing Playlist {title} ({kv.Value.Count} songs) to {fileName}");
//var playlist = new BlistPlaylist(fileName, title, "RankedPlaylistGenerator");
var playlist = new LegacyPlaylist(fileName, title, "RankedPlaylistGenerator");
playlist.SuggestedExtension = "json";
playlist.SetCover(ResourceUtil.GetCoverImage($"RankedPlaylistGenerator.Resources.images.{kv.Key}.png"));
//var playlist = playlistManager.CreatePlaylist(fileName, title, "RankedPlaylistGenerator", ResourceUtil.GetCoverImageLazy($"RankedPlaylistGenerator.Resources.images.{kv.Key}.png"));

foreach (var song in kv.Value.Values)
{
playlist.Add(song);
}
playlistManager.StorePlaylist(playlist);
}
}


private async Task<Dictionary<string, BeatStarEntry>> downloadBeatStar()
{
_log.Logger.Info("Requesting " + _beatStarURL);
var req = new UnityWebRequest(_beatStarURL, "GET");
req.timeout = 10;
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("User-Agent", "RankedPlaylistGenerator/1.0 (+https://github.com/aplulu/ranked-playlist-generator)");
await req.SendWebRequest();
if (req.isNetworkError)
{
throw new Exception("network error: " + req.error);
} else if (req.isHttpError)
{
throw new Exception("http error: " + req.error);
}

using (var reader = new StreamReader(new GZipStream(new MemoryStream(req.downloadHandler.data), CompressionMode.Decompress)))
{
var json = reader.ReadToEnd();
var songs = JsonConvert.DeserializeObject<Dictionary<string, BeatStarEntry>>(json);
return songs;
}
}
}
}
32 changes: 32 additions & 0 deletions RankedPlaylistGenerator/Managers/UnityWebrequestAwaiter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine.Networking;

namespace RankedPlaylistGenerator.Managers
{
public static class UnityWebRequestAsyncOperationExtension
{
public static UnityWebRequestAsyncOperationAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation)
{
return new UnityWebRequestAsyncOperationAwaiter(asyncOperation);
}
}
public class UnityWebRequestAsyncOperationAwaiter : INotifyCompletion
{
UnityWebRequestAsyncOperation _asyncOperation;

public bool IsCompleted => _asyncOperation.isDone;

public UnityWebRequestAsyncOperationAwaiter(UnityWebRequestAsyncOperation asyncOperation)
{
_asyncOperation = asyncOperation;
}

public void GetResult() { }

public void OnCompleted(Action continuation)
{
_asyncOperation.completed += _ => { continuation(); };
}
}
}
14 changes: 14 additions & 0 deletions RankedPlaylistGenerator/Models/BeatStarCharacteristicName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace RankedPlaylistGenerator.Models
{
public enum BeatStarCharacteristicName
{
Unknown,
Standard,
OneSaber,
NoArrows,
Lightshow,
Degree90,
Degree360,
Lawless
}
}
11 changes: 11 additions & 0 deletions RankedPlaylistGenerator/Models/BeatStarEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace RankedPlaylistGenerator.Models
{
public class BeatStarEntry
{
public float bpm;
public List<BeatStarEntryDifficulty> diffs;

}
}
11 changes: 11 additions & 0 deletions RankedPlaylistGenerator/Models/BeatStarEntryDifficulty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace RankedPlaylistGenerator.Models
{
public class BeatStarEntryDifficulty
{
public double pp;
public double star;
public int scores;
public string diff;
public int type;
}
}
Loading

0 comments on commit 63cfe1f

Please sign in to comment.