Documentation & list copying update

This commit is contained in:
unid15
2017-08-09 15:12:48 +02:00
parent 48fd5edfcf
commit 0acab5b196
7 changed files with 151 additions and 134 deletions

View File

@@ -199,6 +199,8 @@ proc/get_space_area()
//y is the minimum //y is the minimum
//z is the maximum //z is the maximum
//Returns 1 if the variable contains a protected list that can't be edited
#define variable_contains_protected_list(var_name) (((var_name) == "contents") || ((var_name) == "locs") || ((var_name) == "vars"))
#define CLAMP01(x) (Clamp(x, 0, 1)) #define CLAMP01(x) (Clamp(x, 0, 1))

View File

@@ -1,60 +1,65 @@
var/global/list/matching_type_list_cache = list() //Returns a list of types derived from [parent_type], that contain the substring [partial_name]
//If the substring ends with a dot, only functions that END with the substring are returned
/proc/matching_type_list(object, parent_type = /atom) var/global/list/get_matching_types_cache = list()
/proc/get_matching_types(partial_name, parent_type = /atom)
//Key string for the cache //Key string for the cache
var/key = "[object]:[parent_type]" var/key = "[partial_name]:[parent_type]"
//Get cached list's copy if it exists //Get cached list's copy if it exists
var/list/cache = matching_type_list_cache[key] var/list/cache = get_matching_types_cache[key]
if(cache) if(cache)
return cache.Copy() return cache.Copy()
var/list/matches = list() var/list/matches = list()
//The string is null or "" - no need for calculations //The string is null or "" - no need for calculations
if(!object || !length(object)) if(!partial_name || !length(partial_name))
return typesof(parent_type) return typesof(parent_type)
if(text_ends_with(object, ".")) //Path ends with a dot - DO NOT include subtypes if(text_ends_with(partial_name, ".")) //Path ends with a dot - DO NOT include subtypes
object = copytext(object, 1, length(object)) //Remove the dot partial_name = copytext(partial_name, 1, length(partial_name)) //Remove the dot
for(var/path in typesof(parent_type)) for(var/path in typesof(parent_type))
if(text_ends_with("[path]", object)) if(text_ends_with("[path]", partial_name))
matches += path matches += path
else //Include subtypes else //Include subtypes
for(var/path in typesof(/atom)) for(var/path in typesof(parent_type))
if(findtext("[path]", object)) if(findtext("[path]", partial_name))
matches += path matches += path
matching_type_list_cache[key] = matches.Copy() //Cache the result
get_matching_types_cache[key] = matches.Copy()
return matches return matches
//Returns a list of variables of an object of type [object_type].
//Because it's impossible to access variables of a type, this proc creates a temporary datum, grabs its variables and deletes it, caching the result in a global list
//object_type CAN'T be a type of a turf (/turf) or an area (/area), because these datums can't be created in nullspace
var/global/list/get_vars_from_type_cache = list() var/global/list/get_vars_from_type_cache = list()
/proc/get_vars_from_type(object_type)
/proc/get_vars_from_type(T) if(ispath(object_type, /atom) && !ispath(object_type, /atom/movable))
if(ispath(T, /atom) && !ispath(T, /atom/movable))
//It's impossible to spawn a turf or an area without a location
//Attempting to proceed will result in a runtime error //Attempting to proceed will result in a runtime error
return null return null
var/list/cache = get_vars_from_type_cache[T] var/list/cache = get_vars_from_type_cache[object_type]
if(cache) if(cache)
return cache.Copy() return cache.Copy()
var/list/variable_list = list() var/list/variable_list = list()
var/datum/temp_datum = new T(null) //Create a temporary datum in nullspace to access the variables
var/datum/temp_datum = new object_type(null)
for(var/variable in temp_datum.vars) for(var/variable in temp_datum.vars)
variable_list.Add(variable) variable_list.Add(variable)
variable_list = sortList(variable_list) //Sort the variable list alphabetically
get_vars_from_type_cache[T] = variable_list //Sort the variable list alphabetically
variable_list = sortList(variable_list)
//Cache the result
get_vars_from_type_cache[object_type] = variable_list
qdel(temp_datum) qdel(temp_datum)
return variable_list return variable_list
var/global/list/existing_typesof_cache = list()
//existing_typesof functions like typesof, with some differences //existing_typesof functions like typesof, with some differences
//1) it only works with pathes derived from /atom //1) it only works with pathes derived from /atom
//2) the returned list contains NO items without an icon state or an icon //2) the returned list contains NO items without an icon state or an icon
@@ -64,8 +69,8 @@ var/global/list/existing_typesof_cache = list()
//resulting in an invisible monster. //resulting in an invisible monster.
//Values are cached, so when doing existing_typesof(/atom), all paths derived from /atom will only be checked on the first call //Values are cached, so when doing existing_typesof(/atom), all paths derived from /atom will only be checked on the first call
//All calls afterwards will return a copy of a list from the cache //All calls with the same path afterwards will return a copy of a list from the cache
var/global/list/existing_typesof_cache = list()
/proc/existing_typesof(var/path) /proc/existing_typesof(var/path)
if(!ispath(path, /atom)) if(!ispath(path, /atom))
return typesof(path) return typesof(path)

View File

@@ -538,12 +538,27 @@ body
return return
var/atom/A = locate(href_list["datumsave"]) var/atom/A = locate(href_list["datumsave"])
var/variable_name = href_list["varnamesave"]
if(A) if(A)
var/saved_value = A.vars[href_list["varnamesave"]] var/saved_value = A.vars[variable_name]
holder.marked_datum = saved_value if(variable_contains_protected_list(variable_name)) //Checks for lists like 'vars', 'contents' and 'locs' that can't be edited
to_chat(usr, "Your marked datum is now: [holder.marked_datum]") to_chat(usr, "<span class='notice'>The list [variable_name] is protected, and can't be saved. Saving a copy of it...</span>")
var/list/L = saved_value
holder.marked_datum = L.Copy()
else if(islist(saved_value))
if(alert("Save this exact list, or a copy of it? A copy is independent, and changing it will not affect the original list.", "Datum saving", "Save Copy", "Save Exact") == "Save Copy")
var/list/L = saved_value
holder.marked_datum = L.Copy()
to_chat(usr, "Saved a copy of the [variable_name] list as your marked datum.")
else
holder.marked_datum = saved_value
to_chat(usr, "Saved the original [variable_name] list as your marked datum.")
else
holder.marked_datum = saved_value
to_chat(usr, "Your marked datum is now: [holder.marked_datum]")
else if(href_list["mob_player_panel"]) else if(href_list["mob_player_panel"])
if(!check_rights(0)) if(!check_rights(0))

View File

@@ -1279,7 +1279,7 @@ var/global/floorIsLava = 0
if(!check_rights(R_SPAWN)) if(!check_rights(R_SPAWN))
return return
var/list/matches = matching_type_list(object, /atom) var/list/matches = get_matching_types(object, /atom)
if(matches.len==0) if(matches.len==0)
return return

View File

@@ -1210,7 +1210,7 @@ client/proc/check_convertables()
if(!check_rights(R_SPAWN)) if(!check_rights(R_SPAWN))
return return
var/list/matches = matching_type_list(object, /datum) - typesof(/turf) var/list/matches = get_matching_types(object, /datum) - typesof(/turf, /area)
if(matches.len == 0) if(matches.len == 0)
to_chat(usr, "Unable to find any matches.") to_chat(usr, "Unable to find any matches.")

View File

@@ -7,11 +7,12 @@
return return
if(istext(target_type)) if(istext(target_type))
target_type = input("Select an object type to mass-modify", "Mass-editing") as null|anything in matching_type_list(target_type, /atom) target_type = input("Select an object type to mass-modify", "Mass-editing") as null|anything in get_matching_types(target_type, /atom)
//get_vars_from_type() fails on turf and area objects. If you want to mass-edit a turf, you have to do it through the varedit window //get_vars_from_type() fails on turf and area objects. If you want to mass-edit a turf, you have to do it through the varedit window
if(ispath(target_type, /atom) && !ispath(target_type, /atom/movable)) if(ispath(target_type, /atom) && !ispath(target_type, /atom/movable))
to_chat(src, "<span class='warning'>It's impossible to perform this task on objects of type [target_type] through the verb. Use the mass edit function from View Variables instead.") to_chat(src, "<span class='warning'>It's impossible to perform this task on objects of type [target_type] through the verb. Use the mass edit function from View Variables instead.")
return
if(!target_type) if(!target_type)
return return
@@ -40,15 +41,15 @@
if("Set new value") if("Set new value")
new_value = variable_set(usr) new_value = variable_set(usr)
mass_modify_variable(target_type, variable_name, new_value, reset_to_initial, include_subtypes) //Log the action before actually performing it, in case it crashes the server
feedback_add_details("admin_verb","MEV")
feedback_add_details("admin_verb","MEV") //If you are copy-pasting this, ensure the 2nd parameter is unique to the new proc!
log_admin("[key_name(src)] mass modified [target_type]'s [variable_name] to [reset_to_initial ? "its initial value" : " [new_value] "]") log_admin("[key_name(src)] mass modified [target_type]'s [variable_name] to [reset_to_initial ? "its initial value" : " [new_value] "]")
message_admins("[key_name_admin(src)] mass modified [target_type]'s [variable_name] to [reset_to_initial ? "its initial value" : " [new_value] "]", 1) message_admins("[key_name_admin(src)] mass modified [target_type]'s [variable_name] to [reset_to_initial ? "its initial value" : " [new_value] "]", 1)
mass_modify_variable(target_type, variable_name, new_value, reset_to_initial, include_subtypes)
//Mass-modifies all atoms of type [type], changing their variable [var_name] to [new_value] //Mass-modifies all atoms of type [type], changing their variable [var_name] to [new_value]
//If reset_to_initial is TRUE, the variables will be reset to their initial values, instead of getting a new value //If reset_to_initial is TRUE, the variables will be reset to their initial values, instead of getting a new value
/proc/mass_modify_variable(type, var_name, new_value, reset_to_initial = FALSE, include_subtypes = TRUE) /proc/mass_modify_variable(type, var_name, new_value, reset_to_initial = FALSE, include_subtypes = TRUE)
var/base_path var/base_path
@@ -62,10 +63,10 @@
if(!ispath(base_path, /atom)) if(!ispath(base_path, /atom))
to_chat(usr, "Mass-editing is not supported for objects of type [base_path]") to_chat(usr, "Mass-editing is not supported for objects of type [base_path]")
return
//BYOND's internal optimisation makes this work better than cycling through every atom
#define is_valid_atom(atom) (atom.type == base_path || (include_subtypes && istype(atom, base_path))) #define is_valid_atom(atom) (atom.type == base_path || (include_subtypes && istype(atom, base_path)))
if(ispath(base_path, /turf)) if(ispath(base_path, /turf))
for(var/turf/A in world) for(var/turf/A in world)
if(is_valid_atom(A)) if(is_valid_atom(A))

View File

@@ -5,16 +5,11 @@ var/list/forbidden_varedit_object_types = list(
/datum/configuration, //prevents people from fucking with logging. /datum/configuration, //prevents people from fucking with logging.
) )
/* //Interface for editing a variable. It doesn't change the variable - just returns its new value.
/client/proc/cmd_modify_object_variables(obj/O as obj|mob|turf|area in world) //If called with just [user] argument, it allows you to create a value such as a string, a number, an empty list, a nearby object, etc...
set category = "Debug" //If called with [edited_datum] and [edited_variable], you gain the ability to get the variable's initial value.
set name = "Edit Variables" //In addition to that, if [autoselect_var_type] is TRUE, the proc will attempt to
set desc="(target) Edit a target item's variables" /proc/variable_set(mob/user, datum/edited_datum = null, edited_variable = null, autoselect_var_type = FALSE)
src.modify_variables(O)
feedback_add_details("admin_verb","EDITV") //If you are copy-pasting this, ensure the 2nd parameter is unique to the new proc!
*/
/proc/variable_set(mob/user, datum/edited_datum, edited_variable, autoselect_var_type = FALSE)
var/client/C var/client/C
if(ismob(user)) if(ismob(user))
@@ -39,112 +34,111 @@ var/list/forbidden_varedit_object_types = list(
#define V_CANCEL "cancel" #define V_CANCEL "cancel"
#define V_MATRIX "matrix" #define V_MATRIX "matrix"
var/list/choices = list(\ var/new_variable_type
"text" = V_TEXT, var/old_value = null //Old value of the variable
"num" = V_NUM, var/new_value //New value of the variable
"type" = V_TYPE,
"empty list" = V_LIST,
"object (nearby)" = V_OBJECT,
"icon" = V_ICON,
"file" = V_FILE,
"client" = V_CLIENT,
"matrix" = V_MATRIX,
"null" = V_NULL,
)
if(C.holder.marked_datum)
var/list_item_name
if(isdatum(C.holder.marked_datum))
list_item_name = "marked datum ([C.holder.marked_datum.type])"
else if(isfile(C.holder.marked_datum))
list_item_name = "marked datum (file)"
else if(isicon(C.holder.marked_datum))
list_item_name = "marked datum (icon)"
else
list_item_name = "marked datum ([C.holder.marked_datum])"
choices[list_item_name] = V_MARKED_DATUM
if(istype(edited_datum) && edited_variable) if(istype(edited_datum) && edited_variable)
choices["restore to default"] = V_RESET old_value = edited_datum.vars[edited_variable]
//Cancel belongs at the end if(autoselect_var_type)
choices["CANCEL"] = V_CANCEL if(isnull(old_value))
to_chat(usr, "Unable to determine variable type.")
else if(isnum(old_value))
to_chat(usr, "Variable appears to be <b>NUM</b>.")
new_variable_type = V_NUM
else if(istext(old_value))
to_chat(usr, "Variable appears to be <b>TEXT</b>.")
new_variable_type = V_TEXT
else if(isloc(old_value))
to_chat(usr, "Variable appears to be <b>REFERENCE</b>. Selecting from nearby objects...")
new_variable_type = V_OBJECT
else if(isicon(old_value))
to_chat(usr, "Variable appears to be <b>ICON</b>.")
new_variable_type = V_ICON
else if(ispath(old_value))
to_chat(usr, "Variable appears to be <b>TYPE</b>.")
new_variable_type = V_TYPE
else if(istype(old_value,/client))
to_chat(usr, "Variable appears to be <b>CLIENT</b>.")
new_variable_type = V_CLIENT
else if(isfile(old_value))
to_chat(usr, "Variable appears to be <b>FILE</b>.")
new_variable_type = V_FILE
else if(islist(old_value))
to_chat(usr, "Variable appears to be <b>LIST</b>.")
new_value = C.mod_list(old_value) //Use a custom interface for list editing
else if(ismatrix(old_value))
to_chat(usr, "Variable appears to be <b>MATRIX</b>.")
new_value = C.modify_matrix_menu(old_value) //Use a custom interface for matrix editing
var/class if(!new_value) //If a custom interface hasn't already set the value
var/var_value = null //Old value of the variable
var/result //New value of the variable
if(autoselect_var_type && istype(edited_datum) && edited_variable) //Build the choices list
var_value = edited_datum.vars[edited_variable] var/list/choices = list(\
"text" = V_TEXT,
"num" = V_NUM,
"type" = V_TYPE,
"empty list" = V_LIST,
"object (nearby)" = V_OBJECT,
"icon" = V_ICON,
"file" = V_FILE,
"client" = V_CLIENT,
"matrix" = V_MATRIX,
"null" = V_NULL,
)
if(isnull(var_value)) if(C.holder.marked_datum) //Add the marked datum option
to_chat(usr, "Unable to determine variable type.") var/list_item_name
else if(isnum(var_value)) if(isdatum(C.holder.marked_datum))
to_chat(usr, "Variable appears to be <b>NUM</b>.") list_item_name = "marked datum ([C.holder.marked_datum.type])"
class = V_NUM else if(isfile(C.holder.marked_datum))
else if(istext(var_value)) list_item_name = "marked datum (file)"
to_chat(usr, "Variable appears to be <b>TEXT</b>.") else if(isicon(C.holder.marked_datum))
class = V_TEXT list_item_name = "marked datum (icon)"
else if(isloc(var_value)) else
to_chat(usr, "Variable appears to be <b>REFERENCE</b>. Selecting from nearby objects...") list_item_name = "marked datum ([C.holder.marked_datum])"
class = V_OBJECT choices[list_item_name] = V_MARKED_DATUM
else if(isicon(var_value))
to_chat(usr, "Variable appears to be <b>ICON</b>.")
class = V_ICON
else if(ispath(var_value))
to_chat(usr, "Variable appears to be <b>TYPE</b>.")
class = V_TYPE
else if(istype(var_value,/client))
to_chat(usr, "Variable appears to be <b>CLIENT</b>.")
class = V_CLIENT
else if(isfile(var_value))
to_chat(usr, "Variable appears to be <b>FILE</b>.")
class = V_FILE
else if(islist(var_value))
to_chat(usr, "Variable appears to be <b>LIST</b>.")
result = C.mod_list(var_value)
else if(ismatrix(var_value))
to_chat(usr, "Variable appears to be <b>MATRIX</b>.")
result = C.modify_matrix_menu(var_value)
if(istype(edited_datum) && edited_variable) //Add the restore to default option
choices["restore to default"] = V_RESET
if(!class && !result) //Add the cancel option
class = input("What kind of variable?","Variable Type") in choices choices["CANCEL"] = V_CANCEL
var/selected_type = choices[class]
if(!new_variable_type)
new_variable_type = input("What kind of variable?","Variable Type") in choices
var/selected_type = choices[new_variable_type]
var/window_title = "Varedit [edited_datum]"
if(!result)
switch(selected_type) switch(selected_type)
if(V_CANCEL) if(V_CANCEL)
return return
if(V_TEXT) if(V_TEXT)
result = input("Enter new text:","Text",null) as text new_value = input("Enter new text:", window_title, old_value) as text
if(V_NUM) if(V_NUM)
result = input("Enter new number:","Num",0) as num new_value = input("Enter new number:", window_title, old_value) as num
if(V_TYPE) if(V_TYPE)
var/partial_type = input("Enter type, or leave blank to see all types", "Type") as text|null var/partial_type = input("Enter type, or leave blank to see all types", window_title, "[old_value]") as text|null
var/list/matches = matching_type_list(partial_type, /datum) var/list/matches = get_matching_types(partial_type, /datum)
result = input("Select type","Type") as null|anything in matches new_value = input("Select type", window_title) as null|anything in matches
if(V_LIST) if(V_LIST)
result = list() new_value = list()
if(V_OBJECT) if(V_OBJECT)
result = input("Select reference:","Reference",src) as mob|obj|turf|area in range(8, get_turf(user)) new_value = input("Select reference:", window_title, old_value) as mob|obj|turf|area in range(8, get_turf(user))
if(V_FILE) if(V_FILE)
result = input("Pick file:","File") as file new_value = input("Pick file:", window_title) as file
if(V_ICON) if(V_ICON)
result = input("Pick icon:","Icon") as icon new_value = input("Pick icon:", window_title) as icon
if(V_CLIENT) if(V_CLIENT)
var/list/keys = list() var/list/keys = list()
@@ -152,32 +146,32 @@ var/list/forbidden_varedit_object_types = list(
if(M.client) if(M.client)
keys += M.client keys += M.client
result = input("Please, select a player!", "Selection", null, null) as null|anything in keys new_value = input("Please, select a player!", window_title, null, null) as null|anything in keys
if(V_MARKED_DATUM) if(V_MARKED_DATUM)
result = C.holder.marked_datum new_value = C.holder.marked_datum
if(V_RESET) if(V_RESET)
if(istype(edited_datum) && edited_variable) if(istype(edited_datum) && edited_variable)
result = initial(edited_datum.vars[edited_variable]) new_value = initial(edited_datum.vars[edited_variable])
edited_datum.vars[edited_variable] = result edited_datum.vars[edited_variable] = new_value
to_chat(user, "Restored '[edited_variable]' to original value - [result]") to_chat(user, "Restored '[edited_variable]' to original value - [new_value]")
if(V_NULL) if(V_NULL)
result = null new_value = null
if(V_MATRIX) if(V_MATRIX)
result = matrix() new_value = matrix()
else else
to_chat(user, "Unknown type: [selected_type]") to_chat(user, "Unknown type: [selected_type]")
if(istype(edited_datum)) if(istype(edited_datum))
if(edited_datum.variable_edited(edited_variable, var_value, result)) if(edited_datum.variable_edited(edited_variable, old_value, new_value))
return var_value //Return the old value if variable_edited blocked the edit return old_value //Return the old value if the variable_edited proc blocked the edit
return result return new_value
#undef V_MARKED_DATUM #undef V_MARKED_DATUM
#undef V_RESET #undef V_RESET