-
Notifications
You must be signed in to change notification settings - Fork 4
/
handle_sounds.sh
179 lines (154 loc) · 6.04 KB
/
handle_sounds.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
#!/usr/bin/env bash
(set -o igncr) 2>/dev/null && set -o igncr; # This comment is required.
### The above line ensures that the script can be run on Cygwin/Linux even with Windows CRNL.
### Generates lua and cfg files to handle .ogg sounds for Factorio
### Modified version of https://github.com/ZwerOxotnik/Mod-generator
main() {
local bold=$(tput bold)
local normal=$(tput sgr0)
### Find info.json
local SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $SCRIPT_DIR
local infojson_exists=false
local script_file=`basename "$0"`
if [[ -s "$SCRIPT_DIR/info.json" ]]; then
local infojson_exists=true
else
cd ..
if [[ -s "$PWD/info.json" ]]; then
local infojson_exists=true
else
cd $SCRIPT_DIR
fi
fi
local mod_folder=$PWD
### Check if sox command exists
### https://sox.sourceforge.net/
local sox_exists=false
if command -v ls &> /dev/null; then
sox_exists=true
fi
local SOUNDS_LIST_FILE=sounds_list.lua
local CFG_FILE=sounds_list.cfg
### Get mod name and version from info.json
### https://stedolan.github.io/jq/
if [ $infojson_exists = true ] ; then
local MOD_NAME=$(jq -r '.name' info.json)
if ! command -v jq &> /dev/null; then
echo "Please install jq https://stedolan.github.io/jq/"
fi
fi
echo "you're in ${bold}$mod_folder${normal}"
read -r -p "Complete path to folder of sounds: $MOD_NAME/" folder_name
local folder_path=$mod_folder/$folder_name
if [ ! -z "$folder_name" ]; then
local rel_folder_path="${folder_name}/"
fi
read -r -p "Add sounds to programmable speakers? [Y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
STATE=1
;;
*)
STATE=2
;;
esac
if [ $STATE -eq 1 ]; then
read -r -p "Insert group name of sounds:" sound_group_name
case "$sound_group_name" in "")
local sound_group_name=$MOD_NAME
;;
esac
fi
local SOUNDS_LIST_PATH="$folder_path/$SOUNDS_LIST_FILE"
rm -f $SOUNDS_LIST_PATH
local CFG_FILE="generated_$sound_group_name".cfg
if [ $infojson_exists = true ] ; then
local CFG_FULLPATH=$mod_folder/locale/en/$CFG_FILE
mkdir -p $mod_folder/locale/en
else
local CFG_FULLPATH=$mod_folder/$CFG_FILE
fi
rm -f $CFG_FULLPATH
if [ $STATE -eq 1 ]; then
echo "### This file auto-generated by https://github.com/ZwerOxotnik/factorio-example-mod" >> $CFG_FULLPATH
echo "### Please, do not change this file manually!" >> $CFG_FULLPATH
echo "[programmable-speaker-instrument]" >> $CFG_FULLPATH
echo $sound_group_name=$sound_group_name >> $CFG_FULLPATH
echo "[programmable-speaker-note]" >> $CFG_FULLPATH
fi
echo "-- This file auto-generated by https://github.com/ZwerOxotnik/factorio-example-mod" >> $SOUNDS_LIST_PATH
echo "-- Please, do not change this file if you're not sure, except sounds_list.name and path!" >> $SOUNDS_LIST_PATH
echo "-- You need require this file to your control.lua and add https://mods.factorio.com/mod/zk-lib in your dependencies" >> $SOUNDS_LIST_PATH
echo "" >> $SOUNDS_LIST_PATH
echo "local sounds_list = {" >> $SOUNDS_LIST_PATH
if [ $STATE -eq 1 ]; then
echo -e "\tname = \"$sound_group_name\", --change me, if you want to add these sounds to programmable speakers" >> $SOUNDS_LIST_PATH
fi
if [ $STATE -eq 2 ]; then
echo -e "\tname = nil --change me, if you want to add these sounds to programmable speakers" >> $SOUNDS_LIST_PATH
fi
echo -e "\tpath = \"__"${MOD_NAME}"__/"${rel_folder_path}"\", -- path to this folder" >> $SOUNDS_LIST_PATH
echo -e "\tsounds = {" >> $SOUNDS_LIST_PATH
###Converts audio files to .ogg format
if [ $sox_exists = true ] ; then
local files=($(find $folder_path/ -type f))
for fullpath in "${files[@]}"; do
### Took from https://stackoverflow.com/a/1403489
local filename="${fullpath##*/}" # Strip longest match of */ from start
local dir="${fullpath:0:${#fullpath} - ${#filename}}" # Substring from 0 thru pos of filename
local base="${filename%.[^.]*}" # Strip shortest match of . plus at least one non-dot char from end
local ext="${filename:${#base} + 1}" # Substring from len of base thru end
if [[ -z "$base" && -n "$ext" ]]; then # If we have an extension and no base, it's really the base
local base=".$ext"
local ext=""
fi
### It's too messy to fix
if ! [[ "$ext" =~ ^(|ogg|txt|lua|zip|json|cfg|md|sample|bat|sh|gitignore|pack|idx|yml|png)$ ]]; then
sox $fullpath $dir/$base.ogg
fi
done
fi
local format=*.ogg
local files=($(find $folder_path/ -name "$format" -type f))
for path in "${files[@]}"; do
local name="$(basename -- $path)"
local name=${name%.*}
echo -e "\t\t{" >> $SOUNDS_LIST_PATH
echo -e "\t\t\tname = \"$name\"", >> $SOUNDS_LIST_PATH
echo -e "\t\t}," >> $SOUNDS_LIST_PATH
if [ $STATE -eq 1 ]; then
echo $name=$name >> $CFG_FULLPATH
fi
done
echo -e "\t}" >> $SOUNDS_LIST_PATH
echo "}" >> $SOUNDS_LIST_PATH
echo "" >> $SOUNDS_LIST_PATH
echo "if puan2_api then" >> $SOUNDS_LIST_PATH
echo " puan2_api.add_sounds(sounds_list)" >> $SOUNDS_LIST_PATH
echo "elseif puan_api then" >> $SOUNDS_LIST_PATH
echo " puan_api.add_sounds(sounds_list)" >> $SOUNDS_LIST_PATH
echo "end" >> $SOUNDS_LIST_PATH
echo "" >> $SOUNDS_LIST_PATH
echo "return sounds_list" >> $SOUNDS_LIST_PATH
echo ""
echo "You're almost ready!${bold}"
if [ ! -s "$mod_folder/control.lua" ] && [ $infojson_exists = true ]; then
echo "require(\"__${MOD_NAME}__/${rel_folder_path}sounds_list\")" >> "$mod_folder/control.lua"
else
if [ $infojson_exists = true ]; then
echo "# You need to write 'require(\"__${MOD_NAME}__/${rel_folder_path}sounds_list\")' in your ${MOD_NAME}/control.lua"
else
echo "# You need to write 'require(\"__mod-name__/${rel_folder_path}sounds_list\")' in your ${MOD_NAME}/control.lua"
fi
fi
echo "# Add string \"zk-lib\" in dependencies of ${MOD_NAME}/info.json, example: '\"dependencies\": [\"zk-lib\"]'"
if [ $STATE -eq 1 ] && [ $infojson_exists = false ]; then
echo "# Put ${CFG_FILE} in folder /locale/en (it'll provide readable text in the game)"
fi
echo "${normal}"
echo ""
echo "if you found a bug or you have a problem with script etc, please, let me know"
echo "This script created by ZwerOxotnik (source: https://github.com/ZwerOxotnik/Mod-generator)"
}
main