-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMusicDatabase.cs
More file actions
111 lines (89 loc) · 4.34 KB
/
MusicDatabase.cs
File metadata and controls
111 lines (89 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace MusicDisplay;
#nullable enable
internal class MusicDatabase : ModSystem
{
private static MusicText UnknownTrack;
private Dictionary<short, MusicText> _tracksById = [];
public override void PostAddRecipes()
{
for (short i = 1; i < MusicID.Count; ++i)
{
var trackName = Language.GetText("Mods.MusicDisplay.TrackNames.Vanilla." + i);
var byLine = Language.GetText("Mods.MusicDisplay.TrackNames.VanillaByLine");
var terraria = Language.GetText("Mods.MusicDisplay.TrackNames.TerrariaName");
if (i is >= MusicID.OtherworldlyRain and <= MusicID.OtherworldlyHallow)
{
terraria = Language.GetText("Mods.MusicDisplay.TrackNames.OtherworldName");
if (i == MusicID.OtherworldlyJungle)
byLine = Language.GetText("Mods.MusicDisplay.TrackNames.OtherworldTinaGuo");
else if (i == MusicID.OtherworldlyUnderground)
byLine = Language.GetText("Mods.MusicDisplay.TrackNames.OtherworldJeffBall");
else if (i == MusicID.OtherworldlyBoss2 || i == MusicID.OtherworldlyLunarBoss)
byLine = Language.GetText("Mods.MusicDisplay.TrackNames.OtherworldFrankKlepacki");
else
byLine = Language.GetText("Mods.MusicDisplay.TrackNames.OtherworldByLine");
}
_tracksById.Add(i, new(trackName, byLine, terraria));
}
UnknownTrack = new MusicText(Language.GetText("Mods.MusicDisplay.TrackNames.UnknownTrack"), LocalizedText.Empty, LocalizedText.Empty, true);
}
public override void Unload() => _tracksById = null!;
internal static MusicText GetMusicText(short lastMusicSlot)
{
var db = ModContent.GetInstance<MusicDatabase>();
return !db._tracksById.TryGetValue(lastMusicSlot, out MusicText value) ? UnknownTrack : value;
}
internal static void AddMusic(object id, object name, object subTitle)
=> throw new Exception("[MusicDisplay] Use the short, LocalizedText, LocalizedText, LocalizedText overload! This overload is outdated.");
internal static void AddMusic(object id, object name, object author, object subTitle, object displayCondition, object? color = null)
{
if (id is not short realID)
throw new ArgumentException("id is not a short!");
if (author is not LocalizedText realAuthor)
{
if (author is string strAuth)
realAuthor = Language.GetText(strAuth);
else
throw new ArgumentException("author is not a LocalizedText or a localization key!");
}
if (name is not LocalizedText realName)
{
if (name is string strName)
realName = Language.GetText(strName);
else
throw new ArgumentException("name is not a LocalizedText or a localization key!");
}
if (subTitle is not LocalizedText realSub)
{
if (subTitle is string strSub)
realSub = Language.GetText(strSub);
else
throw new ArgumentException("subTitle is not a LocalizedText or a localization key!");
}
Func<bool>? realDisplayCondition = null;
if (displayCondition is not null)
{
if (displayCondition is Func<bool> condition)
realDisplayCondition = condition;
else
throw new ArgumentException("displayCondition must be a Func<bool>!");
}
var db = ModContent.GetInstance<MusicDatabase>();
db._tracksById.Add(realID, new MusicText(realName, realAuthor, realSub, shouldDisplay: realDisplayCondition, colors: (Color[]?)color));
}
internal static object GetMusicInfo(short id)
{
if (id == 0)
throw new ArgumentException("A music ID of 0 isn't valid!");
if (!ModContent.GetInstance<MusicDatabase>()._tracksById.TryGetValue(id, out var text) || text.IsUnknown)
throw new ArgumentException($"Music ID {id} isn't registered, or is the placeholder \"Unknown\" track.");
object[] info = [text.MainText, text.Author, text.Subtitle, text.ShouldDisplay];
return info;
}
}