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

###
# Iron Harvest
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260402.8

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='iron-harvest'
GAME_NAME='Iron Harvest'

ARCHIVE_BASE_2_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788).exe'
ARCHIVE_BASE_2_MD5='439ac26376af2bfbf847e38067a98560'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='476651fa957718282b07b92cad0ace70'
ARCHIVE_BASE_2_PART2_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='c8d8c7d638eafb96d39a1b02df4e599d'
ARCHIVE_BASE_2_PART3_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788)-3.bin'
ARCHIVE_BASE_2_PART3_MD5='ff0e9f141ee1d932b83cac390fd42e4e'
ARCHIVE_BASE_2_PART4_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788)-4.bin'
ARCHIVE_BASE_2_PART4_MD5='18653d5ca9807d9f55250acb761cf771'
ARCHIVE_BASE_2_PART5_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_cve-2025-59489_(64bit)_(85788)-5.bin'
ARCHIVE_BASE_2_PART5_MD5='035c3e4cd9a7482b4e88ebc38cc0a8d7'
ARCHIVE_BASE_2_SIZE='30854792'
ARCHIVE_BASE_2_VERSION='1.4.8.2986-gog85788'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/iron_harvest'

ARCHIVE_BASE_1_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453).exe'
ARCHIVE_BASE_1_MD5='1f13da4dc9ce6916075c4f3287feba5a'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='39064f2c73fc1289d6963d60ad3ed20f'
ARCHIVE_BASE_1_PART2_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453)-2.bin'
ARCHIVE_BASE_1_PART2_MD5='6c7307a3c9fd9762e277efde3f38c408'
ARCHIVE_BASE_1_PART3_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453)-3.bin'
ARCHIVE_BASE_1_PART3_MD5='c8a1219731b4d51bf294937691883bbd'
ARCHIVE_BASE_1_PART4_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453)-4.bin'
ARCHIVE_BASE_1_PART4_MD5='6501face360636344b93183c072b9a10'
ARCHIVE_BASE_1_PART5_NAME='setup_iron_harvest_1.4.8.2986_rev._58254_(64bit)_(58453)-5.bin'
ARCHIVE_BASE_1_PART5_MD5='ead1c35efe7a6883a4204550650f2b0f'
ARCHIVE_BASE_1_SIZE='30858160'
ARCHIVE_BASE_1_VERSION='1.4.8.2986-gog58453'

ARCHIVE_BASE_0_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091).exe'
ARCHIVE_BASE_0_MD5='d89a5f6b4a8c89769af3eaadc75953f2'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='5d90f09c6b65889db5754d97cbdfebf3'
ARCHIVE_BASE_0_PART2_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='80be1a755688797cc7a7a5b099add78a'
ARCHIVE_BASE_0_PART3_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='81e7c85bc9017e8ae157bdea2140d852'
ARCHIVE_BASE_0_PART4_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='3279373222b0dfdc37bd4d882d3bcc63'
ARCHIVE_BASE_0_PART5_NAME='setup_iron_harvest_1.4.8.2983_rev._58247_(64bit)_(58091)-5.bin'
ARCHIVE_BASE_0_PART5_MD5='96b6db50d7167699257c1ab6428c8711'
ARCHIVE_BASE_0_SIZE='31000000'
ARCHIVE_BASE_0_VERSION='1.4.8.2983-gog58091'

UNITY3D_NAME='IronHarvest'
# Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Drop unused libraries.
UNITY3D_PLUGINS='
AnselPlugin64.dll
AnselSDK64.dll
CChromaEditorLibrary3.dll
CChromaEditorLibrary64.dll
dsruntime.dll
EOSSDK-Win64-Shipping.dll
fpasm.dll
grpc_csharp_ext.dll
libcrypto-1_1-x64.dll
libssl-1_1-x64.dll
Microsoft_Xbox_Services_141_GDK_C_Thunks.dll
Rewired_DirectInput.dll
XDKChromaEditorLibrary.dll
XGamingRuntimeThunks.dll'
# TODO: Check if the Galaxy libraries are required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll
GalaxyCSharpGlue.dll"
# TODO: Check if the Steam libraries are required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
CSteamworks.dll
steam_api64.dll"

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES="
config
release/MonoBleedingEdge
release/UnityPlayer.dll
release/${UNITY3D_NAME}.exe"
CONTENT_GAME_DATA_SCENES_1_FILES='
gamedata/scenes/scene_sp_*'
CONTENT_GAME_DATA_SCENES_2_FILES='
gamedata/scenes'
CONTENT_GAME_DATA_CINEMATICS_FILES="
release/${UNITY3D_NAME}_Data/StreamingAssets/Cinematics"
CONTENT_GAME_DATA_SETTING_FILES='
gamedata/setting'
CONTENT_GAME_DATA_UI_FILES='
gamedata/ui'
CONTENT_GAME_DATA_FILES="
battleplan
content
gamedata
lang
logic
version
release/${UNITY3D_NAME}_Data"
CONTENT_DOC_DATA_FILES='
licenses'

# Some textures are not displayed correctly with WineD3D,
# the effect is annoying enough to prevent playing in correct conditions.
# (WINE 11.5)
WINE_DIRECT3D_RENDERER='dxvk'
WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Local/GOG.com/Galaxy/Applications/49637353028337449'

APP_MAIN_EXE="release/${UNITY3D_NAME}.exe"
APP_MAIN_ICON="$APP_MAIN_EXE"

PACKAGES_LIST='
PKG_BIN
PKG_DATA_SCENES_1
PKG_DATA_SCENES_2
PKG_DATA_CINEMATICS
PKG_DATA_SETTING
PKG_DATA_UI
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/quicktime, variant=(string)iso'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_SCENES_1
PKG_DATA_SCENES_2
PKG_DATA_CINEMATICS
PKG_DATA_SETTING
PKG_DATA_UI'

PKG_DATA_SCENES_ID="${PKG_DATA_ID}-scenes"
PKG_DATA_SCENES_1_ID="${PKG_DATA_SCENES_ID}-1"
PKG_DATA_SCENES_2_ID="${PKG_DATA_SCENES_ID}-2"
PKG_DATA_SCENES_DESCRIPTION="$PKG_DATA_DESCRIPTION - scenes"
PKG_DATA_SCENES_1_DESCRIPTION="$PKG_DATA_SCENES_DESCRIPTION - 1"
PKG_DATA_SCENES_2_DESCRIPTION="$PKG_DATA_SCENES_DESCRIPTION - 2"

PKG_DATA_CINEMATICS_ID="${PKG_DATA_ID}-cinematics"
PKG_DATA_CINEMATICS_DESCRIPTION="$PKG_DATA_DESCRIPTION - cinematics"

PKG_DATA_SETTING_ID="${PKG_DATA_ID}-setting"
PKG_DATA_SETTING_DESCRIPTION="$PKG_DATA_DESCRIPTION - setting"

PKG_DATA_UI_ID="${PKG_DATA_ID}-ui"
PKG_DATA_UI_DESCRIPTION="$PKG_DATA_DESCRIPTION - ui"

# 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' "release/$(unity3d_name)_Data/Plugins/x86_64" "$(path_libraries)/release/$(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
