Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow playing uploaded sounds through the music player w/ Webroot #4934

Merged
merged 11 commits into from
Nov 22, 2023
104 changes: 72 additions & 32 deletions code/modules/admin/verbs/playsound.dm
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
/client/proc/play_web_sound()
set category = "Admin.Fun"
set name = "Play Internet Sound"
set name = "Play Admin Sound"
if(!check_rights(R_SOUNDS))
return

var/ytdl = CONFIG_GET(string/invoke_youtubedl)
if(!ytdl)
to_chat(src, SPAN_BOLDWARNING("Youtube-dl was not configured, action unavailable"), confidential = TRUE) //Check config.txt for the INVOKE_YOUTUBEDL value
var/sound_mode = tgui_input_list(src, "Play a sound from which source?", "Select Source", list("Youtube-DL", "Upload"))
if(!sound_mode)
return

var/web_sound_input = input("Enter content URL (supported sites only)", "Play Internet Sound via youtube-dl") as text|null
if(!istext(web_sound_input) || !length(web_sound_input))
return
var/list/data = list()
var/log_title = TRUE
var/web_sound_input
if(sound_mode == "Youtube-DL")
var/ytdl = CONFIG_GET(string/invoke_youtubedl)
if(!ytdl)
to_chat(src, SPAN_BOLDWARNING("Youtube-dl was not configured, action unavailable"), confidential = TRUE) //Check config.txt for the INVOKE_YOUTUBEDL value
return

web_sound_input = trim(web_sound_input)
web_sound_input = input("Enter content URL (supported sites only)", "Play Internet Sound via youtube-dl") as text|null
if(!istext(web_sound_input) || !length(web_sound_input))
return

if(findtext(web_sound_input, ":") && !findtext(web_sound_input, GLOB.is_http_protocol))
to_chat(src, SPAN_WARNING("Non-http(s) URIs are not allowed."))
to_chat(src, SPAN_WARNING("For youtube-dl shortcuts like ytsearch: please use the appropriate full url from the website."))
return
web_sound_input = trim(web_sound_input)

var/web_sound_url = ""
var/list/music_extra_data = list()
var/title
if(findtext(web_sound_input, ":") && !findtext(web_sound_input, GLOB.is_http_protocol))
to_chat(src, SPAN_WARNING("Non-http(s) URIs are not allowed."))
to_chat(src, SPAN_WARNING("For youtube-dl shortcuts like ytsearch: please use the appropriate full url from the website."))
return

var/list/output = world.shelleo("[ytdl] --geo-bypass --format \"bestaudio\[ext=mp3]/best\[ext=mp4]\[height<=360]/bestaudio\[ext=m4a]/bestaudio\[ext=aac]\" --dump-single-json --no-playlist -- \"[shell_url_scrub(web_sound_input)]\"")
var/errorlevel = output[SHELLEO_ERRORLEVEL]
var/stdout = output[SHELLEO_STDOUT]
var/stderr = output[SHELLEO_STDERR]
var/list/output = world.shelleo("[ytdl] --geo-bypass --format \"bestaudio\[ext=mp3]/best\[ext=mp4]\[height<=360]/bestaudio\[ext=m4a]/bestaudio\[ext=aac]\" --dump-single-json --no-playlist -- \"[shell_url_scrub(web_sound_input)]\"")
var/errorlevel = output[SHELLEO_ERRORLEVEL]
var/stdout = output[SHELLEO_STDOUT]
var/stderr = output[SHELLEO_STDERR]

if(errorlevel)
to_chat(src, SPAN_WARNING("Youtube-dl URL retrieval FAILED: [stderr]"))
return
if(errorlevel)
to_chat(src, SPAN_WARNING("Youtube-dl URL retrieval FAILED: [stderr]"))
return

var/list/data = list()
try
data = json_decode(stdout)
catch(var/exception/e)
to_chat(src, SPAN_WARNING("Youtube-dl JSON parsing FAILED: [e]: [stdout]"))
return
try
data = json_decode(stdout)
catch(var/exception/e)
to_chat(src, SPAN_WARNING("Youtube-dl JSON parsing FAILED: [e]: [stdout]"))
return

