Files
dayz-servers/lib/typestool.sh

146 lines
3.1 KiB
Bash

#!/bin/sh -e
#
# Wrapper for editing DayZ types.xml using xmlstarlet
#
# 01. "should check code directly not $?"
# shellcheck disable=2181
#
# 02. "can't follow . import"
# shellcheck disable=1090,1091
#
# 03. "possible misspelling"
# shellcheck disable=2153
#
# 04. "use find .. over ls"
# shellcheck disable=2011
#----------------------------------------------------------
# =================================================================
# LOAD GLOBALS FILE
# =================================================================
GLOBALS_FILE="$PWD/globals.sh"
if [ -e "$GLOBALS_FILE" ] ; then
chmod +x "$GLOBALS_FILE"
. "$GLOBALS_FILE"
else
>&2 echo "Could not load: $GLOBALS_FILE"
exit 1
fi
# =================================================================
# example: swap_flag M16A2 count_in_cargo 1 file
swap_flags() {
file=$4
if [ ! -f "$file" ] ; then
>&2 echo "File: '$file' is not a file"
exit 1
fi
if [ "$1" ] && [ "$2" ] && [ "$3" ] ; then
_name=$1
_flag=$2
_value=$3
xmlstarlet ed -P -L \
-u \ "//types/type[@name=\"$_name\"]/flags[@$_flag]/@$_flag" \
-v \
"$_value" \
"$file"
else
>&2 echo "Bad params in swap_flag"
exit 1
fi
}
# example: swap_value M4A1 nominal 3 file
swap_value() {
file=$4
if [ ! -f "$file" ] ; then
>&2 echo "File: '$file' is not a file"
exit 1
fi
if [ "$1" ] && [ "$2" ] && [ "$3" ] ; then
_name=$1
_tag=$2
_value=$3
xmlstarlet ed -P -L \
-u \ "//types/type[@name=\"$_name\"]/$_tag" \
-v \
"$_value" \
"$file"
else
>&2 echo "Bad params in swap_value"
exit 1
fi
}
usage() {
cat <<EOF
Usage: ${0##*/} [--swap-flags|--swap-value] item_name tag value types.xml
--swap-value swap an item tag's value
--swap-flags swap an item's flags sub-value
# ================================
# Examples
# ================================
# --------------------------------
# Ex #1: Swapping a value
# --------------------------------
<type name="M4A1">
<nominal>1</nominal>
</type>
${0##*/} --swap-value M4A1 nominal 2 some_file.xml
# --------------------------------
# Ex #2: Swapping a flags value
# --------------------------------
<type name="M16A2">
<flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
</type>
${0##*/} --swap-flags M16A2 count_in_player 1 some_file.xml
EOF
exit 1
}
check() {
if ! command -v xmlstarlet >/dev/null ; then
die "Please install xmlstarlet"
fi
}
main() {
check || die "Failed check"
if [ "$DEBUG" ] ; then
echo "ADJUSTING TYPE: $2 $3"
fi
case $1 in
-h|--help)
usage
;;
-v|--swap-value)
shift
swap_value "$1" "$2" "$3" "$4" || exit 1
;;
-f|--swap-flags)
shift
swap_flags "$1" "$2" "$3" "$4" || exit 1
;;
esac
}
main "$@"