Skip to content

Commit

Permalink
CAM-29 Fixed JSON loading issues with comments (please don't use comm…
Browse files Browse the repository at this point in the history
…ents in JSON) and differing types for info.json. Fixed SavePatch bug where data would not be cleared from vanilla save. Small change for faster compiling :)
  • Loading branch information
SB15-MD committed Jan 18, 2024
1 parent 2d0c2c4 commit 046f7b8
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 60 deletions.
3 changes: 2 additions & 1 deletion CustomAlbums.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
<ILRepack
LibraryPath="@(IlRepackLibs)"
OutputFile="$(OutputPath)/$(AssemblyName).dll"
InputAssemblies="@(InputAssemblies)" />
InputAssemblies="@(InputAssemblies)"
Parallel="true"/>
</Target>

<Target Name="ILRepackCopy" AfterTargets="ILRepacker" Condition="'$(WORKER)' != 'GitHub'">
Expand Down
5 changes: 4 additions & 1 deletion Data/AlbumInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using static CustomAlbums.Data.SceneEgg;
using System.Text.Json.Serialization;
using CustomAlbums.Utilities;
using static CustomAlbums.Data.SceneEgg;

namespace CustomAlbums.Data
{
Expand Down Expand Up @@ -41,6 +43,7 @@ public Dictionary<int, string> Difficulties
public string Difficulty4 { get; set; } = "0";

public string HideBmsMode { get; set; } = "CLICK";
[JsonConverter(typeof(Converters.NumberConverter))]
public string HideBmsDifficulty { get; set; } = "0";
public string HideBmsMessage { get; set; } = string.Empty;

Expand Down
1 change: 0 additions & 1 deletion Data/CustomAlbumsSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace CustomAlbums.Data
public class CustomAlbumsSave
{
public string SelectedAlbum { get; set; } = string.Empty;
public int SelectedDifficulty { get; set; } = 2;
public HashSet<string> Collections { get; set; } = new();
public HashSet<string> Hides { get; set; } = new();
public Queue<string> History { get; set; } = new();
Expand Down
33 changes: 1 addition & 32 deletions Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Il2CppAssets.Scripts.PeroTools.Commons;
using Il2CppAssets.Scripts.PeroTools.Nice.Datas;
using Il2CppAssets.Scripts.PeroTools.Nice.Interface;
using Il2CppAssets.Scripts.PeroTools.Platforms.Steam;
using Il2CppToolGood.Words;
using static UnityEngine.SpookyHash;

Expand Down Expand Up @@ -109,38 +110,6 @@ internal static void SaveSaveFile()
}
}

/// <summary>
/// Cleans out all custom data from the vanilla save file since this data should not be stored in the vanilla save.
/// </summary>

// TODO: Remove this once it's we verify that saving is not occurring after completing a custom chart.
internal static void SanitizeVanilla()
{
var gameCollections = Singleton<DataManager>.instance["Account"]["Collections"];
var gameHides = Singleton<DataManager>.instance["Account"]["Hides"];
var gameHistory = Singleton<DataManager>.instance["Account"]["History"];
var gameHighest = Singleton<DataManager>.instance["Achievement"]["highest"];
var gameFailCount = Singleton<DataManager>.instance["Achievement"]["fail_count"];
var gameEasyPass = Singleton<DataManager>.instance["Achievement"]["easy_pass"];
var gameHardPass = Singleton<DataManager>.instance["Achievement"]["hard_pass"];
var gameMasterPass = Singleton<DataManager>.instance["Achievement"]["master_pass"];
var gameFullComboMusic = Singleton<DataManager>.instance["Achievement"]["full_combo_music"];
var gameUnlockMasters = Singleton<DataManager>.instance["Account"]["UnlockMasters"];

Logger.Msg("Sanitizing Vanilla save!");
gameCollections.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameHides.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameHistory.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameHighest.GetResult<Il2CppSystem.Collections.Generic.List<IData>>().RemoveAll((Il2CppSystem.Predicate<IData>)(data => data["uid"].GetResult<string>().StartsWith($"{AlbumManager.Uid}-")));
gameFailCount.GetResult<Il2CppSystem.Collections.Generic.List<IData>>().RemoveAll((Il2CppSystem.Predicate<IData>)(data => data["uid"].GetResult<string>().StartsWith($"{AlbumManager.Uid}-")));
gameEasyPass.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameHardPass.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameMasterPass.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameFullComboMusic.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
gameUnlockMasters.GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
}

/// <summary>
/// Saves custom score given scoring information.
/// </summary>
/// <param name="uid">The UID of the chart.</param>
Expand Down
82 changes: 58 additions & 24 deletions Patches/SavePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Logger = CustomAlbums.Utilities.Logger;
using Il2CppPeroPeroGames.DataStatistics;
using System.Reflection;
using static CustomAlbums.Managers.SaveManager;

