From cccb2a159faf9f1f0bfaace404098d28dc6572af Mon Sep 17 00:00:00 2001 From: MistakeNot4892 Date: Fri, 26 Mar 2021 10:44:30 +1100 Subject: [PATCH] Ported updated decls repository from Neb. --- code/__defines/misc.dm | 4 +- code/datums/repositories/decls.dm | 64 ++++++++++++++----- code/modules/hydroponics/seed_controller.dm | 2 +- .../unit_tests/integrated_circuits/prefabs.dm | 11 ++-- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/code/__defines/misc.dm b/code/__defines/misc.dm index 9f0d5369e6..ef46c0cb87 100644 --- a/code/__defines/misc.dm +++ b/code/__defines/misc.dm @@ -492,4 +492,6 @@ GLOBAL_LIST_INIT(all_volume_channels, list( #define NTOS_EMAIL_NONEWMESSAGES 0 #define NTOS_EMAIL_NOTIFALREADY 1 -#define NTOS_EMAIL_NEWMESSAGE 2 \ No newline at end of file +#define NTOS_EMAIL_NEWMESSAGE 2 + +#define GET_DECL(D) (ispath(D, /decl) ? (decls_repository.fetched_decls[D] || decls_repository.get_decl(D)) : null) diff --git a/code/datums/repositories/decls.dm b/code/datums/repositories/decls.dm index 61b24c21e9..5217481ad9 100644 --- a/code/datums/repositories/decls.dm +++ b/code/datums/repositories/decls.dm @@ -1,4 +1,20 @@ -/var/repository/decls/decls_repository = new() +// /decl is a subtype used for singletons that should never have more than one instance +// in existence at a time. If you want to use a /decl you should use a pattern like: +// var/decl/somedecl/mydecl = GET_DECL(/decl/somedecl) + +// /decls are created the first time they are fetched from decls_repository and will +// automatically call Initialize() and such when created in this way. + +// decls_repository.get_decls_of_type() and decls_repository.get_decls_of_subtype() +// can be used similarly to typesof() and subtypesof(), returning assoc instance lists. + +// The /decl commandments: +// I. Thou shalt not create a /decl with new(). +// II. Thou shalt not del() or qdel() a /decl. +// III. Thou shalt not write a decl that relies on arguments supplied to New(). +// IV. Thou shalt not call Initialize() on a /decl. + +var/repository/decls/decls_repository = new() /repository/decls var/list/fetched_decls @@ -11,29 +27,45 @@ fetched_decl_types = list() fetched_decl_subtypes = list() -/repository/decls/proc/decls_of_type(var/decl_prototype) - . = fetched_decl_types[decl_prototype] - if(!.) - . = get_decls(typesof(decl_prototype)) - fetched_decl_types[decl_prototype] = . - -/repository/decls/proc/decls_of_subtype(var/decl_prototype) - . = fetched_decl_subtypes[decl_prototype] - if(!.) - . = get_decls(subtypesof(decl_prototype)) - fetched_decl_subtypes[decl_prototype] = . - /repository/decls/proc/get_decl(var/decl_type) + ASSERT(ispath(decl_type)) . = fetched_decls[decl_type] if(!.) . = new decl_type() fetched_decls[decl_type] = . + var/decl/decl = . + if(istype(decl)) + decl.Initialize() + /repository/decls/proc/get_decls(var/list/decl_types) . = list() for(var/decl_type in decl_types) - .[decl_type] = get_decl(decl_type) + .[decl_type] = get_decl(decl_type) -/decls/Destroy() +/repository/decls/proc/get_decls_unassociated(var/list/decl_types) + . = list() + for(var/decl_type in decl_types) + . += get_decl(decl_type) + +/repository/decls/proc/get_decls_of_type(var/decl_prototype) + . = fetched_decl_types[decl_prototype] + if(!.) + . = get_decls(typesof(decl_prototype)) + fetched_decl_types[decl_prototype] = . + +/repository/decls/proc/get_decls_of_subtype(var/decl_prototype) + . = fetched_decl_subtypes[decl_prototype] + if(!.) + . = get_decls(subtypesof(decl_prototype)) + fetched_decl_subtypes[decl_prototype] = . + +/decl/proc/Initialize() + //SHOULD_CALL_PARENT(TRUE) + //SHOULD_NOT_SLEEP(TRUE) + return + +/decl/Destroy() + //SHOULD_CALL_PARENT(FALSE) crash_with("Prevented attempt to delete a decl instance: [log_info_line(src)]") - return QDEL_HINT_LETMELIVE // Prevents Decl destruction + return QDEL_HINT_LETMELIVE // Prevents decl destruction diff --git a/code/modules/hydroponics/seed_controller.dm b/code/modules/hydroponics/seed_controller.dm index 28315b361a..85c6a5f51e 100644 --- a/code/modules/hydroponics/seed_controller.dm +++ b/code/modules/hydroponics/seed_controller.dm @@ -92,7 +92,7 @@ var/global/datum/controller/plants/plant_controller // Set in New(). S.update_seed() //Might as well mask the gene types while we're at it. - var/list/gene_datums = decls_repository.decls_of_subtype(/decl/plantgene) + var/list/gene_datums = decls_repository.get_decls_of_subtype(/decl/plantgene) var/list/used_masks = list() var/list/plant_traits = ALL_GENES while(plant_traits && plant_traits.len) diff --git a/code/unit_tests/integrated_circuits/prefabs.dm b/code/unit_tests/integrated_circuits/prefabs.dm index 056c0d1568..04b3136640 100644 --- a/code/unit_tests/integrated_circuits/prefabs.dm +++ b/code/unit_tests/integrated_circuits/prefabs.dm @@ -3,8 +3,9 @@ /datum/unit_test/integrated_circuit_prefabs_shall_respect_complexity_and_size_contraints/start_test() var/list/failed_prefabs = list() - for(var/prefab_type in subtypesof(/decl/prefab/ic_assembly)) - var/decl/prefab/ic_assembly/prefab = decls_repository.get_decl(prefab_type) + var/list/prefab_types = decls_repository.get_decls_of_subtype(/decl/prefab/ic_assembly) + for(var/prefab_type in prefab_types) + var/decl/prefab/ic_assembly/prefab = prefab_types[prefab_type] var/obj/item/device/electronic_assembly/assembly = prefab.assembly_type var/available_size = initial(assembly.max_components) @@ -33,9 +34,9 @@ /datum/unit_test/integrated_circuit_prefabs_shall_not_fail_to_create/start_test() var/list/failed_prefabs = list() - for(var/prefab_type in subtypesof(/decl/prefab/ic_assembly)) - var/decl/prefab/ic_assembly/prefab = decls_repository.get_decl(prefab_type) - + var/list/prefab_types = decls_repository.get_decls_of_subtype(/decl/prefab/ic_assembly) + for(var/prefab_type in prefab_types) + var/decl/prefab/ic_assembly/prefab = prefab_types[prefab_type] try var/built_item = prefab.create(get_standard_turf()) if(built_item)