Skip to content

Mod Config: API Method to duplicate a config #490

@KANAjetzt

Description

@KANAjetzt

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions