#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2023 Antoine Le Gonidec <vv221@dotslashplay.it>
set -o errexit

###
# Endless Space
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260404.6

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='endless-space-1'
GAME_NAME='Endless Space'

ARCHIVE_BASE_0_NAME='setup_endlesstm_space_-_definitive_edition_1.0.0._(64185).exe'
ARCHIVE_BASE_0_MD5='84ccf58a734694a63db59e599344a254'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_endlesstm_space_-_definitive_edition_1.0.0._(64185)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='65a88affbdbff4aa78808b941184051b'
ARCHIVE_BASE_0_SIZE='3600000'
ARCHIVE_BASE_0_VERSION='1.1.59-gog64185'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/endless_space_definitive_edition'

UNITY3D_NAME='Endless space'
# Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Drop unused libraries.
# The following plugins are .Net/Mono assemblies,
# we handle them like regular Windows libraries.
UNITY3D_PLUGINS='
ICSharpCode.SharpZipLib.dll
Newtonsoft.Json.dll'
# TODO: Check if the Steam library is required.
# Unlike the ones above, this plugin is a regular win32 library.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Amplitude.Steam.Interop.dll"

CONTENT_PATH_DEFAULT='.'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME0_BIN_FILES='
avprowindowsmedia.dll
fmod_event.dll
fmod_event_net.dll
fmodex.dll
stlport_vc10.5.2.dll'
# The game fails to start if the Galaxy libraries are not available.
CONTENT_GAME1_BIN_FILES='
galaxy.dll
galaxycsharp.dll
galaxycsharpglue.dll'
CONTENT_GAME0_DATA_FILES='
Public
Public_xp1'

# The game engine expects write access to XML files.
USER_PERSISTENT_FILES='
*.xml'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/Endless Space'

# Skip playing the logo movie on launch, as it would fail with the following errors:
# 0024:fixme:quartz:mpeg_splitter_sink_query_accept Unsupported subtype {e436eb85-524f-11ce-9f53-0020af0ba770}.
# 0024:fixme:quartz:mpeg_splitter_sink_query_accept Unsupported subtype {e436eb86-524f-11ce-9f53-0020af0ba770}.
# 0024:fixme:quartz:MediaSeeking_SetRate (003B24E0/003B24EC)->(1.000000): stub !!!
# 0024:fixme:quartz:MediaSeeking_SetRate (003B24E0/003B24EC)->(1.000000): stub !!!
# 0:00:00.947894012      30458      30653 ERROR                   WINE dlls/winegstreamer/unixlib.c:241:push_event: Failed to push qos event 0xd9800d80 to pad qz_sink_0.
# 0:00:07.054013004      30458      30653 ERROR                   WINE dlls/winegstreamer/unixlib.c:241:push_event: Failed to push qos event 0xd98012c0 to pad qz_sink_0.
# 0:00:07.088732699      30458      30653 ERROR                   WINE dlls/winegstreamer/unixlib.c:241:push_event: Failed to push qos event 0xd9801590 to pad qz_sink_0.
# 0:00:07.120354217      30458      30653 ERROR                   WINE dlls/winegstreamer/unixlib.c:241:push_event: Failed to push qos event 0xd98016b0 to pad qz_sink_0.
# (WINE 11.5)
APP_MAIN_OPTIONS='-nologo'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/quicktime, variant=(string)iso
video/x-ms-asf'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'

# Load common functions.

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$PWD" \
		"${HOME}/.local/lib/play.it" \
		'/usr/local/lib/play.it' \
		'/usr/lib/play.it'
	do
		playit_lib_path="${playit_lib_dir}/libplayit.sh.2"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit.sh.2 not found.\n'
	exit 1
fi
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Include Unity3D plugins.
unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Install Unity3D plugins.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
