last fixes
[iso2ps2.git] / iso2ps2
1 #!/bin/bash
2
3 PROGNAME="$(basename $0)"
4 PWD=$(dirname $0)
5 [ "$PWD" ] || PWD=$(pwd)
6 PWD=$(cd $PWD; pwd)
7
8 # default values
9 CACHE=$PWD/cache
10 CONFIGS=$PWD/CONFIGS
11 KLICENSE="e4e54fd67c16c316f47829a30484d843"
12 PS2CLASSIC=$PWD/ps2classic/ps2classic
13 PSNPACKAGE=$PWD/psn_package_npdrm/psn_package_npdrm
14 # HAN HEN
15 MODEL="HEN"
16
17 # formating functions
18 function title () { echo -e "\033[0;1m=== $* ===\033[0;0m"; }
19 function pass () { echo -e "\033[1;32m$*\033[0;0m"; }
20 function warn () { echo -e "\033[1;33m$*\033[0;0m"; }
21 function fail () { echo -e "\033[1;31m$*\033[0;0m"; exit 1; }
22
23 # defaut values
24 id=0
25
26 # get last values
27 [ -f .$PROGNAME ] && source .$PROGNAME
28
29 # init
30 file="$1"
31 [ -f "$file" ] || fail "can't find file"
32 file "$file" | grep -q UDF || fail "file is not an UDF iso"
33
34 # get serial
35 serial=$(isoinfo -f -i "$file" | \
36 grep "^/[A-Z][A-Z][A-Z][A-Z]_[0-9][0-9][0-9]\.[0-9][0-9];" | \
37 sed 's,[/.],,g;s,_,-,;s,;.*,,')
38 [ "$serial" ] || { serial=$(printf "PS2U-1%04d" $id); let id++; }
39
40 # store last value
41 echo > .$PROGNAME
42 echo "id=$id" >> .$PROGNAME
43
44 # get name
45 function getname () {
46 [ -f jlist2.html ] || wget https://psxdatacenter.com/psx2/jlist2.html
47 [ -f plist2.html ] || wget https://psxdatacenter.com/psx2/plist2.html
48 [ -f ulist2.html ] || wget https://psxdatacenter.com/psx2/ulist2.html
49 awk '/>'$file'</ {
50 getline; gsub(/<[^<]*>/, "")
51 sub(/&nbsp;/, " ")
52 gsub(/^[ \t]*/, "")
53 gsub(/[ \t]*$/, "")
54 print
55 }' [jpu]list2.html
56 }
57
58 # get game title
59 [ -f "$CACHE/PS2-GAMEID-TITLE-MASTER.csv" ] || ( cd "$CACHE"; wget https://raw.githubusercontent.com/VTSTech/PS2-OPL-CFG/master/test/PS2-GAMEID-TITLE-MASTER.csv )
60 title=$(awk -F\; '"'$serial'" == $1 {print $2}' "$CACHE/PS2-GAMEID-TITLE-MASTER.csv")
61 [ "$title" ] || title=$(basename "$file" | sed 's/ *(.*//')
62
63 # create CID
64 title "create CID"
65 cid=2P0001-${serial/-}_00-$(echo ${title}0000111122223333 | \
66 tr '[a-z]' '[A-Z]' | \
67 sed 's/[^A-Z0-9]//g;s/\(................\).*$/\1/')
68
69 # main process
70 title "description"
71 echo "File: '$file'"
72 echo "Serial: $serial"
73 echo "Title: '$title'"
74 echo "CID: $cid"
75
76 # install skeleton
77 title "install skeleton"
78 mkdir $serial
79 tar -C $serial -xzf $PWD/skeleton.tgz
80 [ -d "$CACHE" ] || mkdir "$CACHE"
81
82 # images:
83 # - ICON0.PNG 320x176
84 # - PIC0.PNG 1000x560
85 # - PIC1.PNG 1920x1080
86
87 # get cover image
88 title "get cover image"
89 if [ -f "$CACHE/$serial-ICON0.PNG" ]; then
90 cp "$CACHE/$serial-ICON0.PNG" $serial/ICON0.PNG
91 else
92 wget https://psxdatacenter.com/psx2/images2/covers/$serial.jpg
93 if [ -f $serial.jpg ]; then
94 convert $serial.jpg tmp.png
95 mogrify -resize 320x176 tmp.png
96 mogrify -background transparent -gravity center -extent 320x176 tmp.png
97 mv tmp.png $serial/ICON0.PNG
98 rm $serial.jpg
99 cp $serial/ICON0.PNG "$CACHE/$serial-ICON0.PNG"
100 else
101 warn "can't find cover"
102 fi
103 fi
104
105 # get screenshot
106 title "get screenshot"
107 if [ -f "$CACHE/$serial-PIC0.PNG" ]; then
108 cp "$CACHE/$serial-PIC0.PNG" $serial/PIC0.PNG
109 else
110 touch .touch
111 find $HOME/.config/PCSX2/snaps/ -type f | xargs rm .touch
112 pcsx2 "$file"
113 capture=$(find $HOME/.config/PCSX2/snaps/ -type f | sort)
114 if [ "$capture" ]; then
115 cp "$capture" tmp.png
116 mogrify -scale 1000x560 tmp.png
117 mogrify -background transparent -gravity center -extent 1000x560 tmp.png
118 mv tmp.png $serial/PIC0.PNG
119 cp $serial/PIC0.PNG "$CACHE/$serial-PIC0.PNG"
120 else
121 warn "can't find screenshot"
122 fi
123 fi
124
125 # create PARAM.SFO
126 title "create PARAM.SFO"
127 # parameters
128 # - file: PARAM.SFO
129 # 0x120: title
130 # 0x1a0: ${serial/-}
131 # - file: USRDIR/ISO.BIN.EDAT
132 # 0x017: ${serial/-}
133 echo -n $title | xxd | xxd -r -s 0x120 - $serial/PARAM.SFO
134 echo -n ${serial/-} | xxd | xxd -r -s 0x1a0 - $serial/PARAM.SFO
135 echo -n ${serial/-} | xxd | xxd -r -s 0x17- $serial/USRDIR/ISO.BIN.EDAT
136
137 # generate key
138 title "generate key"
139 dd bs=1 count=$(expr ${#KLICENSE} / 2) if=/dev/zero of=$serial/ps2.key
140 echo "00000000 $KLICENSE" | xxd -r - $serial/ps2.key
141
142 # find config file
143 title "find config file"
144 if [ ! -f "$CACHE/$serial.CONFIG" ]; then
145 confname=$(echo $serial | sed 's/-/_/;s/_\(...\)/_\1./')
146 case "$MODEL" in
147 HAN)
148 confname=$confname.CONFIG
149 config=$(cd $CONFIGS; find NET GX SOFTCUSTOM -name $confname | head -1)
150 ;;
151 HEN)
152 confname=$confname.ENC
153 config=$(cd $CONFIGS; find ENC -name $confname | head -1)
154 ;;
155 esac
156 if [ "$config" ]; then
157 cp $CONFIGS/$config "$CACHE/$serial.CONFIG"
158 fi
159 fi
160 [ -f "$CACHE/$serial.CONFIG" ] && cp "$CACHE/$serial.CONFIG" $serial/USRDIR/CONFIG || warn "can't find config"
161
162 # create package config
163 title "create package config"
164 cat > $serial/package.conf << EOF
165 Content-ID = $cid
166 k_licensee = 0x$KLICENSE
167 DRM_Type = Free
168 Content_Type = PS2_Classics_Game
169 PackageVersion = 01.01
170 EOF
171
172 # prepare iso
173 title "prepare iso"
174 $PS2CLASSIC prepare "$file" || fail "can't prepare iso"
175
176 # convert iso
177 title "convert iso"
178 $PS2CLASSIC e cex $serial/ps2.key "$file" $serial/USRDIR/ISO.BIN.ENC ISO.BIN.ENC $cid || fail "can't convert iso"
179
180 # create package
181 title "create package"
182 $PSNPACKAGE $serial || fail "can't create package"
183 mv $serial/$cid.pkg .
184
185 # create rap file
186 title "create rap file"
187 cp $PWD/iso2ps2.rap $cid.rap
188
189 # cleaning
190 title "cleaning"
191 rm -rf $serial