mirror of
https://github.com/LazyDuchess/OpenTS2.git
synced 2025-01-22 08:11:47 -05:00
custom music, ability to map files to resource keys
This commit is contained in:
parent
02b1afeb35
commit
bf63822e4f
3 changed files with 65 additions and 12 deletions
|
@ -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();
|
||||
|
|
|
@ -98,18 +98,25 @@ namespace OpenTS2.Audio
|
|||
foreach(var key in resourceKeys)
|
||||
{
|
||||
var localizedName = key.ToString();
|
||||
foreach(var musicTitle in musicTitles)
|
||||
if (AudioManager.CustomSongNames.TryGetValue(key, out var customSongName))
|
||||
{
|
||||
var stringSet = _contentProvider.GetAsset<StringSetAsset>(musicTitle);
|
||||
var englishStrings = stringSet.StringData.Strings[Languages.USEnglish];
|
||||
|
||||
for (var i = 0; i < englishStrings.Count; i++)
|
||||
localizedName = customSongName;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var musicTitle in musicTitles)
|
||||
{
|
||||
var englishString = englishStrings[i].Value;
|
||||
var hiHash = FileUtils.HighHash(englishString);
|
||||
if (hiHash == key.InstanceHigh)
|
||||
var stringSet = _contentProvider.GetAsset<StringSetAsset>(musicTitle);
|
||||
var englishStrings = stringSet.StringData.Strings[Languages.USEnglish];
|
||||
|
||||
for (var i = 0; i < englishStrings.Count; i++)
|
||||
{
|
||||
localizedName = stringSet.GetString(i);
|
||||
var englishString = englishStrings[i].Value;
|
||||
var hiHash = FileUtils.HighHash(englishString);
|
||||
if (hiHash == key.InstanceHigh)
|
||||
{
|
||||
localizedName = stringSet.GetString(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,11 +52,31 @@ 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;
|
||||
return key.File.GetAssetByTGI(key.TGI);
|
||||
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>
|
||||
|
|
Loading…
Reference in a new issue