else if(sound_mode == "Upload")
var/current_transport = CONFIG_GET(string/asset_transport)
if(current_transport == "simple")
to_chat(src, SPAN_WARNING("Your server is not using an external asset transport. Use 'Play Midi Sound' instead to play sounds directly."))
return

var/soundfile = input(usr, "Choose a sound file to play", "Upload Sound") as null|file
if(!soundfile)
return

var/static/regex/only_extension = regex(@{"\.([a-z0-9]{1,5})$"}, "gi")
var/extension = only_extension.Replace("[soundfile]", "$1")
if(!length(extension))
to_chat(src, SPAN_WARNING("Invalid filename extension."))
return

var/static/regex/remove_extension = regex(@{"\.[a-z0-9]+$"}, "gi")
data["title"] = remove_extension.Replace("[soundfile]", "")

log_title = FALSE
web_sound_input = "[soundfile]"
var/static/playsound_notch = 1
var/asset_name = "admin_sound_[playsound_notch++].[extension]"
SSassets.transport.register_asset(asset_name, soundfile)
message_admins("[key_name_admin(src)] uploaded '[soundfile]' to asset transport.")

data["url"] = SSassets.transport.get_asset_url(asset_name)

var/title
var/web_sound_url = ""
var/list/music_extra_data = list()
if(data["url"])
music_extra_data["link"] = data["url"]
music_extra_data["title"] = data["title"]
Expand All @@ -58,7 +92,7 @@
"Meme" = SOUND_ADMIN_MEME,
"Atmospheric" = SOUND_ADMIN_ATMOSPHERIC
)
var/style = tgui_input_list(src, "Who do you want to play this to?", "Select Listeners", list("Globally", "Xenos", "Marines", "Ghosts", "All Inview", "Single Inview"))
var/style = tgui_input_list(src, "Who do you want to play this to?", "Select Listeners", list("Globally", "Xenos", "Marines", "Ghosts", "All Inview", "Single Inview", "Cancel"))
var/sound_type = tgui_input_list(src, "What kind of sound is this?", "Select Sound Type", sound_type_list)
sound_type = sound_type_list[sound_type]
switch(style)
Expand All @@ -80,6 +114,12 @@
else
return

switch(tgui_alert(src, "Show the name of this sound to the players?", "Sound Name", list("Yes","No","Cancel")))
if("No")
music_extra_data["title"] = "Admin sound"
if("Cancel")
return

for(var/i in targets)
var/mob/M = i
var/client/client = M?.client
Expand All @@ -88,12 +128,12 @@
else
client?.tgui_panel?.stop_music()

log_admin("[key_name(src)] played web sound: [web_sound_input] - [title] - [style]")
message_admins("[key_name_admin(src)] played web sound: [web_sound_input] - [title] - [style]")
log_admin("[key_name(src)] played web sound: [web_sound_input] -[log_title ? " [title] -" : ""] [style]")
message_admins("[key_name_admin(src)] played web sound: [web_sound_input] -[log_title ? " [title] -" : ""] [style]")

/client/proc/stop_web_sound()
set category = "Admin.Fun"
set name = "Stop Internet Sounds"
set name = "Stop Admin Sounds"

if(!check_rights(R_SOUNDS))
return
Expand Down
2 changes: 1 addition & 1 deletion code/modules/tgui_panel/audio.dm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/// Admin music volume, from 0 to 1.
/client/var/admin_music_volume = 1
/client/var/admin_music_volume = 0.2

/**
* public
Expand Down
2 changes: 1 addition & 1 deletion tgui/packages/tgui-panel/settings/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const initialState = {
fontFamily: FONTS[0],
lineHeight: 1.2,
theme: 'light',
adminMusicVolume: 0.5,
adminMusicVolume: 0.2,
// Keep these two state vars for compatibility with other servers
highlightText: '',
highlightColor: '#ffdd44',
Expand Down
Loading