Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #11

Merged
merged 13 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,4 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml
.idea
17 changes: 7 additions & 10 deletions BmsLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Text.Json.Nodes;
using System.Text.Json.Nodes;
using CustomAlbums.Data;
using CustomAlbums.Managers;
using CustomAlbums.Utilities;
Expand Down Expand Up @@ -64,11 +63,11 @@ internal static Bms Load(Stream stream, string bmsName)
if (!key.Contains("BPM")) continue;

var bpmKey = string.IsNullOrEmpty(key[3..]) ? "00" : key[3..];
bpmDict.Add(bpmKey, float.Parse(value, CultureInfo.InvariantCulture));
bpmDict.Add(bpmKey, value.ParseAsFloat());

if (bpmKey != "00") continue;

var freq = 60f / float.Parse(value, CultureInfo.InvariantCulture) * 4f;
var freq = 60f / value.ParseAsFloat() * 4f;
var obj = new JsonObject
{
{ "tick", 0f },
Expand All @@ -83,19 +82,17 @@ internal static Bms Load(Stream stream, string bmsName)
var key = split[0];
var value = split[1];

var beat = int.Parse(key[..3], CultureInfo.InvariantCulture);
var beat = key[..3].ParseAsInt();
var typeCode = key.Substring(3, 2);

if (!Bms.Channels.ContainsKey(typeCode)) continue;

var type = Bms.Channels[typeCode];
if (!Bms.Channels.TryGetValue(typeCode, out var type)) continue;

if (type is Bms.ChannelType.SpTimesig)
{
var obj = new JsonObject
{
{ "beat", beat },
{ "percent", float.Parse(value, CultureInfo.InvariantCulture) }
{ "percent", value.ParseAsFloat() }
};
notePercents.Add(beat, obj);
}
Expand Down Expand Up @@ -549,7 +546,7 @@ private static void ProcessBossData(Bms bms)
private static void ProcessDelay(Bms bms)
{
var scene = bms.Info["GENRE"]?.GetValue<string>() ?? string.Empty;
var sceneIndex = int.Parse(scene.Split('_')[1], CultureInfo.InvariantCulture);
var sceneIndex = scene.Split('_')[1].ParseAsInt();
var sceneInfo = Singleton<StageBattleComponent>.instance.sceneInfo;
var delayCache = new Dictionary<string, Decimal>();

Expand Down
2 changes: 1 addition & 1 deletion CustomAlbums.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</PackageReference>
<PackageReference Include="NAudio.Vorbis" Version="1.5.0" />
<PackageReference Include="NLayer" Version="1.15.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Data/Album.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO.Compression;
using CustomAlbums.Managers;
using CustomAlbums.Utilities;
using Il2CppSystem.Runtime.Remoting.Messaging;
using UnityEngine;
using Logger = CustomAlbums.Utilities.Logger;

Expand Down Expand Up @@ -62,6 +63,7 @@ public Album(string path, int index)
public Dictionary<int, Sheet> Sheets { get; } = new();
public string AlbumName =>
IsPackaged ? $"album_{System.IO.Path.GetFileNameWithoutExtension(Path)}" : $"album_{System.IO.Path.GetFileName(Path)}_folder";
public string Uid => $"{AlbumManager.Uid}-{Index}";

public bool HasFile(string name)
{
Expand Down Expand Up @@ -142,5 +144,7 @@ private void GetSheets()
Sheets.Add(difficulty, new Sheet(hash, this, difficulty));
}
}

public bool HasDifficulty(int difficulty) => Sheets.ContainsKey(difficulty);
}
}
4 changes: 4 additions & 0 deletions Data/AlbumInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public enum HiddenMode
}

public string Name { get; set; } = string.Empty;
[JsonPropertyName("name_romanized")]
public string NameRomanized { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public string LevelDesigner { get; set; } = string.Empty;
public string LevelDesigner1 { get; set; } = string.Empty;
public string LevelDesigner2 { get; set; } = string.Empty;
public string LevelDesigner3 { get; set; } = string.Empty;
public string LevelDesigner4 { get; set; } = string.Empty;
public string LevelDesigner5 { get; set; } = string.Empty;
public string Bpm { get; set; } = "0";
public string Scene { get; set; } = "scene_01";
public SceneEggs SceneEgg { get; set; } = SceneEggs.None;
Expand All @@ -34,6 +36,7 @@ public Dictionary<int, string> Difficulties
if (Difficulty2 != "0") dict.Add(2, Difficulty2);
if (Difficulty3 != "0") dict.Add(3, Difficulty3);
if (Difficulty4 != "0") dict.Add(4, Difficulty4);
if (Difficulty5 != "0") dict.Add(5, Difficulty5);
return dict;
}
}
Expand All @@ -42,6 +45,7 @@ public Dictionary<int, string> Difficulties
public string Difficulty2 { get; set; } = "0";
public string Difficulty3 { get; set; } = "0";
public string Difficulty4 { get; set; } = "0";
public string Difficulty5 { get; set; } = "0";

