Add music info loading infra

This commit is contained in:
BtbN
2019-06-04 16:09:36 +02:00
parent 5af60f3897
commit 9cc15ade97
5 changed files with 111 additions and 2 deletions

View File

@@ -7,6 +7,14 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<None Remove="Data\L44\music_info_l44_8.kbin" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Data\L44\music_info_l44_8.kbin" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using eAmuseCore.KBinXML;
using eAmuseCore.Compression;
namespace ClanServer.Data.L44
{
public class ClanMusicInfo
{
static ClanMusicInfo()
{
Instance = Task.Run(() =>
{
foreach (string name in Assembly.GetExecutingAssembly().GetManifestResourceNames())
{
if (name.EndsWith("music_info_l44_8.kbin", StringComparison.InvariantCultureIgnoreCase))
{
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
using (MemoryStream ms = new MemoryStream())
{
stream.CopyTo(ms);
return new ClanMusicInfo(
new KBinXML(
LZ77.Decompress(
ms.ToArray()
).ToArray()
).Document
);
}
}
}
return null;
});
}
public static Task<ClanMusicInfo> Instance { get; private set; }
private readonly XDocument doc;
private ClanMusicInfo(XDocument doc)
{
this.doc = doc;
BuildMusicList();
}
private List<int> musicIdList;
public IList<int> MusicIdList { get => musicIdList.AsReadOnly(); }
private void BuildMusicList()
{
XElement body = doc.Root.Element("music_data").Element("body");
var musicData = body.Elements("data");
musicIdList = new List<int>(musicData.Count());
foreach (XElement musicEntry in musicData)
{
musicIdList.Add(int.Parse(musicEntry.Element("music_id").Value));
}
}
private static readonly Random rng = new Random();
public List<int> GetRandomSongs(int num)
{
HashSet<int> candidateIndexes = new HashSet<int>();
while (candidateIndexes.Count < num)
candidateIndexes.Add(rng.Next(0, musicIdList.Count));
List<int> result = new List<int>(num);
result.AddRange(candidateIndexes.Select(i => musicIdList[i]));
for (int i = result.Count - 1; i > 0; --i)
{
int k = rng.Next(i + 1);
int v = result[i];
result[i] = result[k];
result[k] = v;
}
return result;
}
}
}

Binary file not shown.

View File

@@ -1,4 +1,5 @@
using System;
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Xml.Linq;
@@ -33,7 +34,7 @@ namespace eAmuseTest
KBinXML kbinxml = new KBinXML(rawData.ToArray());
Console.WriteLine(kbinxml.ToString());
Console.WriteLine(kbinxml);
//GenerateEchidnaSQL(kbinxml);
}

View File

@@ -0,0 +1,8 @@
{
"profiles": {
"eAmuseTest": {
"commandName": "Project",
"workingDirectory": "."
}
}
}