From f4a091dfa460f628f531c449a78c7077696f1aa3 Mon Sep 17 00:00:00 2001 From: Alex <15199219+muskit@users.noreply.github.com> Date: Mon, 18 Aug 2025 00:32:10 -0700 Subject: [PATCH] add data reading & selection table population --- Data/Database.cs | 171 +++++++++++++------------- Data/Song/Chart.cs | 25 ---- Data/Song/Consts.cs | 34 ++++- Data/Song/Song.cs | 29 +++-- UI/Dialogs/DataOpen.axaml | 42 +++++-- UI/Dialogs/DataOpen.axaml.cs | 81 ++++++++++-- UI/Dialogs/Welcome.axaml | 13 +- UI/Views/Selection/Selection.axaml | 6 +- UI/Views/Selection/Selection.axaml.cs | 37 +----- _External/SaturnData | 2 +- 10 files changed, 249 insertions(+), 191 deletions(-) delete mode 100644 Data/Song/Chart.cs diff --git a/Data/Database.cs b/Data/Database.cs index a5b47e1..c79dd46 100644 --- a/Data/Database.cs +++ b/Data/Database.cs @@ -1,102 +1,107 @@ using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.IO; using System.Linq; +using System.Linq.Expressions; using System.Text.Json; +using Avalonia.Threading; +using MercuryConverter.UI.Views; +using SaturnData.Notation.Core; +using SaturnData.Notation.Serialization; +using SaturnData.Notation.Serialization.Mer; +using UAssetAPI; +using UAssetAPI.ExportTypes; +using UAssetAPI.PropertyTypes.Objects; +using UAssetAPI.PropertyTypes.Structs; +using UAssetAPI.UnrealTypes; namespace MercuryConverter.Data; public static class Database { - public static void Setup(string dataDirPath) + public static ObservableCollection Songs = new(); + + public static void SetupNew(string dataPath) { - // Check that path exists - if (!Directory.Exists(dataDirPath)) - { - Console.WriteLine($"Folder {dataDirPath} doesn't exist!"); - return; - } + Dispatcher.UIThread.Invoke(() => Songs.Clear()); - // Get metadata.json - var jPath = Path.Combine(dataDirPath, "metadata.json"); - string jStr; - JsonElement mdObj; - try - { - jStr = File.ReadAllText(jPath); - } - catch (Exception e) - { - Console.WriteLine($"Couldn't read {jPath}: {e}"); - return; - } - try - { - mdObj = JsonDocument.Parse(jStr).RootElement.GetProperty("Exports")[0].GetProperty("Table").GetProperty("Data"); - } - catch (Exception e) - { - Console.WriteLine($"Couldn't parse JSON object: {e}"); - return; - } + var metadataTablePath = Path.Combine(dataPath, "MusicParameterTable.uasset"); + var metadataAsset = new UAsset(metadataTablePath, EngineVersion.VER_UE4_19); + var metadataTable = metadataAsset.Exports[0] as DataTableExport; - // TODO: Clear existing structures - - // Parse metadata.json - foreach (var mdSong in mdObj.EnumerateArray()) + foreach (var data in metadataTable!.Table.Data) { - var id = ""; - var title = ""; - var rubi = ""; - var artist = ""; - var genre = -1; - var copyright = ""; - var bpm = ""; - var version = -1; - var previewTime = -1; - var previewLength = -1; - var jacketPath = ""; - - var level = new string?[] { null, null, null, null }; - var levelBGA = new string?[] { null, null, null, null}; - var levelAudio = new string?[] { null, null, null, null }; - var levelDesigner = new string?[] { null, null, null, null }; - var levelClearRequirements = new string?[] { null, null, null, null }; - - foreach (var prop in mdSong.GetProperty("Value").EnumerateArray()) + if (data["AssetDirectory"].ToString()!.Contains("S99")) { - var value = prop.GetProperty("Value"); - // Console.WriteLine($"{prop.GetProperty("Name")}={prop.GetProperty("Value")}"); - switch (prop.GetProperty("Name").GetString()!) - { - case "AssetDirectory": - id = value.GetString()!; - break; - case "ScoreGenre": - genre = value.GetInt16(); - break; - case "MusicMessage": - title = value.GetString(); - break; - case "ArtistMessage": - artist = value.GetString(); - break; - case "Rubi": - rubi = value.GetString(); - break; - case "Bpm": - bpm = value.GetString(); - break; - case "CopyrightMessage": - var c = value.GetString(); - if (!new string?[] { "", "-", null }.Contains(c)) - { - copyright = c; - } - break; - } + continue; } - Console.WriteLine($"[{id}] {artist} - {title}"); + var previewBegin = ((FloatPropertyData)data["PreviewBeginTime"]).Value; + var previewLen = ((FloatPropertyData)data["PreviewSeconds"]).Value; + + string? cTxt = data["CopyrightMessage"].ToString(); + + var jacketPath = $"{Path.Combine(dataPath, "jackets", data["JacketAssetName"].ToString()!)}.png"; + + try + { + var song = new Song + { + Id = data["AssetDirectory"].ToString()!, + Rubi = data["Rubi"].ToString()!, + Name = data["MusicMessage"].ToString()!, + Artist = data["ArtistMessage"].ToString()!, + Genre = ((IntPropertyData)data["ScoreGenre"]).Value, + Source = ((UInt32PropertyData)data["VersionNo"]).Value, + PreviewTime = previewBegin, + PreviewLen = previewLen, + Jacket = File.Exists(jacketPath) ? jacketPath : null, + Copyright = (cTxt == "-" || cTxt == "") ? null : cTxt, + }; + + foreach (Difficulty diff in Enum.GetValues(typeof(Difficulty))) + { + // skip non-canon difficulties + if (diff == Difficulty.None || diff == Difficulty.WorldsEnd) continue; + + if (GetDiffPair(dataPath, data, diff) is var pair && pair != null) + { + song.charts.Add((diff, pair.Value.Item1, pair.Value.Item2)); + } + } + + Dispatcher.UIThread.Invoke(() => Songs.Add(song)); + } + catch (Exception e) + { + Console.WriteLine($"Couldn't construct a song!\n{e}"); + } } + Console.WriteLine("finished music table"); + } + + private static (Entry, Chart)? GetDiffPair(string dataPath, StructPropertyData song, Difficulty diff) + { + + var level = ((FloatPropertyData)song[Consts.DIFF_LVL_KEY[diff]]).Value; + if (level == 0) + return null; + + var id = song["AssetDirectory"].ToString()!; + var chartFilePath = Path.Combine(dataPath, "MusicData", id, $"{id}_{Consts.DIFF_FILENAME_PREPEND[diff]}.mer"); + var clearThreshold = ((FloatPropertyData)song[Consts.DIFF_CLEAR_KEY[diff]]).Value; + + var e = NotationSerializer.ToEntry(chartFilePath, new NotationReadArgs + { + InferClearThresholdFromDifficulty = false + }); + e.ClearThreshold = clearThreshold; + var c = NotationSerializer.ToChart(chartFilePath, new NotationReadArgs + { + InferClearThresholdFromDifficulty = false + }); + + return (e, c); } } \ No newline at end of file diff --git a/Data/Song/Chart.cs b/Data/Song/Chart.cs deleted file mode 100644 index a1ef026..0000000 --- a/Data/Song/Chart.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; - -namespace MercuryConverter.Data; - - -public class Chart -{ - public required string audioId; - public required string audioOffset; - public required string audioPreviewTime; - public required string audioPreviewDuration; - public required string video; - public required string designer; - public required string clearRequirement; - public required string diffLevel; - public string diffString - { - get - { - var d = Convert.ToDouble(diffLevel); - var i = (int)d; - return $"{(int)d}{(d > i ? "+" : "")}"; - } - } -} \ No newline at end of file diff --git a/Data/Song/Consts.cs b/Data/Song/Consts.cs index 2b425b5..732ec05 100644 --- a/Data/Song/Consts.cs +++ b/Data/Song/Consts.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using SaturnData.Notation.Core; namespace MercuryConverter.Data; @@ -20,7 +21,7 @@ public static class Consts }; public static readonly IReadOnlyDictionary CATEGORY_INDEX = _CATEGORY_INDEX; - private static Dictionary _NUM_SOURCE = new() + private static Dictionary _NUM_SOURCE = new() { { 1, string.Concat(new int[] {87, 65, 67, 67, 65}.Select(i => Convert.ToChar(i))) }, { 2, string.Concat(new int[] {87, 65, 67, 67, 65, 32, 83}.Select(i => Convert.ToChar(i))) }, @@ -28,11 +29,38 @@ public static class Consts { 4, string.Concat(new int[] {87, 65, 67, 67, 65, 32, 76, 73, 76, 89, 32, 82}.Select(i => Convert.ToChar(i))) }, { 5, string.Concat(new int[] {87, 65, 67, 67, 65, 32, 82, 101, 118, 101, 114, 115, 101}.Select(i => Convert.ToChar(i))) } }; - public static readonly IReadOnlyDictionary NUM_SOURCE = _NUM_SOURCE; - public static readonly IReadOnlyDictionary SOURCE_NUM = _NUM_SOURCE.ToDictionary(p => p.Value, p=>p.Key); + public static readonly IReadOnlyDictionary NUM_SOURCE = _NUM_SOURCE; + public static readonly IReadOnlyDictionary SOURCE_NUM = _NUM_SOURCE.ToDictionary(p => p.Value, p => p.Key); private static string[] _DIFFICULTIES = { "Normal", "Hard", "Expert", "Inferno" }; public static readonly IReadOnlyList DIFFICULTIES = _DIFFICULTIES; + + private static readonly Dictionary _DIFF_LVL_KEY = new() + { + {Difficulty.Normal, "DifficultyNormalLv"}, + {Difficulty.Hard, "DifficultyHardLv"}, + {Difficulty.Expert, "DifficultyExtremeLv"}, + {Difficulty.Inferno, "DifficultyInfernoLv"}, + }; + public static readonly IReadOnlyDictionary DIFF_LVL_KEY = _DIFF_LVL_KEY; + + private static readonly Dictionary _DIFF_FILENAME_PREPEND = new() + { + {Difficulty.Normal, "00"}, + {Difficulty.Hard, "01"}, + {Difficulty.Expert, "02"}, + {Difficulty.Inferno, "03"}, + }; + public static readonly IReadOnlyDictionary DIFF_FILENAME_PREPEND = _DIFF_FILENAME_PREPEND; + + private static readonly Dictionary _DIFF_CLEAR_KEY = new() + { + {Difficulty.Normal, "ClearNormaRateNormal"}, + {Difficulty.Hard, "ClearNormaRateHard"}, + {Difficulty.Expert, "ClearNormaRateExtreme"}, + {Difficulty.Inferno, "ClearNormaRateInferno"}, + }; + public static readonly IReadOnlyDictionary DIFF_CLEAR_KEY = _DIFF_CLEAR_KEY; } \ No newline at end of file diff --git a/Data/Song/Song.cs b/Data/Song/Song.cs index 754ec46..654aaa0 100644 --- a/Data/Song/Song.cs +++ b/Data/Song/Song.cs @@ -1,22 +1,29 @@ using System; using System.Collections.Generic; using Avalonia.Media; +using SaturnData.Notation.Core; namespace MercuryConverter.Data; +/// +/// Combining SaturnData's Entry & Chart. +/// public class Song { - /// - /// Format: `Snn-nnn` where `n` is a digit. - /// - public required string Id { get; set;} + public required string Id { get; set; } // Snn-nnn public required string Name { get; set; } public required string Artist { get; set; } - public required string Source { get; set; } - public string rubi; - public string copyright; - public string tempo; - public int genreId; - public string jacket; - public Chart?[] charts = { null, null, null, null }; + public required uint Source { get; set; } + public required string Rubi { get; set; } + public string? Copyright { get; set; } // May have never been used? + public required int Genre { get; set; } + public required string? Jacket { get; set; } + public required float PreviewTime { get; set; } + public required float PreviewLen { get; set; } + public string SourceName => Consts.NUM_SOURCE[Source]; + + + // TODO: For SaturnData.Entry instances, use this Guid format: + // MERCURY_[SONGID]_[DIFF] (each var is int) + public List<(Difficulty, Entry, Chart)> charts = new(); } \ No newline at end of file diff --git a/UI/Dialogs/DataOpen.axaml b/UI/Dialogs/DataOpen.axaml index 5a806aa..74f122f 100644 --- a/UI/Dialogs/DataOpen.axaml +++ b/UI/Dialogs/DataOpen.axaml @@ -6,14 +6,36 @@ xmlns:progRing="clr-namespace:AvaloniaProgressRing;assembly=AvaloniaProgressRing" x:Class="MercuryConverter.UI.Dialogs.DataOpen" > - - - - - + + + + + + + + + + + +