From 925ab52284918eee93cca08346a86419b40fd516 Mon Sep 17 00:00:00 2001
From: xargon <xargonwan@gmail.com>
Date: Fri, 22 Jul 2022 19:23:38 +0200
Subject: [PATCH] Added the option to browse for roms folder

---
 emu-configs/ppssppsdl/controls.ini | 26 +++++++++++++++
 emu-configs/ppssppsdl/ppsspp.ini   |  2 +-
 retrodeck.sh                       | 52 ++++++++++++++++++++++++------
 3 files changed, 70 insertions(+), 10 deletions(-)
 create mode 100644 emu-configs/ppssppsdl/controls.ini

diff --git a/emu-configs/ppssppsdl/controls.ini b/emu-configs/ppssppsdl/controls.ini
new file mode 100644
index 00000000..010c77da
--- /dev/null
+++ b/emu-configs/ppssppsdl/controls.ini
@@ -0,0 +1,26 @@
+[ControlMapping]
+Up = 1-19,10-19
+Down = 1-20,10-20
+Left = 1-21,10-21
+Right = 1-22,10-22
+Circle = 1-52,10-190
+Cross = 1-54,10-189
+Square = 1-29,10-191
+Triangle = 1-47,10-188
+Start = 1-62,10-197
+Select = 1-66,10-196
+L = 1-45,10-194
+R = 1-51,10-195
+An.Up = 1-37,10-4003
+An.Down = 1-39,10-4002
+An.Left = 1-38,10-4001
+An.Right = 1-40,10-4000
+Analog limiter = 1-60
+RapidFire = 1-59
+Fast-forward = 10-4010
+SpeedToggle = 1-68
+Pause = 1-40
+Rewind = 10-4008
+Save State = 1-53
+Load State = 1-48
+Screenshot = 1-46
diff --git a/emu-configs/ppssppsdl/ppsspp.ini b/emu-configs/ppssppsdl/ppsspp.ini
index 375b84ff..b4aac34d 100644
--- a/emu-configs/ppssppsdl/ppsspp.ini
+++ b/emu-configs/ppssppsdl/ppsspp.ini
@@ -1,6 +1,6 @@
 [General]
 FirstRun = False
-RunCount = 2
+RunCount = 0
 Enable Logging = True
 AutoRun = True
 Browse = False
diff --git a/retrodeck.sh b/retrodeck.sh
index ec294444..b6036987 100644
--- a/retrodeck.sh
+++ b/retrodeck.sh
@@ -1,10 +1,15 @@
 #!/bin/bash
 
-lockfile="/var/config/retrodeck/.lock"      # where the lockfile is located
-version="$(cat /app/retrodeck/version)"    # version info taken from the version file
-rdhome="$HOME/retrodeck"                   # the retrodeck home, aka ~/retrodecck
-emuconfigs="/app/retrodeck/emu-configs"    # folder with all the default emulator configs
-sdcard="/run/media/mmcblk0p1"              # Steam Deck SD default path
+# Init default values, this may be overwritten by retrodeck.cfg as it sourced later with global.sh
+
+lockfile=lockfile="/var/config/retrodeck/.lock"            # where the lockfile is located
+emuconfigs="/app/retrodeck/emu-configs"                    # folder with all the default emulator configs
+sdcard="/run/media/mmcblk0p1"                              # Steam Deck SD default path
+rd_conf="/app/retrodeck/retrodeck.cfg"                     # RetroDECK config file path
+version="$(cat /app/retrodeck/version)"                    # version info taken from the version file
+rdhome="$HOME/retrodeck"                                   # the retrodeck home, aka ~/retrodeck
+
+source global.sh
 
 # We moved the lockfile in /var/config/retrodeck in order to solve issue #53 - Remove in a few versions
 if [ -f "$HOME/retrodeck/.lock" ]
@@ -41,7 +46,7 @@ dir_prep() {
     # creating the symlink
     echo "linking $real in $symlink" #DEBUG
     mkdir -pv "$(dirname "$symlink")" # creating the full path except the last folder
-    ln -svf "$real" "$symlink"
+    ln -sv "$real" "$symlink"
 
     # moving everything from the old folder to the new one, delete the old one
     if [ -d "$symlink.old" ];
@@ -139,6 +144,30 @@ standalones_init() {
     mkdir -pv /var/config/rpcs3/
     cp -fvr $emuconfigs/config.yml /var/config/rpcs3/
 
+    # XEMU
+    echo "------------------------"
+    echo "Initializing XEMU"
+    echo "------------------------"
+    mkdir -pv $rdhome/saves/xemu
+    cp -fv $emuconfigs/xemu.toml /var/data/xemu/xemu.toml
+    sed -i 's#/home/deck/retrodeck#'$rdhome'#g' /var/data/xemu/xemu.toml
+    # Preparing HD dummy Image if the image is not found
+    if [ ! -f $rdhome/bios/xbox_hdd.qcow2 ]
+    then
+      wget "https://github.com/mborgerson/xemu-hdd-image/releases/latest/download/xbox_hdd.qcow2.zip" -P $rdhome/bios/
+      unzip $rdhome/bios/xbox_hdd.qcow2.zip $rdhome/bios/
+      rm -rfv $rdhome/bios/xbox_hdd.qcow2.zip
+    fi
+
+    # PPSSPPSDL
+    echo "------------------------"
+    echo "Initializing PPSSPPSDL"
+    echo "------------------------"
+    mkdir -p /var/config/ppsspp/PSP/SYSTEM/
+    cp -fv $emuconfigs/ppssppsdl/* /var/config/ppsspp/PSP/SYSTEM/
+    sed -i 's#/home/deck/retrodeck#'$rdhome'#g' /var/config/ppsspp/PSP/SYSTEM/ppsspp.ini
+
+
     # PICO-8
     # Moved PICO-8 stuff in the finit as only it knows here roms folders is
 
@@ -368,7 +397,8 @@ https://retrodeck.net
       exit
       ;;
     --version*|-v*)
-      cat /var/config/retrodeck/version
+      conf_init
+      echo $version
       exit
       ;;
     --reset-ra*)
@@ -401,7 +431,9 @@ done
 if [ -f "$lockfile" ] && [ "$(cat "$lockfile")" != "$version" ]; 
 then
     echo "Lockfile version is "$(cat "$lockfile")" but the actual version is $version"
-    post_update
+    conf_init         # Initializing/reading the config file (sourced from global.sh)
+    post_update       # Executing post update script
+    conf_write        # Writing variables in the config file (sourced from global.sh)
     start_retrodeck
     exit 0
 fi
@@ -411,7 +443,9 @@ fi
 if [ ! -f "$lockfile" ];
 then
   echo "Lockfile not found"
-  finit
+  conf_init         # Initializing/reading the config file (sourced from global.sh)
+  finit             # Executing First/Force init
+  conf_write        # Writing variables in the config file (sourced from global.sh)
 	exit 0
 fi