public string HideBmsMode { get; set; } = "CLICK";

Expand Down
3 changes: 2 additions & 1 deletion Data/AssetIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ internal class AssetIdentifiers
"_map1",
"_map2",
"_map3",
"_map4"
"_map4",
"_map5"
};
}
}
7 changes: 3 additions & 4 deletions Data/Bms.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Globalization;
using System.Text.Json.Nodes;
using System.Text.Json.Nodes;
using CustomAlbums.Utilities;
using Il2CppAssets.Scripts.GameCore.Managers;
using Il2CppAssets.Scripts.PeroTools.Commons;
Expand Down Expand Up @@ -317,7 +316,7 @@ public float Bpm
get
{
var bpmString = Info["BPM"]?.GetValue<string>() ?? Info["BPM01"]?.GetValue<string>() ?? string.Empty;
return !float.TryParse(bpmString, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var bpm) ? 0f : bpm;
return bpmString.TryParseAsFloat(out var bpm) ? bpm : 0f;
}
}

Expand Down Expand Up @@ -412,7 +411,7 @@ public JsonArray GetNoteData()
if (NoteData is null || NoteData.Count == 0) InitNoteData();
var processed = new JsonArray();

var speedAir = int.Parse(Info["PLAYER"]?.GetValue<string>() ?? "1", CultureInfo.InvariantCulture);
var speedAir = (Info["PLAYER"]?.GetValue<string>() ?? "1").ParseAsInt();
var speedGround = speedAir;

var objectId = 1;
Expand Down
1 change: 0 additions & 1 deletion Data/Sheet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json.Nodes;
using CustomAlbums.Managers;
using CustomAlbums.Utilities;
using Il2CppAssets.Scripts.Database;
using Il2CppAssets.Scripts.GameCore;
Expand Down
7 changes: 3 additions & 4 deletions Managers/AlbumManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.IO.Enumeration;
using CustomAlbums.Data;
using CustomAlbums.Data;
using CustomAlbums.Utilities;
using Il2CppPeroTools2.Resources;
using UnityEngine;
using Logger = CustomAlbums.Utilities.Logger;
Expand Down Expand Up @@ -80,7 +79,7 @@ public static IEnumerable<string> GetAllUid()

public static Album GetByUid(string uid)
{
return LoadedAlbums.FirstOrDefault(album => album.Value.Index == int.Parse(uid[4..], CultureInfo.InvariantCulture)).Value;
return LoadedAlbums.FirstOrDefault(album => album.Value.Index == uid[4..].ParseAsInt()).Value;
}
public static string GetAlbumNameFromUid(string uid)
{
Expand Down
22 changes: 15 additions & 7 deletions Managers/CoverManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CustomAlbums.Data;
using System.Runtime.CompilerServices;
using CustomAlbums.Data;
using CustomAlbums.Utilities;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
Expand Down Expand Up @@ -26,10 +27,14 @@ public static Sprite GetCover(this Album album)

var bytes = stream.ReadFully();

var tex = new Texture2D(2, 2, TextureFormat.ARGB32, false);
tex.LoadImage(bytes);
// Create the textures
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false)
{
wrapMode = TextureWrapMode.MirrorOnce
};
texture.LoadImage(bytes);

var cover = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
var cover = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
CachedCovers.Add(album.Index, cover);

return cover;
Expand Down Expand Up @@ -58,7 +63,7 @@ public static unsafe AnimatedCover GetAnimatedCover(this Album album)
// Get frame data
var frame = gif.Frames[i];
var width = frame.Width;
var height = frame.Width;
var height = frame.Height;

// Get frame pixel data
//
Expand All @@ -75,8 +80,11 @@ public static unsafe AnimatedCover GetAnimatedCover(this Album album)
using var handle = memory.Pin();

