Skip to content

Commit e74344a

Browse files
committed
Add streamcore compat layer with convar
Enable the wa_sc_compat convar to basically implement https://github.com/Vurv78/WebAudio/wiki/From-StreamCore-To-WebAudio#adaption-library in lua.
1 parent e6e2331 commit e74344a

2 files changed

Lines changed: 168 additions & 8 deletions

File tree

lua/autorun/webaudio.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ end
6767

6868
-- SERVER
6969
local WAAdminOnly = CreateConVar("wa_admin_only", "0", FCVAR_REPLICATED, "Whether creation of WebAudio objects should be limited to admins. 0 for everyone, 1 for admins, 2 for superadmins. wa_enable_sv takes precedence over this", 0, 2)
70+
local WASCCompat = CreateConVar("wa_sc_compat", "0", FCVAR_REPLICATED, "Whether streamcore-compatible functions should be generated for E2.", 0, 1)
7071

7172
-- Max in total is ~1023 from ID_LEN writing a 10 bit uint. Assuming ~30 players max using webaudio, can only give ~30.
7273
local WAMaxStreamsPerUser = CreateConVar("wa_stream_max", "5", FCVAR_REPLICATED, "Max number of streams a player can have at once.", 1, 30)
@@ -768,6 +769,7 @@ WebAudio.Common = {
768769
-- Server
769770
WAAdminOnly = WAAdminOnly,
770771
WAMaxStreamsPerUser = WAMaxStreamsPerUser,
772+
WASCCompat = WASCCompat,
771773

772774
-- Shared
773775
WAEnabled = WAEnabled,

lua/entities/gmod_wire_expression2/core/custom/webaudio.lua

Lines changed: 166 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ end
1616
local Common = WebAudio.Common
1717

1818
-- Convars
19-
local Enabled, AdminOnly, FFTEnabled = Common.WAEnabled, Common.WAAdminOnly, Common.WAFFTEnabled
19+
local Enabled, AdminOnly, FFTEnabled, SCCompat = Common.WAEnabled, Common.WAAdminOnly, Common.WAFFTEnabled, Common.WASCCompat
2020
local MaxStreams, MaxVolume, MaxRadius = Common.WAMaxStreamsPerUser, Common.WAMaxVolume, Common.WAMaxRadius
2121

2222
local StreamCounter = WireLib.RegisterPlayerTable()
@@ -237,11 +237,7 @@ cvars.AddChangeCallback("wa_stream_max", function(_cvar_name, _old, new)
237237
end, "wa_stream_max")
238238

239239
local function checkCounter(ply)
240-
local count = StreamCounter[ply] or 0
241-
if count < MaxStreams:GetInt() then
242-
return true
243-
end
244-
return false
240+
return (StreamCounter[ply] or 0) < MaxStreams:GetInt()
245241
end
246242

247243
--#endregion util
@@ -285,7 +281,7 @@ registerFunction("webAudioCanCreate", "", "n", function(self, args)
285281

286282
return (
287283
CreationBurst:check(ply)
288-
and checkCounter(ply, false)
284+
and checkCounter(ply)
289285
) and 1 or 0
290286
end)
291287

@@ -299,7 +295,7 @@ registerFunction("webAudioCanCreate", "s", "n", function(self, args)
299295

300296
return (
301297
CreationBurst:check(ply)
302-
and checkCounter(ply, false)
298+
and checkCounter(ply)
303299
and WebAudio.isWhitelistedURL(url)
304300
) and 1 or 0
305301
end)
@@ -847,10 +843,172 @@ end)
847843

848844
--#endregion getters
849845

