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

###
# Urtuk: The Desolation
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260517.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='urtuk-the-desolation'
GAME_NAME='Urtuk: The Desolation'

ARCHIVE_BASE_0_NAME='urtuk_the_desolation_1_0_0_91b_53003.sh'
ARCHIVE_BASE_0_MD5='4fddaaa42c70b75258a78d4a5b103473'
ARCHIVE_BASE_0_SIZE='740000'
ARCHIVE_BASE_0_VERSION='1.0.0.91b-gog53003'

CONTENT_PATH_DEFAULT='data/noarch/game'
CONTENT_GAME_MAIN_FILES='
desktop-1.0.jar'
# Unless the shipped Java binaries are used, trying to change any game setting triggers a crash:
# Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.ExceptionInInitializerError
#         at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:133)
# Caused by: java.lang.ExceptionInInitializerError
#         at kaleta.hex3.GlobalSettings.saveToDisk(GlobalSettings.java:152)
#         at kaleta.hex3.gameScreens.GlobalSettingsScreen.lambda$null$5897aa15$1(GlobalSettingsScreen.java:71)
#         at kaleta.hex3.utils.UIBuilder$1.onClick(UIBuilder.java:158)
#         at kaleta.hex3.gameScreens.uilayout.UIElementBase.onClickInternal(UIElementBase.java:145)
#         at kaleta.hex3.gameScreens.uilayout.ImageButton.onClickInternal(ImageButton.java:112)
#         at kaleta.hex3.gameScreens.uilayout.UIElementBase.tick(UIElementBase.java:284)
#         at kaleta.hex3.gameScreens.uilayout.UIElementBase.tick(UIElementBase.java:222)
#         at kaleta.hex3.gameScreens.uilayout.UIContainer.tick(UIContainer.java:227)
#         at kaleta.hex3.gameScreens.uilayout.UIContainer.tick(UIContainer.java:227)
#         at kaleta.hex3.gameScreens.uilayout.UIContainer.tick(UIContainer.java:227)
#         at kaleta.hex3.gameScreens.uilayout.UIRoot$PopupRoot.tick(UIRoot.java:86)
#         at kaleta.hex3.gameScreens.uilayout.UIRoot.tick(UIRoot.java:195)
#         at kaleta.hex3.gameScreens.GlobalSettingsScreen.tick(GlobalSettingsScreen.java:759)
#         at kaleta.hex3.HexGame3.render(HexGame3.java:1159)
#         at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
#         at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
# Caused by: java.lang.IllegalArgumentException: Unable to create serializer "com.esotericsoftware.kryo.serializers.FieldSerializer" for class: java.lang.invoke.SerializedLambda
#         at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:67)
#         at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:45)
#         at com.esotericsoftware.kryo.Kryo.newDefaultSerializer(Kryo.java:396)
#         at com.esotericsoftware.kryo.Kryo.getDefaultSerializer(Kryo.java:380)
#         at com.esotericsoftware.kryo.Kryo.register(Kryo.java:410)
#         at kaleta.hex3.utils.KryoSerializer.initKryo(KryoSerializer.java:35)
#         at kaleta.hex3.utils.KryoSerializer.<clinit>(KryoSerializer.java:26)
#         ... 16 more
# Caused by: java.lang.reflect.InvocationTargetException
#         at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
#         at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
#         at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
#         at com.esotericsoftware.kryo.factories.ReflectionSerializerFactory.makeSerializer(ReflectionSerializerFactory.java:54)
#         ... 22 more
# Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Class java.lang.invoke.SerializedLambda.capturingClass accessible: module java.base does not "opens java.lang.invoke" to unnamed module @2133c8f8
#         at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:353)
#         at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:329)
#         at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:277)
#         at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:179)
#         at java.base/java.lang.reflect.Field.setAccessible(Field.java:173)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.buildValidFields(FieldSerializer.java:282)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.rebuildCachedFields(FieldSerializer.java:215)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.rebuildCachedFields(FieldSerializer.java:156)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.<init>(FieldSerializer.java:149)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.<init>(FieldSerializer.java:133)
#         at com.esotericsoftware.kryo.serializers.FieldSerializer.<init>(FieldSerializer.java:129)
#         at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
#         ... 25 more
# (OpenJDK 25.0.3)
CONTENT_GAME_BIN_SHIPPED_FILES='
jre
config.json
urtuk'

USER_PERSISTENT_FILES='
*.bin
*.bin.backup'

APP_MAIN_TYPE_BIN_SYSTEM='java'
APP_MAIN_EXE_BIN_SYSTEM='desktop-1.0.jar'
APP_MAIN_EXE_BIN_SHIPPED='urtuk'
# TODO: Look for the original game icon in the Windows build.
APP_MAIN_ICON='../support/icon.png'

PACKAGES_LIST='
PKG_MAIN
PKG_BIN_SYSTEM
PKG_BIN_SHIPPED'

PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_BIN'
# Easier upgrades from pre-20260517.1 packages
PKG_MAIN_PROVIDES='
urtuk-the-desolation-data'

PKG_BIN_SHIPPED_ARCH='64'
PKG_BIN_ID="${GAME_ID}-bin"
PKG_BIN_SYSTEM_ID="${PKG_BIN_ID}-system"
PKG_BIN_SHIPPED_ID="${PKG_BIN_ID}-shipped"
PKG_BIN_PROVIDES="
$PKG_BIN_ID"
PKG_BIN_SYSTEM_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_SHIPPED_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_SHIPPED_DEPENDENCIES_LIBRARIES='
libasound.so.2
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libpthread.so.0
libstdc++.so.6
libthread_db.so.1
libX11.so.6
libXext.so.6
libXi.so.6
libXrender.so.1
libXtst.so.6'
PKG_BIN_SYSTEM_DESCRIPTION='Using system-provided Java'
PKG_BIN_SHIPPED_DESCRIPTION='Using shipped binaries'

# 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
content_inclusion_default

# Write launchers.

launchers_generation 'PKG_BIN_SYSTEM'
launchers_generation 'PKG_BIN_SHIPPED'

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		message='Utilisation des binaires fournis par %s :'
		bin_system='le système'
		bin_shipped='les développeurs'
	;;
	('en'|*)
		message='Using binaries provided by %s:'
		bin_system='the system'
		bin_shipped='the developers'
	;;
esac
printf '\n'
printf "$message" "$bin_system"
print_instructions 'PKG_MAIN' 'PKG_BIN_SYSTEM'
printf "$message" "$bin_shipped"
print_instructions 'PKG_MAIN' 'PKG_BIN_SHIPPED'

# Clean up.

working_directory_cleanup

exit 0
