Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
514 lines (476 loc) · 17.8 KB
/
Copy pathmain.lua
File metadata and controls
514 lines (476 loc) · 17.8 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
-- main.lua
-- 插件管理大师 v3.9 (最终精简版)
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local UIManager = require("ui/uimanager")
local InfoMessage = require("ui/widget/infomessage")
local ConfirmBox = require("ui/widget/confirmbox")
local PathChooser = require("ui/widget/pathchooser")
local DataStorage = require("datastorage")
local logger = require("logger")
local lfs = require("libs/libkoreader-lfs")
local gettext = require("gettext")
-- 使用全局 G_reader_settings(与 KOReader 内部一致)
local G_reader_settings = _G.G_reader_settings
if not G_reader_settings then
G_reader_settings = require("luasettings"):open(DataStorage:getSettingsDir() .. "/reader.lua")
end
-- 语言检测
local current_lang = gettext.current_lang or ""
local is_chinese = current_lang:match("^zh") and true or false
-- 翻译表
local LANG_TABLE = {
["Plugin Manager"] = "插件管理大师",
["Install / Update Plugin"] = "安装/更新插件",
["Uninstall Plugin"] = "删除插件",
["About"] = "关于",
["(No plugins installed)"] = "(没有安装任何插件)",
[" [Disabled]"] = " [禁用]",
["Are you sure you want to uninstall plugin \"%s\"?\nThis will permanently delete the plugin folder."] = "确定要删除插件 \"%s\" 吗?\n此操作将永久删除插件文件夹。",
["Uninstall"] = "删除",
["Plugin uninstalled. Please restart KOReader for changes to take effect."] = "插件已删除,请重启 KOReader 后生效",
["Uninstall failed: "] = "删除失败:",
["Please select a .zip file"] = "请选择一个 .zip 文件",
["Unable to load DataStorage"] = "无法加载 DataStorage",
["Unable to load lfs"] = "无法加载 lfs",
["File does not exist: "] = "文件不存在:",
["Extracting..."] = "正在解压...",
["Extract failed: "] = "解压失败:",
["No valid .koplugin folder found.\n\nPlease ensure the ZIP contains a folder ending with .koplugin (may be nested)."] = "未找到有效的 .koplugin 文件夹\n\n请确保 ZIP 包内包含一个以 .koplugin 结尾的文件夹(可能嵌套在子目录中)。",
["The following plugins already exist and will be overwritten:"] = "以下插件已存在,将被覆盖更新:",
["Continue?"] = "是否继续?",
["Continue"] = "继续",
["Installation complete. %d plugin(s) installed/updated. Please restart KOReader to load them."] = "安装完成,共安装/更新 %d 个插件,请重启 KOReader 以加载",
["Unable to open ZIP file"] = "无法打开 ZIP 文件",
["Write file failed: "] = "写入文件失败:",
["unzip command failed (return code "] = "unzip 命令失败 (返回码 ",
["Error: "] = "错误:",
["Version: "] = "版本:",
["Author: "] = "作者:",
["Simple way to add/updaet plugin from zip or delete."] = "更简单的安装/更新/删除插件.",
}
local function _(key, ...)
local text = is_chinese and LANG_TABLE[key] or key
if text == nil then text = key end
if select("#", ...) > 0 then
return string.format(text, ...)
end
return text
end
local _T = _
logger.info("[+] Plugin Manager loaded [+]")
local PluginManager = WidgetContainer:extend{
name = "plugin_manager",
}
-- 生成可能的键名(优先短名)
local function get_plugin_keys(short_name, folder_name)
local keys = {}
if short_name and short_name ~= "" then
table.insert(keys, short_name)
end
if folder_name and folder_name ~= "" and folder_name ~= short_name then
table.insert(keys, folder_name)
end
return keys
end
-- 从禁用列表中移除插件
local function remove_from_disabled(short_name, folder_name)
local disabled = G_reader_settings:readSetting("plugins_disabled") or {}
local changed = false
local keys = get_plugin_keys(short_name, folder_name)
for _, key in ipairs(keys) do
if disabled[key] then
disabled[key] = nil
changed = true
end
end
if changed then
G_reader_settings:saveSetting("plugins_disabled", disabled)
G_reader_settings:flush()
end
return changed
end
-- 检查插件是否被禁用
local function is_plugin_disabled(short_name, folder_name)
local disabled = G_reader_settings:readSetting("plugins_disabled") or {}
for _, key in ipairs(get_plugin_keys(short_name, folder_name)) do
if disabled[key] then
return true
end
end
return false
end
function PluginManager:init()
self.ui.menu:registerToMainMenu(self)
self.plugins_dir = DataStorage:getDataDir() .. "/plugins/"
self.last_path = DataStorage:getDataDir() or "/mnt"
self.plugins = {}
self:loadPluginMeta()
local info = debug.getinfo(1)
self.self_path = info.source:sub(2):match("(.*/)")
end
function PluginManager:loadPluginMeta()
local meta_path = debug.getinfo(1).source:sub(2):match("(.*/)") .. "_meta.lua"
local ok, meta = pcall(dofile, meta_path)
if ok and type(meta) == "table" then
self.plugin_name = meta.fullname and _T(meta.fullname) or (meta.name and _T(meta.name)) or _T("Plugin Manager Pro")
self.plugin_version = meta.version or "3.9"
self.plugin_author = meta.author or "KOReader Community"
self.plugin_description = meta.description and _T(meta.description) or _T("Simple way to add/updaet plugin from zip or delete.")
else
self.plugin_name = _T("Plugin Manager Pro")
self.plugin_version = "3.9"
self.plugin_author = "KOReader Community"
self.plugin_description = _T("Simple way to add/updaet plugin from zip or delete.")
end
end
function PluginManager:addToMainMenu(menu_items)
menu_items.plugin_manager = {
text = self.plugin_name,
sorting_hint = "tools",
sub_item_table_func = function()
return self:getMainMenuItems()
end,
}
end
function PluginManager:getMainMenuItems()
return {
{
text = _T("Install / Update Plugin"),
callback = function()
self:chooseZipFile()
end,
},
{
text = _T("Uninstall Plugin"),
separator = true,
sub_item_table_func = function()
return self:getDeletePluginList()
end,
},
{
text = _T("About"),
separator = true,
callback = function()
self:showAbout()
end,
},
}
end
function PluginManager:showAbout()
local info = string.format(
"%s\n%s%s\n\n%s%s\n%s",
self.plugin_name,
_T("Version: "), self.plugin_version,
_T("Author: "), self.plugin_author,
self.plugin_description
)
UIManager:show(InfoMessage:new{
text = info,
timeout = 8,
})
end
function PluginManager:getDeletePluginList()
self:scanPlugins()
local items = {}
if #self.plugins == 0 then
table.insert(items, {
text = _T("(No plugins installed)"),
enabled = false,
})
else
for _, plugin in ipairs(self.plugins) do
local display = plugin.display_name
if plugin.disabled then
display = display .. _T(" [Disabled]")
end
table.insert(items, {
text = display,
callback = function()
self:confirmDeletePlugin(plugin)
end,
})
end
end
return items
end
function PluginManager:confirmDeletePlugin(plugin)
local msg = _T("Are you sure you want to uninstall plugin \"%s\"?\nThis will permanently delete the plugin folder.", plugin.display_name)
UIManager:show(ConfirmBox:new{
text = msg,
ok_text = _T("Uninstall"),
ok_callback = function()
self:deletePlugin(plugin)
end,
})
end
function PluginManager:deletePlugin(plugin)
remove_from_disabled(plugin.short_name, plugin.name)
local ok, err = self:removeDirectory(plugin.path)
if ok then
UIManager:show(InfoMessage:new{
text = _T("Plugin uninstalled. Please restart KOReader for changes to take effect."),
timeout = 4,
})
self:scanPlugins()
else
UIManager:show(InfoMessage:new{
text = _T("Uninstall failed: ") .. tostring(err),
timeout = 5,
})
end
end
function PluginManager:scanPlugins()
self.plugins = {}
local function scan_dir(pattern)
if not lfs.attributes(self.plugins_dir, "mode") then
return
end
for file in lfs.dir(self.plugins_dir) do
if file ~= "." and file ~= ".." then
local full_path = self.plugins_dir .. file
if full_path == self.self_path then
goto continue
end
local attr = lfs.attributes(full_path, "mode")
if attr == "directory" and file:match(pattern) then
local plugin_info = {
path = full_path,
name = file,
short_name = nil,
display_name = file,
disabled = false,
}
local meta_path = full_path .. "/_meta.lua"
local meta_ok, meta = pcall(dofile, meta_path)
if meta_ok and type(meta) == "table" then
plugin_info.display_name = meta.fullname and _T(meta.fullname) or meta.name or file
plugin_info.short_name = meta.name or file:gsub("%.koplugin$", "")
else
plugin_info.short_name = file:gsub("%.koplugin$", "")
end
plugin_info.disabled = is_plugin_disabled(plugin_info.short_name, plugin_info.name)
table.insert(self.plugins, plugin_info)
end
::continue::
end
end
end
scan_dir("%.koplugin$")
table.sort(self.plugins, function(a, b) return a.display_name < b.display_name end)
end
function PluginManager:findPluginFolders(dir)
local results = {}
local function scan(current_path)
for file in lfs.dir(current_path) do
if file ~= "." and file ~= ".." then
local full = current_path .. "/" .. file
local attr = lfs.attributes(full, "mode")
if attr == "directory" then
if file:match("%.koplugin$") then
table.insert(results, full)
else
scan(full)
end
end
end
end
end
scan(dir)
return results
end
function PluginManager:chooseZipFile()
local path_chooser = PathChooser:new{
select_directory = false,
path = self.last_path,
onConfirm = function(file_path)
if not file_path:match("%.zip$") then
UIManager:show(InfoMessage:new{
text = _T("Please select a .zip file"),
timeout = 3,
})
return
end
self.last_path = file_path:match("(.*)/")
if self.last_path == "" then self.last_path = "/" end
logger.info("[+] Selected ZIP: " .. file_path .. " [+]")
self:installPlugin(file_path)
end,
}
UIManager:show(path_chooser)
end
function PluginManager:installPlugin(zip_path)
local ok, DataStorage = pcall(require, "datastorage")
if not ok then
self:showError(_T("Unable to load DataStorage"))
return
end
local ok, lfs = pcall(require, "libs/libkoreader-lfs")
if not ok then
self:showError(_T("Unable to load lfs"))
return
end
if not lfs.attributes(zip_path, "mode") then
self:showError(_T("File does not exist: ") .. zip_path)
return
end
local data_dir = DataStorage:getDataDir() or "."
local plugins_dir = data_dir .. "/plugins/"
local temp_dir = data_dir .. "/plugin_install_temp/"
if lfs.attributes(plugins_dir, "mode") ~= "directory" then
lfs.mkdir(plugins_dir)
end
if lfs.attributes(temp_dir, "mode") == "directory" then
self:removeDirectory(temp_dir)
end
lfs.mkdir(temp_dir)
self:showMessage(_T("Extracting..."), 2)
local ok, err = self:extractZip(zip_path, temp_dir)
if not ok then
self:showError(_T("Extract failed: ") .. tostring(err))
self:cleanup(temp_dir)
return
end
local plugin_folders = self:findPluginFolders(temp_dir)
if #plugin_folders == 0 then
self:showError(_T("No valid .koplugin folder found.\n\nPlease ensure the ZIP contains a folder ending with .koplugin (may be nested)."))
self:cleanup(temp_dir)
return
end
local to_install = {}
local existing_plugins = {}
for _, src in ipairs(plugin_folders) do
local folder_name = src:match("([^/]+)$")
local dest = plugins_dir .. folder_name
if lfs.attributes(dest, "mode") == "directory" then
table.insert(existing_plugins, folder_name)
end
table.insert(to_install, {src = src, name = folder_name, dest = dest})
end
if #existing_plugins > 0 then
local msg = _T("The following plugins already exist and will be overwritten:") .. "\n\n" ..
table.concat(existing_plugins, "\n") .. "\n\n" ..
_T("Continue?")
local confirm = ConfirmBox:new{
text = msg,
ok_text = _T("Continue"),
ok_callback = function()
self:doInstallPlugins(to_install, plugins_dir, temp_dir)
end,
}
UIManager:show(confirm)
else
self:doInstallPlugins(to_install, plugins_dir, temp_dir)
end
end
function PluginManager:doInstallPlugins(install_list, plugins_dir, temp_dir)
local installed_count = 0
local installed_names = {}
for _, item in ipairs(install_list) do
local dest = item.dest
if lfs.attributes(dest, "mode") == "directory" then
self:removeDirectory(dest)
end
os.rename(item.src, dest)
installed_count = installed_count + 1
table.insert(installed_names, item.name)
end
-- 启用插件:从禁用列表中移除
for _, folder_name in ipairs(installed_names) do
local short_name = folder_name:gsub("%.koplugin$", "")
remove_from_disabled(short_name, folder_name)
end
local msg = _T("Installation complete. %d plugin(s) installed/updated. Please restart KOReader to load them.", installed_count)
self:showMessage(msg, 4)
self:cleanup(temp_dir)
self:scanPlugins()
end
function PluginManager:extractZip(zip_path, dest_dir)
local lfs = require("libs/libkoreader-lfs")
local zip = nil
local ok = pcall(function()
zip = require("libs/libkoreader-zip")
end)
if not ok then
pcall(function()
zip = require("lua-zip")
end)
end
if zip and zip.open then
local zfile, err = zip.open(zip_path)
if not zfile then
return false, err or _T("Unable to open ZIP file")
end
for entry in zfile:entries() do
local name = entry:get_name()
if name:sub(-1) == "/" then
local dirpath = dest_dir .. name
pcall(lfs.mkdir, dirpath)
else
local filepath = dest_dir .. name
local dir = filepath:match("(.*)/")
if dir then
pcall(lfs.mkdir, dir)
end
local content = entry:get_content()
if content then
local f, err = io.open(filepath, "wb")
if not f then
zfile:close()
return false, _T("Write file failed: ") .. filepath
end
f:write(content)
f:close()
end
end
end
zfile:close()
return true
else
local cmd = string.format('unzip -q "%s" -d "%s"', zip_path, dest_dir)
local ret = os.execute(cmd)
if ret == 0 then
return true
else
return false, _T("unzip command failed (return code ") .. ret .. ")"
end
end
end
function PluginManager:removeDirectory(path)
local lfs = require("libs/libkoreader-lfs")
local function removeDir(dir)
for f in lfs.dir(dir) do
if f ~= "." and f ~= ".." then
local full = dir .. "/" .. f
if lfs.attributes(full, "mode") == "directory" then
removeDir(full)
else
os.remove(full)
end
end
end
os.remove(dir)
end
local ok, err = pcall(removeDir, path)
return ok, err
end
function PluginManager:cleanup(dir)
local lfs = require("libs/libkoreader-lfs")
if lfs.attributes(dir, "mode") == "directory" then
self:removeDirectory(dir)
end
end
function PluginManager:showMessage(text, timeout)
timeout = timeout or 3
UIManager:show(InfoMessage:new{
text = text,
timeout = timeout,
})
end
function PluginManager:showError(text)
logger.err("[-] ERROR: " .. text)
UIManager:show(InfoMessage:new{
text = _T("Error: ") .. text,
timeout = 5,
})
end
return PluginManager