Skip to content

Commit f5eb5a5

Browse files
authored
refactor: 🚚 Moved methods handling mod state (#283)
* refactor: 🚚 Moved methods handling mod state Moved the methods handling mod state from `ModLoaderMod` to `ModLoaderModManager`. Additionally, all getter methods inside `ModLoaderMod` have been moved together at the bottom. * refactor: ♻️ added `ModLoaderModManager` to `new_global_classes`
1 parent 286f296 commit f5eb5a5

File tree

3 files changed

+127
-115
lines changed

3 files changed

+127
-115
lines changed

addons/mod_loader/api/mod.gd

+45-115
Original file line numberDiff line numberDiff line change
@@ -40,76 +40,6 @@ static func install_script_extension(child_script_path: String) -> void:
4040
_ModLoaderScriptExtension.apply_extension(child_script_path)
4141

4242

43-
# Uninstall a script extension.
44-
#
45-
# Parameters:
46-
# - extension_script_path (String): The path to the extension script to be uninstalled.
47-
#
48-
# Returns: void
49-
static func uninstall_script_extension(extension_script_path: String) -> void:
50-
# Currently this is the only thing we do, but it is better to expose
51-
# this function like this for further changes
52-
_ModLoaderScriptExtension.remove_specific_extension_from_script(extension_script_path)
53-
54-
55-
# Reload all mods.
56-
#
57-
# *Note: This function should be called only when actually necessary
58-
# as it can break the game and require a restart for mods
59-
# that do not fully use the systems put in place by the mod loader,
60-
# so anything that just uses add_node, move_node ecc...
61-
# To not have your mod break on reload please use provided functions
62-
# like ModLoader::save_scene, ModLoader::append_node_in_scene and
63-
# all the functions that will be added in the next versions
64-
# Used to reload already present mods and load new ones*
65-
#
66-
# Returns: void
67-
func reload_mods() -> void:
68-
69-
# Currently this is the only thing we do, but it is better to expose
70-
# this function like this for further changes
71-
ModLoader._reload_mods()
72-
73-
74-
# Disable all mods.
75-
#
76-
# *Note: This function should be called only when actually necessary
77-
# as it can break the game and require a restart for mods
78-
# that do not fully use the systems put in place by the mod loader,
79-
# so anything that just uses add_node, move_node ecc...
80-
# To not have your mod break on disable please use provided functions
81-
# and implement a _disable function in your mod_main.gd that will
82-
# handle removing all the changes that were not done through the Mod Loader*
83-
#
84-
# Returns: void
85-
func disable_mods() -> void:
86-
87-
# Currently this is the only thing we do, but it is better to expose
88-
# this function like this for further changes
89-
ModLoader._disable_mods()
90-
91-
92-
# Disable a mod.
93-
#
94-
# *Note: This function should be called only when actually necessary
95-
# as it can break the game and require a restart for mods
96-
# that do not fully use the systems put in place by the mod loader,
97-
# so anything that just uses add_node, move_node ecc...
98-
# To not have your mod break on disable please use provided functions
99-
# and implement a _disable function in your mod_main.gd that will
100-
# handle removing all the changes that were not done through the Mod Loader*
101-
#
102-
# Parameters:
103-
# - mod_data (ModData): The ModData object representing the mod to be disabled.
104-
#
105-
# Returns: void
106-
func disable_mod(mod_data: ModData) -> void:
107-
108-
# Currently this is the only thing we do, but it is better to expose
109-
# this function like this for further changes
110-
ModLoader._disable_mod(mod_data)
111-
112-
11343
# Register an array of classes to the global scope since Godot only does that in the editor.
11444
#
11545
# Format: `{ "base": "ParentClass", "class": "ClassName", "language": "GDScript", "path": "res://path/class_name.gd" }`
@@ -145,51 +75,6 @@ static func add_translation(resource_path: String) -> void:
14575
ModLoaderLog.info("Added Translation from Resource -> %s" % resource_path, LOG_NAME)
14676

14777

148-
# Gets the ModData from the provided namespace
149-
#
150-
# Parameters:
151-
# - mod_id (String): The ID of the mod.
152-
#
153-
# Returns:
154-
# - ModData: The ModData associated with the provided mod_id, or null if the mod_id is invalid.
155-
static func get_mod_data(mod_id: String) -> ModData:
156-
if not ModLoaderStore.mod_data.has(mod_id):
157-
ModLoaderLog.error("%s is an invalid mod_id" % mod_id, LOG_NAME)
158-
return null
159-
160-
return ModLoaderStore.mod_data[mod_id]
161-
162-
163-
# Gets the ModData of all loaded Mods as Dictionary.
164-
#
165-
# Returns:
166-
# - Dictionary: A dictionary containing the ModData of all loaded mods.
167-
static func get_mod_data_all() -> Dictionary:
168-
return ModLoaderStore.mod_data
169-
170-
171-
# Returns true if the mod with the given mod_id was successfully loaded.
172-
#
173-
# Parameters:
174-
# - mod_id (String): The ID of the mod.
175-
#
176-
# Returns:
177-
# - bool: true if the mod is loaded, false otherwise.
178-
static func is_mod_loaded(mod_id: String) -> bool:
179-
if ModLoaderStore.is_initializing:
180-
ModLoaderLog.warning(
181-
"The ModLoader is not fully initialized. " +
182-
"Calling \"is_mod_loaded()\" in \"_init()\" may result in an unexpected return value as mods are still loading.",
183-
LOG_NAME
184-
)
185-
186-
# If the mod is not present in the mod_data dictionary or the mod is flagged as not loadable.
187-
if not ModLoaderStore.mod_data.has(mod_id) or not ModLoaderStore.mod_data[mod_id].is_loadable:
188-
return false
189-
190-
return true
191-
192-
19378
# Appends a new node to a modified scene.
19479
#
19580
# Parameters:
@@ -236,9 +121,54 @@ static func save_scene(modified_scene: Node, scene_path: String) -> void:
236121
ModLoaderStore.saved_objects.append(packed_scene)
237122

238123

124+
# Gets the ModData from the provided namespace
125+
#
126+
# Parameters:
127+
# - mod_id (String): The ID of the mod.
128+
#
129+
# Returns:
130+
# - ModData: The ModData associated with the provided mod_id, or null if the mod_id is invalid.
131+
static func get_mod_data(mod_id: String) -> ModData:
132+
if not ModLoaderStore.mod_data.has(mod_id):
133+
ModLoaderLog.error("%s is an invalid mod_id" % mod_id, LOG_NAME)
134+
return null
135+
136+
return ModLoaderStore.mod_data[mod_id]
137+
138+
139+
# Gets the ModData of all loaded Mods as Dictionary.
140+
#
141+
# Returns:
142+
# - Dictionary: A dictionary containing the ModData of all loaded mods.
143+
static func get_mod_data_all() -> Dictionary:
144+
return ModLoaderStore.mod_data
145+
146+
239147
# Returns the path to the directory where unpacked mods are stored.
240148
#
241149
# Returns:
242150
# - String: The path to the unpacked mods directory.
243151
static func get_unpacked_dir() -> String:
244152
return _ModLoaderPath.get_unpacked_mods_dir_path()
153+
154+
155+
# Returns true if the mod with the given mod_id was successfully loaded.
156+
#
157+
# Parameters:
158+
# - mod_id (String): The ID of the mod.
159+
#
160+
# Returns:
161+
# - bool: true if the mod is loaded, false otherwise.
162+
static func is_mod_loaded(mod_id: String) -> bool:
163+
if ModLoaderStore.is_initializing:
164+
ModLoaderLog.warning(
165+
"The ModLoader is not fully initialized. " +
166+
"Calling \"is_mod_loaded()\" in \"_init()\" may result in an unexpected return value as mods are still loading.",
167+
LOG_NAME
168+
)
169+
170+
# If the mod is not present in the mod_data dictionary or the mod is flagged as not loadable.
171+
if not ModLoaderStore.mod_data.has(mod_id) or not ModLoaderStore.mod_data[mod_id].is_loadable:
172+
return false
173+
174+
return true

addons/mod_loader/api/mod_manager.gd

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# This Class provides methods to manage mod state.
2+
# *Note: Intended to be used by game developers.*
3+
class_name ModLoaderModManager
4+
extends Reference
5+
6+
7+
const LOG_NAME := "ModLoader:Manager"
8+
9+
10+
# Uninstall a script extension.
11+
#
12+
# Parameters:
13+
# - extension_script_path (String): The path to the extension script to be uninstalled.
14+
#
15+
# Returns: void
16+
static func uninstall_script_extension(extension_script_path: String) -> void:
17+
# Currently this is the only thing we do, but it is better to expose
18+
# this function like this for further changes
19+
_ModLoaderScriptExtension.remove_specific_extension_from_script(extension_script_path)
20+
21+
22+
# Reload all mods.
23+
#
24+
# *Note: This function should be called only when actually necessary
25+
# as it can break the game and require a restart for mods
26+
# that do not fully use the systems put in place by the mod loader,
27+
# so anything that just uses add_node, move_node ecc...
28+
# To not have your mod break on reload please use provided functions
29+
# like ModLoader::save_scene, ModLoader::append_node_in_scene and
30+
# all the functions that will be added in the next versions
31+
# Used to reload already present mods and load new ones*
32+
#
33+
# Returns: void
34+
func reload_mods() -> void:
35+
36+
# Currently this is the only thing we do, but it is better to expose
37+
# this function like this for further changes
38+
ModLoader._reload_mods()
39+
40+
41+
# Disable all mods.
42+
#
43+
# *Note: This function should be called only when actually necessary
44+
# as it can break the game and require a restart for mods
45+
# that do not fully use the systems put in place by the mod loader,
46+
# so anything that just uses add_node, move_node ecc...
47+
# To not have your mod break on disable please use provided functions
48+
# and implement a _disable function in your mod_main.gd that will
49+
# handle removing all the changes that were not done through the Mod Loader*
50+
#
51+
# Returns: void
52+
func disable_mods() -> void:
53+
54+
# Currently this is the only thing we do, but it is better to expose
55+
# this function like this for further changes
56+
ModLoader._disable_mods()
57+
58+
59+
# Disable a mod.
60+
#
61+
# *Note: This function should be called only when actually necessary
62+
# as it can break the game and require a restart for mods
63+
# that do not fully use the systems put in place by the mod loader,
64+
# so anything that just uses add_node, move_node ecc...
65+
# To not have your mod break on disable please use provided functions
66+
# and implement a _disable function in your mod_main.gd that will
67+
# handle removing all the changes that were not done through the Mod Loader*
68+
#
69+
# Parameters:
70+
# - mod_data (ModData): The ModData object representing the mod to be disabled.
71+
#
72+
# Returns: void
73+
func disable_mod(mod_data: ModData) -> void:
74+
75+
# Currently this is the only thing we do, but it is better to expose
76+
# this function like this for further changes
77+
ModLoader._disable_mod(mod_data)

addons/mod_loader/mod_loader_setup.gd

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ const new_global_classes := [
5555
"class": "ModLoaderMod",
5656
"language": "GDScript",
5757
"path": "res://addons/mod_loader/api/mod.gd"
58+
}, {
59+
"base": "Reference",
60+
"class": "ModLoaderModManager",
61+
"language": "GDScript",
62+
"path": "res://addons/mod_loader/api/mod_manager.gd"
5863
}, {
5964
"base": "Object",
6065
"class": "_ModLoaderGodot",

0 commit comments

Comments
 (0)