Files
MercuryConverter/Data/Database.cs
T

107 lines
3.8 KiB
C#
Raw Normal View History

2025-08-13 20:13:40 -07:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2025-08-13 20:13:40 -07:00
using System.IO;
using System.Linq;
using System.Linq.Expressions;
2025-08-13 20:13:40 -07:00
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;
2025-08-13 20:13:40 -07:00
namespace MercuryConverter.Data;
public static class Database
{
public static ObservableCollection<Song> Songs = new();
public static void SetupNew(string dataPath)
2025-08-13 20:13:40 -07:00
{
Dispatcher.UIThread.Invoke(() => Songs.Clear());
2025-08-13 20:13:40 -07:00
var metadataTablePath = Path.Combine(dataPath, "MusicParameterTable.uasset");
var metadataAsset = new UAsset(metadataTablePath, EngineVersion.VER_UE4_19);
var metadataTable = metadataAsset.Exports[0] as DataTableExport;
foreach (var data in metadataTable!.Table.Data)
2025-08-13 20:13:40 -07:00
{
if (data["AssetDirectory"].ToString()!.Contains("S99"))
{
continue;
}
2025-08-13 20:13:40 -07:00
var previewBegin = ((FloatPropertyData)data["PreviewBeginTime"]).Value;
var previewLen = ((FloatPropertyData)data["PreviewSeconds"]).Value;
2025-08-13 20:13:40 -07:00
string? cTxt = data["CopyrightMessage"].ToString();
var jacketPath = $"{Path.Combine(dataPath, "jackets", data["JacketAssetName"].ToString()!)}.png";
try
2025-08-13 20:13:40 -07:00
{
var song = new Song
2025-08-13 20:13:40 -07:00
{
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));
}
2025-08-13 20:13:40 -07:00
}
Dispatcher.UIThread.Invoke(() => Songs.Add(song));
}
catch (Exception e)
{
Console.WriteLine($"Couldn't construct a song!\n{e}");
}
2025-08-13 20:13:40 -07:00
}
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);
2025-08-13 20:13:40 -07:00
}
}