Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hotianbexuanto committed Aug 10, 2024
1 parent 90f83a1 commit c5a1bf3
Show file tree
Hide file tree
Showing 3 changed files with 367 additions and 0 deletions.
111 changes: 111 additions & 0 deletions sh自动json处理显示版(测试加速版).sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/bin/bash

# 设置目录变量
SOURCE_DIR="img"
OUTPUT_FILE="boot.json"

# 检查 SOURCE_DIR 是否存在
if [ ! -d "$SOURCE_DIR" ]; then
echo "错误:目录 $SOURCE_DIR 不存在!"
exit 1
fi

read -p "请输入名称: " NAME
read -p "请输入模组版本 (参考 0.0.1): " VERSION
read -p "请输入支持的 ModLoader 版本 (参考 =1.2.3, <2.0.0, >1.0.0, ^1.2.3): " DEP_VERSION
read -p "请输入游戏版本 (可选, 参考 0.0.0.1, 如果不输入则不在 JSON 中添加此字段): " GAME_VERSION

# 初始化 JSON 输出文件
{
echo "{"
echo ' "name": "'"$NAME"'",'
echo ' "version": "'"$VERSION"'",'
echo ' "scriptFileList_inject_early": [],'
echo ' "scriptFileList_earlyload": [],'
echo ' "scriptFileList_preload": [],'
echo ' "styleFileList": [],'
echo ' "scriptFileList": [],'
echo ' "tweeFileList": [],'
echo ' "imgFileList": ['
} > "$OUTPUT_FILE"

# 使用 ls 和 xargs 提高扫描速度,并将结果保存在数组中
mapfile -t files < <(ls -R "$SOURCE_DIR" | grep -E '\.png$|\.jpg$|\.gif$|^[^./]' | xargs -I{} echo "$SOURCE_DIR"/{})

total_files=${#files[@]}

# 更新进度条并显示任务信息的函数
update_progress_bar() {
local task_name=$1
local progress=$2
local total=$3
local current_file=$4

if [ "$total" -eq 0 ];then
percent=100
else
percent=$(( progress * 100 / total ))
fi

local width=50
local filled=$(( percent * width / 100 ))
local unfilled=$(( width - filled ))
local bar=$(printf "%${filled}s" | tr ' ' '#')
bar+=$(printf "%${unfilled}s" | tr ' ' '.')

printf "\r\033[K当前文件: %s" "$current_file"
printf "\n\033[K%s [%-${width}s] %d%%" "$task_name" "$bar" "$percent"
printf "\n\033[K正在处理: %s" "$task_name"
}

# 处理图像文件并显示进度条
process_image_files() {
local files=("$@")
local img_json=""
local total_files=${#files[@]}

for ((i=0; i<total_files; i++)); do
local file="${files[i]}"
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.gif ]]; then
img_json+=" \"$file\",\n"
fi
update_progress_bar "保存图像文件" $((i+1)) "$total_files" "$file"
done

# 写入所有图像文件到 JSON 文件,并删除最后一行的逗号
echo -e "${img_json%,}" >> "$OUTPUT_FILE"
}

# 扫描文件
process_image_files "${files[@]}"

# 关闭 JSON 数组和对象
{
echo ' ],'
echo ' "additionFile": [],'
echo ' "additionBinaryFile": [],'
echo ' "additionDir": [],'
echo ' "addonPlugin": [],'
echo ' "dependenceInfo": ['
echo ' {'
echo ' "modName": "ModLoader",'
echo ' "version": "'"$DEP_VERSION"'"'
echo ' }'
} >> "$OUTPUT_FILE"

# 添加 GameVersion 条件
if [[ -n "$GAME_VERSION" ]]; then
{
echo ' ,'
echo ' {'
echo ' "modName": "GameVersion",'
echo ' "version": "'"$GAME_VERSION"'"'
} >> "$OUTPUT_FILE"
fi

{
echo ' ]'
echo "}"
} >> "$OUTPUT_FILE"

echo -e "\n目录结构已保存到 $OUTPUT_FILE"
125 changes: 125 additions & 0 deletions sh自动json处理显示版(测试加速版)可能稍微快一点.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash

