Sfoglia il codice sorgente

major fastdl re-think

UltimateByte 9 anni fa
parent
commit
ae9d308084
1 ha cambiato i file con 173 aggiunte e 195 eliminazioni
  1. 173 195
      lgsm/functions/command_fastdl.sh

+ 173 - 195
lgsm/functions/command_fastdl.sh

@@ -62,13 +62,14 @@ if [ "${gamename}" == "Garry's Mod" ]; then
 	fi
 fi
 
+# Clears any fastdl directory content
 fn_clear_old_fastdl(){
 	# Clearing old FastDL
 	if [ -d "${fastdldir}" ]; then
 		echo -en "clearing existing FastDL directory ${fastdldir}..."
-		rm -R "${fastdldir:?}"/*
+		rm -R "${fastdldir:?}"
 		exitcode=$?
-		if [ ${exitcode} -ne 0 ]; then
+		if [ "${exitcode}" -ne 0 ]; then
 			fn_print_fail_eol_nl
 			fn_script_log_fatal "Clearing existing FastDL directory ${fastdldir}"
 			core_exit.sh
@@ -80,36 +81,6 @@ fn_clear_old_fastdl(){
 	fi
 }
 
-fn_clear_old_fastdl_alt(){
-	if [ -d "${fastdldir}" ]; then
-		echo -e "removing existing FastDL files"
-		sleep 0.1
-		fileswc=1
-		totalfileswc=$(find "${fastdldir}" | wc -l)
-		tput sc
-		while read -r filetoremove; do
-			tput rc; tput el
-			printf "removing ${fileswc} / ${totalfileswc} : ${filetoremove}..."
-			((fileswc++))
-			rm -rf "${filetoremove}"
-			((exitcode=$?))
-			if [ ${exitcode} -ne 0 ]; then
-				fn_script_log_fatal "Removing ${filetoremove}"
-				break
-			else
-				fn_script_log_pass "Removing ${filetoremove}"
-			fi
-			sleep 0.01
-		done < <(find "${fastdldir}")
-		if [ ${exitcode} -ne 0 ]; then
-			fn_print_fail_eol_nl
-			core_exit.sh
-		else
-			fn_print_ok_eol_nl
-		fi
-	fi
-}
-
 fn_fastdl_dirs(){
 	# Check and create directories
 	if [ ! -d "${webdir}" ]; then
@@ -142,59 +113,146 @@ fn_fastdl_dirs(){
 	fi
 }
 
-fn_fastdl_gmod(){
-	# Copy all needed files for FastDL
-	if [ -n "${copyflag}" ]; then
-		# Clear previous FastDL dir
-		fn_clear_old_fastdl_alt
-		fn_fastdl_dirs
-		echo -e "copying files to ${fastdldir}"
-		fn_script_log_info "Copying files to ${fastdldir}"
+# Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
+fn_human_readable_file_size(){
+    local abbrevs=(
+        $((1 << 60)):ZB
+        $((1 << 50)):EB
+        $((1 << 40)):TB
+        $((1 << 30)):GB
+        $((1 << 20)):MB
+        $((1 << 10)):KB
+        $((1)):bytes
+    )
+
+    local bytes="${1}"
+    local precision="${2}"
+
+    if [[ "${bytes}" == "1" ]]; then
+        echo "1 byte"
+    else
+        for item in "${abbrevs[@]}"; do
+            local factor="${item%:*}"
+            local abbrev="${item#*:}"
+            if [[ "${bytes}" -ge "${factor}" ]]; then
+                local size="$(bc -l <<< "${bytes} / ${factor}")"
+                printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
+                break
+            fi
+        done
+    fi
+}
+
+# Provides info about the fastdl directory content and prompts for confirmation
+fn_fastdl_preview(){
+	# Remove any file list
+	if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
+		rm -f "${tmpdir}/fastdl_files_to_compress.txt"
+	fi
+	echo -e "analysing required files"
+	fn_script_log_info "Analysing required files"
+	# Garry's Mod
+	if [ "${gamename}" == "Garry's Mod" ]; then
+		cd "${systemdir}" || exit
+		allowed_extentions_array=( "*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav" )
+		for allowed_extention in "${allowed_extentions_array[@]}"; do
+			fileswc=0
+			tput sc
+			while read -r ext; do
+				((fileswc++))
+				tput rc; tput el
+				printf "gathering ${allowed_extention} : ${fileswc}..."
+				echo "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
+			done < <(find . -type f -iname ${allowed_extention})
+			if [ ${fileswc} != 0 ]; then
+				fn_print_ok_eol_nl
+			fi
+		done
+	# Source engine
 	else
-		if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
-			rm -f "${tmpdir}/fastdl_files_to_compress.txt"
-		fi
-		echo -e "analysing required files"
-		fn_script_log_info "Analysing required files"
+		fastdl_directories_array=( "maps" "materials" "models" "particles" "sounds" "resources" )
+		for directory in "${fastdl_directories_array[@]}"; do
+			if [ -d "${systemdir}/${directory}" ]; then
+				if [ "${directory}" == "maps" ]; then
+					local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
+				elif [ "${directory}" == "materials" ]; then
+					local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" )
+				elif [ "${directory}" == "models" ]; then
+					local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
+				elif [ "${directory}" == "particles" ]; then
+					local allowed_extentions_array=( "*.pcf" )
+				elif [ "${directory}" == "sounds" ]; then
+					local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
+				fi
+				for allowed_extention in "${allowed_extentions_array[@]}"; do
+					fileswc=0
+					tput sc
+					while read -r ext; do
+						((fileswc++))
+						tput rc; tput el
+						printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
+						echo "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
+					done < <(find "${systemdir}/${directory}" -type f -iname ${allowed_extention})
+					tput rc; tput el
+					printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
+					if [ ${fileswc} != 0 ]; then
+						fn_print_ok_eol_nl
+					fi
+				done
+			fi
+		done
+	fi
+	if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
+		echo "calculating total file size..."
+		sleep 0.5
+		totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
+		# Calculates total file size
+		while read dufile; do
+			filesize=$(stat -c %s "${dufile}")
+			filesizetotal=$(( ${filesizetotal} + ${filesize} ))
+			exitcode=$?
+			if [ "${exitcode}" != 0 ]; then
+				fn_print_fail_eol_nl
+				fn_script_log_fatal "Calculating total file size."
+				core_exit.sh
+			fi
+		done <"${tmpdir}/fastdl_files_to_compress.txt"
+	else
+		fn_print_fail_eol_nl "generating file list"
+		fn_script_log_fatal "Generating file list."
+		core_exit.sh
 	fi
-	cd "${systemdir}"
-	local allowed_extentions_array=( "*.ain" "*.bsp" "*.mdl" "*.mp3" "*.ogg" "*.otf" "*.pcf" "*.phy" "*.png" "*.vtf" "*.vmt" "*.vtx" "*.vvd" "*.ttf" "*.wav" )
+	echo "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
+	fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
+	rm "${tmpdir}/fastdl_files_to_compress.txt"
+	if ! fn_prompt_yn "Continue?" Y; then
+		fn_script_log "User exited"
+		core_exit.sh
+	fi	
+}
+
+# Builds Garry's Mod fastdl directory content
+fn_fastdl_gmod(){
+	cd "${systemdir}" || exit
 	for allowed_extention in "${allowed_extentions_array[@]}"
 	do
 		fileswc=0
 		tput sc
-		if [ -z "${copyflag}" ]; then
+		while read -r fastdlfile; do
+			((fileswc++))
 			tput rc; tput el
 			printf "copying ${allowed_extention} : ${fileswc}..."
-		fi
-		while read -r ext; do
-			((fileswc++))
-			if [ -n "${copyflag}" ]; then
-				tput rc; tput el
-				printf "copying ${allowed_extention} : ${fileswc}..."
-				sleep 0.01
-				cp --parents "${ext}" "${fastdldir}"
-				exitcode=$?
-				if [ ${exitcode} -ne 0 ]; then
-					fn_print_fail_eol_nl
-					fn_script_log_fatal "Copying ${ext} > ${fastdldir}"
-					core_exit.sh
-				else
-					fn_script_log_pass "Copying ${ext} > ${fastdldir}"
-				fi
+			cp --parents "${fastdlfile}" "${fastdldir}"
+			exitcode=$?
+			if [ ${exitcode} -ne 0 ]; then
+				fn_print_fail_eol_nl
+				fn_script_log_fatal "Copying ${fastdlfile} > ${fastdldir}"
+				core_exit.sh
 			else
-				tput rc; tput el
-				printf "gathering ${allowed_extention} : ${fileswc}..."
-				sleep 0.01
-				echo "${ext}" >> "${tmpdir}/fastdl_files_to_compress.txt"
+				fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}"
 			fi
 		done < <(find . -type f -iname ${allowed_extention})
-
-		if [ -z "${copyflag}" ]; then
-			tput rc; tput el
-			printf "gathering ${allowed_extention} : ${fileswc}..."
-		fi
-		if [ ${fileswc} != 0 ]&&[ -n "${copyflag}" ]||[ -z "${copyflag}" ]; then
+		if [ ${fileswc} != 0 ]; then
 			fn_print_ok_eol_nl
 		fi
 	done
@@ -248,153 +306,74 @@ fn_fastdl_gmod(){
 			filesizetotal=$(( ${filesizetotal} + ${filesize} ))
 		done <"${tmpdir}/fastdl_files_to_compress.txt"
 	fi
-
-	if [ -z "${copyflag}" ]; then
-		echo "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
-		fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
-		rm "${tmpdir}/fastdl_files_to_compress.txt"
-		if fn_prompt_yn "Continue?" Y; then
-			copyflag=1
-			fn_fastdl_gmod
-		else
-			core_exit.sh
-		fi
-	fi
-}
-
-# Using this gist https://gist.github.com/agunnerson-ibm/efca449565a3e7356906
-fn_human_readable_file_size() {
-    local abbrevs=(
-        $((1 << 60)):ZB
-        $((1 << 50)):EB
-        $((1 << 40)):TB
-        $((1 << 30)):GB
-        $((1 << 20)):MB
-        $((1 << 10)):KB
-        $((1)):bytes
-    )
-
-    local bytes="${1}"
-    local precision="${2}"
-
-    if [[ "${bytes}" == "1" ]]; then
-        echo "1 byte"
-    else
-        for item in "${abbrevs[@]}"; do
-            local factor="${item%:*}"
-            local abbrev="${item#*:}"
-            if [[ "${bytes}" -ge "${factor}" ]]; then
-                local size="$(bc -l <<< "${bytes} / ${factor}")"
-                printf "%.*f %s\n" "${precision}" "${size}" "${abbrev}"
-                break
-            fi
-        done
-    fi
 }
 
 fn_fastdl_source(){
-	# Copy all needed files for FastDL
-	if [ -n "${copyflag}" ]; then
-		fn_clear_old_fastdl_alt
-		fn_fastdl_dirs
-		echo -e "copying files to ${fastdldir}"
-		fn_script_log_info "Copying files to ${fastdldir}"
-	else
-		if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
-			rm -f "${tmpdir}/fastdl_files_to_compress.txt"
-		fi
-		echo -e "analysing required files"
-		fn_script_log_info "Analysing required files"
-	fi
-
-	local directories_array=( "maps" "materials" "models" "particles" "sounds" "resources" )
-	for directory in "${directories_array[@]}"
+	for directory in "${fastdl_directories_array[@]}"
 	do
 		if [ -d "${systemdir}/${directory}" ]; then
 			if [ "${directory}" == "maps" ]; then
 				local allowed_extentions_array=( "*.bsp" "*.ain" "*.nav" "*.jpg" "*.txt" )
 			elif [ "${directory}" == "materials" ]; then
 				local allowed_extentions_array=( "*.vtf" "*.vmt" "*.vbf" )
+			elif [ "${directory}" == "models" ]; then
+				local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.phy" "*.jpg" "*.png" )
 			elif [ "${directory}" == "particles" ]; then
 				local allowed_extentions_array=( "*.pcf" )
 			elif [ "${directory}" == "sounds" ]; then
-				local allowed_extentions_array=( "*.vtx" "*.vvd" "*.mdl" "*.mdl" "*.phy" "*.jpg" "*.png" )
+				local allowed_extentions_array=( "*.wav" "*.mp3" "*.ogg" )
 			fi
 			for allowed_extention in "${allowed_extentions_array[@]}"
 			do
 				fileswc=0
 				tput sc
-				if [ -z "${copyflag}" ]; then
+				while read -r fastdlfile; do
+					((fileswc++))
 					tput rc; tput el
 					printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
-				fi
-				while read -r mapfile; do
-					((fileswc++))
-					if [ -n "${copyflag}" ]; then
-						tput rc; tput el
-						printf "copying ${directory} ${allowed_extention} : ${fileswc}..."
-						sleep 0.01
-						if [ ! -d "${fastdldir}/${directory}" ]; then
-							mkdir "${fastdldir}/${directory}"
-						fi
-						cp "${mapfile}" "${fastdldir}/${directory}"
-						exitcode=$?
-						if [ ${exitcode} -ne 0 ]; then
-							fn_print_fail_eol_nl
-							fn_script_log_fatal "Copying ${mapfile} > ${fastdldir}/${directory}"
-							core_exit.sh
-						else
-							fn_script_log_pass "Copying ${mapfile} > ${fastdldir}/${directory}"
-						fi
+					sleep 0.01
+					if [ ! -d "${fastdldir}/${directory}" ]; then
+						mkdir "${fastdldir}/${directory}"
+					fi
+					cp "${fastdlfile}" "${fastdldir}/${directory}"
+					exitcode=$?
+					if [ "${exitcode}" -ne 0 ]; then
+						fn_print_fail_eol_nl
+						fn_script_log_fatal "Copying ${fastdlfile} > ${fastdldir}/${directory}"
+						core_exit.sh
 					else
-						tput rc; tput el
-						printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
-						sleep 0.01
-						echo "${mapfile}" >> "${tmpdir}/fastdl_files_to_compress.txt"
+						fn_script_log_pass "Copying ${fastdlfile} > ${fastdldir}/${directory}"
 					fi
 				done < <(find "${systemdir}/${directory}" -type f -iname ${allowed_extention})
-
-				if [ -z "${copyflag}" ]; then
-					tput rc; tput el
-					printf "gathering ${directory} ${allowed_extention} : ${fileswc}..."
-				fi
-				if [ ${fileswc} != 0 ]&&[ -n "${copyflag}" ]||[ -z "${copyflag}" ]; then
+				if [ ${fileswc} != 0 ]; then
 					fn_print_ok_eol_nl
 				fi
 			done
 		fi
 	done
+}
 
-	if [ -f "${tmpdir}/fastdl_files_to_compress.txt" ]; then
-		totalfiles=$(wc -l < "${tmpdir}/fastdl_files_to_compress.txt")
-		# Calculates total file size
-		while read dufile; do
-			filesize=$(du -b "${dufile}"| awk '{ print $1 }')
-			filesizetotal=$(( ${filesizetotal} + ${filesize} ))
-		done <"${tmpdir}/fastdl_files_to_compress.txt"
-	fi
-
-	if [ -z "${copyflag}" ]; then
-		echo "about to compress ${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
-		fn_script_log_info "${totalfiles} files, total size $(fn_human_readable_file_size ${filesizetotal} 0)"
-		rm "${tmpdir}/fastdl_files_to_compress.txt"
-		if fn_prompt_yn "Continue?" Y; then
-			copyflag=1
-			fn_fastdl_source
-		else
-			core_exit.sh
-		fi
+# Builds the fastdl directory content
+fn_fastdl_build(){
+	# Copy all needed files for FastDL
+	echo -e "copying files to ${fastdldir}"
+	fn_script_log_info "Copying files to ${fastdldir}"
+	if [ "${gamename}" == "Garry's Mod" ]; then
+		fn_fastdl_gmod
+		fn_fastdl_gmod_dl_enforcer
+	else
+		fn_fastdl_source
 	fi
 }
 
 # Generate lua file that will force download any file into the FastDL directory
-fn_fastdl_gmod_lua_enforcer(){
+fn_fastdl_gmod_dl_enforcer(){
 	# Clear old lua file
 	if [ -f "${luafastdlfullpath}" ]; then
 		echo -en "removing existing download enforcer: ${luafastdlfile}..."
 		rm "${luafastdlfullpath:?}"
 		exitcode=$?
-		if [ ${exitcode} -ne 0 ]; then
+		if [ "${exitcode}" -ne 0 ]; then
 			fn_print_fail_eol_nl
 			fn_script_log_fatal "removing existing download enforcer ${luafastdlfullpath}"
 			core_exit.sh
@@ -408,11 +387,11 @@ fn_fastdl_gmod_lua_enforcer(){
 		echo -en "creating new download enforcer: ${luafastdlfile}..."
 		touch "${luafastdlfullpath}"
 		# Read all filenames and put them into a lua file at the right path
-		find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n' | while read line; do
-			echo "resource.AddFile( "\""${line}"\"" )" >> "${luafastdlfullpath}"
-		done
+		while read line; do
+			echo "resource.AddFile( \"${line}\" )" >> "${luafastdlfullpath}"
+		done < <(find "${fastdldir:?}" \( -type f ! -name "*.bz2" \) -printf '%P\n')
 		exitcode=$?
-		if [ ${exitcode} -ne 0 ]; then
+		if [ "${exitcode}" -ne 0 ]; then
 			fn_print_fail_eol_nl
 			fn_script_log_fatal "creating new download enforcer ${luafastdlfullpath}"
 			core_exit.sh
@@ -423,6 +402,7 @@ fn_fastdl_gmod_lua_enforcer(){
 	fi
 }
 
+# Compresses FastDL files using bzip2
 fn_fastdl_bzip2(){
 	echo -en "compressing files..."
 	echo ""
@@ -430,7 +410,7 @@ fn_fastdl_bzip2(){
 		echo -en "\r\033[Kcompressing ${filetocompress}..."
 		bzip2 -f "${filetocompress}"
 		exitcode=$?
-		if [ ${exitcode} -ne 0 ]; then
+		if [ "${exitcode}" -ne 0 ]; then
 			fn_print_fail_eol_nl
 			fn_script_log_fatal "compressing ${filetocompress}"
 			core_exit.sh
@@ -442,12 +422,10 @@ fn_fastdl_bzip2(){
 }
 
 # Run functions
-if [ "${gamename}" == "Garry's Mod" ]; then
-	fn_fastdl_gmod
-	fn_fastdl_gmod_lua_enforcer
-else
-	fn_fastdl_source
-fi
+fn_fastdl_preview
+fn_clear_old_fastdl
+fn_fastdl_dirs
+fn_fastdl_build
 fn_fastdl_bzip2
 # Finished message
 echo "FastDL files are located in:"