module-loader.tin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. #foreach {*{xtt-modules[]}} {name} {
  145. #if { "$name" != "$moduleName" } {
  146. #format modules {%s%s} {$modules} {{$name}{$xtt-modules[$name]}};
  147. };
  148. };
  149. #var {xtt-modules} {$modules};
  150. #unvar xtt.module-doc[$moduleName];
  151. #local prefix {$moduleName};
  152. #replace prefix {/} {_};
  153. #unvar {${prefix}-loaded};
  154. class.kill {$moduleName};
  155. };
  156. #alias {RLM} {reload-module};
  157. #alias {reload-module} {
  158. #local moduleName {%1};
  159. #if { "$moduleName" == "" } {
  160. warnLog 用法: reload-module <模块全限定名称>;
  161. #return;
  162. };
  163. kill-module {%1};
  164. load-module {%1} {%2};
  165. };
  166. #alias {DM} {disable-module};
  167. #alias {disable-module} {
  168. #local moduleName {%1};
  169. #local dontCallStop {%2};
  170. #if { "$xtt-modules[$moduleName][TYPE]" == "纯模块" && "$dontCallStop" != "true" } {
  171. ${moduleName}.Stop;
  172. #return;
  173. };
  174. #if { "$moduleName" == "" } {
  175. warnLog 用法: disable-module <模块全限定名称>;
  176. #return;
  177. };
  178. #if { "$xtt-modules[$moduleName]" == "" } {
  179. errLog 模块 $moduleName 尚未加载模块。;
  180. #return;
  181. };
  182. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "false" } {
  183. warnLog 模块 $moduleName 已经禁用。;
  184. #return;
  185. };
  186. #var {xtt-modConfig[$moduleName][ENABLE]} {false};
  187. class.disable {$moduleName};
  188. infoLog <129>模块 $moduleName 已经禁用。<299>;
  189. };
  190. #alias {EM} {enable-module};
  191. #alias {enable-module} {
  192. #local moduleName {%1};
  193. #local dontCallStart {%2};
  194. #if { "$xtt-modules[$moduleName][TYPE]" == "纯模块" && "$dontCallStart" != "true" } {
  195. ${moduleName}.Start;
  196. #return;
  197. };
  198. #if { "$moduleName" == "" } {
  199. warnLog 用法: enable-module <模块全限定名称>;
  200. #return;
  201. };
  202. #if { "$xtt-modules[$moduleName]" == "" } {
  203. errLog 模块 $moduleName 尚未加载模块。;
  204. #return;
  205. };
  206. #if { "$xtt-modConfig[$moduleName][ENABLE]" == "true" } {
  207. warnLog 模块 $moduleName 已经启用。;
  208. #return;
  209. };
  210. #var {xtt-modConfig[$moduleName][ENABLE]} {true};
  211. class.enable {$moduleName};
  212. infoLog <129>模块 $moduleName 已经启用。<299>;
  213. };
  214. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  215. #alias {MODS} {list-modules};
  216. #alias {list-modules} {
  217. #local pattern {%1};
  218. #local format { %c%-40s %-8s %-8s %-4s %-6s %-6s %s};
  219. #echo {%c%h} {cyan} { 已加载模块 -- 请用 MOD <模块名> 来查看详细内容 };
  220. #echo {$format} {cyan} {模块名称} {作者} {类型} {状态} {事件} {配置项} {说明};
  221. #echo {$format} {cyan} {-----------------} {-------} {-------} {----} {----} {----} {------------------------------------};
  222. #local name {};
  223. #local count {0};
  224. #foreach {*{xtt-modules[]}} {name} {
  225. #local metaInfo {$xtt-modules[$name]};
  226. #local cnName {$metaInfo[NAME]};
  227. #local author {$metaInfo[AUTHOR]};
  228. #local type {$metaInfo[TYPE]};
  229. #local desc {$metaInfo[DESC]};
  230. #local config {$metaInfo[CONFIG]};
  231. #local enable {<129>开启<299>};
  232. #local hasConfig {有};
  233. #local hasEvents {无};
  234. #if { "$pattern" != "" } {
  235. #if { "$name" != "%*$pattern%*"
  236. && "$cnName" != "%*$pattern%*"
  237. && "$author" != "%*$pattern%*"
  238. && "$desc" != "%*$pattern%*"
  239. } {
  240. #continue;
  241. };
  242. };
  243. #math count {$count + 1};
  244. #if { "$config" == "" } {
  245. #local hasConfig {无};
  246. };
  247. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$name"}}};
  248. #if { &event[] > 0 } {
  249. #local hasEvents {<169>&event[]个<299>};
  250. };
  251. #if { "$type" == "纯模块" } {
  252. #local type {<129>纯模块<299>};
  253. };
  254. #elseif { "$type" == "混合模块" } {
  255. #local type {<139>混合模块<299>};
  256. };
  257. #if { "$xtt-modConfig[$name][ENABLE]" == "false" } {
  258. #local enable {<119>禁用<299>};
  259. };
  260. #echo {$format} {white} {@genModuleLink{$name;MOD}} {$author} {$type} {$enable} {$hasEvents} {$hasConfig} {$desc};
  261. };
  262. #if { $count > 10 } {
  263. #echo {%c%h} {cyan} { 共列出 $count 项模块信息,若嫌内容过多,你可以用 MOD <关键字> 来模糊查询以缩小范围 };
  264. };
  265. #else {
  266. #echo {%c%h} {cyan} { 共列出 $count 项模块信息 };
  267. };
  268. };
  269. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  270. #alias {MOD} {look-module %0; #buffer end};
  271. #alias {look-module} {
  272. #local moduleName {%1};
  273. #local metaInfo {$xtt-modules[$moduleName]};
  274. #local cnName {$metaInfo[NAME]};
  275. #local path {$metaInfo[PATH]};
  276. #local type {$metaInfo[TYPE]};
  277. #local enable {$xtt-modConfig[$moduleName][ENABLE]};
  278. #local desc {$metaInfo[DESC]};
  279. #local author {$metaInfo[AUTHOR]};
  280. #local note {$metaInfo[NOTE]};
  281. #local config {$metaInfo[CONFIG]};
  282. #if { "$moduleName" == "" } {
  283. warnLog 用法: look-module <模块全限定名称>;
  284. #return;
  285. };
  286. #if { "$xtt-modules[$moduleName]" == "" } {
  287. errLog 模块 $moduleName 尚未加载。;
  288. #return;
  289. };
  290. #if { "$enable" == "false" } {
  291. #local enable {<119>禁用<299>};
  292. };
  293. #else {
  294. #local enable {<129>开启<299>};
  295. };
  296. #local moduleLink {@genModuleLink{$moduleName;HELP}};
  297. #echo {%c%h} {cyan} { $moduleLink<269> };
  298. #echo {%s} { <269>名称:<299> $moduleLink <269>类型:<299> $type <269>状态:<299> $enable <269>作者:<299> $author};
  299. #echo {%s} { <269>脚本路径:<299> $path};
  300. #if { "$type" == "弱模块" } {
  301. #echo {%c%h} {cyan} { 更多信息仅纯模块可见 };
  302. #return;
  303. };
  304. #echo {%s} { <269>说明:<299> $desc};
  305. #echo {%s} { <269>备注:<299> $note};
  306. #if { "$type" == "纯模块" } {
  307. #echo {%s} { <269>接口:<299> 你可以通过 $moduleName\.Start/Stop 命令来启动/停止模块。};
  308. };
  309. #local format { %c%-20s %-20s %s};
  310. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$moduleName"}}};
  311. #if { &event[] > 0 } {
  312. #local name {};
  313. #echo {%s} { <269>提供事件列表:<299>};
  314. #echo {$format} {cyan} {事件名称} {事件类型} {说明};
  315. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  316. #foreach {*event[]} {name} {
  317. #local type {$event[$name][type]};
  318. #local desc {$event[$name][desc]};
  319. #echo {$format} {light cyan} {$name} {$type} {$desc};
  320. };
  321. };
  322. #if { &config[] > 0 } {
  323. #echo {%c%h} {cyan} {};
  324. #local count {0};
  325. #echo {%s} { <269>配置参数表:<299>};
  326. #echo {$format} {cyan} {配置项} {当前值} {作用};
  327. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  328. #local key {};
  329. #foreach {*config[]} {key} {
  330. #math count {$count + 1};
  331. #local value {$xtt-modConfig[$moduleName][$key]};
  332. #local desc {$config[$key]};
  333. #local prefix {$moduleName};
  334. #replace prefix {/} {_};
  335. #echo {$format} {light cyan} {$key} {{$value}} {$desc};
  336. };
  337. #echo {%c%h} {cyan} { 共列出 $count 项配置信息 };
  338. };
  339. #echo {};
  340. #echo {%c%h} {cyan} { <169>接下来进一步调查该模块的变量、别名和触发情况<069> };
  341. #echo {};
  342. xtt.ListVar $moduleName; #echo {};
  343. xtt.ListFunc $moduleName; #echo {};
  344. xtt.ListAlias $moduleName; #echo {};
  345. xtt.ListAction $moduleName; #echo {};
  346. };
  347. #alias {make-starter} {
  348. #local moduleName {%1};
  349. #local prefix {%2};
  350. class.open module-loader;
  351. #tab {%1.Start};
  352. #alias {%1.Start} {
  353. #if { "$xtt-modConfig[%1][ENABLE]" == "false" } {
  354. enable-module {%1} {true};
  355. };
  356. #local config {%%0};
  357. #local metaInfo {${%2[META]}};
  358. #foreach {*metaInfo[config][]} {key} {
  359. class.open {%1};
  360. #format {%2[config][$key]} {%s} {$config[$key]};
  361. class.close {%1};
  362. };
  363. #format xtt-modConfig {%s%s} {$xtt-modConfig} {{%1}{${%2[config]}}};
  364. class.do {%1} %1.Run;
  365. };
  366. #tab {%1.Stop};
  367. #alias {%1.Stop} {
  368. #local reason {%%1};
  369. #if { "$reason" == "" } {
  370. #format reason {人为操作};
  371. };
  372. infoLog <169>由于<139>$reason<169>,%1 模块停止运行。<299>;
  373. #if @existsAlias{%1.Pause} {
  374. %1.Pause;
  375. };
  376. #if { "$xtt-modConfig[%1][ENABLE]" == "true" } {
  377. disable-module {%1} {true};
  378. };
  379. };
  380. class.close module-loader;
  381. };
  382. #alias {LL} {load-lib};
  383. #alias {load-lib} {load-module lib/%1};
  384. #alias {RLL} {reload-lib};
  385. #alias {reload-lib} {
  386. kill-module {lib/%1};
  387. load-lib {%1} {%2};
  388. };
  389. #nop 给出一个用于屏幕显示的模块名称。会自动根据是否有文档点亮 MSLP 链接;
  390. #func {genModuleLink} {
  391. #local {name} {%1};
  392. #local {cmd} {%2};
  393. #if { "$name" == "" } {
  394. #return {};
  395. };
  396. #if { &xtt-modules[$name][] == 0 } {
  397. #return {$name};
  398. };
  399. #local cnName {$xtt-modules[$name][NAME]};
  400. #if { "$cnName" != "" } {
  401. #local cnName { (<169>$cnName<299>)};
  402. };
  403. #if { "$cmd" != "{MOD|HELP}" } {
  404. #return {$name$cnName};
  405. };
  406. #if { "$cmd" == "HELP" && &xtt.module-doc[$name][] > 0 } {
  407. #local name {@mslp.Help{$name;$name}};
  408. #local name {<140>$name<299>$cnName};
  409. };
  410. #else {
  411. #local name {@mslp.Module{$name;$name}};
  412. #local name {<149>$name<299>$cnName};
  413. };
  414. #return {$name};
  415. };
  416. class.close module-loader;