mirror of
https://github.com/muskit/MercuryConverter.git
synced 2026-06-02 20:24:26 -07:00
add data reading & selection table population
This commit is contained in:
+88
-83
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 ? "+" : "")}";
|
||||
}
|
||||
}
|
||||
}
|
||||
+31
-3
@@ -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<int, string> CATEGORY_INDEX = _CATEGORY_INDEX;
|
||||
|
||||
private static Dictionary<int, string> _NUM_SOURCE = new()
|
||||
private static Dictionary<uint, string> _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<int, string> NUM_SOURCE = _NUM_SOURCE;
|
||||
public static readonly IReadOnlyDictionary<string, int> SOURCE_NUM = _NUM_SOURCE.ToDictionary(p => p.Value, p=>p.Key);
|
||||
public static readonly IReadOnlyDictionary<uint, string> NUM_SOURCE = _NUM_SOURCE;
|
||||
public static readonly IReadOnlyDictionary<string, uint> SOURCE_NUM = _NUM_SOURCE.ToDictionary(p => p.Value, p => p.Key);
|
||||
|
||||
private static string[] _DIFFICULTIES = {
|
||||
"Normal", "Hard", "Expert", "Inferno"
|
||||
};
|
||||
public static readonly IReadOnlyList<string> DIFFICULTIES = _DIFFICULTIES;
|
||||
|
||||
private static readonly Dictionary<Difficulty, string> _DIFF_LVL_KEY = new()
|
||||
{
|
||||
{Difficulty.Normal, "DifficultyNormalLv"},
|
||||
{Difficulty.Hard, "DifficultyHardLv"},
|
||||
{Difficulty.Expert, "DifficultyExtremeLv"},
|
||||
{Difficulty.Inferno, "DifficultyInfernoLv"},
|
||||
};
|
||||
public static readonly IReadOnlyDictionary<Difficulty, string> DIFF_LVL_KEY = _DIFF_LVL_KEY;
|
||||
|
||||
private static readonly Dictionary<Difficulty, string> _DIFF_FILENAME_PREPEND = new()
|
||||
{
|
||||
{Difficulty.Normal, "00"},
|
||||
{Difficulty.Hard, "01"},
|
||||
{Difficulty.Expert, "02"},
|
||||
{Difficulty.Inferno, "03"},
|
||||
};
|
||||
public static readonly IReadOnlyDictionary<Difficulty, string> DIFF_FILENAME_PREPEND = _DIFF_FILENAME_PREPEND;
|
||||
|
||||
private static readonly Dictionary<Difficulty, string> _DIFF_CLEAR_KEY = new()
|
||||
{
|
||||
{Difficulty.Normal, "ClearNormaRateNormal"},
|
||||
{Difficulty.Hard, "ClearNormaRateHard"},
|
||||
{Difficulty.Expert, "ClearNormaRateExtreme"},
|
||||
{Difficulty.Inferno, "ClearNormaRateInferno"},
|
||||
};
|
||||
public static readonly IReadOnlyDictionary<Difficulty, string> DIFF_CLEAR_KEY = _DIFF_CLEAR_KEY;
|
||||
}
|
||||
+18
-11
@@ -1,22 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Avalonia.Media;
|
||||
using SaturnData.Notation.Core;
|
||||
|
||||
namespace MercuryConverter.Data;
|
||||
|
||||
/// <summary>
|
||||
/// Combining SaturnData's Entry & Chart.
|
||||
/// </summary>
|
||||
public class Song
|
||||
{
|
||||
/// <summary>
|
||||
/// Format: `Snn-nnn` where `n` is a digit.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user