// Create the textures
var texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
texture.LoadRawTextureData((IntPtr)handle.Pointer, memory.Length * sizeof(IntPtr));
var texture = new Texture2D(width, height, TextureFormat.RGBA32, false)
{
wrapMode = TextureWrapMode.MirrorOnce
};
texture.LoadRawTextureData((IntPtr)handle.Pointer, memory.Length * Unsafe.SizeOf<Rgba32>());
texture.Apply(false);

// Create the sprite with the given texture and add it to the sprites array
Expand Down
52 changes: 45 additions & 7 deletions Managers/SaveManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Globalization;
using System.Text;
using System.Text;
using System.Text.Json;
using CustomAlbums.Data;
using CustomAlbums.Utilities;
using System.IO.Compression;

namespace CustomAlbums.Managers
{
Expand Down Expand Up @@ -63,7 +63,7 @@ internal static void FixSaveFile()
{
var unlockedHighest = SaveData.Highest.Where(kv =>
kv.Value.ContainsKey(3) && kv.Value.TryGetValue(2, out var chartSave) && chartSave.Evaluate >= 4).Select(kv => kv.Key);
var folderCharts = AlbumManager.LoadedAlbums.Where(kv => kv.Value.Sheets.ContainsKey(2) && kv.Value.Sheets.ContainsKey(3) && !kv.Value.IsPackaged).Select(kv => kv.Key);
var folderCharts = AlbumManager.LoadedAlbums.Where(kv => kv.Value.HasDifficulty(2) && kv.Value.HasDifficulty(3) && !kv.Value.IsPackaged).Select(kv => kv.Key);
var concat = unlockedHighest.Concat(folderCharts);
SaveData.UnlockedMasters.UnionWith(concat);
}
Expand All @@ -86,6 +86,36 @@ internal static void FixSaveFile()
SaveData.FullCombo = fixedDictionaryFc;
}

private static void RestoreBackup()
{
var backupPath = Path.Join(SaveLocation, "Backups", "Backups.zip");

// If a backup does not exist at all
if (!File.Exists(backupPath))
{
Logger.Fail("No backups found. Please delete the CustomAlbums.json file in UserData folder to create a new save.");
return;
}

// Traverse the .zip file, trying every single backup in this archive
using var backupEntries = ZipFile.OpenRead(backupPath);
foreach (var backup in backupEntries.Entries.OrderByDescending(bak => bak.LastWriteTime)
.Where(bak => bak.Name.EndsWith("CustomAlbums.json.bak")))
{
try
{
SaveData = Json.Deserialize<CustomAlbumsSave>(backup.Open());
}
catch (Exception)
{
continue;
}
Logger.Success($"Restored backup from {backup.LastWriteTime.DateTime}.");
return;
}
Logger.Fail("Could not restore save file. Please delete the CustomAlbums.json file in UserData folder to create a new save.");
}

internal static void LoadSaveFile()
{
if (!ModSettings.SavingEnabled) return;
Expand All @@ -97,8 +127,15 @@ internal static void LoadSaveFile()
}
catch (Exception ex)
{
if (ex is FileNotFoundException) SaveData = new CustomAlbumsSave();
else Logger.Warning("Failed to load save file. " + ex.StackTrace);
if (ex is FileNotFoundException)
{
SaveData = new CustomAlbumsSave();
}
else
{
Logger.Warning("Could not load save file. Attempting to restore backup...");
RestoreBackup();
}
}
}

Expand All @@ -121,6 +158,7 @@ internal static void SaveSaveFile()
}
}

/// <summary>
/// Saves custom score given scoring information.
/// </summary>
/// <param name="uid">The UID of the chart.</param>
Expand Down Expand Up @@ -171,10 +209,10 @@ internal static void SaveScore(string uid, int musicDifficulty, int score, float
newScore.Score = Math.Max(score, newScore.Score);
newScore.Combo = Math.Max(maxCombo, newScore.Combo);
newScore.Evaluate = Math.Max(newEvaluate, newScore.Evaluate);
newScore.AccuracyStr = string.Create(CultureInfo.InvariantCulture, $"{newScore.Accuracy / 100:P2}");
newScore.AccuracyStr = (newScore.Accuracy / 100).ToStringInvariant("P2");
newScore.Clear++;

if (musicDifficulty is 2 && AlbumManager.LoadedAlbums[albumName].Sheets.ContainsKey(3) && newScore.Evaluate >= 4)
if (musicDifficulty is 2 && AlbumManager.LoadedAlbums[albumName].HasDifficulty(3) && newScore.Evaluate >= 4)
SaveData.UnlockedMasters.Add(albumName);

if (miss != 0) return;
Expand Down
Loading