module-loader.tin 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 模块名称:模块加载器
  4. 模块说明:本文件属于框架代码的一部分,不建议修改。如有需求请在 GitHub 发 issue 或者 PR
  5. 版权声明:本文件属于 PaoTin++ 的一部分
  6. ===========
  7. PaoTin++ © 2020~2023 的所有版权均由担子炮(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 path {};
  34. #foreach {$moduleName;$moduleName/__init__;$moduleName/__main__} {path} {
  35. class.open {load-module-$moduleName};
  36. #line sub var #action {^#READ {{plugins/($path)\.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 path {plugins/%s.tin} {$path};
  49. #line local {
  50. #nop 模块名称;
  51. #local MODULE {$moduleName};
  52. #nop 模块路径;
  53. #local PATH {$path};
  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} {$path};
  63. };
  64. #if { "${${prefix}-loaded}" == "true" } {
  65. class.kill {load-module-$moduleName};
  66. init-module {$moduleName} {$path} {$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 _ {%0};
  218. #local format { %c%-30s %-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. #math count {$count + 1};
  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 { "$config" == "" } {
  236. #local hasConfig {无};
  237. };
  238. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$name"}}};
  239. #if { &event[] > 0 } {
  240. #local hasEvents {<169>&event[]个<299>};
  241. };
  242. #if { "$type" == "纯模块" } {
  243. #local type {<129>纯模块<299>};
  244. };
  245. #elseif { "$type" == "混合模块" } {
  246. #local type {<139>混合模块<299>};
  247. };
  248. #if { "${xtt-modConfig[$moduleName][ENABLE]}" == "false" } {
  249. #local enable {<119>禁用<299>};
  250. };
  251. #echo {$format} {white} {@genModuleLink{$name;MOD}} {$author} {$type} {$enable} {$hasEvents} {$hasConfig} {$desc};
  252. };
  253. #echo {%c%h} {cyan} { 共列出 $count 项模块信息 };
  254. };
  255. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  256. #alias {MOD} {look-module %0; #buffer end};
  257. #alias {look-module} {
  258. #local moduleName {%1};
  259. #local metaInfo {${xtt-modules[$moduleName]}};
  260. #local cnName {$metaInfo[NAME]};
  261. #local path {$metaInfo[PATH]};
  262. #local type {$metaInfo[TYPE]};
  263. #local enable {${xtt-modConfig[$moduleName][ENABLE]}};
  264. #local desc {$metaInfo[DESC]};
  265. #local author {$metaInfo[AUTHOR]};
  266. #local note {$metaInfo[NOTE]};
  267. #local config {$metaInfo[CONFIG]};
  268. #if { "$moduleName" == "" } {
  269. warnLog 用法: look-module <模块全限定名称>;
  270. #return;
  271. };
  272. #if { "${xtt-modules[$moduleName]}" == "" } {
  273. errLog 模块 $moduleName 尚未加载。;
  274. #return;
  275. };
  276. #if { "$enable" == "false" } {
  277. #local enable {<119>禁用<299>};
  278. };
  279. #else {
  280. #local enable {<129>开启<299>};
  281. };
  282. #local moduleLink {@genModuleLink{$moduleName;HELP}};
  283. #echo {%c%h} {cyan} { $moduleLink<269> };
  284. #echo {%s} { <269>名称:<299> $moduleLink <269>类型:<299> $type <269>状态:<299> $enable <269>作者:<299> $author};
  285. #echo {%s} { <269>脚本路径:<299> $path};
  286. #if { "$type" == "弱模块" } {
  287. #echo {%c%h} {cyan} { 更多信息仅纯模块可见 };
  288. #return;
  289. };
  290. #echo {%s} { <269>说明:<299> $desc};
  291. #echo {%s} { <269>备注:<299> $note};
  292. #if { "$type" == "纯模块" } {
  293. #echo {%s} { <269>接口:<299> 你可以通过 $moduleName\.Start/Stop 命令来启动/停止模块。};
  294. };
  295. #local format { %c%-20s %-20s %s};
  296. #local event {@fp.Filter{{$gValidEvent};{"VALUE[module]"=="$moduleName"}}};
  297. #if { &event[] > 0 } {
  298. #local name {};
  299. #echo {%s} { <269>提供事件列表:<299>};
  300. #echo {$format} {cyan} {事件名称} {事件类型} {说明};
  301. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  302. #foreach {*event[]} {name} {
  303. #local type {$event[$name][type]};
  304. #local desc {$event[$name][desc]};
  305. #echo {$format} {light cyan} {$name} {$type} {$desc};
  306. };
  307. };
  308. #if { &config[] <= 0 } {
  309. #echo {%c%h} {cyan} {};
  310. #return;
  311. };
  312. #local count {0};
  313. #echo {%s} { <269>配置参数表:<299>};
  314. #echo {$format} {cyan} {配置项} {当前值} {作用};
  315. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  316. #local key {};
  317. #foreach {*config[]} {key} {
  318. #math count {$count + 1};
  319. #local value ${xtt-modConfig[$moduleName][$key]};
  320. #local desc {$config[$key]};
  321. #local prefix {$moduleName};
  322. #replace prefix {/} {_};
  323. #echo {$format} {light cyan} {$key} {{$value}} {$desc};
  324. };
  325. #echo {%c%h} {cyan} { 共列出 $count 项配置信息 };
  326. };
  327. #alias {make-starter} {
  328. #local moduleName {%1};
  329. #local prefix {%2};
  330. class.open module-loader;
  331. #tab {%1.Start};
  332. #alias {%1.Start} {
  333. #if { "${xtt-modConfig[%1][ENABLE]}" == "false" } {
  334. enable-module {%1} {true};
  335. };
  336. #local config {%%0};
  337. #local metaInfo {${%2[META]}};
  338. #foreach {*metaInfo[config][]} {key} {
  339. class.open {%1};
  340. #format {%2[config][$key]} {%s} {$config[$key]};
  341. class.close {%1};
  342. };
  343. #format xtt-modConfig {%s%s} {${xtt-modConfig}} {{%1}{${%2[config]}}};
  344. class.do {%1} %1.Run;
  345. };
  346. #tab {%1.Stop};
  347. #alias {%1.Stop} {
  348. #local reason {%%1};
  349. #if { "$reason" == "" } {
  350. #format reason {人为操作};
  351. };
  352. infoLog <169>由于<139>$reason<169>,%1 模块停止运行。<299>;
  353. #if @existsAlias{%1.Pause} {
  354. %1.Pause;
  355. };
  356. #if { "${xtt-modConfig[%1][ENABLE]}" == "true" } {
  357. disable-module {%1} {true};
  358. };
  359. };
  360. class.close module-loader;
  361. };
  362. #alias {LL} {load-lib};
  363. #alias {load-lib} {load-module lib/%1};
  364. #alias {RLL} {reload-lib};
  365. #alias {reload-lib} {
  366. kill-module {lib/%1};
  367. load-lib {%1} {%2};
  368. };
  369. #nop 给出一个用于屏幕显示的模块名称。会自动根据是否有文档点亮 MSLP 链接;
  370. #func {genModuleLink} {
  371. #local {name} {%1};
  372. #local {cmd} {%2};
  373. #if { "$name" == "" } {
  374. #return {};
  375. };
  376. #if { &xtt-modules[$name][] == 0 } {
  377. #return {$name};
  378. };
  379. #local cnName {$xtt-modules[$name][NAME]};
  380. #if { "$cnName" != "" } {
  381. #local cnName { (<169>$cnName<299>)};
  382. };
  383. #if { "$cmd" != "{MOD|HELP}" } {
  384. #return {$name$cnName};
  385. };
  386. #if { "$cmd" == "HELP" && &xtt.module-doc[$name][] > 0 } {
  387. #local name {@mslp.Help{$name;$name}};
  388. #local name {<140>$name<299>$cnName};
  389. };
  390. #else {
  391. #local name {@mslp.Module{$name;$name}};
  392. #local name {<149>$name<299>$cnName};
  393. };
  394. #return {$name};
  395. };
  396. class.close module-loader;