846+
--#region compat
847+
if SCCompat:GetBool() then
848+
---@param index integer
849+
---@param volume number?
850+
---@param url string
851+
---@param ent GEntity
852+
local function start(self, index, ent, url, volume)
853+
local existing = self.data.lazy_mfs[index]
854+
if existing then
855+
existing:Destroy(true)
856+
end
857+
858+
local ply = self.player
859+
checkPermissions(self)
860+
861+
if not WebAudio.isWhitelistedURL(url) then
862+
if WebAudio.Common.CustomWhitelist then
863+
E2Lib.raiseException("This URL is not whitelisted! The server has a custom whitelist.", nil, self.trace)
864+
else
865+
E2Lib.raiseException("This URL is not whitelisted! See github.com/Vurv78/WebAudio/blob/main/WHITELIST.md", nil, self.trace)
866+
end
867+
end
868+
869+
-- Creation Time Quota
870+
if not CreationBurst:use(ply) then
871+
E2Lib.raiseException("You are creating WebAudios too fast. Check webAudioCanCreate before calling!", nil, self.trace)
872+
end
873+
874+
-- Stream Count Quota
875+
if not checkCounter(ply) then
876+
E2Lib.raiseException("Reached maximum amount of WebAudio streams! Check webAudioCanCreate or webAudiosLeft before calling!", nil, self.trace)
877+
end
878+
879+
local wa = registerStream(self, url, ply)
880+
wa:SetParent(ent)
881+
self.data.lazy_mfs[index] = wa
882+
883+
timer.Simple(0, function()
884+
wa:Play()
885+
end)
886+
887+
return 1
888+
end
889+
890+
__e2setcost(500)
891+
892+
registerFunction("streamHelp", "", "n", function(self, args)
893+
self.player:PrintMessage(HUD_PRINTTALK, "Streamcore -> WebAudio: https://github.com/Vurv78/WebAudio/wiki/From-StreamCore-To-WebAudio")
894+
end)
895+
896+
registerFunction("streamCanStart", "", "n", function(self, args)
897+
local ply = self.player
898+
899+
return (
900+
CreationBurst:check(ply)
901+
and checkCounter(ply)
902+
) and 1 or 0
903+
end)
904+
905+
--! For you mfs who are too lazy to use the [Adaption Library](https://github.com/Vurv78/WebAudio/wiki/From-StreamCore-To-WebAudio#adaption-library)
906+
registerFunction("streamStart", "e:ns", "n", function(self, args)
907+
local op1, op2, op3 = args[2], args[3], args[4]
908+
local ent, idx, url = op1[1](self, op1), op2[1](self, op2), op3[1](self, op2)
909+
910+
return start(self, idx, ent, url)
911+
end)
912+
913+
registerFunction("streamStart", "e:nsn", "n", function(self, args)
914+
local op1, op2, op3, op4 = args[2], args[3], args[4], args[5]
915+
local ent, idx, url, volume = op1[1](self, op1), op2[1](self, op2), op3[1](self, op2), op4[1](self, op4)
916+
return start(self, idx, ent, url, volume)
917+
end)
918+
919+
registerFunction("streamStart", "e:nns", "n", function(self, args)
920+
local op1, op2, op3, op4 = args[2], args[3], args[4], args[5]
921+
local ent, idx, volume, url = op1[1](self, op1), op2[1](self, op2), op3[1](self, op2), op4[1](self, op4)
922+
923+
return start(self, idx, ent, url, volume)
924+
end)
925+
926+
registerFunction("streamStop", "n", "n", function(self, args)
927+
local op1 = args[2]
928+
local idx = op1[1](self, op1)
929+
930+
checkPermissions(self)
931+
932+
local wa = self.data.lazy_mfs[idx]
933+
if wa then
934+
wa:Destroy(true)
935+
return 1
936+
else
937+
return self:throw("Invalid stream!", 0)
938+
end
939+
end)
940+
941+
registerFunction("streamVolume", "nn", "n", function(self, args)
942+
local op1, op2 = args[2], args[3]
943+
local idx, volume = op1[1](self, op1), op2[1](self, op2)
944+
945+
checkPermissions(self)
946+
947+
local wa = self.data.lazy_mfs[idx]
948+
if wa then
949+
return wa:SetVolume( math.min(volume, MaxVolume:GetInt() / 100) )
950+
and wa:Transmit(false)
951+
and 1 or 0
952+
else
953+
return self:throw("Invalid stream!", 0)
954+
end
955+
end)
956+
957+
registerFunction("streamRadius", "nn", "n", function(self, args)
958+
local op1, op2 = args[2], args[3]
959+
local idx, radius = op1[1](self, op1), op2[1](self, op2)
960+
961+
checkPermissions(self)
962+
963+
local wa = self.data.lazy_mfs[idx]
964+
if wa then
965+
return wa:SetRadius( math.min(radius, MaxRadius:GetInt()) )
966+
and wa:Transmit(false)
967+
and 1 or 0
968+
else
969+
return self:throw("Invalid stream!", 0)
970+
end
971+
end)
972+
973+
-- How many seconds to wait in between streamStart calls:
974+
registerFunction("streamLimit", "", "n", function(self, args)
975+
return CREATE_REGEN
976+
end)
977+
978+
registerFunction("streamMaxRadius", "", "n", function(self, args)
979+
return MaxRadius:GetInt()
980+
end)
981+
982+
registerFunction("streamAdminOnly", "", "n", function(self, args)
983+
return AdminOnly
984+
end)
985+
986+
registerFunction("streamDisable3D", "n", "n", function(self, args)
987+
local op1 = args[2]
988+
local idx = op1[1](self, op1)
989+
990+
checkPermissions(self)
991+
992+
local wa = self.data.lazy_mfs[idx]
993+
if wa then
994+
return wa:Set3DEnabled(false)
995+
and wa:Transmit(false)
996+
and 1 or 0
997+
else
998+
return self:throw("Invalid stream!", 0)
999+
end
1000+
end)
1001+
end
1002+
--#endregion
1003+
8501004
--#region cleanup
8511005

8521006
registerCallback("construct", function(self)
8531007
self.data.webaudio_streams = {}
1008+
1009+
if SCCompat:GetBool() then
1010+
self.data.lazy_mfs = {}
1011+
end
8541012
end)
8551013

8561014
registerCallback("destruct", function(self)

0 commit comments

Comments
 (0)