-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimize.sh
executable file
·81 lines (67 loc) · 2.1 KB
/
optimize.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
#!/usr/bin/bash
set -e
# Check for required commands
for cmd in jq pngcheck oxipng cwebp ffmpeg parallel
do
if ! command -v $cmd >/dev/null 2>&1
then
echo >&2 "$cmd is required but it's not installed. Aborting."; exit 1
fi
done
# Check if the user provided a directory path
if [ $# -ne 1 ]
then
echo "Usage: $0 path/to/game_dir"; exit 1
fi
game_dir="${1%/}"
# Check if input path is a RPG Maker MV/MZ game
if [[ ! -d "$game_dir"/www ]]
then
echo "Error: Input path is not a RPG Maker MV/MZ game."; exit 1
fi
# Check if files are encrypted
hasEncryptedImages=$(jq -r '.hasEncryptedImages' "$game_dir"/www/data/System.json)
hasEncryptedAudio=$(jq -r '.hasEncryptedAudio' "$game_dir"/www/data/System.json)
if [[ "$hasEncryptedImages" == "true" ]] || [[ "$hasEncryptedAudio" == "true" ]]
then
echo "Files are encrypted. Run the decrypt.sh script."; exit 1
fi
# Function to optimize image files
optimize_image() {
local file="$1"
# Check if a file is an APNG file
if pngcheck -v "$file" | grep -q -E "acTL"
then
oxipng --opt max --strip safe "$file" || echo >&2 "oxipng failed on $file. Continuing"
else
cwebp -lossless "$file" -o "$file" || echo >&2 "cwebp failed on $file. Continuing."
fi
}
# Function to optimize audio files
optimize_audio() {
local file="$1"
ffmpeg -i "$file" -c:a libopus "${file%.ogg}.opus"
mv "${file%.ogg}.opus" "$file"
}
export -f optimize_image
export -f optimize_audio
# Backup original files
if [ ! -d "$game_dir"/www_backup ]
then
cp -R "$game_dir"/www "$game_dir"/www_backup
else
echo "Backup already exists. Skipping backup."
fi
# Optimize icon separately
if [ -f "$game_dir"/www/icon/icon.png ]
then
oxipng --opt max --strip all "$game_dir"/www/icon/icon.png
else
echo >&2 "Icon doesn't exist. Ignoring."
fi
# Run functions multithreaded
find "$game_dir"/www -type f -name "*.png" ! -name "icon.png" | parallel optimize_image
find "$game_dir"/www -type f -name "*.ogg" | parallel optimize_audio
# Disable the AudioStreaming plugin
sed -i 's/\("name"\s*:\s*"AudioStreaming"\s*,"status"\s*:\s*\)true/\1false/' "$game_dir"/www/js/plugins.js
echo "Finished!"