新增自动Mod管理功能,支持通过环境变量配置Mod ID,自动下载和更新Mod,并优化软链接创建逻辑。

This commit is contained in:
Dcr
2025-07-25 14:49:54 +08:00
parent 1dd3ec77b7
commit 34c2e6dd05
2 changed files with 89 additions and 26 deletions

View File

@@ -80,30 +80,65 @@ start_dayzserver() {
cd /root/Steam/steamapps/common/DayZServer
# 启动服务器之前,检查mod是否有更新.
bash /opt/updatemod.sh
updatemodpath="/root/Steam/steamapps/workshop/content/221100"
target_dir="/root/Steam/steamapps/common/DayZServer/client_mod"
find "$updatemodpath" -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d $'\0' mod_dir; do
meta_file=$(find "$mod_dir" -type f -name "meta.cpp" -print -quit)
# 1. 删除target_dir目录下的所有软链接
echo "清理现有的软链接..."
if [ -d "$target_dir" ]; then
find "$target_dir" -type l -delete
echo "已删除 $target_dir 目录下的所有软链接"
else
mkdir -p "$target_dir"
echo "创建目录: $target_dir"
fi
# 2. 根据MOD_IDS列表创建软链接
if [ -n "$MOD_IDS" ]; then
echo "开始根据MOD_IDS创建软链接..."
IFS=',' read -ra MOD_ARRAY <<< "$MOD_IDS"
if [[ -f "$meta_file" ]]; then
mod_name=$(grep -oP 'name\s*=\s*"\K[^"]*' "$meta_file")
for mod_id in "${MOD_ARRAY[@]}"; do
echo "处理Mod ID: $mod_id"
if [[ -n "$mod_name" ]]; then
symlink_path="${target_dir}/@${mod_name}"
if [[ ! -L "$symlink_path" ]]; then
echo "为 [$mod_name] 创建软链接: $symlink_path$mod_dir"
ln -s "$mod_dir" "$symlink_path"
# 查找对应的mod目录
mod_dir=$(find "$updatemodpath" -mindepth 1 -maxdepth 1 -type d -name "$mod_id" -print -quit)
if [[ -n "$mod_dir" ]]; then
echo "找到Mod目录: $mod_dir"
# 查找meta.cpp文件获取mod名称
meta_file=$(find "$mod_dir" -type f -name "meta.cpp" -print -quit)
if [[ -f "$meta_file" ]]; then
mod_name=$(grep -oP 'name\s*=\s*"\K[^"]*' "$meta_file")
if [[ -n "$mod_name" ]]; then
symlink_path="${target_dir}/@${mod_name}"
echo "为 [$mod_name] (ID: $mod_id) 创建软链接: $symlink_path$mod_dir"
ln -s "$mod_dir" "$symlink_path"
else
echo "警告: 在 $meta_file 中未找到 name 字段使用ID作为名称"
symlink_path="${target_dir}/@${mod_id}"
echo "为 [ID: $mod_id] 创建软链接: $symlink_path$mod_dir"
ln -s "$mod_dir" "$symlink_path"
fi
else
echo "软链接已存在: $symlink_path, 跳过创建"
echo "警告: 在 $mod_dir 中未找到 meta.cpp 文件使用ID作为名称"
symlink_path="${target_dir}/@${mod_id}"
echo "为 [ID: $mod_id] 创建软链接: $symlink_path$mod_dir"
ln -s "$mod_dir" "$symlink_path"
fi
else
echo "警告: $meta_file 中未找到 name 字段"
echo "警告: 未找到ID为 $mod_id 的Mod目录"
fi
else
echo "警告: 在 $mod_dir 中未找到 meta.cpp 文件"
fi
done
done
echo "软链接创建完成!"
else
echo "未配置MOD_IDS跳过软链接创建"
fi
# 初始化mod变量