SOURCE_DIR="img"
OUTPUT_FILE="boot.json"
THREADS=4 # 设置线程数

read -p "请输入名称: " NAME
read -p "请输入模组版本 (参考 0.0.1): " VERSION
read -p "请输入支持的 ModLoader 版本 (参考 2.30 =1.2.3, <2.0.0, >1.0.0, ^1.2.3): " DEP_VERSION
read -p "请输入游戏版本 (可选, 参考 0.0.0.1, 如果不输入则不在 JSON 中添加此字段): " GAME_VERSION

# Function to update the progress bar and display task info
update_progress_bar() {
local task_name=$1
local progress=$2
local total=$3
local current_file=$4

if [ "$total" -eq 0 ]; then
percent=100
else
percent=$(( progress * 100 / total ))
fi

local width=50
local filled=$(( percent * width / 100 ))
local unfilled=$(( width - filled ))
local bar=$(printf "%${filled}s" | tr ' ' '#')
bar+=$(printf "%${unfilled}s" | tr ' ' '.')

# 清除屏幕上的行,确保进度条和文件名分别显示在不同的行
printf "\r\033[K当前文件: %s" "$current_file"
printf "\n\033[K%s [%-${width}s] %d%%" "$task_name" "$bar" "$percent"
}

# 扫描文件和目录并显示进度条
scan_files_and_dirs() {
local files=("$@")
local total_files=${#files[@]}
local processed=0

for file in "${files[@]}"; do
((processed++))
update_progress_bar "扫描文件和目录" "$processed" "$total_files" "$file"
done
}

# 处理图像文件并显示进度条
process_image_files() {
local files=("$@")
local total_files=${#files[@]}
local processed=0
local img_files=()

for file in "${files[@]}"; do
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.gif ]]; then
img_files+=("$file")
fi
done

local total_img_files=${#img_files[@]}
for img_file in "${img_files[@]}"; do
((processed++))
update_progress_bar "保存图像文件" "$processed" "$total_img_files" "$img_file"
echo " \"$img_file\"," >> "$OUTPUT_FILE"
done

# 删除最后一行的逗号并关闭 JSON 数组
sed -i '$ s/,$//' "$OUTPUT_FILE"
}

# 初始化 JSON 输出文件
{
echo "{"
echo ' "name": "'"$NAME"'",'
echo ' "version": "'"$VERSION"'",'
echo ' "scriptFileList_inject_early": [],'
echo ' "scriptFileList_earlyload": [],'
echo ' "scriptFileList_preload": [],'
echo ' "styleFileList": [],'
echo ' "scriptFileList": [],'
echo ' "tweeFileList": [],'
echo ' "imgFileList": ['
} > "$OUTPUT_FILE"

# 扫描所有文件和目录
mapfile -t files < <(find "$SOURCE_DIR" -type d -o -type f)

# 扫描文件和目录
scan_files_and_dirs "${files[@]}"

# 处理图像文件
process_image_files "${files[@]}"

# 关闭 JSON 对象
{
echo ' ],'
echo ' "additionFile": [],'
echo ' "additionBinaryFile": [],'
echo ' "additionDir": [],'
echo ' "addonPlugin": [],'
echo ' "dependenceInfo": ['
echo ' {'
echo ' "modName": "ModLoader",'
echo ' "version": "'"$DEP_VERSION"'"'
echo ' }'
} >> "$OUTPUT_FILE"

# 添加 GameVersion 条件
if [[ -n "$GAME_VERSION" ]]; then
{
echo ' ,'
echo ' {'
echo ' "modName": "GameVersion",'
echo ' "version": "'"$GAME_VERSION"'"'
echo ' }'
} >> "$OUTPUT_FILE"
fi

{
echo ' ]'
echo "}"
} >> "$OUTPUT_FILE"

echo -e "\n目录结构已保存到 $OUTPUT_FILE"
131 changes: 131 additions & 0 deletions sh自动json处理显示版(测试加速版)没修好( ).sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#!/bin/bash

SOURCE_DIR="img"
OUTPUT_FILE="boot.json"
THREADS=8 # 设置线程数

read -p "请输入名称: " NAME
read -p "请输入模组版本 (参考 0.0.1): " VERSION
read -p "请输入支持的 ModLoader 版本 (参考 2.30 =1.2.3, <2.0.0, >1.0.0, ^1.2.3): " DEP_VERSION
read -p "请输入游戏版本 (可选, 参考 0.0.0.1, 如果不输入则不在 JSON 中添加此字段): " GAME_VERSION

# Function to update the progress bar and display task info
update_progress_bar() {
local task_name=$1
local progress=$2
local total=$3
local current_file=$4

if [ "$total" -eq 0 ]; then
percent=100
else
percent=$(( progress * 100 / total ))
fi

local width=50
local filled=$(( percent * width / 100 ))
local unfilled=$(( width - filled ))
local bar=$(printf "%${filled}s" | tr ' ' '#')
bar+=$(printf "%${unfilled}s" | tr ' ' '.')

# 清除屏幕上的行,确保进度条和文件名分别显示在不同的行
printf "\r\033[K当前文件: %s" "$current_file"
printf "\n\033[K%s [%-${width}s] %d%%" "$task_name" "$bar" "$percent"
printf "\n\033[K正在处理: %s" "$task_name"
}

# 扫描文件和目录并显示进度条
scan_files_and_dirs() {
local files=("$@")
local total_files=${#files[@]}
local processed=0

export -f update_progress_bar
export total_files
export processed

echo "${files[@]}" | xargs -n 1 -P $THREADS -I {} bash -c '
file="{}"
((processed++))
update_progress_bar "扫描文件和目录" $processed $total_files "$file"
'
}

# 处理图像文件并显示进度条
process_image_files() {
local files=("$@")
local img_files=()
local total_files=${#files[@]}
local processed=0

for file in "${files[@]}"; do
if [[ "$file" == *.png || "$file" == *.jpg || "$file" == *.gif ]]; then
img_files+=("$file")
fi
done

local total_img_files=${#img_files[@]}

for ((i=0; i<total_img_files; i++)); do
update_progress_bar "保存图像文件" $((i+1)) "$total_img_files" "${img_files[i]}"
done

# 一次性将图像文件写入到 JSON 文件中
echo ' "imgFileList": [' >> "$OUTPUT_FILE"
for img_file in "${img_files[@]}"; do
echo " \"$img_file\"," >> "$OUTPUT_FILE"
done

# 删除最后一行的逗号并关闭 JSON 数组
sed -i '$ s/,$//' "$OUTPUT_FILE"
echo ' ],' >> "$OUTPUT_FILE"
}

# 初始化 JSON 输出文件
{
echo "{"
echo ' "name": "'"$NAME"'",'
echo ' "version": "'"$VERSION"'",'
echo ' "scriptFileList_inject_early": [],'
echo ' "scriptFileList_earlyload": [],'
echo ' "scriptFileList_preload": [],'
echo ' "styleFileList": [],'
echo ' "scriptFileList": [],'
echo ' "tweeFileList": [],'
} > "$OUTPUT_FILE"

# 扫描文件
mapfile -t files < <(find "$SOURCE_DIR" -type d -o -type f)

# 多线程处理文件
scan_files_and_dirs "${files[@]}"
process_image_files "${files[@]}"

# 关闭 JSON 对象
{
echo ' "additionFile": [],'
echo ' "additionBinaryFile": [],'
echo ' "additionDir": [],'
echo ' "addonPlugin": [],'
echo ' "dependenceInfo": ['
echo ' {'
echo ' "modName": "ModLoader",'
echo ' "version": "'"$DEP_VERSION"'"'
echo ' }'
} >> "$OUTPUT_FILE"

# 添加 GameVersion 条件
if [[ -n "$GAME_VERSION" ]]; then
{
echo ' ,'
echo ' {'
echo ' "modName": "GameVersion",'
echo ' "version": "'"$GAME_VERSION"'"'
echo ' }'
} >> "$OUTPUT_FILE"
fi

echo ' ]' >> "$OUTPUT_FILE"
echo "}" >> "$OUTPUT_FILE"

echo -e "\n目录结构已保存到 $OUTPUT_FILE"

0 comments on commit c5a1bf3

Please sign in to comment.