module-loader.tin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 模块名称:模块加载器
  4. 模块说明:本文件属于框架代码的一部分,不建议修改。如有需求请在 GitHub 发 issue 或者 PR
  5. 版权声明:本文件属于 PaoTin++ 的一部分
  6. ===========
  7. PaoTin++ © 2020~2024 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
  8. 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
  9. ===========
  10. */
  11. load-file {framework/class.tin};
  12. class.open module-loader;
  13. #var xtt-modules {};
  14. #var xtt-modConfig {};
  15. #alias {LM} {load-module};
  16. #alias {load-module} {
  17. #local moduleName {%1};
  18. #local moduleConfig {%2};
  19. #if { "$moduleName" == "" } {
  20. warnLog 用法: load-module <模块全限定名称> <模块参数>;
  21. #return;
  22. };
  23. #nop 检查有没有加载成功。;
  24. #local prefix {$moduleName};
  25. #replace prefix {/} {_};
  26. #if { "${${prefix}-loaded}" == "true" } {
  27. infoLog 模块 $moduleName 已载入。;
  28. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "false" } {
  29. infoLog 但是该模块已经被禁用,如果你想要重新启用,请使用「<129>enable-module %1<299>」。;
  30. };
  31. #return;
  32. };
  33. #local modulePath {};
  34. #foreach {$moduleName;$moduleName/__init__;$moduleName/__main__} {modulePath} {
  35. class.open {load-module-$moduleName};
  36. #line sub var #action {^#READ {{plugins/($modulePath)\.tin}}: FILE NOT FOUND.$} {
  37. class.kill {load-module-$moduleName};
  38. #var {${prefix}-loaded} {false};
  39. #line gag;
  40. };
  41. class.close {load-module-$moduleName};
  42. class.open module-loader;
  43. #var {${prefix}-loaded} {true};
  44. class.close module-loader;
  45. class.open {$moduleName};
  46. #var {$prefix} {};
  47. class.close {$moduleName};
  48. #format modulePath {plugins/%s.tin} {$modulePath};
  49. #line local {
  50. #nop 模块名称;
  51. #local MODULE {$moduleName};
  52. #nop 模块路径;
  53. #local PATH {$modulePath};
  54. #nop 模块中触发器的默认 ID;
  55. #local ID {%!{(?:|id=$moduleName)}};
  56. #nop 文本开始;
  57. #local {B} {^{[> ]*}};
  58. #nop 文本结束;
  59. #local {E} {%!{(?:|id=$moduleName)}$};
  60. #nop 颜色触发中的文本结束;
  61. #local {CE} {%!c$E};
  62. class.read {$moduleName} {$modulePath};
  63. };
  64. #if { "${${prefix}-loaded}" == "true" } {
  65. class.kill {load-module-$moduleName};
  66. init-module {$moduleName} {$modulePath} {$moduleConfig};
  67. #return;
  68. };
  69. };
  70. errLog 模块 $moduleName 加载失败。;
  71. #return;
  72. };
  73. #alias {init-module} {
  74. #local moduleName {%1};
  75. #local modulePath {%2};
  76. #local modConfig {%3};
  77. #local prefix {$moduleName};
  78. #replace prefix {/} {_};
  79. #nop 检查有没有加载成功。;
  80. #if { "${${prefix}-loaded}" != "true" } {
  81. #return;
  82. };
  83. #nop 检查有没有初始化过;
  84. #if { "$xtt-modules[$moduleName]" != "" } {
  85. #return;
  86. };
  87. #local metaVarName {};
  88. #format metaVarName {%s[META]} {$prefix};
  89. #local metaInfo {
  90. ${$metaVarName}
  91. {PATH}{$modulePath}
  92. };
  93. #if { "${$metaVarName}" == "" } {
  94. #local metaInfo[TYPE] {弱模块};
  95. };
  96. #elseif @existsAlias{${moduleName}.Run} {
  97. #local metaInfo[TYPE] {纯模块};
  98. };
  99. #else {
  100. #local metaInfo[TYPE] {混合模块};
  101. };
  102. #var {xtt-modules[$moduleName]} {$metaInfo};
  103. #if { "$metaInfo[TYPE]" != "弱模块" } {
  104. #if { "$metaInfo[CONFIG]" != "" } {
  105. #foreach {*metaInfo[CONFIG][]} {key} {
  106. class.open {$moduleName};
  107. #var {${prefix}[config][$key]} {$modConfig[$key]};
  108. class.close {$moduleName};
  109. class.open module-loader;
  110. #var xtt-modConfig[$moduleName][$key] {$modConfig[$key]};
  111. class.close module-loader;
  112. };
  113. };
  114. #local initFunc {${prefix}.Init};
  115. #if @existsFunction{$initFunc} {
  116. class.open {$moduleName};
  117. #local ok @$initFunc{};
  118. class.close {$moduleName};
  119. #if { "$ok" == "false" } {
  120. errLog 模块 $moduleName 加载失败。;
  121. class.kill {$moduleName};
  122. #return;
  123. };
  124. };
  125. };
  126. okLog 模块 $moduleName 已初始化成功。;
  127. #if { "$metaInfo[TYPE]" == "纯模块" } {
  128. okLog 本模块是纯模块,请使用 ${moduleName}.Start/Stop 来控制启动停止。;
  129. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "" } {
  130. disable-module {$moduleName} {true};
  131. };
  132. make-starter {$moduleName} {$prefix};
  133. };
  134. };
  135. #alias {KM} {kill-module};
  136. #alias {kill-module} {
  137. #local moduleName {%1};
  138. #if { "$moduleName" == "" } {
  139. warnLog 用法: kill-module <模块全限定名称>;
  140. #return;
  141. };
  142. infoLog 卸载 $moduleName。;
  143. #local modules {};
  144. #local name {};
  145. #foreach {*{xtt-modules[]}} {name} {
  146. #if { "$name" != "$moduleName" } {
  147. #format modules {%s%s} {$modules} {{$name}{$xtt-modules[$name]}};
  148. };
  149. };
  150. #var {xtt-modules} {$modules};
  151. #unvar xtt.module-doc[$moduleName];
  152. #local prefix {$moduleName};
  153. #replace prefix {/} {_};
  154. #unvar {${prefix}-loaded};
  155. class.kill {$moduleName};
  156. };
  157. #alias {RLM} {reload-module};
  158. #alias {reload-module} {
  159. #local moduleName {%1};
  160. #if { "$moduleName" == "" } {
  161. warnLog 用法: reload-module <模块全限定名称>;
  162. #return;
  163. };
  164. kill-module {%1};
  165. load-module {%1} {%2};
  166. };
  167. #alias {DM} {disable-module};
  168. #alias {disable-module} {
  169. #local moduleName {%1};
  170. #local dontCallStop {%2};
  171. #if { "$xtt-modules[$moduleName][TYPE]" == "纯模块" && "$dontCallStop" != "true" } {
  172. ${moduleName}.Stop;
  173. #return;
  174. };
  175. #if { "$moduleName" == "" } {
  176. warnLog 用法: disable-module <模块全限定名称>;
  177. #return;
  178. };
  179. #if { "$xtt-modules[$moduleName]" == "" } {
  180. errLog 模块 $moduleName 尚未加载模块。;
  181. #return;
  182. };
  183. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "false" } {
  184. warnLog 模块 $moduleName 已经禁用。;
  185. #return;
  186. };
  187. #var {xtt-modConfig[$moduleName][ENABLE]} {false};
  188. class.disable {$moduleName};
  189. infoLog <129>模块 $moduleName 已经禁用。<299>;
  190. };
  191. #alias {EM} {enable-module};
  192. #alias {enable-module} {
  193. #local moduleName {%1};
  194. #local dontCallStart {%2};
  195. #if { "$xtt-modules[$moduleName][TYPE]" == "纯模块" && "$dontCallStart" != "true" } {
  196. ${moduleName}.Start;
  197. #return;
  198. };
  199. #if { "$moduleName" == "" } {
  200. warnLog 用法: enable-module <模块全限定名称>;
  201. #return;
  202. };
  203. #if { "$xtt-modules[$moduleName]" == "" } {
  204. errLog 模块 $moduleName 尚未加载模块。;
  205. #return;
  206. };
  207. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "true" } {
  208. warnLog 模块 $moduleName 已经启用。;
  209. #return;
  210. };
  211. #var {xtt-modConfig[$moduleName][ENABLE]} {true};
  212. class.enable {$moduleName};
  213. infoLog <129>模块 $moduleName 已经启用。<299>;
  214. };
  215. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  216. #alias {MODS} {list-modules};
  217. #alias {list-modules} {
  218. #local pattern {%1};
  219. #local format { %c%-40s %-8s %-8s %-4s %-6s %-6s %s};
  220. #echo {%c%h} {cyan} { 已加载模块 -- 请用 MOD <模块名> 来查看详细内容 };
  221. #echo {$format} {cyan} {模块名称} {作者} {类型} {状态} {事件} {配置项} {说明};
  222. #echo {$format} {cyan} {-----------------} {-------} {-------} {----} {----} {----} {------------------------------------};
  223. #local name {};
  224. #local count {0};
  225. #foreach {*{xtt-modules[]}} {name} {
  226. #local metaInfo {$xtt-modules[$name]};
  227. #local cnName {$metaInfo[NAME]};
  228. #local author {$metaInfo[AUTHOR]};
  229. #local type {$metaInfo[TYPE]};
  230. #local desc {$metaInfo[DESC]};
  231. #local config {$metaInfo[CONFIG]};
  232. #local enable {<129>开启<299>};
  233. #local hasConfig {有};
  234. #local hasEvents {无};
  235. #if { "$pattern" != "" } {
  236. #if { "$name" != "%*$pattern%*"
  237. && "$cnName" != "%*$pattern%*"
  238. && "$author" != "%*$pattern%*"
  239. && "$desc" != "%*$pattern%*"
  240. } {
  241. #continue;
  242. };
  243. };
  244. #math count {$count + 1};
  245. #if { "$config" == "" } {
  246. #local hasConfig {无};
  247. };
  248. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$name"}}};
  249. #if { &event[] > 0 } {
  250. #local hasEvents {<169>&event[]个<299>};
  251. };
  252. #if { "$type" == "纯模块" } {
  253. #local type {<129>纯模块<299>};
  254. };
  255. #elseif { "$type" == "混合模块" } {
  256. #local type {<139>混合模块<299>};
  257. };
  258. #if { "$xtt-modConfig[$name][ENABLE]" == "false" } {
  259. #local enable {<119>禁用<299>};
  260. };
  261. #echo {$format} {white} {@genModuleLink{$name;MOD}} {$author} {$type} {$enable} {$hasEvents} {$hasConfig} {$desc};
  262. };
  263. #if { $count > 10 } {
  264. #echo {%c%h} {cyan} { 共列出 $count 项模块信息,若嫌内容过多,你可以用 MOD <关键字> 来模糊查询以缩小范围 };
  265. };
  266. #else {
  267. #echo {%c%h} {cyan} { 共列出 $count 项模块信息 };
  268. };
  269. };
  270. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  271. #alias {MOD} {look-module %0; #buffer end};
  272. #alias {look-module} {
  273. #local moduleName {%1};
  274. #local metaInfo {$xtt-modules[$moduleName]};
  275. #local cnName {$metaInfo[NAME]};
  276. #local path {$metaInfo[PATH]};
  277. #local type {$metaInfo[TYPE]};
  278. #local enable {$xtt-modConfig[$moduleName][ENABLE]};
  279. #local desc {$metaInfo[DESC]};
  280. #local author {$metaInfo[AUTHOR]};
  281. #local note {$metaInfo[NOTE]};
  282. #local config {$metaInfo[CONFIG]};
  283. #if { "$moduleName" == "" } {
  284. warnLog 用法: look-module <模块全限定名称>;
  285. #return;
  286. };
  287. #if { "$xtt-modules[$moduleName]" == "" } {
  288. errLog 模块 $moduleName 尚未加载。;
  289. #return;
  290. };
  291. #if { "$enable" == "false" } {
  292. #local enable {<119>禁用<299>};
  293. };
  294. #else {
  295. #local enable {<129>开启<299>};
  296. };
  297. #local moduleLink {@genModuleLink{$moduleName;HELP}};
  298. #echo {%c%h} {cyan} { $moduleLink<269> };
  299. #echo {%s} { <269>名称:<299> $moduleLink <269>类型:<299> $type <269>状态:<299> $enable <269>作者:<299> $author};
  300. #echo {%s} { <269>脚本路径:<299> $path};
  301. #if { "$type" == "弱模块" } {
  302. #echo {%c%h} {cyan} { 更多信息仅纯模块可见 };
  303. #return;
  304. };
  305. #echo {%s} { <269>说明:<299> $desc};
  306. #echo {%s} { <269>备注:<299> $note};
  307. #if { "$type" == "纯模块" } {
  308. #echo {%s} { <269>接口:<299> 你可以通过 $moduleName\.Start/Stop 命令来启动/停止模块。};
  309. };
  310. #local format { %c%-20s %-20s %s};
  311. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$moduleName"}}};
  312. #if { &event[] > 0 } {
  313. #local name {};
  314. #echo {%s} { <269>提供事件列表:<299>};
  315. #echo {$format} {cyan} {事件名称} {事件类型} {说明};
  316. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  317. #foreach {*event[]} {name} {
  318. #local type {$event[$name][type]};
  319. #local desc {$event[$name][desc]};
  320. #echo {$format} {light cyan} {$name} {$type} {$desc};
  321. };
  322. };
  323. #if { &config[] > 0 } {
  324. #echo {%c%h} {cyan} {};
  325. #local count {0};
  326. #echo {%s} { <269>配置参数表:<299>};
  327. #echo {$format} {cyan} {配置项} {当前值} {作用};
  328. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  329. #local key {};
  330. #foreach {*config[]} {key} {
  331. #math count {$count + 1};
  332. #local value {$xtt-modConfig[$moduleName][$key]};
  333. #local desc {$config[$key]};
  334. #local prefix {$moduleName};
  335. #replace prefix {/} {_};
  336. #echo {$format} {light cyan} {$key} {{$value}} {$desc};
  337. };
  338. #echo {%c%h} {cyan} { 共列出 $count 项配置信息 };
  339. };
  340. #echo {};
  341. #echo {%c%h} {cyan} { <169>接下来进一步调查该模块的变量、别名和触发情况<069> };
  342. #echo {};
  343. xtt.ListVar $moduleName; #echo {};
  344. xtt.ListFunc $moduleName; #echo {};
  345. xtt.ListAlias $moduleName; #echo {};
  346. xtt.ListAction $moduleName; #echo {};
  347. };
  348. #alias {make-starter} {
  349. #local moduleName {%1};
  350. #local prefix {%2};
  351. class.open module-loader;
  352. #tab {%1.Start};
  353. #alias {%1.Start} {
  354. #if { "$xtt-modConfig[%1][ENABLE]" == "false" } {
  355. enable-module {%1} {true};
  356. };
  357. #local config {%%0};
  358. #local metaInfo {${%2[META]}};
  359. #foreach {*metaInfo[config][]} {key} {
  360. class.open {%1};
  361. #format {%2[config][$key]} {%s} {$config[$key]};
  362. class.close {%1};
  363. };
  364. #format xtt-modConfig {%s%s} {$xtt-modConfig} {{%1}{${%2[config]}}};
  365. class.do {%1} %1.Run;
  366. };
  367. #tab {%1.Stop};
  368. #alias {%1.Stop} {
  369. #local reason {%%1};
  370. #if { "$reason" == "" } {
  371. #format reason {人为操作};
  372. };
  373. infoLog <169>由于<139>$reason<169>,%1 模块停止运行。<299>;
  374. #if @existsAlias{%1.Pause} {
  375. %1.Pause;
  376. };
  377. #if { "$xtt-modConfig[%1][ENABLE]" == "true" } {
  378. disable-module {%1} {true};
  379. };
  380. };
  381. class.close module-loader;
  382. };
  383. #alias {LL} {load-lib};
  384. #alias {load-lib} {load-module lib/%1};
  385. #alias {RLL} {reload-lib};
  386. #alias {reload-lib} {
  387. kill-module {lib/%1};
  388. load-lib {%1} {%2};
  389. };
  390. #nop 给出一个用于屏幕显示的模块名称。会自动根据是否有文档点亮 MSLP 链接;
  391. #func {genModuleLink} {
  392. #local {name} {%1};
  393. #local {cmd} {%2};
  394. #if { "$name" == "" } {
  395. #return {};
  396. };
  397. #if { &xtt-modules[$name][] == 0 } {
  398. #return {$name};
  399. };
  400. #local cnName {$xtt-modules[$name][NAME]};
  401. #if { "$cnName" != "" } {
  402. #local cnName { (<169>$cnName<299>)};
  403. };
  404. #if { "$cmd" != "{MOD|HELP}" } {
  405. #return {$name$cnName};
  406. };
  407. #if { "$cmd" == "HELP" && &xtt.module-doc[$name][] > 0 } {
  408. #local name {@mslp.Help{$name;$name}};
  409. #local name {<140>$name<299>$cnName};
  410. };
  411. #else {
  412. #local name {@mslp.Module{$name;$name}};
  413. #local name {<149>$name<299>$cnName};
  414. };
  415. #return {$name};
  416. };
  417. class.close module-loader;