namespace CustomAlbums.Patches
{
Expand Down Expand Up @@ -112,7 +113,7 @@ private static bool InjectPnlPreparation(PnlPreparation __instance, bool forceRe
ClearAndRefreshPanels(__instance, forceReload);

var recordPanel = __instance.pnlRecord;
var currentChartData = SaveManager.SaveData.GetChartSaveDataFromUid(currentMusicInfo.uid);
var currentChartData = SaveData.GetChartSaveDataFromUid(currentMusicInfo.uid);
var highestExists = currentChartData.TryGetPropertyValue("Highest", out var currentChartHighest);

// If no highest data exists then early return
Expand Down Expand Up @@ -179,7 +180,7 @@ private static bool Prefix(MusicInfo musicInfo)
if (!musicInfo.uid.StartsWith($"{AlbumManager.Uid}-")) return true;
if (!ModSettings.SavingEnabled) return false;

SaveManager.SaveData.Hides.Add(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
SaveData.Hides.Add(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
return true;
}
}
Expand All @@ -192,7 +193,7 @@ private static bool Prefix(MusicInfo musicInfo)
if (!musicInfo.uid.StartsWith($"{AlbumManager.Uid}-")) return true;
if (!ModSettings.SavingEnabled) return false;

SaveManager.SaveData.Hides.Remove(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
SaveData.Hides.Remove(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
return true;
}
}
Expand All @@ -205,7 +206,7 @@ private static bool Prefix(MusicInfo musicInfo)
if (!musicInfo.uid.StartsWith($"{AlbumManager.Uid}-")) return true;
if (!ModSettings.SavingEnabled) return false;

SaveManager.SaveData.Collections.Add(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
SaveData.Collections.Add(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
return true;
}
}
Expand All @@ -218,7 +219,7 @@ private static bool Prefix(MusicInfo musicInfo)
if (!musicInfo.uid.StartsWith($"{AlbumManager.Uid}-")) return true;
if (!ModSettings.SavingEnabled) return false;

SaveManager.SaveData.Collections.Remove(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
SaveData.Collections.Remove(AlbumManager.GetAlbumNameFromUid(musicInfo.uid));
return true;
}
}
Expand All @@ -231,10 +232,10 @@ private static bool Prefix(string musicUid)
if (!musicUid.StartsWith($"{AlbumManager.Uid}-")) return true;
if (!ModSettings.SavingEnabled) return false;

if (SaveManager.SaveData.History.Count == 10)
SaveManager.SaveData.History.Dequeue();
if (SaveData.History.Count == 10)
SaveData.History.Dequeue();

SaveManager.SaveData.History.Enqueue(AlbumManager.GetAlbumNameFromUid(musicUid));
SaveData.History.Enqueue(AlbumManager.GetAlbumNameFromUid(musicUid));
return true;
}
}
Expand All @@ -252,7 +253,7 @@ internal class UploadScorePatch
private static void Postfix(string musicUid, int musicDifficulty, string characterUid, string elfinUid, int hp, int score, float acc, int maximumCombo, string evaluate, int miss)
{
if (!musicUid.StartsWith($"{AlbumManager.Uid}-")) return;
SaveManager.SaveScore(musicUid, musicDifficulty, score, acc, maximumCombo, evaluate, miss);
SaveScore(musicUid, musicDifficulty, score, acc, maximumCombo, evaluate, miss);
}
}

Expand Down Expand Up @@ -299,24 +300,24 @@ private static void Postfix(PnlVictory __instance)
if (!ModSettings.SavingEnabled) return;

var albumName = AlbumManager.GetAlbumNameFromUid(GlobalDataBase.dbBattleStage.musicUid);
if (!SaveManager.SaveData.Highest.TryGetValue(albumName, out var highest))
if (!SaveData.Highest.TryGetValue(albumName, out var highest))
return;

// If the chart has been played then enable the "HI-SCORE" UI element
var difficulty = GlobalDataBase.s_DbBattleStage.m_MapDifficulty;
__instance.m_CurControls.highScoreTitle.enabled = SaveManager.PreviousScore != "-";
__instance.m_CurControls.highScoreTxt.enabled = SaveManager.PreviousScore != "-";
__instance.m_CurControls.highScoreTitle.enabled = PreviousScore != "-";
__instance.m_CurControls.highScoreTxt.enabled = PreviousScore != "-";

// Gets the current run score
var score = Singleton<TaskStageTarget>.instance.GetScore();

// Enable "New Best!!" UI element if the chart hasn't been played, or the new play is a higher score
if (!highest.ContainsKey(difficulty))
__instance.m_CurControls.newBest.SetActive(true);
if (SaveManager.PreviousScore != "-")
__instance.m_CurControls.newBest.SetActive(score > int.Parse(SaveManager.PreviousScore));
if (PreviousScore != "-")
__instance.m_CurControls.newBest.SetActive(score > int.Parse(PreviousScore));

__instance.m_CurControls.highScoreTxt.text = SaveManager.PreviousScore;
__instance.m_CurControls.highScoreTxt.text = PreviousScore;
}
}

