# Print the Unity3D name for the current game
# This function should not fail if no Unity3D name is set for the current game,
# so it can be used to automatically detect games using the "unity3d" type variant.
# USAGE: unity3d_name
# RETURN: the Unity3D name, a string that can include spaces,
#         or an empty string if none is set
unity3d_name() {
	context_value 'UNITY3D_NAME'
}

# Print the list of Unity3D plugins to include for the current game
# USAGE: unity3d_plugins
# RETURN: the list of plugins to include, one per line
unity3d_plugins() {
	context_value 'UNITY3D_PLUGINS'
}

# Include the shipped Unity3D plugins into the given package.
# USAGE: content_inclusion_unity3d_plugins $package
## TODO: This function should be expanded to handle plugins of Windows builds.
content_inclusion_unity3d_plugins() {
	local package=$1

	# Check the package architecture, return early if it should not include any plugin.
	local package_architecture architecture_string
	package_architecture=$(package_architecture "$package")
	case "$package_architecture" in
		('32')
			architecture_string='x86'
		;;
		('64')
			architecture_string='x86_64'
		;;
		(*)
			return 0
		;;
	esac

	# Set the plugins source path.
	local path_archive_contents unity3d_name
	path_archive_contents=$(path_archive_contents)
	unity3d_name=$(unity3d_name)
	local \
		plugins_directory="${unity3d_name}_Data/Plugins" \
		plugins_path="${path_archive_contents}/${unity3d_name}_Data/Plugins"

	# Proceed with the actual files inclusion.
	local target_directory
	target_directory=$(
		set_current_package "$package"
		path_libraries
	)
	## Include files from the x86_64 or x86 sub-directory.
	if [ -d "${plugins_path}/${architecture_string}" ]; then
		unity3d_plugins_inclusion "$package" "${plugins_directory}/${architecture_string}" "$target_directory"
	fi
	## Include files directly from the Plugins directory.
	if [ -d "$plugins_path" ]; then
		unity3d_plugins_inclusion "$package" "$plugins_directory" "$target_directory"
	fi
}

# Proceed with the actual inclusion of Unity3D plugins files from the given directory.
# USAGE: unity3d_plugins_inclusion $package $source_directory $target_directory
unity3d_plugins_inclusion() {
	local \
		package=$1 \
		source_directory=$2 \
		target_directory=$3

	# Include files listed as UNITY3D_PLUGINS.
	local CONTENT_UNITY3D_PLUGINS_FILES
	# shellcheck disable=SC2034
	CONTENT_UNITY3D_PLUGINS_FILES=$(unity3d_plugins)
	# shellcheck disable=SC2034
	local CONTENT_UNITY3D_PLUGINS_RELATIVE_PATH=$source_directory
	content_inclusion 'UNITY3D_PLUGINS' "$package" "$target_directory"

	# Delete remaining files.
	local path_archive_contents
	path_archive_contents=$(path_archive_contents)
	find "${path_archive_contents}/${source_directory}" -type f -delete
	find "${path_archive_contents}/${source_directory}" -type d -empty -delete
}

# Unity3D - Print the list of files to include from the archive for a given identifier
# USAGE: unity3d_content_files_default $content_id
# RETURN: a list of paths relative to the path for the given identifier,
#         line breaks are used as separator between each item,
#         this list can include globbing patterns,
#         this list can be empty
unity3d_content_files_default() {
	local content_id
	content_id="$1"

	local unity3d_name
	unity3d_name=$(unity3d_name)

	local application_type
	application_type=$(application_default_type)

	local content_files
	case "$content_id" in
		('GAME_BIN')
			case "$application_type" in
				('native')
					## TODO: An explicit list of .so files should be set, to prevent the inclusion of unwanted libraries.
					content_files="
					MonoBleedingEdge
					${unity3d_name}_Data/Mono
					${unity3d_name}_Data/MonoBleedingEdge
					${unity3d_name}.x86_64
					${unity3d_name}.x86
					${unity3d_name}
					*.so"
				;;
				('wine')
					content_files="
					Mono
					MonoBleedingEdge
					${unity3d_name}_Data/Mono
					${unity3d_name}_Data/MonoBleedingEdge
					${unity3d_name}_Data/Plugins
					${unity3d_name}.exe
					baselib.dll
					GameAssembly.dll
					UnityPlayer.dll"
					## Game scripts targeting ./play.it < 2.28 might rely on more .dll files being included.
					if ! compatibility_level_is_at_least '2.28'; then
						content_files="$content_files
						*.dll"
					fi
				;;
			esac
		;;
		('GAME_BIN32')
			case "$application_type" in
				('native')
					content_files="
					MonoBleedingEdge/x86
					${unity3d_name}_Data/Mono/x86
					${unity3d_name}_Data/MonoBleedingEdge/x86
					${unity3d_name}.x86
					${unity3d_name}"
				;;
			esac
		;;
		('GAME_BIN64')
			case "$application_type" in
				('native')
					content_files="
					MonoBleedingEdge/x86_64
					${unity3d_name}_Data/Mono/x86_64
					${unity3d_name}_Data/MonoBleedingEdge/x86_64
					${unity3d_name}.x86_64
					${unity3d_name}"
				;;
			esac
		;;
		('GAME_DATA')
			case "$application_type" in
				('native')
					content_files="
					${unity3d_name}_Data"
				;;
				('wine')
					content_files="
					${unity3d_name}_Data"
				;;
			esac
		;;
	esac

	printf '%s' "${content_files:-}"
}

