forked from Klubas/moonlight-retropie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moonlight.sh
executable file
·482 lines (409 loc) · 13.3 KB
/
moonlight.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/bin/bash
set -e # exit on error
# validate if root
if [ "$user" == "root" ]; then
echo -e "This script isn't supposed to be run as root\nExiting..."
exit 0
fi
#get current dir from this answer
#https://stackoverflow.com/a/630645
prg=$0
if [ ! -e "$prg" ]; then
case $prg in
(*/*) exit 1;;
(*) prg=$(wich -v -- "$prg") || exit;;
esac
fi
dir=$( cd -P -- "$(dirname -- "$prg")" && pwd -P ) || exit
prg=$dir/$(basename -- "$prg") || exit
wd="$(dirname `printf '%s\n' $prg`)"
#echo $wd
user="`whoami`"
home_dir="$HOME"
arg="$1"
arg1="$2"
# add sources for moonlight
function add_sources {
# $1 = jessie or stretch
if grep -q "deb http://archive.itimmer.nl/raspbian/moonlight stretch main" /etc/apt/sources.list; then
echo -e "NOTE: Moonlight Source Exists - Skipping"
else
echo -e "Adding Moonlight to Sources List"
echo "deb http://archive.itimmer.nl/raspbian/moonlight stretch main" >> /etc/apt/sources.list
fi
}
# remove old moonlight and build from master
function remove_moonlight {
echo -e "Removing old moonlights"
sudo apt-get --purge remove moonlight-embedded
if
[ -d "moonlight-embedded" ]; then rm -Rf moonlight-embedded;
fi
}
# update system and pakeges
function update_pakages {
echo -e "Cleaning, and removing old unused packages"
apt-get clean
apt-get autoremove
sudo apt-get -f install
dpkg --configure -a
echo -e "updating system, and pakages"
sudo apt-get update
echo -e "Checking Moonlight pakages"
# deb http://archive.itimmer.nl/raspbian/moonlight stretch main
# deb http://archive.itimmer.nl/raspbian/moonlight jessie main
# deb http://archive.itimmer.nl/raspbian/moonlight wheezy main
# libudev0 or libudev1
# rbp-userland-osmc or rbp-userland-dev-osmc
INSTALL_PKGS=" libopus0 libexpat1 libasound2 libudev1 libavahi-client3 libcurl3 libevdev2 libenet7 libraspberrypi0 libssl-dev libopus-dev libasound2-dev libudev-dev libavahi-client-dev libcurl4-openssl-dev libevdev-dev libexpat1-dev libpulse-dev uuid-dev libenet-dev cmake gcc g++ libraspberrypi-dev fakeroot debhelper "
for i in $INSTALL_PKGS; do
sudo apt-get install -y $i
done
}
# build moonlight from master
function build_moonlight_from_master {
echo -e "cloning Moonlight repository"
git clone https://github.com/irtimmer/moonlight-embedded.git
cd moonlight-embedded
echo -e "Getting submodules"
git submodule update --init --recursive
mkdir build
cd build/
echo -e "Building Moonlight"
cmake ../
make
echo -e "Installing Moonlight"
sudo make install
echo -e "Creating the necessary links and cache"
sudo ldconfig
}
# fetch and install gpg keys
function install_gpg_keys {
# $1 can be -f to force overwriting
if [ -f ./itimmer.gpg ]; then
echo -n "NOTE: GPG Key Exists - "
if [ "$1" == '-f' ]; then
echo -e "Overwriting"
rm ./itimmer.gpg
else
echo -e "Skipping"
return 0
fi
fi
wget http://archive.itimmer.nl/itimmer.gpg
echo "sudo apt-key add itimmer.gpg"
sudo apt-key add itimmer.gpg
rm ./itimmer.gpg
}
# pair moonlight with steam pc
function pair_moonlight {
echo -e "Once you have input your STEAM PC's IP Address below, you will be given a PIN"
echo -e "Input this on the STEAM PC to pair with Moonlight. \n"
read -p "Input STEAM PC's IP Address here :`echo $'\n> '`" ip
sudo -u pi moonlight pair $ip
}
#create steam menu
function create_menu {
if [ -f /opt/retropie/configs/all/emulationstation/es_systems.cfg ]
then
echo -e "Removing Duplicate Systems File"
rm /opt/retropie/configs/all/emulationstation/es_systems.cfg
fi
echo -e "Copying Systems Config File"
cp /etc/emulationstation/es_systems.cfg /opt/retropie/configs/all/emulationstation/es_systems.cfg
if grep -q "<platform>steam</platform>" /opt/retropie/configs/all/emulationstation/es_systems.cfg; then
echo -e "NOTE: Steam Entry Exists - Skipping"
else
echo -e "Adding Steam to Systems"
sed -i -e 's|</systemList>| <system>\n <name>steam</name>\n <fullname>Steam</fullname>\n <path>~/RetroPie/roms/moonlight</path>\n <extension>.sh .SH</extension>\n <command>bash %ROM%</command>\n <platform>steam</platform>\n <theme>steam</theme>\n </system>\n</systemList>|g' /opt/retropie/configs/all/emulationstation/es_systems.cfg
fi
}
#add steam launch scripts
function create_launch_scripts {
echo -e "Create Script Folder"
mkdir -p /home/pi/RetroPie/roms/moonlight
if [ "$1" == '-f' ]; then
echo -e "NOTE: Removing old scripts"
remove_launch_scripts
fi
cd /home/pi/RetroPie/roms/moonlight
echo -e "Create Scripts"
if [ -f ./720p30fps.sh ]; then
echo -e "NOTE: 720p30fps Exists - Skipping"
else
echo "#!/bin/bash" > 720p30fps.sh
echo "moonlight stream -720 -fps 30 "$ip"" >> 720p30fps.sh
fi
if [ -f ./720p60fps.sh ]; then
echo -e "NOTE: 720p60fps Exists - Skipping"
else
echo "#!/bin/bash" > 720p60fps.sh
echo "moonlight stream -720 -fps 60 "$ip"" >> 720p60fps.sh
fi
if [ -f ./1080p30fps.sh ]; then
echo -e "NOTE: 1080p30fps Exists - Skipping"
else
echo "#!/bin/bash" > 1080p30fps.sh
echo "moonlight stream -1080 -fps 30 "$ip"" >> 1080p30fps.sh
fi
if [ -f ./1080p60fps.sh ]; then
echo -e "NOTE: 1080p60fps Exists - Skipping"
else
echo "#!/bin/bash" > 1080p60fps.sh
echo "moonlight stream -1080 -fps 60 "$ip"" >> 1080p60fps.sh
fi
echo -e "Make Scripts Executable"
sudo chmod +x 720p30fps.sh
sudo chmod +x 720p60fps.sh
sudo chmod +x 1080p30fps.sh
sudo chmod +x 1080p60fps.sh
cd "$wd"
}
#remove steam laucnh scripts
function remove_launch_scripts {
cd /home/pi/RetroPie/roms/moonlight/
[ "$(ls -A .)" ] && rm * || echo -n ""
}
#define files permissions -- Not sure if this is really needed but I'll leave it here
function set_permissions {
echo -e "Changing File Permissions"
sudo chown -R pi:pi /home/pi/RetroPie/roms/moonlight/
sudo chown pi:pi /opt/retropie/configs/all/emulationstation/es_systems.cfg
}
#change controller mapping -- currrently doesn't work
function map_controller {
echo -e "\nWIP\n"
return 0
#possible solution
#https://retropie.org.uk/forum/topic/11225/moonlight-no-mapping-available-for-dev-input-event2-030000005e040000a102000007010000
if [ "$(ls -A /home/pi/RetroPie/roms/moonlight/)" ]; then
sudo cp /usr/local/share/moonlight/gamecontrollerdb.txt /usr/local/share/moonlight/gamecontrollerdb.bak
sudo cp /home/pi/moonlight-embedded/gamecontrollerdb.txt /usr/local/share/moonlight/gamecontrollerdb.txt
read -n 1 -s -p "Make sure your controller is plugged in and press anykey to continue"
ls -l /dev/input
echo -e "Type the device name (it's probably one of the eventX): "
read -p "> " controller
sudo moonlight map -input /dev/input/"$controller"
# 2>&1 | tee /home/pi/RetroPie/roms/moonlight/NewControllerMap.txt
#Get controller id, and mapping. Check if the controller is in gamecontrollerdb.txt and if so ask to override else ask to add new.
fi
cd "$wd"
else
echo -e "You need to generate your launch scripts first."
return 0
fi
}
#change default audio output
#could be updated to set subdevices, but i'm not sure how that works
function set_audio_output {
if [ "$(ls -A /home/pi/RetroPie/roms/moonlight/)" ]; then
if [ "$arg1" ]; then
device="$arg1"
subdevice="0"
else
echo -e "Choose your preferred audio output:"
echo -e "Tip: Use aplay -l to see installed devices"
echo -e "0 - Audio Jack"
echo -e "1 - HDMI"
echo -n "> "
read device
subdevice="0"
fi
audio_out="hw:$device,$subdevice"
cd /home/pi/RetroPie/roms/moonlight/
if [ -f ./720p30fps.sh ]; then
if [ -z "`sed -n '/-audio/p' ./720p30fps.sh`" ]; then
sed -i "s/^moonlight.*/& -audio hw:0,0/" 720p30fps.sh
fi
sed -i "s/hw:[[:digit:]],[[:digit:]]/$audio_out/" 720p30fps.sh
fi
if [ -f ./720p60fps.sh ]; then
if [ -z "`sed -n '/-audio/p' ./720p60fps.sh`" ]; then
sed -i "s/^moonlight.*/& -audio hw:0,0/" 720p60fps.sh
fi
sed -i "s/hw:[[:digit:]],[[:digit:]]/$audio_out/" 720p60fps.sh
fi
if [ -f ./1080p30fps.sh ]; then
if [ -z "`sed -n '/-audio/p' ./1080p30fps.sh`" ]; then
sed -i "s/^moonlight.*/& -audio hw:0,0/" 1080p30fps.sh
fi
sed -i "s/hw:[[:digit:]],[[:digit:]]/$audio_out/" 1080p30fps.sh
fi
if [ -f ./1080p60fps.sh ]; then
if [ -z "`sed -n '/-audio/p' ./1080p60fps.sh`" ]; then
sed -i "s/^moonlight.*/& -audio hw:0,0/" 1080p60fps.sh
fi
sed -i "s/hw:[[:digit:]],[[:digit:]]/$audio_out/" 1080p60fps.sh
fi
cd "$wd"
else
echo -e "You need to generate your launch scripts first."
return 1
fi
if [ "$arg1" ]; then
return 1
fi
}
#create menu entries for sound options
function sound_menu {
config_menu
echo "$wd/moonlight.sh 7 0" > /home/pi/RetroPie/roms/moonlight/audio_jack.sh
echo "$wd/moonlight.sh 7 1" > /home/pi/RetroPie/roms/moonlight/hdmi.sh
echo -e "Make executable"
sudo chmod +x /home/pi/RetroPie/roms/moonlight/audio_jack.sh
sudo chmod +x /home/pi/RetroPie/roms/moonlight/hdmi.sh
}
#update this script
function update_script {
cd "$wd"
if [ -f ./moonlight.sh ]
then
echo -e "Removing Script"
rm ./moonlight.sh
fi
#wget https://techwiztime.com/moonlight.sh --no-check
wget https://raw.githubusercontent.com/jacobmix/moonlight-retropie/master/moonlight.sh --no-check
if [ ! -x ./moonlight ]; then
echo "Making it executable"
sudo chmod +x "$wd"/moonlight.sh
fi
}
#restart this script
function restart_script {
if [ -z $arg ]; then
"$wd"/moonlight.sh
fi
}
#add this script to emulation EmulationStation steam menu
function config_menu {
if [ -f /home/pi/RetroPie/roms/moonlight/moonlight.sh ]; then
echo -e "Do you wish to remove the configuration menu? (Y)es / (N)o / (0)verwite"
echo -n "> "
read option
case "$option" in
y|Y) rm /home/pi/RetroPie/roms/moonlight/moonlight.sh; return 0 ;;
n|N) return 0 ;;
o|O) rm /home/pi/RetroPie/roms/moonlight/moonlight.sh;;
*) echo -e "Invalid."; return 0 ;;
esac
fi
ln $wd/moonlight.sh /home/pi/RetroPie/roms/moonlight/moonlight.sh
}
#you can call the script passing one of the menu options as the first arg
if [ $# -eq 0 ]; then
echo -e "\n****************************************************************"
echo -e "Welcome to the Moonlight Installer Script for RetroPie v18.00.01"
echo -e "****************************************************************\n"
echo -e "Select an option:"
echo -e " * 1: Build Moonlight from master, Pair, Install Scripts, Install Menus"
echo -e " * 2: Install Launch Scripts"
echo -e " * 3: Remove Launch Scripts"
echo -e " * 4: Re Pair Moonlight with PC"
echo -e " * 5: Refresh SYSTEMS Config File"
echo -e " * 6: Update This Script"
echo -e " * 7: Audio Output options"
echo -e " * 8: Controller Mapping"
echo -e " * 9: Put this Script on the EmulationStation menu"
echo -e " * 0: Exit"
echo -n " > "
read NUM
else
NUM=$arg
fi
case "$NUM" in
1)
echo -e "\nUpdate System and install moonlight"
echo -e "**************************\n"
add_sources
install_gpg_keys
remove_moonlight
update_pakages
build_moonlight_from_master
echo -e "\nPair Moonlight with PC"
echo -e "**********************************\n"
pair_moonlight
echo -e "\nCreate STEAM Menu for RetroPie"
echo -e "*****************************************\n"
create_menu
echo -e "\nCreate 1080p+720p Launch Scripts for RetroPie"
echo -e "**********************************************************\n"
create_launch_scripts
echo -e "\nMaking Everything PI Again :)"
echo -e "******************************************\n"
set_permissions
echo -e "Everything should now be installed and setup correctly."
echo -e "To be safe, it's recommended that you perform a reboot now."
echo -e "\nIf you don't want to reboot now, press N\n"
read -p "Reboot Now (y/n)? " choice
case "$choice" in
y|Y ) shutdown -r now;;
n|N ) restart_script;;
* ) echo "invalid";;
esac
;;
2)
echo -e "\nCreate 1080p + 720p Launch Scripts for RetroPie"
echo -e "***********************************************\n"
create_launch_scripts
echo -e "\n**** 1080p + 720p Launch Scripts Creation Complete!!!! ****"
restart_script
;;
3)
echo -e "\nRemove All Steam Launch Scripts"
echo -e "***********************************\n"
remove_launch_scripts
echo -e "\n**** Launch Script Removal Complete!!! ****"
restart_script
;;
4)
echo -e "\nRe-Pair Moonlight with another PC"
echo -e "*********************************\n"
pair_moonlight
echo -e "\n**** Re-Pair Process Complete!!!! ****"
restart_script
;;
5)
echo -e "\nRefresh RetroPie Systems File"
echo -e "*****************************\n"
create_menu
echo -e "\n**** Refreshing Retropie Systems File Complete!!!! ****"
restart_script
;;
6)
echo -e "\nUpdate This Script"
echo -e "*****************************\n"
update_script
restart_script
;;
7)
echo -e "\nChange default audio output"
echo -e "*****************************\n"
set_audio_output "$arg2"
echo -e "\nCreate shortcuts in EmulationStation Steam menu? (y/n)"
read -p "> " opt
case $opt in
y|Y)
sound_menu
echo -e "Menu entries created" ;;
*)
echo -e "No";;
esac
restart_script
;;
8)
echo -e "\nMapping the controller"
echo -e "**********************************\n"
map_controller
restart_script
;;
9)
echo -e "\nCreate menu entry"
echo -e "**********************************\n"
echo -e "NOTE: You won't be able to interact with this script using a controller!"
config_menu
restart_script
;;
0) exit 1;;
*) echo "INVALID NUMBER!" ;;
esac
exit 0