-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreeper.sh
199 lines (167 loc) · 6.96 KB
/
screeper.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
#!/bin/bash
# This script is intended to be the RetroPie's runcommand-onend.sh
# please, rename it to /opt/retropie/configs/all/runcommand-onend.sh
#
# What does it do?
##################
# If the user takes screenshots during a game, the most recent screenshot
# will be the emulationstation image for this game.
#
# Conditions to make it work:
#############################
# You must have these settings in your retroarch.cfg (system specific or
# global; note: system specific takes precedence):
#
# auto_screenshot_filename = "false"
# screenshot_directory = "/path/to/screenshots/folder"
#
# The screenshot directory must exist, otherwise the RetroArch won't be able
# to save the screenshots.
echo "--- start of $(basename $0) ---" >&2
# variables ##################################################################
readonly system="$1"
readonly full_path_rom="$3"
readonly retroarch_cfg="/opt/retropie/configs/all/retroarch.cfg"
readonly system_ra_cfg="/opt/retropie/configs/$system/retroarch.cfg"
readonly gamelist="$HOME/RetroPie/roms/$system/gamelist.xml"
readonly gamelist_user="$HOME/.emulationstation/gamelists/$system/gamelist.xml"
readonly gamelist_global="/etc/emulationstation/gamelists/$system/gamelist.xml"
rom_file="${full_path_rom##*/}"
rom="${rom_file%.*}"
image="$rom.png"
source "/opt/retropie/lib/inifuncs.sh"
# functions ##################################################################
function get_configs() {
iniConfig ' = ' '"'
# only go on if the auto_screenshot_filename is false
iniGet "auto_screenshot_filename" "$system_ra_cfg"
if ! [[ "$ini_value" =~ ^(false|0)$ ]]; then
# if true, the user explicitly wanna turn it off for this specific system
if [[ "$ini_value" =~ ^(true|1)$ ]]; then
echo "Auto scraper is off for $system. Exiting..." >&2
exit 0
fi
# if it's neither true nor false (absent), try the global config
iniGet "auto_screenshot_filename" "$retroarch_cfg"
if ! [[ "$ini_value" =~ ^(false|0)$ ]]; then
echo "Auto scraper is off. Exiting..." >&2
exit 0
fi
fi
# getting the screenshots directory
# try system specific, if not found try the global one
iniGet "screenshot_directory" "$system_ra_cfg"
screenshot_dir="$ini_value"
if [[ -z "$screenshot_dir" ]]; then
iniGet "screenshot_directory" "$retroarch_cfg"
screenshot_dir="$ini_value"
if [[ -z "$screenshot_dir" ]]; then
echo "You must set a path for 'screenshot_directory' in \"retroarch.cfg\"." >&2
echo "Aborting..." >&2
exit 1
fi
fi
# dealing with the tilde '~'
screenshot_dir="${screenshot_dir/#~/$HOME}"
# if there is no "customized gamelist.xml", copy the user specific,
# if it fails, copy the global one, if it fails, create one from scratch
if ! [[ -f "$gamelist" ]]; then
echo "Copying \"$gamelist_user\" to \"$gamelist\"." >&2
if ! cp "$gamelist_user" "$gamelist" 2>/dev/null; then
echo "Failed to copy \"$gamelist_user\"." >&2
echo "Copying \"$gamelist_global\" to \"$gamelist\"." >&2
if ! cp "$gamelist_global" "$gamelist" 2>/dev/null; then
echo "Failed to copy \"$gamelist_global\"." >&2
echo "Creating a new \"$gamelist\" from scratch." >&2
# create a gamelist.xml from scratch
echo '
<?xml version="1.0"?>
<gameList>
</gameList>' > "$gamelist"
fi
fi
fi
}
# sometimes we need to convert strings to a XML-friendly entries
function echo_xml_safe() {
output="$(sed 's#\&#\&#g' <<< "$@")"
output="$(
sed "
s#\"#\"#g
s#'#\'#g
s#<#\<#g
s#>#\>#g" <<< "$output"
)"
echo "$output"
}
# sometimes we need to escape characters to make them to NOT be considered a REGEX
function echo_regex_safe() {
echo "$(sed 's#[][&\^]#\\&#g' <<< "$@")"
}
# starting point #############################################################
get_configs
# if there is no screenshot named "ROM Name.png", we have nothing to do here
if ! [[ -f "$screenshot_dir/$image" ]]; then
echo "There is no screenshot for \"$rom\" in the \"$screenshot_dir\" folder." >&2
echo "\"$screenshot_dir/$image\" not found!" >&2
echo "Exiting..." >&2
exit 0
fi
mkdir -p "/home/pi/RetroPie/roms/$system/screenshots/"
mv "$screenshot_dir/$image" "/home/pi/RetroPie/roms/$system/screenshots/$image" || exit 0
# now we must the XML-safe versions of some variables
xfull_path_rom="$(echo_xml_safe "$full_path_rom")"
xrom_file="$(echo_xml_safe "$rom_file")"
xrom="$(echo_xml_safe "$rom")"
ximage="$(echo_xml_safe "$image")"
xscreenshot_dir="$(echo_xml_safe "/home/pi/RetroPie/roms/$system/screenshots")"
# regex to detect if the gamelist.xml has an entry for this rom:
rom_regex="<path>.*$(echo_regex_safe "$xrom_file")<\/path>"
# new <image> field
new_img_regex="<image>$xscreenshot_dir/$ximage</image>"
# if this rom present in gamelist.xml
if grep -q "$rom_regex" "$gamelist"; then
new_img_regex="$(echo_regex_safe "$new_img_regex")"
# check if this rom entry has an <image> field
if sed "/$rom_regex/,/<\/game>/!d" "$gamelist" | grep -q "<image>" ; then
# the <image> entry MUST be on a single line and match the pattern:
# anything followed by rom name followed or not by "-image" followed
# by dot followed by 3 chars
old_img_regex="<image>.*$(echo_regex_safe "$xrom")\(-image\)\?\....</image>"
sed -i "s|$old_img_regex|$new_img_regex|" "$gamelist"
# this rom is present in gamelist.xml but has no <image> field
else
sed -i "/$rom_regex/ s|.*|&\n\t\t${new_img_regex}|" "$gamelist"
fi
# this rom is not present in gamelist.xml
else
# some gamelist.xml with no entries has just one line with "<gameList />"
sed -i '/<gameList \/>/ s|.*|<gameList>\n<\/gameList>|' "$gamelist"
# No <name> field when system is arcade, fba, mame-*, neogeo. It'll
# make the ES get the real game name from its own code (MameNameMap.cpp).
if [[ "$system" =~ ^(mame-.*|fba|arcade|neogeo)$ ]]; then
game_name=
else
game_name="<name>$xrom</name>"
fi
# there is no entry for this game yet, let's create it
gamelist_entry="
<game>
<path>$xfull_path_rom</path>
$game_name
<desc></desc>
$new_img_regex
<releasedate></releasedate>
<developer></developer>
<publisher></publisher>
<genre></genre>
</game>"
# escaping it for a safe sed use
gamelist_entry="$(echo_regex_safe "$gamelist_entry")"
# putting backslash at the end of the lines
gamelist_entry="$(sed 's#$#\\#' <<< "$gamelist_entry")"
# in the substitution below the trailing "n&" takes advantage of the
# backslash in the end of gamelist_entry (OK, this is inelegant, but works)
sed -i "/<\/gameList>/ s|.*|${gamelist_entry}n&|" "$gamelist"
fi
echo "--- end of $(basename $0) ---" >&2