-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAmxxEditor.sh
263 lines (212 loc) · 8.36 KB
/
AmxxEditor.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/bin/bash
# AMXX Plugin Compiler Script
# Copyright (C) 2017-2018 Evandro Coan <https://github.com/evandrocoan>
#
# Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# Neither the name Evandro Coan nor the names of any
# contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or ( at
# your option ) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
printf "\\nCompiling %s... " "$2"
# Put here the paths to the folders where do you want to install the plugin.
# You must to provide at least one folder.
#
# Declare an array variable.
# You can access them using echo "${arr[0]}", "${arr[1]}"
declare -a folders_list=(
"F:/SteamCMD/steamapps/common/Half-Life/czero/addons/amxmodx/plugins"
"F:/SteamCMD/steamapps/common/Half-Life/cstrike/addons/amxmodx/plugins"
"F:/SteamLibrary/steamapps/common/Sven Co-op Dedicated Server/svencoop/addons/amxmodx/plugins"
)
# Where is your compiler?
#
# Examples:
#
# "F:/SteamCMD/steamapps/common/Half-Life/czero/addons/amxmodx/scripting/amxxpc.exe"
# "/home/jack/steam/steamapps/common/Half-Life/czero/addons/amxmodx/scripting/compiler.sh"
#
AMXX_COMPILER_PATH="F:/SteamCMD/steamapps/common/Half-Life/czero/addons/amxmodx/scripting/amxxpc.exe"
# Import the helper functions.
# The time flag file path
updateFlagFilePath="/tmp/.amxx_flag_file.txt"
# Save the current seconds, only if it is not already saved
if ! [ -f $updateFlagFilePath ]
then
# Create a flag file to avoid override the initial time and save it.
printf "%s" "$(date +%s.%N)" > $updateFlagFilePath
# printf "$1\\n"
printf "Current time: %s\\n" "$(date)"
fi
# Clean the flag file
cleanUpdateFlagFile()
{
if [ -f $updateFlagFilePath ]
then
cat $updateFlagFilePath
rm $updateFlagFilePath
fi
}
# Calculates and prints to the screen the seconds elapsed since this script started.
showTheElapsedSeconds()
{
# Clean the flag file and read the time
scriptStartSecond=$(cleanUpdateFlagFile)
# Calculates whether the seconds program parameter is an integer number
isFloatNumber "$scriptStartSecond"
# `$?` captures the return value of the previous function call command
# Print help when it is not passed a second command line argument integer
if [ $? -eq 1 ]
then
scripExecutionTimeResult=$(awk "BEGIN {printf \"%.2f\",$(date +%s.%N)-$scriptStartSecond}")
integer_time="$(float_to_integer "$scripExecutionTimeResult")"
printf "Took '%s' " "$(convert_seconds "$integer_time" "$scripExecutionTimeResult")"
printf "seconds to run the script, %s.\\n" "$(date +%H:%M:%S)"
else
printf "Could not calculate the seconds to run '%s'.\\n" "$1"
fi
}
# Convert seconds to hours, minutes, seconds, milliseconds
# https://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds
#
# Awk printf number in width and round it up
# https://unix.stackexchange.com/questions/131073/awk-printf-number-in-width-and-round-it-up
convert_seconds()
{
# printf "$1$2\\n"
printf "%s %s" "$1" "$2" | awk '{printf("%d:%02d:%02d:%02d.%02.0f", ($1/60/60/24), ($1/60/60%24), ($1/60%60), ($1%60), (($2-$1)*100))}'
}
# Bash: Float to Integer
# https://unix.stackexchange.com/questions/89712/bash-float-to-integer
float_to_integer()
{
awk 'BEGIN{for (i=1; i<ARGC;i++)
printf "%.0f\\n", ARGV[i]}' "$@"
}
# Determine whether its first parameter is empty or not.
#
# Returns 1 if empty, otherwise returns 0.
isEmpty()
{
if [ -z ${1+x} ]
then
return 1
fi
return 0
}
# Determine whether the first parameter is an integer or not.
#
# Returns 1 if the specified string is an integer, otherwise returns 0.
isInteger()
{
# Calculates whether the first function parameter $1 is a number
isEmpty "$1"
# `$?` captures the return value of the previous function call command
# Notify an invalid USB port number passed as parameter.
if ! [ $? -eq 1 ]
then
if [ "$1" -eq "$1" ] 2>/dev/null
then
return 1
fi
fi
return 0
}
# Determine whether the first parameter is an integer or not.
#
# Returns 1 if the specified string is an integer, otherwise returns 0.
isFloatNumber()
{
# Calculates whether the first function parameter $1 is a number
isEmpty "$1"
# `$?` captures the return value of the previous function call command
# Notify an invalid USB port number passed as parameter.
if ! [ $? -eq 1 ]
then
# Removed the file extension, just in case there exists.
firstFloatNumberPart=$(printf "%s" "$1" | cut -d'.' -f 1)
secondFloatNumberPart=$(printf "%s" "$1" | cut -d'.' -f 2)
# Checks whether the first float number part is an integer.
isInteger "$firstFloatNumberPart"
if ! [ $# -eq 1 ]
then
return 0
fi
# Checks whether the second float number part is an integer.
isInteger "$secondFloatNumberPart"
if [ $# -eq 1 ]
then
return 1
fi
fi
return 0
}
# $1 is the first shell argument and $2 is the second shell argument passed by AmxxEditor.sublime-build
# Usually they should be the plugin's file full path and the plugin's file name without extension.
#
# Example: $1="F:/SteamCMD/steamapps/common/Half-Life/czero/addons/my_plugin.sma"
PLUGIN_SOURCE_CODE_FILE_PATH=$1
# %4 is the path of the folder where the plugin source code is.
# Example F:\SteamCMD\steamapps\common\Half-Life\czero\addons\
SOURCE_CODE_FOLDER=$4
SOURCE_CODE_INCLUDE_FOLDER=$SOURCE_CODE_FOLDER/include
# Build the compiler include folder path
COMPILER_FOLDER_PATH=$(dirname "${AMXX_COMPILER_PATH}")
COMPILER_INCLUDE_FOLDER_PATH=$COMPILER_FOLDER_PATH/include
# Example: $2="my_plugin"
printf "\\n"
PLUGIN_BASE_FILE_NAME="$2"
PLUGIN_BINARY_FILE_PATH=${folders_list[0]}/$PLUGIN_BASE_FILE_NAME.amxx
if [[ $PLUGIN_BASE_FILE_NAME == "" ]]
then
printf "You must to save the plugin before to compile it.\\n"
else
# Delete the old binary in case some crazy problem on the compiler, or in the system while copy it.
# So, this way there is not way you are going to use the wrong version of the plugin without knowing it.
if [ -f "$PLUGIN_BINARY_FILE_PATH" ]
then
rm "$PLUGIN_BINARY_FILE_PATH"
fi
# To call the compiler to compile the plugin to the output folder $PLUGIN_BINARY_FILE_PATH
# Comment the following line and uncomment the next line to it, if you not want to override your compiler files
"$AMXX_COMPILER_PATH" -i"$SOURCE_CODE_INCLUDE_FOLDER" -o"$PLUGIN_BINARY_FILE_PATH" "$PLUGIN_SOURCE_CODE_FILE_PATH"
# "$AMXX_COMPILER_PATH" -i"$COMPILER_INCLUDE_FOLDER_PATH" -i"$SOURCE_CODE_INCLUDE_FOLDER" -o"$PLUGIN_BINARY_FILE_PATH" "$PLUGIN_SOURCE_CODE_FILE_PATH"
# If there was a compilation error, there is nothing more to be done.
if [ -f "$PLUGIN_BINARY_FILE_PATH" ]
then
printf "\\nInstalling the plugin to the folder %s\\n" "${folders_list[0]}"
# Remove the first element, as it was already processed and it is the source file.
unset "folders_list[0]"
# Now loop through the above array
for current_output_folder in "${folders_list[@]}"
do
printf "Installing the plugin to the folder %s\\n" "$current_output_folder"
rm "$current_output_folder/$PLUGIN_BASE_FILE_NAME.amxx"
cp "$PLUGIN_BINARY_FILE_PATH" "$current_output_folder"
done
fi
fi
FULL_PATH_TO_SCRIPT=$(echo "$0" | sed -r 's|\\|\/|g' | sed -r 's|:||g')
printf "\\n"
showTheElapsedSeconds "$FULL_PATH_TO_SCRIPT"