forked from jsbursik/LPLmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lplmaker.bash
executable file
·148 lines (133 loc) · 4.04 KB
/
lplmaker.bash
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
#!/bin/bash
REVISION="2019.09.04"
# Script originally written by u/ShiftyAxel on reddit for windows, rewritten in bash for linux.
# Create a configuration file to adapt to your system and needs, an example configuration is provided.
# Your retroarch directory (contains paylists folder, cores folder, etc.), typically in ~/.config/retroarch
RetroArchDir="$HOME/.config/retroarch"
# Your base ROMs directory
RomsDir="$HOME/Roms"
# Where cores are located, if you use core updater it will be something like ~/.config/retroarch/cores/
CoresDir="/usr/lib/libretro"
HAS_CONFIG=0
# Load configuration file(s)
if [[ -f ~/.config/lplmaker.conf ]]; then
source ~/.config/lplmaker.conf
HAS_CONFIG=1
fi
if [[ -f ./lplmaker.conf ]]; then
source ./lplmaker.conf
HAS_CONFIG=1
fi
if [[ $HAS_CONFIG -ne 1 ]]; then
# Defaults. See lplmaker.conf for more details.
x=0
RomDirs[$x]="MAME"
CoreLibs[$x]="DETECT" # or e.g. "mame_libretro.so"
CoreNames[$x]="DETECT" # or e.g. "Arcade (MAME)"
PlaylistNames[$x]="Arcade"
SupportedExtensions[$x]="zip"
ScanZips[$x]=0
QueryMame[$x]=1
fi
# TODO: Allow recursive scans
x=0
clear
echo '!!'
echo '!! **** WARNING **** '
echo '!!'
echo '!! The following playlists will be overwritten if they exist.'
echo '!! Back them up now if you'\''d like to do so or they'\''ll be gone.'
echo '!!'
for PlayList in "${PlaylistNames[@]}"; do
echo "!! - $PlayList"
done
echo '!!'
echo
read -p "Press [Enter] key to continue (or abort with CTRL+C)..."
# Prints a playlist entry.
# The format is:
# Path
# Label
# Core library path (or DETECT)
# Core name (or DETECT)
# CRC
# Database
print_rom_entry() {
local SystemIndex="$1"
local RomPath="$2"
local RomTitle="$3"
if [[ ${QueryMame[$SystemIndex]} -eq 1 ]]; then
# Use MAME to get the Rom title
local rt=$(mame -ll "$RomTitle" \
| awk '{$1="";print}' \
| sed -e 1d -e 's/^[[:space:]]*"//' -e 's/"$//' \
)
if [[ -n $rt ]]; then
RomTitle="$rt"
fi
fi
echo $RomPath
echo $RomTitle
if [[ "${CoreLibs[$SystemIndex]}" != 'DETECT' ]]; then
echo $CoresDir/${CoreLibs[$SystemIndex]}
else
echo DETECT
fi
echo ${CoreNames[$SystemIndex]}
echo 0\|crc
echo "${PlaylistNames[$SystemIndex]}".lpl
}
get_rom_count() {
local globs="$1"
local scanzips="$2"
local uncompressed=$(ls $globs 2>/dev/null | wc -l)
local compressed=0
if [[ "$scanzips" -eq 1 ]]; then
compressed=$(ls *.zip 2>/dev/null | wc -l)
fi
echo $(( $uncompressed + $compressed ))
}
# progress(current index, total, current file name)
progress() {
local index=$1
local total=$2
local current="$3"
# Trim to at most 64 characters
printf "[%d/%d] - %-64s\r" $index $total "${current:0:63}" >&2
}
while [ -n "${RomDirs[$x]}" ]; do
pushd "$RomsDir/${RomDirs[$x]}" > /dev/null
echo "======================================================================"
echo "${PlaylistNames[$x]}"
echo "======================================================================"
echo "Writing playlist now..."
SupportedExtensions[$x]=$(echo ${SupportedExtensions[$x]} | sed -r 's/^| / \*\./g')
PlayList="$RetroArchDir/playlists/${PlaylistNames[$x]}".lpl
:> "$PlayList"
FileCount=$(get_rom_count "${SupportedExtensions[$x]}" "${ScanZips[$x]}")
declare -i FileNum=1
for f in ${SupportedExtensions[$x]} ; do
[ -f "$f" ] || continue
progress $FileNum $FileCount "$f"
print_rom_entry $x "$RomsDir/${RomDirs[$x]}/$f" "${f%%.*}"
FileNum+=1
done >> "$PlayList"
# Scan zips for supported ROMs?
if [[ "${ScanZips[$x]}" -eq 1 ]]; then
for zip in *.zip ; do
[[ -f "$zip" ]] || continue
# Test file contents against known extensions
progress $FileNum $FileCount "$zip"
while read CompressedFile ; do
print_rom_entry "$x" "$RomsDir/${RomDirs[$x]}/$zip#$CompressedFile" "${CompressedFile%%.*}"
done < <(zipinfo -1 "$zip" | grep -E "$SupportedExtensionsRE")
FileNum+=1
done
fi >> "$PlayList"
printf '\r%80s' '' >&2 # Clear the progress output
echo
echo "$PlayList"
echo
popd > /dev/null
x+=1
done