Skip to content

Commit

Permalink
[ci skip] Refactors cassettes and the cassette player
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Dec 15, 2024
1 parent 3642c34 commit c88299f
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 412 deletions.
6 changes: 6 additions & 0 deletions code/__HELPERS/~monkestation-helpers/text.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Checks to see if a string starts with http:// or https://
/proc/is_http_protocol(text)
var/static/regex/http_regex
if(isnull(http_regex))
http_regex = new("^https?://")
return findtext(text, http_regex)
3 changes: 0 additions & 3 deletions code/_globalvars/_regexes.dm
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//These are a bunch of regex datums for use /((any|every|no|some|head|foot)where(wolf)?\sand\s)+(\.[\.\s]+\s?where\?)?/i
GLOBAL_DATUM_INIT(is_http_protocol, /regex, regex("^https?://"))
GLOBAL_DATUM_INIT(is_http_protocol_non_secure, /regex, regex("^http?://"))


GLOBAL_DATUM_INIT(is_website, /regex, regex("http|www.|\[a-z0-9_-]+.(com|org|net|mil|edu)+", "i"))
GLOBAL_DATUM_INIT(is_email, /regex, regex("\[a-z0-9_-]+@\[a-z0-9_-]+.\[a-z0-9_-]+", "i"))
Expand Down
4 changes: 2 additions & 2 deletions code/modules/admin/verbs/playsound.dm
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
message_admins("[key_name(user)] stopped web sounds.")
web_sound_url = null
stop_web_sounds = TRUE
if(web_sound_url && !findtext(web_sound_url, GLOB.is_http_protocol))
if(web_sound_url && !is_http_protocol(web_sound_url))
tgui_alert(user, "The media provider returned a content URL that isn't using the HTTP or HTTPS protocol. This is a security risk and the sound will not be played.", "Security Risk", list("OK"))
to_chat(user, span_boldwarning("BLOCKED: Content URL not using HTTP(S) Protocol!"), confidential = TRUE)

Expand Down Expand Up @@ -183,7 +183,7 @@

if(length(web_sound_input))
web_sound_input = trim(web_sound_input)
if(findtext(web_sound_input, ":") && !findtext(web_sound_input, GLOB.is_http_protocol))
if(findtext(web_sound_input, ":") && !is_http_protocol(web_sound_input))
to_chat(src, span_boldwarning("Non-http(s) URIs are not allowed."), confidential = TRUE)
to_chat(src, span_warning("For youtube-dl shortcuts like ytsearch: please use the appropriate full URL from the website."), confidential = TRUE)
return
Expand Down
2 changes: 1 addition & 1 deletion code/modules/requests/request_manager.dm
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ GLOBAL_DATUM_INIT(requests, /datum/request_manager, new)
if(request.req_type != REQUEST_INTERNET_SOUND)
to_chat(usr, "Request doesn't have a sound to play.", confidential = TRUE)
return TRUE
if(findtext(request.message, ":") && !findtext(request.message, GLOB.is_http_protocol))
if(findtext(request.message, ":") && !is_http_protocol(request.message))
to_chat(usr, "Request is not a valid URL.", confidential = TRUE)
return TRUE

Expand Down
129 changes: 70 additions & 59 deletions monkestation/code/modules/cassettes/cassette_db/cassette_datum.dm
Original file line number Diff line number Diff line change
@@ -1,61 +1,72 @@
/datum/cassette_data
var/cassette_name
var/cassette_author
var/cassette_desc
var/cassette_author_ckey

var/cassette_design_front
var/cassette_design_back

var/list/songs

var/list/song_names

var/cassette_id
var/approved
var/file_name


/datum/cassette_data/proc/populate_data(file_id)
var/file = file("data/cassette_storage/[file_id].json")
if(!fexists(file))
return FALSE
var/list/data = json_decode(file2text(file))

cassette_name = data["name"]
cassette_desc = data["desc"]

cassette_design_front = data["side1_icon"]
cassette_design_back = data["side2_icon"]

songs = data["songs"]

song_names = data["song_names"]

cassette_author = data["author_name"]
cassette_author_ckey = data["author_ckey"]

cassette_id = file_id

/datum/cassette
/// The name of the cassette.
var/name
/// The description of the cassette.
var/desc
/// The unique ID of the cassette.
var/id
/// If the cassette is approved or not.
var/approved = FALSE
/// Information about the author of this cassette.
var/datum/cassette_author/author

/// The front side of the cassette.
var/datum/cassette_side/front
/// The back side of the cassette.
var/datum/cassette_side/back

/datum/cassette/New()
. = ..()
author = new
front = new
back = new

/// Imports cassette date from the old format.
/datum/cassette/proc/import_old_format(list/data)
name = data["name"]
desc = data["desc"]
approved = data["approved"]

file_name = "data/cassette_storage/[file_id].json"

return TRUE

/datum/cassette_data/proc/generate_cassette(turf/location)
if(!location)
return
var/obj/item/device/cassette_tape/new_tape = new(location)
new_tape.name = cassette_name
new_tape.cassette_desc_string = cassette_desc
new_tape.icon_state = cassette_design_front
new_tape.side1_icon = cassette_design_front
new_tape.side2_icon = cassette_design_back
new_tape.songs = songs
new_tape.song_names = song_names
new_tape.author_name = cassette_author
new_tape.ckey_author = cassette_author_ckey
new_tape.approved_tape = approved

new_tape.update_appearance()
author.name = data["author_name"]
author.ckey = ckey(data["author_ckey"])

for(var/i in 1 to 2)
var/datum/cassette_side/side = get_side(i % 2) // side2 = 0, side1 = 1
var/side_name = "side[i]"
var/list/song_urls = data["songs"][side_name]
var/list/song_names = data["song_names"][side_name]
if(length(song_urls) != length(song_names))
stack_trace("amount of song urls for [side_name] ([length(song_urls)]) did not match amount of song names for [side_name] ([length(song_names)])")
continue
side.design = data["[side_name]_icon"]
for(var/idx in 1 to length(song_urls))
side.songs += new /datum/cassette_song(song_names[idx], song_urls[idx])

/// Simple helper to get a side of the cassette.
/// TRUE is front side, FALSE is back side.
/datum/cassette/proc/get_side(front_side = TRUE) as /datum/cassette_side
RETURN_TYPE(/datum/cassette_side)
return front_side ? front : back

/datum/cassette_author
/// The character name of the cassette author.
var/name
/// The ckey of the cassette author.
var/ckey

/datum/cassette_side
/// The design of this side of the cassette.
var/design = "cassette_flip"
/// The songs on this side of the cassette.
var/list/datum/cassette_song/songs = list()

/datum/cassette_song
/// The name of the song.
var/name
/// The URL of the song.
var/url

/datum/cassette_song/New(name, url)
. = ..()
src.name = name
src.url = url
Loading

0 comments on commit c88299f

Please sign in to comment.