-
Notifications
You must be signed in to change notification settings - Fork 3
/
MSBTMedia.lua
173 lines (131 loc) · 6.69 KB
/
MSBTMedia.lua
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
-------------------------------------------------------------------------------
-- Title: Mik's Scrolling Battle Text Media
-- Author: Mikord
-------------------------------------------------------------------------------
-- Create module and set its name.
local module = {}
local moduleName = "Media"
MikSBT[moduleName] = module
-------------------------------------------------------------------------------
-- Imports.
-------------------------------------------------------------------------------
-- Local references to various modules for faster access.
local MSBTProfiles = MikSBT.Profiles
local L = MikSBT.translations
-- Local references to various functions for faster access.
local string_sub = string.sub
local string_len = string.len
-------------------------------------------------------------------------------
-- Constants.
-------------------------------------------------------------------------------
-- The default sound files to use.
local DEFAULT_SOUND_FILES = {
["MSBT Low Health"] = "Interface\\Addons\\MikScrollingBattleText\\Sounds\\LowHealth.ogg",
["MSBT Low Mana"] = "Interface\\Addons\\MikScrollingBattleText\\Sounds\\LowMana.ogg",
["MSBT Cooldown"] = "Interface\\Addons\\MikScrollingBattleText\\Sounds\\Cooldown.ogg",
}
-- Set the default font files to use to the locale specific fonts.
local DEFAULT_FONT_FILES = L.FONT_FILES
-- LibSharedMedia support.
local SML = LibStub("LibSharedMedia-3.0")
local SML_LANG_MASK_ALL = 255
-------------------------------------------------------------------------------
-- Private variables.
-------------------------------------------------------------------------------
local fonts = {}
local sounds = {}
-------------------------------------------------------------------------------
-- Font functions.
-------------------------------------------------------------------------------
-- ****************************************************************************
-- Registers a font.
-- See the included API.html file for usage info.
-- ****************************************************************************
local function RegisterFont(fontName, fontPath)
-- Don't do anything if the font name or font path is invalid.
if (type(fontName) ~= "string" or type(fontPath) ~= "string") then return end
if (fontName == "" or fontPath == "") then return end
-- Register with MSBT and shared media.
fonts[fontName] = fontPath
SML:Register("font", fontName, fontPath, SML_LANG_MASK_ALL)
end
-- ****************************************************************************
-- Returns an iterator for the table containing the registered fonts.
-- See the included API.html file for usage info.
-- ****************************************************************************
local function IterateFonts()
return pairs(fonts)
end
-------------------------------------------------------------------------------
-- Sound functions.
-------------------------------------------------------------------------------
-- ****************************************************************************
-- Registers a sound.
-- See the included API.html file for usage info.
-- ****************************************************************************
local function RegisterSound(soundName, soundPath)
-- Don't do anything if the sound name or sound path is invalid.
-- Allow non-string entries for soundPath as of Patch 8.2.0.
if (type(soundName) ~= "string") then return end
-- Ensure that the custom file path is either a number (FileDataID)
-- Or a string that begins with "Interface" and ends with either ".mp3" or ".ogg"
local soundPathLower = string.lower(soundPath)
if (not soundPath or soundName == "" or soundPath == "" or (type(soundPath) == "string" and ((string.find(soundPathLower, "interface") or 0) ~= 1 or (not string.find(soundPathLower, ".mp3") and not string.find(soundPathLower, ".ogg"))))) then
return
end
-- Register with MSBT.
sounds[soundName] = soundPath
-- Register with shared media.
SML:Register("sound", soundName, soundPath)
end
-- ****************************************************************************
-- Returns an iterator for the table containing the registered sounds.
-- See the included API.html file for usage info.
-- ****************************************************************************
local function IterateSounds()
return pairs(sounds)
end
-------------------------------------------------------------------------------
-- Event handlers.
-------------------------------------------------------------------------------
-- ****************************************************************************
-- Called by shared media when media is registered.
-- ****************************************************************************
local function SMLRegistered(event, mediaType, name)
if (mediaType == "font") then
fonts[name] = SML:Fetch(mediaType, name)
elseif (mediaType == "sound") then
sounds[name] = SML:Fetch(mediaType, name)
end
end
-- ****************************************************************************
-- Called when the mod variables are initialized.
-- ****************************************************************************
local function OnVariablesInitialized()
-- Register custom fonts and sounds.
for fontName, fontPath in pairs(MSBTProfiles.savedMedia.fonts) do RegisterFont(fontName, fontPath) end
for soundName, soundPath in pairs(MSBTProfiles.savedMedia.sounds) do RegisterSound(soundName, soundPath) end
end
-------------------------------------------------------------------------------
-- Initialization.
-------------------------------------------------------------------------------
-- Register default fonts and sounds.
for fontName, fontPath in pairs(DEFAULT_FONT_FILES) do RegisterFont(fontName, fontPath) end
for soundName, soundPath in pairs(DEFAULT_SOUND_FILES) do RegisterSound(soundName, soundPath) end
-- Register the currently available fonts and sounds in shared media with MSBT.
for index, fontName in pairs(SML:List("font")) do fonts[fontName] = SML:Fetch("font", fontName) end
for index, soundName in pairs(SML:List("sound")) do sounds[soundName] = SML:Fetch("sound", soundName) end
-- Register a callback with shared media to keep MSBT synced.
SML.RegisterCallback("MSBTSharedMedia", "LibSharedMedia_Registered", SMLRegistered)
-------------------------------------------------------------------------------
-- Module interface.
-------------------------------------------------------------------------------
-- Protected Variables.
module.fonts = fonts
module.sounds = sounds
-- Protected Functions.
module.RegisterFont = RegisterFont
module.RegisterSound = RegisterSound
module.IterateFonts = IterateFonts
module.IterateSounds = IterateSounds
module.OnVariablesInitialized = OnVariablesInitialized