custom music, ability to map files to resource keys

This commit is contained in:
Nahuel Rocchetti 2024-06-24 22:39:22 -03:00
parent 02b1afeb35
commit bf63822e4f
3 changed files with 65 additions and 12 deletions

View file

@ -1,10 +1,13 @@
using OpenTS2.Common;
using OpenTS2.Common.Utils;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
using OpenTS2.Engine;
using OpenTS2.Files;
using OpenTS2.Files.Formats.DBPF;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -25,6 +28,7 @@ namespace OpenTS2.Audio
[GameProperty(true)]
public static bool MuteAudioOnFocusLoss = true;
public static Dictionary<ResourceKey, string> CustomSongNames;
public static List<ResourceKey> AudioAssets { get; private set; }
public static Action OnInitialized;
private static Dictionary<uint, ResourceKey> AudioAssetByLowInstanceID;
@ -37,8 +41,28 @@ namespace OpenTS2.Audio
Core.OnFinishedLoading += Initialize;
}
private void LoadCustomMusic()
{
CustomSongNames = new Dictionary<ResourceKey, string>();
var musicDir = Path.Combine(Filesystem.GetUserPath(), "Music");
var stationDirs = Directory.GetDirectories(musicDir);
foreach(var stationDir in stationDirs)
{
var stationName = Path.GetFileName(stationDir);
var audioFiles = Directory.GetFiles(stationDir, "*.*").Where(file => file.ToLowerInvariant().EndsWith(".mp3") || file.ToLowerInvariant().EndsWith(".wav"));
foreach(var audioFile in audioFiles)
{
var songName = Path.GetFileNameWithoutExtension(audioFile);
var key = new ResourceKey(songName, FileUtils.LowHash(stationName), TypeIDs.AUDIO);
ContentProvider.MapFileToResource(audioFile, key);
CustomSongNames[key] = songName;
}
}
}
private void Initialize()
{
LoadCustomMusic();
AudioAssetByLowInstanceID = new Dictionary<uint, ResourceKey>();
AudioAssetByInstanceID = new Dictionary<ResourceKey, ResourceKey>();
AudioAssets = ContentProvider.ResourceMap.Keys.Where(key => key.TypeID == TypeIDs.AUDIO || key.TypeID == TypeIDs.HITLIST).ToList();

View file

@ -98,6 +98,12 @@ namespace OpenTS2.Audio
foreach(var key in resourceKeys)
{
var localizedName = key.ToString();
if (AudioManager.CustomSongNames.TryGetValue(key, out var customSongName))
{
localizedName = customSongName;
}
else
{
foreach (var musicTitle in musicTitles)
{
var stringSet = _contentProvider.GetAsset<StringSetAsset>(musicTitle);
@ -113,6 +119,7 @@ namespace OpenTS2.Audio
}
}
}
}
var song = new Song(key, localizedName);
songList.Add(song);
}

View file

@ -10,6 +10,7 @@ using OpenTS2.Files;
using OpenTS2.Files.Formats.DBPF;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -34,6 +35,7 @@ namespace OpenTS2.Content
get { return _contentEntries; }
}
private readonly Dictionary<ResourceKey, string> _fileMap = new Dictionary<ResourceKey, string>();
private readonly Dictionary<ResourceKey, DBPFEntry> _resourceMap = new Dictionary<ResourceKey, DBPFEntry>();
private readonly List<DBPFFile> _contentEntries = new List<DBPFFile>();
private readonly Dictionary<uint, DBPFFile> _entryByGroupID = new Dictionary<uint, DBPFFile>();
@ -50,12 +52,32 @@ namespace OpenTS2.Content
this.Cache = new ContentCache(this);
}
public void MapFileToResource(string path, ResourceKey key)
{
_fileMap[key] = Filesystem.GetRealPath(path);
var entry = new DBPFEntry();
entry.TGI = key;
_resourceMap[key] = entry;
}
AbstractAsset InternalLoadAsset(CacheKey key)
{
if (key.File == null)
return null;
if (key.File != null)
{
return key.File.GetAssetByTGI(key.TGI);
}
else
{
if (_fileMap.TryGetValue(key.TGI, out var result))
{
var codec = Codecs.Get(key.TGI.TypeID);
var fileBytes = File.ReadAllBytes(result);
var asset = codec.Deserialize(fileBytes, key.TGI, null);
return asset;
}
}
return null;
}
/// <summary>
/// Gets all entries of a specific type.