Expand All @@ -329,18 +330,46 @@ private static void CleanCustomData()
{
if (!ModSettings.SavingEnabled) return;

Singleton<DataManager>.instance["Account"]["Hides"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
Singleton<DataManager>.instance["Account"]["History"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
Singleton<DataManager>.instance["Account"]["Collections"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
DataHelper.hides.RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
DataHelper.history.RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
DataHelper.collections.RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));
DataHelper.tasks.RemoveAll((Il2CppSystem.Predicate<string>)(uid => uid.StartsWith($"{AlbumManager.Uid}-")));

if (DataHelper.selectedAlbumUid == "music_package_999")
DataHelper.selectedAlbumUid = "music_package_0";

if (DataHelper.selectedAlbumTagIndex == AlbumManager.Uid)
DataHelper.selectedAlbumTagIndex = 0;

if (DataHelper.selectedMusicUidFromInfoList.StartsWith($"{AlbumManager.Uid}-"))
{
DataHelper.selectedMusicUidFromInfoList = "0-0";
SaveData.SelectedAlbum = AlbumManager.GetAlbumNameFromUid(DataHelper.selectedMusicUidFromInfoList);
}
else
{
SaveData.SelectedAlbum = string.Empty;
}

foreach (var task in DataHelper.tasks)
{
Singleton<DataManager>.instance["Task"][task].GetResult<Il2CppSystem.Collections.Generic.List<IData>>().RemoveAll((Il2CppSystem.Predicate<IData>)(data => data["uid"].GetResult<string>().StartsWith($"{AlbumManager.Uid}")));
}
}

private static void InjectCustomData()
{
if (!ModSettings.SavingEnabled) return;

Singleton<DataManager>.instance["Account"]["Hides"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().AddManagedRange(SaveManager.SaveData.Hides.GetAlbumUidsFromNames());
Singleton<DataManager>.instance["Account"]["History"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().AddManagedRange(SaveManager.SaveData.History.GetAlbumUidsFromNames());
Singleton<DataManager>.instance["Account"]["Collections"].GetResult<Il2CppSystem.Collections.Generic.List<string>>().AddManagedRange(SaveManager.SaveData.Collections.GetAlbumUidsFromNames());
DataHelper.hides.AddManagedRange(SaveData.Hides.GetAlbumUidsFromNames());
DataHelper.history.AddManagedRange(SaveData.History.GetAlbumUidsFromNames());
DataHelper.collections.AddManagedRange(SaveData.Collections.GetAlbumUidsFromNames());

if (!SaveData.SelectedAlbum.StartsWith($"{AlbumManager.Uid}-")) return;

DataHelper.selectedAlbumUid = "music_package_999";
DataHelper.selectedAlbumTagIndex = 999;
DataHelper.selectedMusicUidFromInfoList = SaveData.SelectedAlbum ?? "0-0";
}


Expand All @@ -351,7 +380,12 @@ internal class SaveLocalPatch
private static void Prefix()
{
if (!ModSettings.SavingEnabled) return;
SaveManager.SaveSaveFile();

if (DataHelper.selectedMusicUidFromInfoList.StartsWith($"{AlbumManager.Uid}-"))
SaveData.SelectedAlbum = $"{AlbumManager.GetAlbumNameFromUid(DataHelper.selectedMusicUidFromInfoList)}";

SaveSaveFile();
CleanCustomData();
}

private static void Postfix()
Expand Down Expand Up @@ -380,7 +414,7 @@ private static void Prefix()
{
if (!ModSettings.SavingEnabled) return;
Backup.InitBackups();
SaveManager.SaveSaveFile();
SaveSaveFile();
CleanCustomData();
InjectCustomData();
}
Expand All @@ -393,7 +427,7 @@ private static void Prefix(ref bool isLocal)
{
if (!ModSettings.SavingEnabled || !isLocal) return;

SaveManager.SaveSaveFile();
SaveSaveFile();
CleanCustomData();
}

Expand Down
2 changes: 1 addition & 1 deletion Utilities/Backup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ internal class Backup

internal static void InitBackups()
{
// TODO: FIX THIS c:
Directory.CreateDirectory(BackupPath);

CompressBackups();
CreateBackup(BackupVanilla, Singleton<DataManager>.instance.ToBytes());
CreateBackup(BackupVanillaDebug, JsonSerializer.Serialize(ToJsonDict(Singleton<DataManager>.instance.datas)));
CreateBackup(BackupCustom, SaveManager.SaveData);
ClearOldBackups();
SaveManager.SanitizeVanilla();
}
private static void CreateBackup(string filePath, object data)
{
Expand Down
27 changes: 27 additions & 0 deletions Utilities/Converters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace CustomAlbums.Utilities
{
internal class Converters
{
public class NumberConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
{
return reader.TokenType == JsonTokenType.Number ? reader.GetInt32().ToString() : reader.GetString();
}

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}
}
}
4 changes: 4 additions & 0 deletions Utilities/Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static class Json
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,

// God this sucks
ReadCommentHandling = JsonCommentHandling.Skip,

Converters =
{
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
Expand Down

0 comments on commit 046f7b8

Please sign in to comment.