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

###
# Galacticare
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260315.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='galacticare'
GAME_NAME='Galacticare'

ARCHIVE_BASE_3_NAME='setup_galacticare_1.2.3_(88331).exe'
ARCHIVE_BASE_3_MD5='b8474e193e1d99372e42f62b08061501'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_galacticare_1.2.3_(88331)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='c313432fe1d1cce94bd4e9cb76322f7d'
ARCHIVE_BASE_3_PART2_NAME='setup_galacticare_1.2.3_(88331)-2.bin'
ARCHIVE_BASE_3_PART2_MD5='4377215cdeaa6ecb76d293d696e500cb'
ARCHIVE_BASE_3_SIZE='29187597'
# From 1.2.3.1 to 1.2.3, hopefully a symptom of developers mathematics
# and not an actual rollback to an older game build.
ARCHIVE_BASE_3_VERSION='1.2.3-gog88331'
ARCHIVE_BASE_3_URL='https://www.gog.com/game/galacticare'

ARCHIVE_BASE_2_NAME='setup_galacticare_1.2.3.1plus6d76582b76_(82662).exe'
ARCHIVE_BASE_2_MD5='7b070946d0dc45e0df58b073195d48fb'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_galacticare_1.2.3.1plus6d76582b76_(82662)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='dd3d876d9b90e018a83e3722d4f89f3f'
ARCHIVE_BASE_2_PART2_NAME='setup_galacticare_1.2.3.1plus6d76582b76_(82662)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='c4bc049c0f14a2cf190d27763513f65e'
ARCHIVE_BASE_2_SIZE='29191051'
ARCHIVE_BASE_2_VERSION='1.2.3.1-gog82662'

ARCHIVE_BASE_1_NAME='setup_galacticare_1.2.3plus6d76582b76_(82235).exe'
ARCHIVE_BASE_1_MD5='7d95382f9ccd962ed3c571f30c8ca642'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_galacticare_1.2.3plus6d76582b76_(82235)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='54c02d98af9b70b80e6438e12c4c9611'
ARCHIVE_BASE_1_PART2_NAME='setup_galacticare_1.2.3plus6d76582b76_(82235)-2.bin'
ARCHIVE_BASE_1_PART2_MD5='d420139ae4cebdcae18183d5fb77c982'
ARCHIVE_BASE_1_PART3_NAME='setup_galacticare_1.2.3plus6d76582b76_(82235)-3.bin'
ARCHIVE_BASE_1_PART3_MD5='ad4998209a459ccc9fd23c43fa53554f'
ARCHIVE_BASE_1_PART4_NAME='setup_galacticare_1.2.3plus6d76582b76_(82235)-4.bin'
ARCHIVE_BASE_1_PART4_MD5='0c927f423541c464acf3e27d0922f7fe'
ARCHIVE_BASE_1_SIZE='34968590'
ARCHIVE_BASE_1_VERSION='1.2.3-gog82235'

ARCHIVE_BASE_0_NAME='setup_galacticare_1.2.0plus73e3612ea7_(81088).exe'
ARCHIVE_BASE_0_MD5='87f45a1769017c88f4a0ed323d1ca6e5'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_galacticare_1.2.0plus73e3612ea7_(81088)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='54c02d98af9b70b80e6438e12c4c9611'
ARCHIVE_BASE_0_PART2_NAME='setup_galacticare_1.2.0plus73e3612ea7_(81088)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='8afab5b704640ef48ea419517feda31f'
ARCHIVE_BASE_0_PART3_NAME='setup_galacticare_1.2.0plus73e3612ea7_(81088)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='9042ba9cb019e5ec52e8ff0560d5470d'
ARCHIVE_BASE_0_PART4_NAME='setup_galacticare_1.2.0plus73e3612ea7_(81088)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='94cc896f1bf5ada6099d75200b666178'
ARCHIVE_BASE_0_SIZE='34948708'
ARCHIVE_BASE_0_VERSION='1.2.0-gog81088'

UNITY3D_NAME='Galacticare'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# Prevent a loading loop.
UNITY3D_PLUGINS='
GalaxyCSharpGlue.dll'
# GalaxyCSharpGlue.dll requirement
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll"

CONTENT_PATH_DEFAULT='.'
# galacticare.exe requirements
CONTENT_LIBS_BIN_FILES='
UnityPlayer.dll'
# Prevent a mono loading failure on launch.
CONTENT_LIBS0_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/mono-2.0-bdwgc.dll'
# Prevent a failure to load actual gameplay.
CONTENT_LIBS1_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/MonoPosixHelper.dll'
CONTENT_GAME_DATA_CHARACTERS_FILES='
galacticare_data/streamingassets/assetbundles/characters-*'
CONTENT_GAME_DATA_ENVIRONMENT_FILES='
galacticare_data/streamingassets/assetbundles/environment-*'
CONTENT_GAME_DATA_ROOMS_FILES='
galacticare_data/streamingassets/assetbundles/rooms-*
galacticare_data/streamingassets/assetbundles/props-*'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Brightrock Games/Galacticare'

PACKAGES_LIST='
PKG_BIN
PKG_DATA_CHARACTERS
PKG_DATA_ENVIRONMENT
PKG_DATA_ROOMS
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_CHARACTERS
PKG_DATA_ENVIRONMENT
PKG_DATA_ROOMS'

PKG_DATA_CHARACTERS_ID="${PKG_DATA_ID}-characters"
PKG_DATA_CHARACTERS_DESCRIPTION="$PKG_DATA_DESCRIPTION - characters"

PKG_DATA_ENVIRONMENT_ID="${PKG_DATA_ID}-environment"
PKG_DATA_ENVIRONMENT_DESCRIPTION="$PKG_DATA_DESCRIPTION - environment"

PKG_DATA_ROOMS_ID="${PKG_DATA_ID}-rooms"
PKG_DATA_ROOMS_DESCRIPTION="$PKG_DATA_DESCRIPTION - rooms and props"

# 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 'libplayit2.sh not found.\n'
	exit 1
fi
# shellcheck source=libplayit2.sh
. "$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/x86_64" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Install shipped libraries.
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 "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
