Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod Config: API Method to duplicate a config #490

Open
KANAjetzt opened this issue Dec 14, 2024 · 0 comments
Open

Mod Config: API Method to duplicate a config #490

KANAjetzt opened this issue Dec 14, 2024 · 0 comments
Labels
3.x 4.x enhancement New feature or request
Milestone

Comments

@KANAjetzt
Copy link
Member

A method to duplicate an existing config (in most cases the default config) would simplify the creation of a modifiable config since the default config is read-only.

For example:
duplicate_config(config: ModConfig, config_name: String) -> ModConfig:

## Creates a new configuration for a mod.[br]
## [br]
## [b]Parameters:[/b][br]
## - [code]mod_id[/code] ([String]): The ID of the mod.[br]
## - [code]config_name[/code] ([String]): The name of the configuration.[br]
## - [code]config_data[/code] ([Dictionary]): The configuration data to be stored.[br]
## [br]
## [b]Returns:[/b][br]
## - [ModConfig]: The created ModConfig object if successful, or null otherwise.
static func create_config(mod_id: String, config_name: String, config_data: Dictionary) -> ModConfig:
var default_config: ModConfig = get_default_config(mod_id)
if not default_config:
ModLoaderLog.error(
"Failed to create config \"%s\". No config schema found for \"%s\"."
% [config_name, mod_id], LOG_NAME
)
return null
# Make sure the config name is not empty
if config_name == "":
ModLoaderLog.error(
"Failed to create config \"%s\". The config name cannot be empty."
% config_name, LOG_NAME
)
return null
# Make sure the config name is unique
if ModLoaderStore.mod_data[mod_id].configs.has(config_name):
ModLoaderLog.error(
"Failed to create config \"%s\". A config with the name \"%s\" already exists."
% [config_name, config_name], LOG_NAME
)
return null
# Create the config save path based on the config_name
var config_file_path := _ModLoaderPath.get_path_to_mod_configs_dir(mod_id).path_join("%s.json" % config_name)
# Initialize a new ModConfig object with the provided parameters
var mod_config := ModConfig.new(
mod_id,
config_data,
config_file_path
)
# Check if the mod_config is valid
if not mod_config.is_valid:
return null
# Store the mod_config in the mod's ModData
ModLoaderStore.mod_data[mod_id].configs[config_name] = mod_config
# Save the mod_config to a new config JSON file in the mod's config directory
var is_save_success := mod_config.save_to_file()
if not is_save_success:
return null
ModLoaderLog.debug("Created new config \"%s\" for mod \"%s\"" % [config_name, mod_id], LOG_NAME)
return mod_config

@KANAjetzt KANAjetzt added enhancement New feature or request 4.x 3.x labels Dec 14, 2024
@KANAjetzt KANAjetzt added this to the 4.x - 7.x milestone Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x 4.x enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant