Created
June 24, 2020 02:49
-
-
Save 987123879113/333d412fa7ae5ddf189d28672837c38e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using System.Linq; | |
using NUnit.Framework; | |
using RhythmCodex.Ddr.Converters; | |
using RhythmCodex.Ddr.Models; | |
using RhythmCodex.Ddr.Processors; | |
using RhythmCodex.Ddr.Streamers; | |
using RhythmCodex.Meta.Models; | |
using RhythmCodex.Ssq.Converters; | |
using RhythmCodex.Ssq.Streamers; | |
using RhythmCodex.Stepmania; | |
using RhythmCodex.Stepmania.Converters; | |
using RhythmCodex.Stepmania.Model; | |
using RhythmCodex.Stepmania.Streamers; | |
using RhythmCodex.Vag.Converters; | |
using RhythmCodex.Vag.Heuristics; | |
using RhythmCodex.Vag.Streamers; | |
using System.Runtime.Serialization.Json; | |
using RhythmCodex.Infrastructure; | |
namespace RhythmCodex.Ddr.Integration | |
{ | |
public class DdrPs2FileDataIntegrationTests : BaseIntegrationFixture | |
{ | |
private string GamePath => Path.Combine("D:", "maxusa"); | |
private string ExecutablePath => Path.Combine(GamePath, "SLUS_204.37"); | |
private string FileDataPath => Path.Combine(GamePath, "DATA", "FILEDATA.BIN"); | |
private string OutPath => Path.Combine("D:", "ddr-out", "maxusa"); | |
[Test] | |
public void Test_Export_SM() | |
{ | |
using var mdSource = new FileStream(ExecutablePath, FileMode.Open, FileAccess.Read); | |
var metadataDecoder = Resolve<IDdrPs2MetadataTableStreamReader>(); | |
var dbDecoder = Resolve<IDdrPs2DatabaseDecoder>(); | |
var rawMetaDatas = metadataDecoder.Get(mdSource, mdSource.Length).Select(dbDecoder.Decode).ToList(); | |
using var source = new FileStream(FileDataPath, FileMode.Open, FileAccess.Read); | |
var streamer = Resolve<IDdrPs2FileDataStepStreamReader>(); | |
var output = streamer.Read(source, source.Length); | |
var tableDecoder = Resolve<IDdrPs2FileDataTableDecoder>(); | |
var table = tableDecoder.Decode(output); | |
var metadataDecorator = Resolve<IDdrMetadataDecorator>(); | |
var ssqReader = Resolve<ISsqStreamReader>(); | |
var ssqDecoder = Resolve<ISsqDecoder>(); | |
var chartSets = table.Select((e, i) => | |
{ | |
var chartRaw = ssqReader.Read(new MemoryStream(e.Data)); | |
var charts = ssqDecoder.Decode(chartRaw); | |
var idMd = rawMetaDatas.FirstOrDefault(md => md.InternalId == i + 1); | |
var chartSet = new ChartSet | |
{ | |
Charts = charts, | |
RawChart = e.Data, | |
Metadata = new Metadata() | |
}; | |
metadataDecorator.Decorate(chartSet, idMd, new MetadataDecoratorFileExtensions()); | |
return chartSet; | |
}).ToList(); | |
var smEncoder = Resolve<ISmEncoder>(); | |
var smWriter = Resolve<ISmStreamWriter>(); | |
var index = 0; | |
if (!Directory.Exists(OutPath)) { | |
Directory.CreateDirectory(OutPath); | |
} | |
foreach (var cs in chartSets) | |
{ | |
var commands = smEncoder.Encode(cs); | |
using var stream = this.OpenWrite(Path.Combine(OutPath, cs.Metadata[ChartTag.TitleTag], $"{index:D4}.sm")); | |
File.WriteAllBytes(Path.Combine(OutPath, cs.Metadata[ChartTag.TitleTag], cs.Metadata[ChartTag.TitleTag] + ".ssq"), cs.RawChart); | |
smWriter.Write(stream, commands); | |
stream.Flush(); | |
string jsonString = "{\n"; | |
var strings = new System.Collections.Generic.List<string>(); | |
strings.Add(string.Format("\t\"title\": \"{0}\"", cs.Metadata[ChartTag.TitleTag])); | |
strings.Add(string.Format("\t\"subtitle\": \"{0}\"", cs.Metadata[ChartTag.SubTitleTag])); | |
strings.Add(string.Format("\t\"artist\": \"{0}\"", cs.Metadata[ChartTag.ArtistTag])); | |
strings.Add(string.Format("\t\"offset\": {0}", cs.Metadata[ChartTag.OffsetTag])); | |
strings.Add(string.Format("\t\"bpms\": \"{0}\"", cs.Metadata[ChartTag.DisplayBpmTag])); | |
strings.Add("\t\"difficulties\": {"); | |
var strings2 = new System.Collections.Generic.List<string>(); | |
foreach (var chart in cs.Charts) | |
{ | |
strings2.Add( | |
string.Format( | |
"\t\t\"{1}-{0}\": {2}", | |
chart[NotesCommandTag.DifficultyTag].ToLower(), | |
chart[NotesCommandTag.TypeTag], | |
(chart[NumericData.PlayLevel] ?? BigRational.One).GetWholePart() | |
) | |
); | |
} | |
strings.Add(string.Join(",\n", strings2).Replace("{,", "{").TrimEnd().TrimEnd(',') + "\n\t}"); | |
jsonString += string.Join(",\n", strings).Replace("{,", "{"); | |
jsonString += "\n}"; | |
File.WriteAllText(Path.Combine(OutPath, cs.Metadata[ChartTag.TitleTag], "metadata.json"), jsonString); | |
index++; | |
} | |
} | |
// [Test] | |
// [Explicit] | |
// public void Test_Export_WAV() | |
// { | |
// using var mdSource = new FileStream(ExecutablePath, FileMode.Open, FileAccess.Read); | |
// var md = Resolve<IDdrPs2MetadataTableStreamReader>(); | |
// var metaDatas = md.Get(mdSource, mdSource.Length); | |
// using var source = new FileStream(FileDataPath, FileMode.Open, FileAccess.Read); | |
// var remaining = source.Length; | |
// var reader = new BinaryReader(source); | |
// var heuristic = new SvagHeuristic(Resolve<IVagStreamReader>()); | |
// var decoder = Resolve<ISvagDecoder>(); | |
// var index = 0; | |
// while (remaining > 0x800) | |
// { | |
// var oldPosition = source.Position; | |
// var buffer = reader.ReadBytes(0x800); | |
// var match = heuristic.Match(buffer); | |
// if (match == null) | |
// continue; | |
// source.Position = oldPosition; | |
// var svag = heuristic.Read(match, source); | |
// var decoded = decoder.Decode(svag); | |
// this.WriteSound(decoded, Path.Combine(OutPath, $"{index:D4}.wav")); | |
// index++; | |
// source.Position = oldPosition + 0x800; | |
// } | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment