From df490306ba5a8c93c44782a1bc0b9feb000c415d Mon Sep 17 00:00:00 2001 From: Lucy Date: Wed, 11 Jun 2025 21:42:45 -0400 Subject: [PATCH] Add a "Count Atoms/Datums" debug verb (#91529) ## About The Pull Request This adds a new `+DEBUG` verb, "Count Atoms/Datums". It outputs a (sorted) json file of how many of each datum/atom typepath instances there are. Ported from https://github.com/Monkestation/Monkestation2.0/pull/6893 ![2025-06-08 (1749414807)](https://github.com/user-attachments/assets/43f83a9a-6a69-4473-bdff-581eb8db1944) ![2025-06-08 (1749414825) ~ notepad++](https://github.com/user-attachments/assets/6b395177-7efd-445f-aebd-9cdff9f19f0f) ## Changelog :cl: admin: Added a "Count Atoms/Datums" debug verb. /:cl: --- code/modules/admin/verbs/debug.dm | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/code/modules/admin/verbs/debug.dm b/code/modules/admin/verbs/debug.dm index fa09f840805..75c3afdc47e 100644 --- a/code/modules/admin/verbs/debug.dm +++ b/code/modules/admin/verbs/debug.dm @@ -955,3 +955,46 @@ ADMIN_VERB_CUSTOM_EXIST_CHECK(queue_tracy) ADMIN_VERB(debug_mc_dependencies, R_DEBUG, "Debug MC Dependencies", "Debug MC dependencies.", ADMIN_CATEGORY_DEBUG) var/datum/mc_dependency_ui/data = new /datum/mc_dependency_ui() data.ui_interact(usr) + +ADMIN_VERB(count_instances, R_DEBUG, "Count Atoms/Datums", "Count how many atom or datum instances there are of each type, then output it to a JSON to download.", ADMIN_CATEGORY_DEBUG) + var/option = tgui_alert(user, "What type of instances do you wish to count?", "Instance Count", list("Atoms", "Datums")) + if(!option) + return + var/list/result + to_chat(user, span_notice("Beginning instance count ([option])"), type = MESSAGE_TYPE_DEBUG) + switch(option) + if("Atoms") + result = count_atoms() + if("Datums") + result = count_datums() + + if(result) + to_chat(user, span_adminnotice("Counted [length(result)] instances, sending compiled JSON file now."), type = MESSAGE_TYPE_DEBUG) + var/tmp_path = "tmp/instance_count_[user.ckey].json" + fdel(tmp_path) + rustg_file_write(json_encode(result, JSON_PRETTY_PRINT), tmp_path) + var/exportable_json = file(tmp_path) + DIRECT_OUTPUT(user, ftp(exportable_json, "[LOWER_TEXT(option)]_instance_count_round_[GLOB.round_id].json")) + fdel(tmp_path) + +#ifndef OPENDREAM +/proc/count_atoms() + . = list() + for(var/datum/thing in world) //atoms (don't believe its lies) + .[thing.type]++ + sortTim(., cmp = GLOBAL_PROC_REF(cmp_numeric_dsc), associative = TRUE) + +/proc/count_datums() + . = list() + for(var/datum/thing) + .[thing.type]++ + sortTim(., cmp = GLOBAL_PROC_REF(cmp_numeric_dsc), associative = TRUE) +#else +/proc/count_atoms() + . = list() + CRASH("count_atoms not supported on OpenDream") + +/proc/count_datums() + . = list() + CRASH("count_datums not supported on OpenDream") +#endif