add data reading & selection table population

This commit is contained in:
Alex
2025-08-18 00:32:10 -07:00
parent 7c407a7a84
commit f4a091dfa4
10 changed files with 249 additions and 191 deletions
+88 -83
View File
@@ -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<Song> 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);
}
}