-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_retroarch
More file actions
387 lines (366 loc) · 14.2 KB
/
random_retroarch
File metadata and controls
387 lines (366 loc) · 14.2 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
#set -x
#set -v
# script and utility path wherer all files from git are
SCRIPT_PATH="/mnt/d/Telechargements/emulations/script"
# TODO set by param
. ${SCRIPT_PATH}/param_random_retroarch.txt
# Functions
function randomize(){
MIN=0
MAX=$(($1-1))
#echo "max randomize: $1"
SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
RANDOM=$SEED; # init RANDOM
#random_number_console=$(( ${RANDOM} % MAX + MIN /32767))
#random_number_game=$(( $SEED % MAX + MIN ))
random_number=$(( RANDOM*(MAX)/32768))
}
function select_console(){
### CONSOLE
# list console from directory
CONSOLES=$(ls -dX */| sed "s/\///")
# exclude of some console
for excluded_console in $EXCLUDED_CONSOLES; do
CONSOLES=$(echo "$CONSOLES" | sed "/${excluded_console}/d")
done
# or define a list of console directory
#CONSOLES="megadrive
#nes
#snes
#mastersystem
#gw"
#CONSOLES="supergrafx"
# get the number of console
max_console=$(echo "$CONSOLES" | wc -l)
# echo "max_console: $max_console"
while [[ "${console}" == "" || ! -f "${currentPlaylist}" ]] ; do
#generate random number for console selection
randomize $max_console
console=$(echo "${CONSOLES}" | sed -n "$((random_number + 1))p")
get_gamelist
done
echo -e " Console: ${console}\tRandom number: ${random_number}"
}
function get_gamelist(){
# construct the gamelist
# if a categorie is set get gamelist from the categorie file
# else
# if on linux from the gamelist.xml file
# if on windows from listing of console directory
currentPlaylist="${ROM_PATH}/${console}/gamelist.xml"
# if a categorie is define and the file that define the list of game of this categorie exist
if [[ -v categorie && -f "${ROM_PATH}/${console}/${categorie}_list.txt" ]] ;then
echo " Categorie: ${categorie}"
# search for current console entry in the global _launchedgames.txt file
# and search all that is not in _launchedgames from the game categorie file
# to construct the game list
gamelist=$(grep -i "${console}" "${SCRIPT_PATH}/_launchedgames.txt" |cut -d'_' -f2- | sed 's/ (.*//' | sed '/ $/d'| grep -viFf - ${ROM_PATH}/${console}/${categorie}_list.txt)
else
# if a saga is define
if [[ -v saga ]] ;then
echo " Saga: ${saga}"
# get all game of the current console that contains saga name
gamelist=$(ls "${ROM_PATH}/${console}" | grep -ve "\.dat" -ve "\.srm" -ve "\.txt" -ve "\.xml" -ve "\.html" -ve "\.nfo" -ve "/" | grep -iE "${saga}"|sort -n)
# else get list of games
else
if [ $(cat /proc/version | grep -c microsoft) == 0 ] ; then
# LINUX json playlist
echo -e " ENV: Linux"
gamelist=$(grep '"path' "$currentPlaylist" | cut -d ':' -f2 | sed 's/",$//' | sed 's/^ "//' )
else
echo -e " ENV: Windows"
# windows new format not compressed
# gamelist retroarch
#old #gamelist=$(grep '"path' "$RETROARCH_PLAYLIST/$currentPlaylist" | cut -d '"' -f4 | sed 's/",$//' | sed 's/^ "//')
#gamelist=$(grep "<path>" $currentPlaylist | sed "s/.*<path>\.\/\(.*\)<\/path>/\1/")
gamelist=$(ls -p "${ROM_PATH}/${console}" | grep -ve "\.dat" -ve "\.srm" -ve "\.txt" -ve "\.xml" -ve "\.html" -ve "\.nfo" -ve "/") #list game in directory
fi
fi
fi
full_maxgame=$(echo "$gamelist"| sed '/^$/d' | wc -l)
#echo "$gamelist"
}
function search_rom(){
tmpname=""
# main prefered language
main_country="Fr F"
# alternative language with precedence order
other_country="E Eu Europe UE U USA JUE JE J Japan Unl PD W K AVE"
for country in ${main_country}; do # check Fr
if [[ "${tmpname}" == "" ]] ; then # good dump [!]
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) \[!\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # without information
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country})\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside []
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) \[.*\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside () and []
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) (.*) \[.*\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside ()
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) (.*)\.")
fi
if [[ "${tmpname}" != "" ]] ; then # if found set it to the rom var
manage_multi_entry_and_cue
break
fi
done
# if search_rom is called with a translation parameter filter search for specified translation
if [[ $1 != "" ]];then
for country in ${other_country}; do # check all other with trad [T+...] given in parameter
if [[ "${tmpname}" == "" ]] ; then # trad [T+...]
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) \[${1}.*\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # anything trad Fr [T+...]
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) (.*) \[${1}.*\]\.")
fi
if [[ "${tmpname}" != "" ]] ; then # if found set it to the rom var
manage_multi_entry_and_cue
break
fi
done
else
for country in ${other_country}; do # check all other without trad
if [[ "${tmpname}" == "" ]] ; then # good dump [!]
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) \[!\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # without information
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country})\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside []
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) \[.*\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside () and []
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) (.*) \[.*\]\.")
fi
if [[ "${tmpname}" == "" ]] ; then # with anything inside ()
tmpname=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext} (${country}) (.*)\.")
fi
if [[ "${tmpname}" != "" ]] ; then # if found set it to the rom var
manage_multi_entry_and_cue
break
fi
done
fi
}
function manage_multi_entry_and_cue(){
# cut on first or cue
if [[ $(echo ${tmpname} | grep -c "\.cue") == 0 ]]; then
rom=$(echo "${tmpname}" | head -n 1)
else
rom=$(echo "${tmpname}" | grep "\.cue")
fi
if [[ ${console} == 'pcenginecd' || ${console} == 'neogeocd' ]]; then
rom=$(echo "${tmpname}" | sed -e 's/\.bin/\.cue/g' -e 's/\.img/\.cue/g' -e 's/\.sub/\.cue/g' -e 's/\.ccd/\.cue/g' )
fi
}
function select_game(){
#echo " IN Select_game"
### Select a rom
# or from the gamelist.xml file
# or from listing
# or if a categorie is set from the categorie file list
# number of game in the playlist
maxgame=$(echo "$gamelist"| sed '/^$/d' | wc -l)
if [[ ${maxgame} -eq 0 ]]; then
echo "No game matching for ${console}"
console=""
is_a_listed_game=0
return
fi
# randomly choose a game from the playlist
randomize ${maxgame}
rom=$(echo "${gamelist}" | sed -n "$((random_number + 1))p")
echo -e " ROM: ${rom}\tRandom number: ${random_number}"
#remove selected rom from the gamelist
rom_escaped=$(echo "${rom}" | sed 's/[][]/\\&/g')
gamelist=$(echo "$gamelist" | sed "/${rom_escaped}/d")
#echo "Game list: $gamelist"
# list all roms that contains rom name
rom=$(echo "${rom}" | sed 's/\&/\&/g' )
# TO FORCE ROM
#rom="SD Gundam - Operation U.C. (Japan)"
rom_w_ext=$(echo "${rom}" |sed -e 's/ (.*)//g' -e 's/ \[.*\]//g' -e 's/\....$//g') # remove extention in rom name depend if name come from xml or file list
#echo "rom: ${rom}"
#echo "all rom matching name ${rom_w_ext}: "
#echo " $(ls "${ROM_PATH}/${console}/" | grep -ie "^${rom_w_ext}")"
# translation string inside rom name to be search (set to empty aka "" for no translation search)
trad="T+Fre"
search_rom ${trad}
# if the rom var is empty find any trad EN matching rom
if [[ "${tmpname}" == "" ]]; then
trad="T+Eng"
search_rom ${trad}
fi
if [[ "${tmpname}" == "" ]]; then
search_rom
fi
#
if [[ "${favorites}" == "true" ]]; then
console=$(echo "${rom}" | sed -e 's/.*\\\\\(.*\)\\\\.*/\1/')
console=${console}
fi
# TODO: improve categorie search
if [[ -v categorie ]]; then
# if the rom var is empty find any matching rom
if [[ "${tmpname}" == "" ]] ; then
# TODO: manage r-type 3 / ganada && multiple rom matching
rom=$(ls "${ROM_PATH}/${console}/" | grep -ie "${rom_w_ext}.*\.")
fi
fi
tmpname=${rom}
manage_multi_entry_and_cue
#echo "$gamelist"
## if file for launchedgame doesn't exist create it
if [[ ! -f "${SCRIPT_PATH}/${categorie}_launchedgames.txt" ]]; then
touch ${SCRIPT_PATH}/${categorie}_launchedgames.txt
fi
# if the game is not found in the launchedgames list file add it
if [[ $(grep -c -F "${console}_${rom}" "${SCRIPT_PATH}/${categorie}_launchedgames.txt") == 0 ]] ; then
echo "${console}_${rom}" >> "${SCRIPT_PATH}/${categorie}_launchedgames.txt"
is_a_listed_game=0 # var for the until loop
else
#else rset is listed game and reset console select
is_a_listed_game=1
console=""
fi
# clear the laucnhed games file if the number of game for this console is reached
if [[ $(grep -c -F "${console}" "${SCRIPT_PATH}/${categorie}_launchedgames.txt") -ge ${full_maxgame} ]] ; then
sed "/${console}.*/d" -i "${SCRIPT_PATH}/${categorie}_launchedgames.txt"
fi
#echo " OUT Select_game"
}
function get_core(){
# get the core for the game
# on linux from the gamelist.xml
# on windows from launchbox file
if [ $(cat /proc/version | grep -c microsoft) == 0 ] ; then
# get it from linux playlist
if [[ "${favorites}" == "true" ]]; then
core=$(echo "${cores}" | sed -n "$((random_number + 1))p" | sed -e 's/.*\\//' -e 's/\..*//')
core=$(grep "$core" $EMULATOR_DEF_PATH | sed 's/.*>\(.*\)<.*/\1/' | head -n 1)
else
core=$(grep '"default_core_path' "$RETROARCH_PLAYLIST/$currentPlaylist" | cut -d ':' -f2 | sed 's/",$//' | sed 's/^ "//')
fi
else
# get core from gamelist game core or gamelist default core or launchbox list
# core="-L \""$(grep -A2 -F "${rom}" "$RETROARCH_PLAYLIST/$currentPlaylist" | grep "core" | cut -d'"' -f4)"\""
# if [[ "${core}" == "-L \"\"" || "${core}" == "-L \"DETECT\"" ]] ;then
# # old
# #core=$(grep '"default_core_path' "$RETROARCH_PLAYLIST/$currentPlaylist" | cut -d '=' -f3 |sed 's/^ //')
# #
# core="-L \""$(grep '"default_core_path' "$RETROARCH_PLAYLIST/$currentPlaylist" | cut -d '"' -f4 |sed 's/^ //')"\""
# if [ "${core}" == "-L \"\"" ] ;then
if [[ "${favorites}" == "true" ]]; then
core=$(echo "${cores}"| sed -n "$((random_number + 1))p" | sed -e 's/.*\\//' -e 's/\..*//')
core=$(grep "$core" $EMULATOR_DEF_PATH | sed 's/.*>\(.*\)<.*/\1/' | head -n 1)
else
core=$(grep -A1 ">${console}<" "${EMULATOR_DEF_PATH}" | grep Command | sed 's/.*>\(.*\)<.*/\1/')
fi
#echo ${core}
# fi
# fi
fi
}
function generate_obs_title(){
# generate html for obs from template file
sed -e "s/CONSOLE/${console}/g" -e "s/CURRENTGAME/${current_game}/g" "${SCRIPT_PATH}/retro.html.template" > "${SCRIPT_PATH}/retro.html"
sed -e "s/CONSOLE/${console}/g" -e "s/CURRENTGAME/${current_game}/g" "${SCRIPT_PATH}/retro_video.html.template" > "${SCRIPT_PATH}/retro_video.html"
}
function set_stream_title(){
# generate script content to set the good title
title=$( echo "${current_game}" | sed "s/'/'\"'\"'/g" | sed 's/(PD)//')
console_name=$(grep "<System>" "${ROM_PATH}/${console}/gamelist.xml" | sed -e 's/.*<System>//' -e 's/<\/.*//')
if [[ -v categorie ]] ; then
sed -e "s/TITLE/${title} un ${categorie} sur ${console_name}/g" "${SCRIPT_PATH}/set_game_title.sh" > "${SCRIPT_PATH}/set_current_game_title.sh"
else
sed -e "s/TITLE/${title} sur ${console_name}/g" "${SCRIPT_PATH}/set_game_title.sh" > "${SCRIPT_PATH}/set_current_game_title.sh"
fi
# call the api script to change twitch title
ret=$(bash "${SCRIPT_PATH}/set_current_game_title.sh")
if [[ $(echo "${ret}" | grep -c "error") != 0 ]];then
echo -n " Cannot set stream title: "
echo " ${ret}"
fi
}
function start_game(){
# start the game or the emulator
# on windows generated html for obs
# and execute twitch api command to change stream title
if [ $(cat /proc/version | grep -c microsoft) == 0 ] ; then
retroarch ${core} "${ROM_PATH}/${console}/${rom}"
else
# rom="SD Ultra Battle - Seven & Ultraman + ST BIOS (ST) [h1][o1].smc"
# console="snes"
echo " Core: ${core}"
if [ "${core}" != "" ] ; then
current_game=$(echo ${rom%.*} |sed -e "s/&/\\\&/g")
echo " Game: ${current_game}"
if [[ -v obs ]]; then
generate_obs_title
fi
if [[ -v stream ]]; then
set_stream_title
fi
# force specific standalone emulator to be use for some console
case ${console} in
"atarist")
echo '"'D:\\Jeux steam\\retro\\hatari-2.6.0_windows64\\hatari.exe'"' '"'${ROM_PATH_WIN}/${console}/${rom}'"'
exit
;;
"3ds")
echo '"'D:\\Jeux steam\\retro\\citra\\citra-qt.exe'"' '"'${ROM_PATH_WIN}/${console}/${rom}'"'
exit
;;
"pc98")
if [[ "${rom}" == "Policenauts.cmd" ]]; then
echo '"'${ROM_PATH_WIN}/${console}/${rom%.cmd}/np21nt.exe'"'
exit
fi
;;
"ps2")
echo '"'D:\\Jeux steam\\retro\\PCSX2 1.4.0\\pcsx2.exe'"' --fullscreen --fullboot '"'${ROM_PATH_WIN}/${console}/${rom}'"'
exit
;;
"Playstation 2")
echo '"'D:\\Jeux steam\\retro\\PCSX2 1.4.0\\pcsx2.exe'"' --fullscreen --fullboot '"'${ROM_PATH_WIN}/${console}/${rom}'"'
exit
;;
# "psx")
# echo '"'D:\\Jeux steam\\retro\\epsxe\\epsxe.exe'"' -nogui -loadbin '"'${rom}'"'
# exit
# ;;
esac
# echo the return for windows batch execute command
echo "${RETROARCH_PATH_WIN}\retroarch.exe -f " ${core} '"'${ROM_PATH_WIN}\\${console}\\${rom}'"'
else # echo no core found for the console
echo "set /p end=No core for ${console}"
fi
fi
}
### Main
is_a_listed_game=1
console=""
cd "${ROM_PATH}"
until [[ ${is_a_listed_game} == 0 ]]; do
if [[ "${favorites}" == "true" ]]; then
cores=$(grep -e \"core_path "${RETROARCH_PATH}/content_favorites.lpl")
gamelist=$(grep -e \"path "${RETROARCH_PATH}/content_favorites.lpl" | sed -e 's/.* "//' -e 's/",//')
select_game
rom=$(echo "${rom}" | sed -e 's/.*\\//' -e 's/",//')
else
while [[ "${console}" == "" ]]; do
select_console
if [[ -f "${currentPlaylist}" ]] ;then # if there is gamelist in the directory it's that there is some game so...
select_game
#else
# console=""
fi
done
fi
done
get_core
start_game