Skip to content

Commit

Permalink
big 1.35.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwasyx committed Apr 12, 2024
1 parent eff5344 commit 8566f92
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 127 deletions.
42 changes: 20 additions & 22 deletions ScoreSaber/Core/Daemons/UploadDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using UnityEngine;
using ScoreSaber.Core.Utils;
using static ScoreSaber.UI.Leaderboard.ScoreSaberLeaderboardViewController;
using System.Threading;

namespace ScoreSaber.Core.Daemons {

Expand All @@ -29,16 +30,16 @@ internal class UploadDaemon : IDisposable, IUploadDaemon {
private readonly LeaderboardService _leaderboardService = null;

private readonly PlayerDataModel _playerDataModel = null;
private readonly CustomLevelLoader _customLevelLoader = null;
private readonly MaxScoreCache _maxScoreCache = null;

private const string UPLOAD_SECRET = "f0b4a81c9bd3ded1081b365f7628781f";

public UploadDaemon(PlayerService playerService, LeaderboardService leaderboardService, ReplayService replayService, PlayerDataModel playerDataModel, CustomLevelLoader customLevelLoader) {
public UploadDaemon(PlayerService playerService, LeaderboardService leaderboardService, ReplayService replayService, PlayerDataModel playerDataModel, MaxScoreCache maxScoreCache) {
_playerService = playerService;
_replayService = replayService;
_leaderboardService = leaderboardService;
_playerDataModel = playerDataModel;
_customLevelLoader = customLevelLoader;
_maxScoreCache = maxScoreCache;

SetupUploader();
Plugin.Log.Debug("Upload service setup!");
Expand All @@ -63,13 +64,13 @@ private void SetupUploader() {

// Standard uploader
public void Three(StandardLevelScenesTransitionSetupDataSO standardLevelScenesTransitionSetupDataSO, LevelCompletionResults levelCompletionResults) {
Five(standardLevelScenesTransitionSetupDataSO.gameMode, standardLevelScenesTransitionSetupDataSO.difficultyBeatmap, levelCompletionResults, standardLevelScenesTransitionSetupDataSO.practiceSettings != null);
Five(standardLevelScenesTransitionSetupDataSO.gameMode, standardLevelScenesTransitionSetupDataSO.beatmapLevel, standardLevelScenesTransitionSetupDataSO.beatmapKey, levelCompletionResults, standardLevelScenesTransitionSetupDataSO.practiceSettings != null);
}

// Multiplayer uploader
public void Four(MultiplayerLevelScenesTransitionSetupDataSO multiplayerLevelScenesTransitionSetupDataSO, MultiplayerResultsData multiplayerResultsData) {

if (multiplayerLevelScenesTransitionSetupDataSO.difficultyBeatmap == null) {
if (multiplayerLevelScenesTransitionSetupDataSO.beatmapLevel == null) {
return;
}
if (multiplayerResultsData.localPlayerResultData.multiplayerLevelCompletionResults.levelCompletionResults == null) {
Expand All @@ -82,10 +83,10 @@ public void Four(MultiplayerLevelScenesTransitionSetupDataSO multiplayerLevelSce
return;
}

Five(multiplayerLevelScenesTransitionSetupDataSO.gameMode, multiplayerLevelScenesTransitionSetupDataSO.difficultyBeatmap, multiplayerResultsData.localPlayerResultData.multiplayerLevelCompletionResults.levelCompletionResults, false);
Five(multiplayerLevelScenesTransitionSetupDataSO.gameMode, multiplayerLevelScenesTransitionSetupDataSO.beatmapLevel, multiplayerLevelScenesTransitionSetupDataSO.beatmapKey, multiplayerResultsData.localPlayerResultData.multiplayerLevelCompletionResults.levelCompletionResults, false);
}

public void Five(string gameMode, IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults levelCompletionResults, bool practicing) {
public void Five(string gameMode, BeatmapLevel beatmapLevel, BeatmapKey beatmapKey, LevelCompletionResults levelCompletionResults, bool practicing) {
try {

if (Plugin.ReplayState.IsPlaybackEnabled) { return; }
Expand All @@ -97,7 +98,7 @@ public void Five(string gameMode, IDifficultyBeatmap difficultyBeatmap, LevelCom
}

if (gameMode == "Solo" || gameMode == "Multiplayer") {
Plugin.Log.Debug($"Starting upload process for {difficultyBeatmap.level.levelID}:{difficultyBeatmap.level.songName}");
Plugin.Log.Debug($"Starting upload process for {beatmapKey.levelId}:{beatmapLevel.songName}");
if (practicing) {
// If practice write replay at this point
_replayService.WriteSerializedReplay().RunTask();
Expand All @@ -115,7 +116,7 @@ public void Five(string gameMode, IDifficultyBeatmap difficultyBeatmap, LevelCom
_replayService.WriteSerializedReplay().RunTask();
return;
}
Six(difficultyBeatmap, levelCompletionResults);
Six(beatmapLevel, beatmapKey, levelCompletionResults);
}
} catch (Exception ex) {
UploadStatusChanged?.Invoke(UploadStatus.Error, "Failed to upload score, error written to log.");
Expand All @@ -124,13 +125,10 @@ public void Five(string gameMode, IDifficultyBeatmap difficultyBeatmap, LevelCom
}

//This starts the upload processs
async void Six(IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults levelCompletionResults) {
async void Six(BeatmapLevel beatmapLevel, BeatmapKey beatmapKey, LevelCompletionResults levelCompletionResults) {

if (difficultyBeatmap.level is CustomBeatmapLevel) {
var defaultEnvironment = _customLevelLoader.LoadEnvironmentInfo(null, false);

var beatmapData = await difficultyBeatmap.GetBeatmapDataAsync(defaultEnvironment, _playerDataModel.playerData.playerSpecificSettings);
int maxScore = ScoreModel.ComputeMaxMultipliedScoreForBeatmap(beatmapData);
if (!beatmapLevel.hasPrecalculatedData) {
int maxScore = await _maxScoreCache.GetMaxScore(beatmapLevel, beatmapKey);

if (levelCompletionResults.multipliedScore > maxScore) {
UploadStatusChanged?.Invoke(UploadStatus.Error, "Failed to upload (score was impossible)");
Expand All @@ -140,7 +138,7 @@ async void Six(IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults leve

try {
UploadStatusChanged?.Invoke(UploadStatus.Packaging, "Packaging score...");
ScoreSaberUploadData data = ScoreSaberUploadData.Create(difficultyBeatmap, levelCompletionResults, _playerService.localPlayerInfo, GetVersionHash());
ScoreSaberUploadData data = ScoreSaberUploadData.Create(beatmapLevel, beatmapKey, levelCompletionResults, _playerService.localPlayerInfo, GetVersionHash());
string scoreData = JsonConvert.SerializeObject(data);

// TODO: Simplify now that we're open source
Expand All @@ -151,23 +149,23 @@ async void Six(IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults leve
.ToLower();

string scoreDataHex = BitConverter.ToString(Swap(Encoding.UTF8.GetBytes(scoreData), Encoding.UTF8.GetBytes(key))).Replace("-", "");
Seven(data, scoreDataHex, difficultyBeatmap, levelCompletionResults).RunTask();
Seven(data, scoreDataHex, beatmapLevel, beatmapKey, levelCompletionResults).RunTask();
} catch (Exception ex) {
UploadStatusChanged?.Invoke(UploadStatus.Error, "Failed to upload score, error written to log.");
Plugin.Log.Error($"Failed to upload score: {ex}");
}
}
}

public async Task Seven(ScoreSaberUploadData scoreSaberUploadData, string uploadData, IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults results) {
public async Task Seven(ScoreSaberUploadData scoreSaberUploadData, string uploadData, BeatmapLevel beatmapLevel, BeatmapKey beatmapKey, LevelCompletionResults results) {
try {
UploadStatusChanged?.Invoke(UploadStatus.Packaging, "Packaging replay...");
byte[] serializedReplay = await _replayService.WriteSerializedReplay();

UploadStatusChanged?.Invoke(UploadStatus.Packaging, "Checking leaderboard ranked status...");

bool ranked = true;
Leaderboard currentLeaderboard = await _leaderboardService.GetCurrentLeaderboard(difficultyBeatmap);
Leaderboard currentLeaderboard = await _leaderboardService.GetCurrentLeaderboard(beatmapKey);

if (currentLeaderboard != null) {
ranked = currentLeaderboard.leaderboardInfo.ranked;
Expand Down Expand Up @@ -243,7 +241,7 @@ public async Task Seven(ScoreSaberUploadData scoreSaberUploadData, string upload
}

if (done && !failed) {
SaveLocalReplay(scoreSaberUploadData, difficultyBeatmap, serializedReplay);
SaveLocalReplay(scoreSaberUploadData, beatmapKey, serializedReplay);
Plugin.Log.Info("Score uploaded!");
UploadStatusChanged?.Invoke(UploadStatus.Success, $"Score uploaded!");
}
Expand All @@ -260,7 +258,7 @@ public async Task Seven(ScoreSaberUploadData scoreSaberUploadData, string upload
}
}

private void SaveLocalReplay(ScoreSaberUploadData scoreSaberUploadData, IDifficultyBeatmap difficultyBeatmap, byte[] replay) {
private void SaveLocalReplay(ScoreSaberUploadData scoreSaberUploadData, BeatmapKey beatmapKey, byte[] replay) {

if (replay == null) {
Plugin.Log.Error("Failed to write local replay; replay is null");
Expand All @@ -269,7 +267,7 @@ private void SaveLocalReplay(ScoreSaberUploadData scoreSaberUploadData, IDifficu

try {
if (Plugin.Settings.saveLocalReplays) {
string replayPath = $@"{Settings.replayPath}\{scoreSaberUploadData.playerId}-{scoreSaberUploadData.songName.ReplaceInvalidChars().Truncate(155)}-{difficultyBeatmap.difficulty.SerializedName()}-{difficultyBeatmap.parentDifficultyBeatmapSet.beatmapCharacteristic.serializedName}-{scoreSaberUploadData.leaderboardId}.dat";
string replayPath = $@"{Settings.replayPath}\{scoreSaberUploadData.playerId}-{scoreSaberUploadData.songName.ReplaceInvalidChars().Truncate(155)}-{beatmapKey.difficulty.SerializedName()}-{beatmapKey.beatmapCharacteristic.serializedName}-{scoreSaberUploadData.leaderboardId}.dat";
File.WriteAllBytes(replayPath, replay);
}
} catch (Exception ex) {
Expand Down
33 changes: 23 additions & 10 deletions ScoreSaber/Core/Data/Models/LeaderboardUploadData.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Newtonsoft.Json;
using HarmonyLib;
using Newtonsoft.Json;
using ScoreSaber.Core.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace ScoreSaber.Core.Data.Models {
Expand Down Expand Up @@ -50,20 +52,20 @@ internal class ScoreSaberUploadData {
[JsonProperty("deviceControllerRightIdentifier")]
internal string deviceControllerRightIdentifier;

internal static ScoreSaberUploadData Create(IDifficultyBeatmap difficultyBeatmap, LevelCompletionResults results, LocalPlayerInfo playerInfo, string infoHash) {
internal static ScoreSaberUploadData Create(BeatmapLevel beatmapLevel, BeatmapKey beatmapKey, LevelCompletionResults results, LocalPlayerInfo playerInfo, string infoHash) {

ScoreSaberUploadData data = new ScoreSaberUploadData();

string[] levelInfo = difficultyBeatmap.level.levelID.Split('_');
data.gameMode = $"Solo{difficultyBeatmap.parentDifficultyBeatmapSet.beatmapCharacteristic.serializedName}";
data.difficulty = BeatmapDifficultyMethods.DefaultRating(difficultyBeatmap.difficulty);
string[] levelInfo = beatmapKey.levelId.Split('_');
data.gameMode = $"Solo{beatmapKey.beatmapCharacteristic.serializedName}";
data.difficulty = BeatmapDifficultyMethods.DefaultRating(beatmapKey.difficulty);
data.infoHash = infoHash;
data.leaderboardId = levelInfo[2];
data.songName = difficultyBeatmap.level.songName;
data.songSubName = difficultyBeatmap.level.songSubName;
data.songAuthorName = difficultyBeatmap.level.songAuthorName;
data.levelAuthorName = difficultyBeatmap.level.levelAuthorName;
data.bpm = Convert.ToInt32(difficultyBeatmap.level.beatsPerMinute);
data.songName = beatmapLevel.songName;
data.songSubName = beatmapLevel.songSubName;
data.songAuthorName = beatmapLevel.songAuthorName;
data.levelAuthorName = friendlyLevelAuthorName(beatmapLevel.allMappers, beatmapLevel.allLighters);
data.bpm = Convert.ToInt32(beatmapLevel.beatsPerMinute);

data.playerName = playerInfo.playerName;
data.playerId = playerInfo.playerId;
Expand All @@ -81,5 +83,16 @@ internal static ScoreSaberUploadData Create(IDifficultyBeatmap difficultyBeatmap
data.deviceControllerRightIdentifier = VRDevices.GetDeviceControllerRight();
return data;
}

static string friendlyLevelAuthorName(string[] mappers, string[] lighters) {
List<string> mappersAndLighters = new List<string>();
mappersAndLighters.AddRange(mappers);
mappersAndLighters.AddRange(lighters);

if(mappersAndLighters.Count <= 1) {
return mappersAndLighters.FirstOrDefault();
}
return $"{string.Join(", ", mappersAndLighters.Take(mappersAndLighters.Count - 1))} & {mappersAndLighters.Last()}";
}
}
}
10 changes: 6 additions & 4 deletions ScoreSaber/Core/Data/Wrappers/LeaderboardInfoMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace ScoreSaber.Core.Data.Wrappers {
internal class LeaderboardInfoMap {
internal LeaderboardInfo leaderboardInfo { get; set; }
internal IDifficultyBeatmap difficultyBeatmap { get; set; }
internal BeatmapLevel beatmapLevel { get; set; }
internal BeatmapKey beatmapKey { get; set; }
internal string songHash { get; set; }

internal LeaderboardInfoMap(LeaderboardInfo leaderboardInfo, IDifficultyBeatmap difficultyBeatmap) {
this.difficultyBeatmap = difficultyBeatmap;
internal LeaderboardInfoMap(LeaderboardInfo leaderboardInfo, BeatmapLevel beatmapLevel, BeatmapKey beatmapKey) {
this.beatmapLevel = beatmapLevel;
this.beatmapKey = beatmapKey;
this.leaderboardInfo = leaderboardInfo;
this.songHash = difficultyBeatmap.level.levelID.Split('_')[2];
this.songHash = beatmapKey.levelId.Split('_')[2];
}
}
}
6 changes: 3 additions & 3 deletions ScoreSaber/Core/Data/Wrappers/LeaderboardMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ internal class LeaderboardMap {
internal LeaderboardInfoMap leaderboardInfoMap { get; set; }
internal ScoreMap[] scores { get; set; }

internal LeaderboardMap(Leaderboard leaderboard, IDifficultyBeatmap difficultyBeatmap, IReadonlyBeatmapData beatmapData) {
this.leaderboardInfoMap = new LeaderboardInfoMap(leaderboard.leaderboardInfo, difficultyBeatmap);
internal LeaderboardMap(Leaderboard leaderboard, BeatmapLevel beatmapLevel, BeatmapKey beatmapKey, int maxMultipliedScore) {
this.leaderboardInfoMap = new LeaderboardInfoMap(leaderboard.leaderboardInfo, beatmapLevel, beatmapKey);
this.scores = new ScoreMap[leaderboard.scores.Length];
for (int i = 0; i < leaderboard.scores.Length; i++) {
this.scores[i] = new ScoreMap(leaderboard.scores[i], this.leaderboardInfoMap, beatmapData);
this.scores[i] = new ScoreMap(leaderboard.scores[i], this.leaderboardInfoMap, maxMultipliedScore);
}
}

Expand Down
6 changes: 3 additions & 3 deletions ScoreSaber/Core/Data/Wrappers/ScoreMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class ScoreMap {
internal GameplayModifiers gameplayModifiers { get; set; }
internal string formattedPlayerName { get; set; }

internal ScoreMap(Score _score, LeaderboardInfoMap customLeaderboardInfo, IReadonlyBeatmapData beatmapData) {
internal ScoreMap(Score _score, LeaderboardInfoMap customLeaderboardInfo, int maxMultipliedScore) {
score = _score;

GameplayModifiersMap replayMods = new GameplayModifiersMap();
Expand All @@ -26,10 +26,10 @@ internal ScoreMap(Score _score, LeaderboardInfoMap customLeaderboardInfo, IReado
}
}

double maxScore = ScoreModel.ComputeMaxMultipliedScoreForBeatmap(beatmapData) * replayMods.totalMultiplier;
double maxScore = maxMultipliedScore * replayMods.totalMultiplier;

this.parent = customLeaderboardInfo;
this.hasLocalReplay = LeaderboardUtils.LocalReplayExists(customLeaderboardInfo.difficultyBeatmap, this);
this.hasLocalReplay = LeaderboardUtils.LocalReplayExists(customLeaderboardInfo.beatmapLevel, customLeaderboardInfo.beatmapKey, this);
this.score.weight = Math.Round(score.weight * 100, 2);
this.score.pp = Math.Round(score.pp, 2);
this.accuracy = Math.Round((score.modifiedScore / maxScore) * 100, 2);
Expand Down
3 changes: 3 additions & 0 deletions ScoreSaber/Core/MainInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ScoreSaber.Core.ReplaySystem;
using ScoreSaber.Core.ReplaySystem.UI;
using ScoreSaber.Core.Services;
using ScoreSaber.Core.Utils;
using ScoreSaber.Patches;
using ScoreSaber.UI.Elements.Leaderboard;
using ScoreSaber.UI.Elements.Profile;
Expand All @@ -26,6 +27,8 @@ public override void InstallBindings() {
Container.Bind<GlobalLeaderboardService>().AsSingle();
Container.Bind<LeaderboardService>().AsSingle();
Container.Bind<PlayerService>().AsSingle();

Container.Bind<MaxScoreCache>().AsSingle();


Container.Bind<PanelView>().FromNewComponentAsViewController().AsSingle();
Expand Down
1 change: 0 additions & 1 deletion ScoreSaber/Core/ReplaySystem/Accessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ internal static class Accessors
internal static readonly PropertyAccessor<ColorNoteVisuals, bool>.Setter SetCircleVisibility = PropertyAccessor<ColorNoteVisuals, bool>.GetSetter("showCircle");
internal static readonly FieldAccessor<ColorNoteVisuals, MaterialPropertyBlockController[]>.Accessor NoteMaterialBlocks = FieldAccessor<ColorNoteVisuals, MaterialPropertyBlockController[]>.GetAccessor("_materialPropertyBlockControllers");

internal static readonly FieldAccessor<ResultsViewController, IDifficultyBeatmap>.Accessor resultsViewControllerDifficultyBeatmap = FieldAccessor<ResultsViewController, IDifficultyBeatmap>.GetAccessor("_difficultyBeatmap");
internal static readonly FieldAccessor<ResultsViewController, LevelCompletionResults>.Accessor resultsViewControllerLevelCompletionResults = FieldAccessor<ResultsViewController, LevelCompletionResults>.GetAccessor("_levelCompletionResults");

internal static readonly FieldAccessor<ModalView, bool>.Accessor animateParentCanvas = FieldAccessor<ModalView, bool>.GetAccessor("_animateParentCanvas");
Expand Down
6 changes: 3 additions & 3 deletions ScoreSaber/Core/ReplaySystem/Recorders/MetadataRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public Metadata Export() {

return new Metadata() {
Version = "3.0.0",
LevelID = _gameplayCoreSceneSetupData.difficultyBeatmap.level.levelID,
Difficulty = BeatmapDifficultyMethods.DefaultRating(_gameplayCoreSceneSetupData.difficultyBeatmap.difficulty),
Characteristic = _gameplayCoreSceneSetupData.difficultyBeatmap.parentDifficultyBeatmapSet.beatmapCharacteristic.serializedName,
LevelID = _gameplayCoreSceneSetupData.beatmapLevel.levelID,
Difficulty = BeatmapDifficultyMethods.DefaultRating(_gameplayCoreSceneSetupData.beatmapKey.difficulty),
Characteristic = _gameplayCoreSceneSetupData.beatmapKey.beatmapCharacteristic.serializedName,
Environment = _gameplayCoreSceneSetupData.environmentInfo.serializedName,
Modifiers = GetModifierList(_gameplayCoreSceneSetupData.gameplayModifiers),
NoteSpawnOffset = _beatmapObjectSpawnControllerInitData.noteJumpValue,
Expand Down
Loading

0 comments on commit 8566f92

Please sign in to comment.