Skip to content

Commit

Permalink
feat: 更新存档继承的方案, 允许保留未知变量
Browse files Browse the repository at this point in the history
  • Loading branch information
MemoryShadow committed Sep 13, 2023
1 parent 6fcb00f commit cefcc72
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ jobs:
echo -e "\n\nexport CheckID='${CheckID}'" | sudo tee -a /etc/minecraftctl/config; \
cat /etc/minecraftctl/config | grep --color \"^export CheckID='${CheckID}'\"; \
echo minecraftctl updating...; \
# TODO 这里理论上在当前阶段会失败, 因为配置文件升级没有考虑有自定变量的因素. 但是随着拓展的出现, 支持自定义变量是肯定要有的. \
yum update -y ${pwd_path}/packages/${{ needs.build.outputs.RPM_AMD64 }} | grep 'minecraftctl' | grep -v '\[#* *\]';\
yum update -y ${pwd_path}/packages/${{ needs.build.outputs.RPM_AMD64 }} | grep 'minecraftctl' | grep -v '\[#* *\]'; \
cat /etc/minecraftctl/config | grep ${CheckID};"
Linux-Universal-Installation-and-testing:
Expand Down
116 changes: 57 additions & 59 deletions build/postinst
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
#!/bin/bash
#*升级后还原配置信息
###
# @Date: 2023-08-13 16:39:57
# @LastEditors: MemoryShadow
# @LastEditTime: 2023-09-13 09:59:42
# @Description: 升级后还原并升级配置信息
# Copyright (c) 2023 by MemoryShadow@outlook.com, All Rights Reserved.
###

# 加载等待升级的配置文件
source /etc/minecraftctl/config
# 如果配置文件存在,执行升级操作, 加载旧的配置文件
#*第零步, 将新的配置加载进内存
if [ -e /etc/minecraftctl/config.bak ]; then
source /etc/minecraftctl/config.bak
rm /etc/minecraftctl/config
# 对旧的配置文件进行兼容性处理(映射)
cat<<EOF>/etc/minecraftctl/config
# 游戏文件夹路径
export GamePath="${GamePath}"
# 会话名(若在修改时任有服务端在运行,会导致残留进程)
export ScreenName="${screen_name:-${ScreenName}}"
# 启动时内存(MB)
export StartCache=${startCache:-${StartCache}}
# 最大内存(MB)
export MaxCache=${MaxCache}
# 你要连接的中转主机的地址是什么
export MasterHost=${MasterHost}
# 启用SSL(https)?
export HostProtocol=${HostProtocol}
# 是否忽略可能不安全的链接
# 如果你的中转主机SSL证书与这个域名不匹配或者不由某官方机构颁发就填true
export Insecure=${Insecure}
# 是否启用第三方验证
export Authlib=${authlib:-${Authlib}}
# 第三方验证的验证服务地址
export AuthlibInjector="${authlib_injectorVer:-${AuthlibInjector}}"
# 服务器启动核心文件名(不含后缀)
export MainJAR="${MainJAR}"
# 服务器核心,备份文件时使用,支持的值为以下列表
# 模式名称:等效的值
# official:official,forge
# unofficial:unofficial,bukkit,spigot,paper,purpur,airplane
# 注: 要启用备份配置文件功能请在Backup目录下创建Config文件夹
export ServerCore="${ServerCore}"
# 备份所使用的线程数量(如果因为线程过多被杀,请调回来)
export BackupThread=${BackupThread}
# 备份文件的压缩质量(调整至更大会占用相当大的内存空间)
# 可选的值: 0~9
export BackupCompressLevel=${BackupCompressLevel}
# 离开时等待的秒数(若是超过此时间服务器还没停止,就会强制杀死进程)
export StopWaitTimeMax=${StopWaitTimeMax}
# minecraftctl显示的语言
export Language="${Language:-zh-CN}"
# minecraftctl安装路径
export InstallPath="${InstallPath:-/opt/minecraftctl}"
# 配置刹车时长(秒), 避免长时间过高的CPU占用
export SleepSecond=${SleepSecond:-15}
# 配置工作时长(秒)
export WorkSecond=${WorkSecond:-15}
# 设置切片超时时间(秒), 如果完成切片的时间超过这个值就放弃刹车
export WorkExceedSecond=${WorkExceedSecond:-50}
# 配置每个工作切片周期(行)
export WorkPart=${WorkPart:-1000}
EOF
if [ -e /etc/minecraftctl/config.bak ]; then
rm /etc/minecraftctl/config.bak
source /etc/minecraftctl/config.bak;
# 创建一个缓存配置
if ! declare -p Config_Cache >/dev/null 2>&1; then
declare -Ax Config_Cache=()
fi
#*第一步, 升级原有配置文件
while read -r line || [[ -n ${line} ]]; do
if [[ ${line} == "export"* ]]; then
Key=${line%=*}; Key=${Key#* };
Value=${line#*=};
# 过滤器过滤指定的Key并重定向或做出调整
case ${Key} in
screen_name|ScreenName)
Key="ScreenName";
;;
startCache|StartCache)
Key="StartCache";
;;
authlib|Authlib)
Key="Authlib";
;;
authlib_injectorVer|AuthlibInjector)
Key="AuthlibInjector";
;;
*)
# 默认情况下也是原样输出
echo "export ${Key}=${Value}";
;;
esac
Config_Cache[${Key}]=${Value};
else echo ${line}; fi
done < "/etc/minecraftctl/config.bak" > /etc/minecraftctl/config2
#*第二步, 将配置文件没有写入的部分写入配置文件
Cache='';
while read -r line || [[ -n ${line} ]]; do
if [[ ${line} == "export"* ]]; then
# 如果以export开头, 就匹配检查是否被处理过
Key=${line%=*}; Key=${Key#* };
Value=${line#*=};
if [ -z "${Config_Cache["${Key}"]}" ]; then
echo -e "${Cache}";
echo "export ${Key}=${Value}";
fi
Cache='';
else
# 如果不以export开头, 就将其写入注释缓存中
Cache="${Cache}${Cache:+\n}${line}";
fi
done < "/etc/minecraftctl/config" >> /etc/minecraftctl/config2
rm /etc/minecraftctl/config.bak
fi

0 comments on commit cefcc72

Please sign in to comment.