-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmods.lua
More file actions
100 lines (87 loc) · 3.27 KB
/
Copy pathmods.lua
File metadata and controls
100 lines (87 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- 定义路径配置文件路径
local pathConfigFilePath = "./Resources/Server/ServerInfo/path.yml"
-- 加载并解析 path.yml 文件中的 mod list 路径
local function loadModListPath()
local file, err = io.open(pathConfigFilePath, "r")
if not file then
print("Failed to open path config file: " .. pathConfigFilePath .. " Error: " .. err)
return nil
end
-- 逐行读取文件,找到第三行
local lineNumber = 0
local modListPath = ""
for line in file:lines() do
lineNumber = lineNumber + 1
if lineNumber == 3 then
-- 查找 mod list 的值
local pathMatch = string.match(line, "mod list%s*:%s*(.+)")
if pathMatch then
modListPath = pathMatch
break
end
end
end
file:close()
if modListPath ~= "" then
return modListPath
else
print("Failed to find mod list path in path config file.")
return nil
end
end
-- 定义插件的初始化函数
function onInit_ListClientFiles()
-- 定义要遍历的目录路径
local clientDir = "./Resources/Client"
-- 检查目录是否存在
if FS.Exists(clientDir) then
-- 获取目录中的所有文件
local files = FS.ListFiles(clientDir)
-- 获取保存路径
local modListPath = loadModListPath()
if modListPath then
-- 确保目标目录存在
local targetDir = FS.GetParentFolder(modListPath)
if not FS.Exists(targetDir) then
if not FS.CreateDirectory(targetDir) then
print("Failed to create target directory: " .. targetDir)
return
end
end
-- 打开(或创建)目标文件用于写入
local fileHandle, err = io.open(modListPath, "w")
if not fileHandle then
print("Failed to open target file: " .. modListPath .. " Error: " .. err)
return
end
-- 遍历并写入每个文件的名称及大小
for _, file in ipairs(files) do
local filePath = FS.ConcatPaths(clientDir, file)
-- 获取文件大小
local fileHandleRead = io.open(filePath, "rb")
if fileHandleRead then
fileHandleRead:seek("end")
local fileSize = fileHandleRead:seek()
fileHandleRead:close()
-- 转换文件大小为MB
local fileSizeMB = fileSize / (1024 * 1024)
-- 格式化为小数点后两位
fileSizeMB = string.format("%.2f", fileSizeMB)
-- 写入到目标文件
fileHandle:write(file .. ":" .. fileSizeMB .. " MB\n")
else
print("Failed to open file: " .. filePath)
end
end
-- 关闭文件
fileHandle:close()
print("Mod list and sizes saved")
else
print("Failed to load mod list path from path.yml")
end
else
print("Directory does not exist: " .. clientDir)
end
end
-- 注册服务器初始化事件
MP.RegisterEvent("onInit", "onInit_ListClientFiles")