Skip to content

Instantly share code, notes, and snippets.

@moonexpr
Last active May 28, 2017 18:24
Show Gist options
  • Save moonexpr/a42dffe41975ad51ef4064ff3bcf917c to your computer and use it in GitHub Desktop.
Save moonexpr/a42dffe41975ad51ef4064ff3bcf917c to your computer and use it in GitHub Desktop.
Assists with sound assets over HTTP[s]
/**
mgr = soundmanager:Create ()
mgr:SetSource ("https://wiki.teamfortress.com/w/images/e/ef/Vote_started.wav")
mgr:Play ()
**/
soundmanager = {}
soundmanager.__index = soundmanager
function soundmanager:Create ()
self = {}
setmetatable (self, soundmanager)
return self
end
function soundmanager:SetSource (strPath)
self ["_SOURCE"] = strPath
end
function soundmanager:GetSource ()
return self ["_SOURCE"]
end
function soundmanager:GetHash ()
if not self:GetSource () then
return ""
end
return util.CRC (self:GetSource ())
end
function soundmanager:SetContents (strData)
self ["_CONTENTS"] = strData
end
function soundmanager:GetContents (strData)
return self ["_CONTENTS"]
end
function soundmanager:IsHTTP ()
if not self:GetSource () then
return false
end
return tobool (self:GetSource ():find ("http"))
end
function soundmanager:IsLocal ()
if not self:GetSource () then
return false
end
tblResults = file.Find (self:GetSource (), "GAME")
return tobool (tblResults and #tblResults > 0)
end
function soundmanager:IsSaved ()
tblAudio = file.Find (string.format ("ISoundManager/%s audio.dat", self:GetHash ()), "DATA")
tblMeta = file.Find (string.format ("ISoundManager/%s jmeta.dat", self:GetHash ()), "DATA")
return tobool (tblAudio and #tblAudio > 0) and tobool (tblMeta and #tblMeta > 0)
end
function soundmanager:FailedDownload (strMessage)
error (string.format ("[soundmanager] HTTP request failed to download media: %s [%s]", strMessage, self:GetSource () or "missing source [??]"))
end
function soundmanager:SuccessDownload (iCode, strData)
if not self:GetSource () then
error ("Invalid Sound!")
end
hook.Call ("SoundDownloaded", GAMEMODE, self:GetSource (), strData)
hook.Call ("Downloaded", self, self:GetSource (), strData)
if self.OnDownload then
self:OnDownload ()
end
self:SetContents (strData)
self:Save ()
end
function soundmanager:Download ()
if self:IsHTTP () then
if not self:GetSource () then
error ("cannot download media because there is no set source!")
end
funcSuccess = function (iCode, strData)
self:SuccessDownload (iCode, strData)
end
funcFailed = function (iCode, strData)
self:SuccessDownload (iCode, strData)
end
self:GetSource ()
HTTP ({
method = "get",
url = self:GetSource (),
failed = funcFailed,
success = funcSuccess,
})
elseif self:IsLocal () then
strData = file.Read (self:GetSource ())
self.SuccessDownload (200, strData)
else
error ("[soundmanager] Invalid Source!")
end
end
function soundmanager:GetMeta ()
return {
["_SOURCE"] = self:GetSource (),
["_TIMESTAMP"] = os.time (),
}
end
function soundmanager:Save ()
if not self:GetContents () then
error ("[soundmanager] Attempted to truncate an empty file!")
end
_, tblDir = file.Find ("ISoundManager/*", "DATA")
if not tblDir or #tblDir == 0 then
file.CreateDir ("ISoundManager")
end
file.Write (string.format ("ISoundManager/%s audio.dat", self:GetHash ()), self:GetContents ())
file.Write (string.format ("ISoundManager/%s jmeta.dat", self:GetHash ()), util.TableToJSON (self:GetMeta ()))
hook.Call ("SoundSaved", GAMEMODE, self:GetHash (), self:GetSource ())
hook.Call ("Saved", self, self:GetHash (), self:GetSource ())
if self.OnSaved then
self:OnSaved ()
end
end
function soundmanager:Play (funcCallback, strOption)
if not self:IsSaved () and not soundmanager ["_RECURPROTECT"] then
soundmanager ["_RECURPROTECT"] = true
self:Download ()
function self:OnSaved ()
self:Play ()
end
elseif not self:IsSaved () and soundmanager ["_RECURPROTECT"] then
error ("[soundmanager] Maximum download attempts reached.")
end
if not funcCallback then
funcCallback = function (IGModAudioChannel)
IGModAudioChannel:Play ()
end
end
if self.OnPlay then
self:OnPlay ()
end
sound.PlayFile (string.format ("data/ISoundManager/%s audio.dat", self:GetHash ()), strOption or "mono", funcCallback)
end
net.Receive ("ISoundManager/Play", function ()
mgr = soundmanager:Create ()
mgr:SetSource (net.ReadString ())
mgr:Play ()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment