1#!/bin/bash -e
2# Install Total War WARHAMMER II on Linux and generate launcher scripts and preference templates
3
4# Base constants
5#
6export STEAM_GAME_ID=594570
7export GAME_PREFS="$DEBUG_REAL_HOME/.local/share/feral-interactive/Total War WARHAMMER II"
8export GAME_INSTALL_DIR_BASE="steamapps/common/Total War WARHAMMER II/"
9export DEFAULT_STEAM_INSTALL_BASE="$DEBUG_REAL_HOME/.steam/steam"
10
11
12# Try and install the game in case it isn't already
13#
14echo "Ensuring game is installed"
15HOME="$DEBUG_REAL_HOME" steam "steam://install/$STEAM_GAME_ID"
16
17
18# Work out the steam install directory
19#
20export CONFIG_PATH="$DEBUG_REAL_HOME/.steam/steam/config/config.vdf"
21echo "Searching ${CONFIG_PATH} for install directories"
22_INSTALL_PATHS=$( awk '/BaseInstallFolder/ { gsub(/"/, "", $2); print $2 }' "${CONFIG_PATH}" )
23
24# Find one that contains the game
25while read -r STEAM_PATH; do
26    _NEW_FULL_PATH="${STEAM_PATH}/${GAME_INSTALL_DIR_BASE}"
27    echo "Checking for game install: ${_NEW_FULL_PATH}"
28    if [ -d "${_NEW_FULL_PATH}" ]; then
29        echo "Found game install: ${_NEW_FULL_PATH}"
30        export GAME_INSTALL_DIR="${_NEW_FULL_PATH}"
31    fi
32done <<< "${_INSTALL_PATHS}"
33
34# Allow the default location as well
35if [ ! -d "${GAME_INSTALL_DIR}" ]; then
36    export GAME_INSTALL_DIR="${DEFAULT_STEAM_INSTALL_BASE}/${GAME_INSTALL_DIR_BASE}"
37    echo "Using default directory for game install: ${GAME_INSTALL_DIR}"
38fi
39
40# Bail if we still couldn't find the game
41if [ ! -f "${GAME_INSTALL_DIR}/TotalWarhammer2.sh" ]; then
42    >&2 echo "Missing run script in install dir - ${GAME_INSTALL_DIR}/TotalWarhammer2.sh"
43    exit 1
44fi
45
46# Gather the steam env variables the game runs with
47#
48echo "Gathering environment variables for game"
49HOME="$DEBUG_REAL_HOME" steam steam://run/$STEAM_GAME_ID &
50sleep 6
51GAME_PID=$( pidof TotalWarhammer2 | cut -d' ' -f1 )
52if [ -z "$GAME_PID" ]; then
53    echo "Could not find process TotalWarhammer2"
54    exit 1
55fi
56
57echo '#!/bin/bash' > steam-env-vars.sh
58echo "# Collected steam environment for Total War: Warhammer II\n# PID : $GAME_PID" >> steam-env-vars.sh
59while read -rd $'\0' ENV ; do
60    NAME=$(echo "$ENV" | cut -zd= -f1); VAL=$(echo "$ENV" | cut -zd= -f2)
61    case $NAME in
62	*DBUS*) true
63	;;
64	*)
65        echo "export $NAME=\"$VAL\""
66	;;
67    esac
68done < "/proc/$GAME_PID/environ" >> steam-env-vars.sh
69killall -9 TotalWarhammer2
70sleep 6
71
72
73
74if [ -z "${STEAM_ACCOUNT_ID}" ]; then
75    pushd "${GAME_PREFS}/SaveData/"
76    STEAM_ACCOUNT_ID="$(ls |head -1)"
77    popd
78else
79    STEAM_ACCOUNT_ID="Steam Saves (${STEAM_ACCOUNT_ID})"
80fi
81
82RESULTS_PREFIX="${GAME_PREFS}/SaveData/${STEAM_ACCOUNT_ID}/"
83
84
85# Create the game launching script
86#
87echo "Generating run script"
88cat > tww2.sh <<- EOM
89#!/bin/bash
90# Generated run script for Total War: WARHAMMER II
91# $( date )
92
93# Source the steam runtime environment
94#
95. steam-env-vars.sh
96
97# Run the game
98#
99cd "${GAME_INSTALL_DIR}"
100./TotalWarhammer2.sh
101
102# Grab the output (most recent non _frametimes txt file)
103RESULTS_DIR="${RESULTS_PREFIX}benchmarks/"
104mkdir -p "\${RESULTS_DIR}"
105cd "\${RESULTS_DIR}"
106true > "\$LOG_FILE"
107FPS_VALUES=\$( grep -A3 "frames per second" \$(ls -t | grep -P "benchmark_.*[0-9]+.txt" | head -n 1) | tail -n 3 )
108cat benchmark_*.txt >>  "\$LOG_FILE"
109echo "\${FPS_VALUES}" >> "\$LOG_FILE"
110EOM
111chmod +x tww2.sh
112
113
114# Create the template preferences file
115#
116echo "Generating settings template"
117cat > preferences.template.xml <<- EOM
118<?xml version="1.0" encoding="UTF-8"?>
119<registry>
120    <key name="HKEY_CURRENT_USER">
121        <key name="Software">
122            <key name="Feral Interactive">
123                <key name="Total War WARHAMMER II">
124                    <key name="Setup">
125                        <!-- resolution -->
126                        <value name="ScreenH" type="integer">@screen_height@</value>
127                        <value name="ScreenW" type="integer">@screen_width@</value>
128                        
129                        <!-- disable pausing -->
130                        <value name="AllowPausing" type="integer">0</value>
131                        <value name="PauseMoviesOnPause" type="integer">0</value>
132                        <value name="PauseOnSuspend" type="integer">0</value>
133                        <value name="PauseSoundOnPause" type="integer">0</value>
134                        <value name="PauseTimersOnPause" type="integer">0</value>
135
136                        <value name="AllowSendUsageData" type="integer">0</value>
137
138
139                        <!-- Don't show splash screen -->
140                        <value name="GameOptionsDialogLastTab" type="integer">60000</value>
141                        <value name="GameOptionsDialogShouldShow" type="integer">0</value>
142                        <value name="GameOptionsDialogShouldShowBigPicture" type="integer">0</value>
143                        <value name="GameOptionsDialogShown" type="integer">1</value>
144
145                        <!-- Disable Splash Screen Warnings -->
146                        <value name="SoftwareUpdatedAskedUser" type="integer">1</value>
147                        <value name="SoftwareUpdatedCanCheck" type="integer">0</value>
148                        
149                        <!-- Skip default settings -->
150                        <value name="SkipDefaultSettings" type="integer">1</value>
151                        <value name="SkipDriverWarnings" type="integer">1</value>
152                        <value name="SkipOSWarnings" type="integer">1</value>
153
154                        <key name="GraphicsSettings">
155                            <value name="gfx_aa" type="integer">@gfx_aa@</value>
156                            <value name="gfx_alpha_blend" type="integer">0</value>
157                            <value name="gfx_blood_effects" type="integer">1</value>
158                            <value name="gfx_building_quality" type="integer">@gfx_building_quality@</value>
159                            <value name="gfx_depth_of_field" type="integer">0</value>
160                            <value name="gfx_distortion" type="integer">1</value>
161                            <value name="gfx_effects_quality" type="integer">@gfx_effects_quality@</value>
162                            <value name="gfx_first_run" type="integer">0</value>
163                            <value name="gfx_fog" type="integer">@gfx_fog@</value>
164                            <value name="gfx_gamma_setting" type="binary">0000000000000040</value>
165                            <value name="gfx_gpu_select" type="integer">0</value>
166                            <value name="gfx_grass_quality" type="integer">@gfx_grass_quality@</value>
167                            <value name="gfx_lighting_quality" type="integer">@gfx_lighting_quality@</value>
168                            <value name="gfx_screen_space_reflections" type="integer">0</value>
169                            <value name="gfx_shadow_quality" type="integer">@gfx_shadow_quality@</value>
170                            <value name="gfx_sharpening" type="integer">1</value>
171                            <value name="gfx_sky_quality" type="integer">@gfx_sky_quality@</value>
172                            <value name="gfx_ssao" type="integer">@gfx_ssao@</value>
173                            <value name="gfx_terrain_quality" type="integer">@gfx_terrain_quality@</value>
174                            <value name="gfx_tesselation" type="integer">0</value>
175                            <value name="gfx_texture_filtering" type="integer">@gfx_texture_filtering@</value>
176                            <value name="gfx_texture_quality" type="integer">@gfx_texture_quality@</value>
177                            <value name="gfx_tree_quality" type="integer">@gfx_tree_quality@</value>
178                            <value name="gfx_unit_quality" type="integer">@gfx_unit_quality@</value>
179                            <value name="gfx_unit_size" type="integer">@gfx_unit_size@</value>
180                            <value name="gfx_unlimited_video_memory" type="integer">0</value>
181                            <value name="gfx_vignette" type="integer">0</value>
182                            <value name="gfx_vsync" type="integer">0</value>
183                            <value name="gfx_water_quality" type="integer">@gfx_water_quality@</value>
184                            <value name="porthole_3d" type="integer">@porthole_3d@</value>
185                        </key>
186                    </key>
187                </key>
188            </key>
189            <key name="MacDoze">
190                <key name="Config">
191                    <value name="ExtraCommandLine" type="string">game_startup_mode benchmark_auto_quit script/benchmarks/@benchmark_name@</value>
192                    <value name="ExtraCommandLineEnabled" type="integer">1</value>
193                </key>
194            </key>
195        </key>
196    </key>
197</registry>
198EOM
199