-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_png_media.sh
executable file
·332 lines (288 loc) · 13 KB
/
generate_png_media.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
# ----------------------------------------------------------
# Creates images (playfield + backglass) for ASAPCabinetFE
# Opens all tables and screenshots playfield and backglass
# Saves them in table_name/images/ folder as:
# table.png and backglass.png (as set in config.ini)
# ----------------------------------------------------------
# Options:
# --now, -n Generate both missing playfield and backglass images
# --tables-only, -t Capture only missing table images
# --backglass-only, -b Capture only missing backglass images
# --dry-run Simulate --now mode, printing actions without executing them
# --tables List tables missing 'table.png' image and exit
# --backglass List tables missing 'backglass.png' image and exit
# --wheel List tables missing 'wheel.png' image and exit
# --dmd List tables missing 'dmd.png' image and exit
# --force, -f Generate both images, overwriting existing ones
# --clean, -c Removes 'table.png' and 'backglass.png' created by this script
# -h, --help Show this help message and exit
#
# Dependencies: xdotool, imagemagick
# Tarso Galvão Feb/2025
#set -x
# ANSI color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE='\033[0;34m'
NC="\033[0m" # No Color
CONFIG_FILE="config.ini"
# Function to get values from INI filef
echo -e "${BLUE}Initializing variables...${NC}"
get_ini_value() {
local section="$1"
local key="$2"
local value
echo -e "Searching for [$section] -> $key in $CONFIG_FILE" >&2 # Debugging output
value=$(awk -F= -v section="$section" -v key="$key" '
BEGIN { inside_section=0 }
/^\[.*\]$/ { inside_section=($0 == "[" section "]") }
inside_section && $1 ~ "^[ \t]*" key "[ \t]*$" { gsub(/^[ \t]+|[ \t]+$/, "", $2); gsub(/\r/, "", $2); print $2; exit }
' "$CONFIG_FILE")
echo -e "Found value: '$value'" >&2 # Debugging output
echo -e "$value"
}
# Load values from config.ini
if [[ -f "$CONFIG_FILE" ]]; then
ROOT_FOLDER=$(get_ini_value "VPX" "TablesPath")
echo -e "${YELLOW}ROOT_FOLDER: $ROOT_FOLDER${NC}"
VPX_EXECUTABLE=$(get_ini_value "VPX" "ExecutableCmd")
echo -e "${YELLOW}VPX_EXECUTABLE: $VPX_EXECUTABLE${NC}"
WHEEL_IMAGE=$(get_ini_value "CustomMedia" "WheelImage")
echo -e "${YELLOW}WHEEL_IMAGE: $WHEEL_IMAGE${NC}"
DMD_IMAGE=$(get_ini_value "CustomMedia" "DmdImage")
echo -e "${YELLOW}DMD_IMAGE: $DMD_IMAGE${NC}"
BACKGLASS_IMAGE=$(get_ini_value "CustomMedia" "BackglassImage")
echo -e "${YELLOW}BACKGLASS_IMAGE: $BACKGLASS_IMAGE${NC}"
TABLE_IMAGE=$(get_ini_value "CustomMedia" "TableImage")
echo -e "${YELLOW}TABLE_IMAGE: $TABLE_IMAGE${NC}"
else
echo -e "${RED}ERROR: config.ini not found. Exiting...${NC}"
exit 1
fi
SCREENSHOT_DELAY=12 # seconds
WINDOW_TITLE_VPX="Visual Pinball Player"
WINDOW_TITLE_BACKGLASS="B2SBackglass"
# WINDOW_TITLE_DMD1="PinMAME"
# WINDOW_TITLE_DMD2="FlexDMD"
check_xdotool() {
if ! command -v xdotool &> /dev/null; then
echo "Error: xdotool is not installed. Please install it to proceed."
echo "Debian: sudo apt install xdotool"
exit 1
fi
}
check_imagemagick() {
if ! command -v convert &> /dev/null; then
echo "Error: ImageMagick (convert) is not installed. Please install it to proceed."
echo "Debian: sudo apt install imagemagick"
exit 1
fi
}
# Run checks
check_xdotool
check_imagemagick
# Help function
show_help() {
echo -e "\nCreates ${GREEN}PNG images ${YELLOW}(playfield + backglass)${NC} for \033[4mASAPCabinetFE\033[0m"
echo -e "Opens all tables and screenshots playfield and backglass"
echo -e "Saves them in ${YELLOW}tables/<table_folder>/${NC} following ${YELLOW}config.ini${NC} settings"
echo -e "${BLUE}Usage:${NC} $0 [${BLUE}--now${NC} | ${BLUE}--dry-run${NC} | ${BLUE}--tables-only${NC} | ${BLUE}--backglass-only${NC}] [${YELLOW}--wheel${NC}] [${YELLOW}--dmd${NC}] [${RED}--force${NC}] [${RED}--clean${NC}]"
echo -e ""
echo -e " ${BLUE}--now, -n Capture missing table, backglass and dmd images"
echo -e " ${BLUE}--tables-only, -t ${NC}Capture only missing table images"
echo -e " ${BLUE}--backglass-only, -b ${NC}Capture only missing backglass images"
echo -e " ${BLUE}--dmd-only, -d ${NC}Capture only missing dmd images"
echo -e " ${GREEN}--dry-run Simulate --now mode, printing actions without executing them"
echo -e " ${YELLOW}--table ${NC}List tables missing table images and exit"
echo -e " ${YELLOW}--backglass ${NC}List tables missing backglass images and exit"
echo -e " ${YELLOW}--wheel ${NC}List tables missing wheel images and exit"
# echo -e " ${YELLOW}--dmd ${NC}List tables missing dmd images and exit"
echo -e " ${RED}--force, -f ${NC}Force rebuilding media even if they already exist"
echo -e " ${RED}--clean, -c Removes all PNG images created by this script"
echo -e "\n ${NC}-h, --help Show this help message and exit"
echo -e "\n${YELLOW}Note:${NC} No args shows this help."
exit 0
}
# Check for help or no arguments
if [ "$#" -eq 0 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
show_help
fi
DRY_RUN=false
FORCE=false
MODE="none" # Default: no action unless specified
# Parse arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=true
MODE="all"
echo -e "${YELLOW}Dry-run mode: No changes will be made.${NC}"
shift
;;
--force|-f)
FORCE=true
MODE="all"
echo -e "${BLUE}Capturing all images, overwriting existing ones.${NC}"
shift
;;
--now|-n)
MODE="all"
echo -e "${BLUE}Capturing all missing images.${NC}"
shift
;;
--tables-only|-t)
MODE="tables"
echo -e "${BLUE}Capturing only missing table images.${NC}"
shift
;;
--backglass-only|-b)
MODE="backglass"
echo -e "${BLUE}Capturing only missing backglass images.${NC}"
shift
;;
# --dmd-only|-d)
# MODE="dmd"
# echo -e "${BLUE}Capturing only missing dmd images.${NC}"
# shift
# ;;
--wheel|--dmd|--tables|--backglass)
case "$1" in
--wheel) IMAGE_TYPE="$WHEEL_IMAGE" ;;
--dmd) IMAGE_TYPE="$DMD_IMAGE" ;;
--tables) IMAGE_TYPE="$TABLE_IMAGE" ;;
--backglass) IMAGE_TYPE="$BACKGLASS_IMAGE" ;;
esac
echo -e "${BLUE}Using tables directory: ${GREEN}$ROOT_FOLDER"
echo -e "${BLUE}Checking for tables missing ${GREEN}<table_folder>/$IMAGE_TYPE...${NC}\n"
for vpx_file in "$ROOT_FOLDER"/*/*.vpx; do
if [ -f "$vpx_file" ]; then
table_dir=$(dirname "$vpx_file")
image_file="$table_dir/$IMAGE_TYPE"
if [ ! -f "$image_file" ]; then
echo -e "${GREEN}->${YELLOW} '$(basename "$table_dir")'${NC}"
fi
fi
done
echo -e "\n${BLUE}These tables have ${RED}no <table_folder>/$IMAGE_TYPE${BLUE} images.${NC}"
echo -e "${BLUE}Place them in ${YELLOW}$ROOT_FOLDER<table_folder>/$IMAGE_TYPE${NC}"
exit 0
;;
--clean|-c)
echo -e "${RED}Removing all table and backglass PNG files from all tables...${NC}"
# Extract filenames, regardless of path
TABLE_FILENAME=$(basename "$(basename "$TABLE_IMAGE")")
BACKGLASS_FILENAME=$(basename "$(basename "$BACKGLASS_IMAGE")")
# Find and remove files
find "$ROOT_FOLDER" -type f \( -name "$TABLE_FILENAME" -o -name "$BACKGLASS_FILENAME" \) -exec rm -v {} \;
echo -e "${GREEN}Media removed.${NC}"
exit 0
;;
*)
echo -e "\n${RED}Unknown option: $1${NC}"
show_help
;;
esac
done
# If no mode was set, show help
if [ "$MODE" == "none" ]; then
show_help
fi
find "$ROOT_FOLDER" -name "*.vpx" | while read -r VPX_PATH; do
TABLE_NAME=$(basename "$VPX_PATH" .vpx)
TABLE_DIR=$(dirname "$VPX_PATH")
IMAGES_FOLDER="${TABLE_DIR}/${TABLE_IMAGE%/*}"
TABLE_SCREENSHOT="${TABLE_DIR}/${TABLE_IMAGE}"
BACKGLASS_SCREENSHOT="${TABLE_DIR}/${BACKGLASS_IMAGE}"
DMD_SCREENSHOT="${TABLE_DIR}/${DMD_IMAGE}"
echo -e "${BLUE}Processing: $(basename "$(dirname "$VPX_PATH")")${NC}"
# Skip if images exist and --force isn't used
if [ "$MODE" == "all" ] && [ -f "$TABLE_SCREENSHOT" ] && [ -f "$BACKGLASS_SCREENSHOT" ] && [ -f "$DMD_SCREENSHOT" ]&& [ "$FORCE" != true ]; then
echo -e "${YELLOW} All images already exist, skipping.${NC}"
continue
elif [ "$MODE" == "tables" ] && [ -f "$TABLE_SCREENSHOT" ] && [ "$FORCE" != true ]; then
echo -e "${YELLOW} Table image already exists, skipping.${NC}"
continue
elif [ "$MODE" == "backglass" ] && [ -f "$BACKGLASS_SCREENSHOT" ] && [ "$FORCE" != true ]; then
echo -e "${YELLOW} Backglass image already exists, skipping.${NC}"
continue
# elif [ "$MODE" == "dmd" ] && [ -f "$DMD_SCREENSHOT" ] && [ "$FORCE" != true ]; then
# echo -e "${YELLOW} DMD image already exists, skipping.${NC}"
# continue
fi
if [ "$FORCE" == true ]; then
echo -e "${RED} --force used, overriding existing files.${NC}"
fi
if [ "$DRY_RUN" == true ]; then
echo -e "${GREEN} Would create screenshots for: $TABLE_NAME${NC}"
continue
fi
mkdir -p "$IMAGES_FOLDER"
# Launch VPinballX_GL
WINE_X11_DRV=x11 "$VPX_EXECUTABLE" -play "$VPX_PATH" > /dev/null 2>&1 &
VPX_PID=$!
sleep "$SCREENSHOT_DELAY"
# Capture table screenshot
if [ "$MODE" != "backglass" ] && [ "$MODE" != "dmd" ] && { [ ! -f "$TABLE_SCREENSHOT" ] || [ "$FORCE" == true ]; }; then
WINDOW_ID_TABLE=$(xdotool search --name "$WINDOW_TITLE_VPX" | head -n 1)
if [ -n "$WINDOW_ID_TABLE" ]; then
if ! import -window "$WINDOW_ID_TABLE" "$TABLE_SCREENSHOT"; then
echo -e "${RED}Error: Failed to capture table screenshot.${NC}"
else
echo -e "${GREEN} Saved table screenshot: $TABLE_SCREENSHOT${NC}"
mogrify -strip "$TABLE_SCREENSHOT"
fi
else
echo -e "${RED}Error: VPinballX_GL window not found.${NC}"
fi
elif [ "$MODE" != "backglass" ]; then
echo -e "${YELLOW} Table screenshot already exists, skipping capture.${NC}"
fi
# Capture backglass screenshot
if [ "$MODE" != "tables" ] && [ "$MODE" != "dmd" ] && { [ ! -f "$BACKGLASS_SCREENSHOT" ] || [ "$FORCE" == true ]; }; then
WINDOW_ID_BACKGLASS=$(xdotool search --name "$WINDOW_TITLE_BACKGLASS" | head -n 1)
if [ -n "$WINDOW_ID_BACKGLASS" ]; then
if ! import -window "$WINDOW_ID_BACKGLASS" "$BACKGLASS_SCREENSHOT"; then
echo -e "${RED}Error: Failed to capture backglass screenshot.${NC}"
else
echo -e "${GREEN} Saved backglass screenshot: $BACKGLASS_SCREENSHOT${NC}"
mogrify -strip "$BACKGLASS_SCREENSHOT"
fi
else
echo -e "${RED}Error: B2SBackglass window not found.${NC}"
fi
elif [ "$MODE" != "tables" ]; then
echo -e "${YELLOW} Backglass screenshot already exists, skipping capture.${NC}"
fi
# # Capture dmd screenshot
# if [ "$MODE" != "table" ] && [ "$MODE" != "backglass" ] && { [ ! -f "$DMD_SCREENSHOT" ] || [ "$FORCE" == true ]; }; then
# # Check if any DMD window exists
# WINDOW_ID_DMD=$(xdotool search --name "$WINDOW_TITLE_DMD1" | head -n 1)
# if [ -z "$WINDOW_ID_DMD" ]; then
# WINDOW_ID_DMD=$(xdotool search --name "$WINDOW_TITLE_DMD2" | head -n 1)
# fi
# # If no DMD window is found, skip the screenshot process and continue the loop
# if [ -z "$WINDOW_ID_DMD" ]; then
# echo -e "${YELLOW} No DMD window found, skipping DMD screenshot.${NC}"
# else
# # Capture screenshot
# if import -window "$WINDOW_ID_DMD" "$DMD_SCREENSHOT"; then
# echo -e "${GREEN} Saved dmd screenshot: $DMD_SCREENSHOT${NC}"
# mogrify -strip "$DMD_SCREENSHOT"
# else
# echo -e "${RED}Error: Failed to capture dmd screenshot.${NC}"
# fi
# fi
# elif [ "$MODE" != "tables" ]; then
# echo -e "${YELLOW} Dmd screenshot already exists, skipping capture.${NC}"
# fi
# Kill VPX process
kill "$VPX_PID" 2>/dev/null
sleep 2
if kill -0 "$VPX_PID" 2>/dev/null; then
kill -9 "$VPX_PID" 2>/dev/null
fi
echo -e "${GREEN}Screenshots saved for $TABLE_NAME${NC}"
done
echo -e "${GREEN}Finished processing all tables.${NC}"