Skip to content

Commit

Permalink
refactor: send title screen image only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaxeer committed Jan 28, 2024
1 parent ff9303e commit bb79414
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 67 deletions.
1 change: 0 additions & 1 deletion modular_ss220/title_screen/code/_title_screen_defines.dm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#define DEFAULT_TITLE_SCREEN_IMAGE_NAME "default.gif"
#define DEFAULT_TITLE_SCREEN_IMAGE_PATH 'modular_ss220/title_screen/icons/default.gif'

#define DEFAULT_TITLE_HTML {"
Expand Down
2 changes: 1 addition & 1 deletion modular_ss220/title_screen/code/dead.dm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/mob/dead/Login()
. = ..()
hide_title_screen()
SStitle.hide_title_screen_from(client)
2 changes: 1 addition & 1 deletion modular_ss220/title_screen/code/living.dm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/mob/living/Login()
. = ..()
hide_title_screen()
SStitle.hide_title_screen_from(client)
2 changes: 1 addition & 1 deletion modular_ss220/title_screen/code/new_player.dm
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/mob/new_player/Login()
. = ..()
SStitle.show_title_screen(src)
SStitle.show_title_screen_to(client)
12 changes: 5 additions & 7 deletions modular_ss220/title_screen/code/title_screen_controls.dm
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
if(!file)
return

message_admins("Custom title Screen: [file]")
// SStitle.change_title_screen(file)
SStitle.set_title_image(file)
if("Reset")
SStitle.change_title_screen()
SStitle.set_title_image()
if("Cancel")
return

Expand Down Expand Up @@ -54,9 +53,9 @@
set category = "Special Verbs"

if(istype(mob, /mob/new_player))
SStitle.current_title_screen.show_to(mob)
SStitle.show_title_screen_to(src)
else
SStitle.current_title_screen.hide_from(mob)
SStitle.hide_title_screen_from(src)

/**
* An admin debug command that enables you to change the HTML on the go.
Expand All @@ -76,7 +75,6 @@
if(!new_html)
return

SStitle.title_html = new_html
SStitle.show_title_screen()
SStitle.set_title_html(new_html)

message_admins("[key_name_admin(usr)] has changed the title screen HTML.")
25 changes: 12 additions & 13 deletions modular_ss220/title_screen/code/title_screen_datum.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@
/// The current title screen being displayed, as `/datum/asset_cache_item`
var/datum/asset_cache_item/screen_image

/datum/title_screen/New(title_html, notice, screen_image_file_name, screen_image_file)
/datum/title_screen/New(title_html = DEFAULT_TITLE_HTML, notice, screen_image_file)
src.title_html = title_html
src.notice = notice
set_screen_image(screen_image_file_name, screen_image_file)
set_screen_image(screen_image_file)

/datum/title_screen/proc/set_screen_image(screen_image_file)
if(!screen_image_file)
return

/datum/title_screen/proc/set_screen_image(screen_image_file_name, screen_image_file)
if(!isfile(screen_image_file))
screen_image_file = fcopy_rsc(screen_image_file)

screen_image = SSassets.transport.register_asset(screen_image_file_name, screen_image_file)
screen_image = SSassets.transport.register_asset("[screen_image_file]", screen_image_file)

/datum/title_screen/proc/show_to(client/viewer)
if(ismob(viewer))
var/mob/viewer_mob = viewer
viewer = viewer_mob.client

if(!viewer)
return

Expand All @@ -31,20 +30,20 @@
var/datum/asset/lobby_asset = get_asset_datum(/datum/asset/simple/lobby_fonts)
lobby_asset.send(viewer)

SSassets.transport.send_assets(viewer, screen_image)
SSassets.transport.send_assets(viewer, screen_image.name)

viewer << browse(get_title_html(), "window=title_browser")

/datum/title_screen/proc/hide_from(client/viewer)
if(client?.mob)
winset(client, "title_browser", "is-disabled=true;is-visible=false")
winset(client, "status_bar", "is-visible=true")
if(viewer?.mob)
winset(viewer, "title_browser", "is-disabled=true;is-visible=false")
winset(viewer, "status_bar", "is-visible=true")

/**
* Get the HTML of title screen.
*/
/datum/title_screen/proc/get_title_html()
var/list/html = list()
var/list/html = list(title_html)

var/screen_image_url = SSassets.transport.get_asset_url(asset_cache_item = screen_image)
if(screen_image_url)
Expand Down
136 changes: 93 additions & 43 deletions modular_ss220/title_screen/code/title_screen_subsystem.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,134 @@
init_order = INIT_ORDER_TITLE
/// Currently set title screen
var/datum/title_screen/current_title_screen
/// The list of possible title screens to rotate through, as: title_screen_name -> title_screen_path
var/list/title_screens = list()
/// The list of image files available to be picked for title screen
var/list/title_images_pool = list()

/datum/controller/subsystem/title/Initialize()
setup_title_screen()
fill_title_images_pool()
current_title_screen = new(title_html = get_title_html(), screen_image_file = pick_title_image())
show_title_screen_to_all_new_players()

/datum/controller/subsystem/title/Recover()
current_title_screen = SStitle.current_title_screen
title_screens = SStitle.title_screens
title_images_pool = SStitle.title_images_pool

/**
* Iterates over all files in `TITLE_SCREENS_LOCATION` and loads all valid title screens to `title_screens` var.
*/
/datum/controller/subsystem/title/proc/load_title_screens()
var/list/valid_title_screens = list()
for(var/screen in flist(TITLE_SCREENS_LOCATION))
if(validate_title_screen_name(screen))
valid_title_screens += screen

