-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_zk_sprite_list.sh
123 lines (95 loc) · 3.1 KB
/
create_zk_sprite_list.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
#!/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 file to create data from .png for https://mods.factorio.com/mod/zk-lib
### Original source: https://github.com/ZwerOxotnik/zk-lib
######
main() {
### Check if identify command exists
local has_errors=false
if ! command -v identify &> /dev/null; then
echo "identify: command not found"
has_errors=true
fi
if [ $has_errors = true ] ; then
exit 1
fi
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
local SPRITE_LIST_FILE=zk_sprite_list.lua
### 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 images: $MOD_NAME/" folder_name
local folder_path=$mod_folder/$folder_name
if [ ! -z "$folder_name" ]; then
local rel_folder_path="${folder_name}/"
fi
local SPRITE_LIST_PATH="$folder_path/$SPRITE_LIST_FILE"
rm -f $SPRITE_LIST_PATH
echo '
--[[###
This file auto-generated by script of https://github.com/ZwerOxotnik/zk-lib
Add "zk-lib >= 0.13.0" in your dependencies
This file should be in root folder of a mod!!!
]]--###
' >> $SPRITE_LIST_PATH
echo "local data_stage_data = {" >> $SPRITE_LIST_PATH
local format=*.[Pp][Nn][Gg]
local files=($(find $folder_path/ -name "$format" -type f))
for path in "${files[@]}"; do
local name="$(basename -- $path)"
local name=${name%.*}
echo -e "\t[\"$name\"] = {" >> $SPRITE_LIST_PATH
echo -e "\t\tfilename = \"__${MOD_NAME}__/$rel_folder_path$name.png\"", >> $SPRITE_LIST_PATH
echo -e "\t\t$(identify -format 'width=%w, height=%h,' $path)" >> $SPRITE_LIST_PATH
echo -e "\t}," >> $SPRITE_LIST_PATH
done
echo "}" >> $SPRITE_LIST_PATH
echo "" >> $SPRITE_LIST_PATH
echo '
for name, _data in pairs(data_stage_data) do
_data.type = _data.type or "sprite"
_data.name = _data.name or name
end
if IS_DATA_STAGE or IS_SETTING_STAGE then
return data_stage_data
else
local control_stage_data = {}
for name, _data in pairs(data_stage_data) do
control_stage_data[name] = _data.name
end
return control_stage_data
end' >> $SPRITE_LIST_PATH
echo ""
echo "You're almost ready!${bold}"
echo "# Add string \"zk-lib >= 0.13.0\" in dependencies of ${MOD_NAME}/info.json, example: '\"dependencies\": [\"zk-lib >= 0.13.0\"]'"
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/zk-lib)"
}
main