for(var/title_screen_name in valid_title_screens)
var/file_path = "[TITLE_SCREENS_LOCATION][title_screen_name]"
if(fexists(file_path))
title_screens[title_screen_name] = fcopy_rsc(file_path)
/datum/controller/subsystem/title/proc/fill_title_images_pool()
for(var/file_name in flist(TITLE_SCREENS_LOCATION))
if(validate_filename(file_name))
var/file_path = "[TITLE_SCREENS_LOCATION][file_name]"
title_images_pool += fcopy_rsc(file_path)

/**
* Checks wheter passed title is valid
* Currently validates extension and checks whether it's special image like default title screen etc.
*/
/datum/controller/subsystem/title/proc/validate_title_screen_name(title_screen_to_validate)
var/static/list/title_screens_to_ignore = list("blank.png", DEFAULT_TITLE_SCREEN_IMAGE_NAME)
if(title_screen_to_validate in title_screens_to_ignore)
/datum/controller/subsystem/title/proc/validate_filename(filename)
var/static/list/title_screens_to_ignore = list("blank.png")
if(filename in title_screens_to_ignore)
return FALSE

var/static/list/supported_extensions = list("gif", "jpg", "jpeg", "png", "svg")
var/extstart = findlasttext(filename, ".")
if(!extstart)
return FALSE

var/static/list/supported_extensions = list("gif", "jpg", "jpeg","png", "svg")
var/extension = findlasttext_char(title_screen_to_validate, ".")
var/extension = copytext(filename, extstart + 1)
return (extension in supported_extensions)

/**
* Show the title screen to all new players.
*/
/datum/controller/subsystem/title/proc/show_title_screen(list/viewers)
for(var/mob/new_player/viewer in viewers)
INVOKE_ASYNC(current_title_screen, TYPE_PROC_REF(/datum/title_screen, show_to), viewer)
/datum/controller/subsystem/title/proc/show_title_screen_to_all_new_players()
if(!current_title_screen)
return

for(var/mob/new_player/viewer in GLOB.player_list)
show_title_screen_to(viewer.client)

/**
* Show the title screen to specific client.
*/
/datum/controller/subsystem/title/proc/show_title_screen_to(client/viewer)
if(!viewer || !current_title_screen)
return

INVOKE_ASYNC(current_title_screen, TYPE_PROC_REF(/datum/title_screen, show_to), viewer)

/**
* Hide the title screen from specific client.
*/
/datum/controller/subsystem/title/proc/hide_title_screen_from(client/viewer)
if(!viewer || !current_title_screen)
return

INVOKE_ASYNC(current_title_screen, TYPE_PROC_REF(/datum/title_screen, hide_from), viewer)

/**
* Adds a notice to the main title screen in the form of big red text!
*/
/datum/controller/subsystem/title/proc/set_notice(new_title)
current_title_screen.notice = new_title ? sanitize_text(new_title) : null
show_title_screen()
/datum/controller/subsystem/title/proc/set_notice(new_notice)
new_notice = new_notice ? sanitize_text(new_notice) : null

if(!current_title_screen)
if(!new_notice)
return

current_title_screen = new(notice = new_notice)
else
current_title_screen.notice = new_notice

show_title_screen_to_all_new_players()

/**
* Replaces html of title screen
*/
/datum/controller/subsystem/title/proc/set_title_html(new_html)
if(!new_html)
return

if(!current_title_screen)
current_title_screen = new(title_html = new_html)
else
current_title_screen.title_html = new_html

show_title_screen_to_all_new_players()

/**
* Changes the title screen to a new image.
* Changes title image to desired
*/
/datum/controller/subsystem/title/proc/change_title_screen()
var/screen_image_file_name
var/screen_image_file
if(length(title_screens))
screen_image_file_name = pick(title_screens)
screen_image_file = title_screens[screen_image_file_name]
/datum/controller/subsystem/title/proc/set_title_image(desired_image_file)
if(desired_image_file)
if(!isfile(desired_image_file))
CRASH("Not a file passed to `/datum/controller/subsystem/title/proc/set_title_image`")
else
screen_image_file_name = DEFAULT_TITLE_SCREEN_IMAGE_NAME
screen_image_file = DEFAULT_TITLE_SCREEN_IMAGE_PATH
desired_image_file = pick_title_image()

if(current_title_screen)
current_title_screen.set_screen_image(screen_image_file_name, screen_image_file)
if(!current_title_screen)
current_title_screen = new(screen_image_file = desired_image_file)
else
current_title_screen = new(screen_image_file_name = screen_image_file_name, screen_image_file = screen_image_file)
current_title_screen.set_screen_image(desired_image_file)

show_title_screen_to_all_new_players()

show_title_screen()
/**
* Picks title image from `title_images_pool` list. If the list is empty, `DEFAULT_TITLE_HTML` is returned
*/
/datum/controller/subsystem/title/proc/pick_title_image()
return length(title_images_pool) ? pick(title_images_pool) : DEFAULT_TITLE_SCREEN_IMAGE_PATH

/datum/controller/subsystem/title/proc/setup_title_screen()
load_title_screens()
change_title_screen()

/**
* Tries to read title html from config, if none found - backups to `DEFAULT_TITLE_HTML`
*/
/datum/controller/subsystem/title/proc/get_title_html()
if(!fexists("config/title_html.txt"))
error(span_boldwarning("Unable to read title_html.txt, reverting to backup title html, please check your server config and ensure this file exists."))
current_title_screen.title_html = DEFAULT_TITLE_HTML
return DEFAULT_TITLE_HTML
else
current_title_screen.title_html = file2text("config/title_html.txt")
return file2text("config/title_html.txt")

#undef TITLE_SCREENS_LOCATION

0 comments on commit bb79414

Please sign in to comment.