module-loader.tin 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #nop vim: set filetype=tt:;
  2. /*
  3. 模块名称:模块加载器
  4. 模块说明:本文件属于框架代码的一部分,不建议修改。如有需求请在 GitHub 发 issue 或者 PR
  5. 版权声明:本文件属于 PaoTin++ 的一部分
  6. ===========
  7. PaoTin++ © 2020~2022 的所有版权均由担子炮(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 但是该模块已经被禁用,如果你想要重新启用,请使用「<120>enable-module %1<070>」。;
  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}" == "true" } {
  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}" == "true" } {
  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. #local prefix {$moduleName};
  151. #replace prefix {/} {_};
  152. #unvar {${prefix}-loaded};
  153. class.kill {$moduleName};
  154. };
  155. #alias {RLM} {reload-module};
  156. #alias {reload-module} {
  157. #local moduleName {%1};
  158. #if { "$moduleName" == "" } {
  159. warnLog 用法: reload-module <模块全限定名称>;
  160. #return;
  161. };
  162. kill-module {%1};
  163. load-module {%1} {%2};
  164. };
  165. #alias {DM} {disable-module};
  166. #alias {disable-module} {
  167. #local moduleName {%1};
  168. #local dontCallStop {%2};
  169. #if { "${xtt-modules[$moduleName][TYPE]}" == "纯模块" && "$dontCallStop" != "true" } {
  170. ${moduleName}.Stop;
  171. #return;
  172. };
  173. #if { "$moduleName" == "" } {
  174. warnLog 用法: disable-module <模块全限定名称>;
  175. #return;
  176. };
  177. #if { "${xtt-modules[$moduleName]}" == "" } {
  178. errLog 模块 $moduleName 尚未加载模块。;
  179. #return;
  180. };
  181. #if { "${xtt-modConfig[$moduleName][ENABLE]}" == "false" } {
  182. warnLog 模块 $moduleName 已经禁用。;
  183. #return;
  184. };
  185. #var {xtt-modConfig[$moduleName][ENABLE]} {false};
  186. class.disable {$moduleName};
  187. infoLog <120>模块 $moduleName 已经禁用。<070>;
  188. };
  189. #alias {EM} {enable-module};
  190. #alias {enable-module} {
  191. #local moduleName {%1};
  192. #local dontCallStart {%2};
  193. #if { "${xtt-modules[$moduleName][TYPE]}" == "纯模块" && "$dontCallStart" != "true" } {
  194. ${moduleName}.Start;
  195. #return;
  196. };
  197. #if { "$moduleName" == "" } {
  198. warnLog 用法: enable-module <模块全限定名称>;
  199. #return;
  200. };
  201. #if { "${xtt-modules[$moduleName]}" == "" } {
  202. errLog 模块 $moduleName 尚未加载模块。;
  203. #return;
  204. };
  205. #if { "${xtt-modConfig[$moduleName][ENABLE]}" == "true" } {
  206. warnLog 模块 $moduleName 已经启用。;
  207. #return;
  208. };
  209. #var {xtt-modConfig[$moduleName][ENABLE]} {true};
  210. class.enable {$moduleName};
  211. infoLog <120>模块 $moduleName 已经启用。<070>;
  212. };
  213. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  214. #alias {MODS} {list-modules};
  215. #alias {list-modules} {
  216. #local _ {%0};
  217. #local format { %c%-30s %-8s %-8s %-4s %-6s %-6s %s};
  218. #echo {%c%h} {cyan} { 已加载模块 -- 请用 MOD <模块名> 来查看详细内容 };
  219. #echo {$format} {cyan} {模块名称} {作者} {类型} {状态} {事件} {配置项} {说明};
  220. #echo {$format} {cyan} {------------} {-------} {-------} {----} {----} {----} {------------------------------------};
  221. #local name {};
  222. #local count {0};
  223. #foreach {*{xtt-modules[]}} {name} {
  224. #math count {$count + 1};
  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 {<120>开启<070>};
  232. #local hasConfig {有};
  233. #local hasEvents {无};
  234. #if { "$cnName" != "" } {
  235. #local cnName {(<160>$cnName<070>)};
  236. };
  237. #if { "$config" == "" } {
  238. #local hasConfig {无};
  239. };
  240. #local event {@filter{{$gValidEvent};{"VALUE[module]"=="$name"}}};
  241. #if { &event[] > 0 } {
  242. #local hasEvents {<160>&event[]个<070>};
  243. };
  244. #if { "$type" == "纯模块" } {
  245. #local type {<120>纯模块<070>};
  246. };
  247. #elseif { "$type" == "混合模块" } {
  248. #local type {<130>混合模块<070>};
  249. };
  250. #if { "${xtt-modConfig[$moduleName][ENABLE]}" == "false" } {
  251. #local enable {<110>禁用<070>};
  252. };
  253. #echo {$format} {white} {$name $cnName} {$author} {$type} {$enable} {$hasEvents} {$hasConfig} {$desc};
  254. };
  255. #echo {%c%h} {cyan} { 共列出 $count 项模块信息 };
  256. };
  257. #nop 列出所有已加载模块以及它们的开关状态、配置参数;
  258. #alias {MOD} {look-module};
  259. #alias {look-module} {
  260. #local moduleName {%1};
  261. #local metaInfo {${xtt-modules[$moduleName]}};
  262. #local cnName {$metaInfo[NAME]};
  263. #local path {$metaInfo[PATH]};
  264. #local type {$metaInfo[TYPE]};
  265. #local enable {${xtt-modConfig[$moduleName][ENABLE]}};
  266. #local desc {$metaInfo[DESC]};
  267. #local author {$metaInfo[AUTHOR]};
  268. #local note {$metaInfo[NOTE]};
  269. #local config {$metaInfo[CONFIG]};
  270. #if { "$moduleName" == "" } {
  271. warnLog 用法: look-module <模块全限定名称>;
  272. #return;
  273. };
  274. #if { "${xtt-modules[$moduleName]}" == "" } {
  275. errLog 模块 $moduleName 尚未加载。;
  276. #return;
  277. };
  278. #if { "$enable" == "false" } {
  279. #local enable {<110>禁用<070>};
  280. };
  281. #else {
  282. #local enable {<120>开启<070>};
  283. };
  284. #if { "$cnName" != "" } {
  285. #local cnName { (<160>$cnName<070>)<060>};
  286. };
  287. #echo {%c%h} {cyan} { $moduleName$cnName };
  288. #echo {%s} { <060>名称:<070> $moduleName$cnName <060>类型:<070> $type <060>状态:<070> $enable <060>作者:<070> $author};
  289. #echo {%s} { <060>脚本路径:<070> $path};
  290. #if { "$type" == "弱模块" } {
  291. #echo {%c%h} {cyan} { 更多信息仅纯模块可见 };
  292. #return;
  293. };
  294. #echo {%s} { <060>说明:<070> $desc};
  295. #echo {%s} { <060>备注:<070> $note};
  296. #if { "$type" == "纯模块" } {
  297. #echo {%s} { <060>接口:<070> 你可以通过 $moduleName\.Start/Stop 命令来启动/停止模块。};
  298. };
  299. #local format { %c%-20s %-20s %s};
  300. #local event {@filter{{$gValidEvent};{"VALUE[module]"=="$moduleName"}}};
  301. #if { &event[] > 0 } {
  302. #local name {};
  303. #echo {%s} { <060>提供事件列表:<070>};
  304. #echo {$format} {cyan} {事件名称} {事件类型} {说明};
  305. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  306. #foreach {*event[]} {name} {
  307. #local type {$event[$name][type]};
  308. #local desc {$event[$name][desc]};
  309. #echo {$format} {light cyan} {$name} {$type} {$desc};
  310. };
  311. };
  312. #if { &config[] <= 0 } {
  313. #echo {%c%h} {cyan} {};
  314. #return;
  315. };
  316. #local count {0};
  317. #echo {%s} { <060>配置参数表:<070>};
  318. #echo {$format} {cyan} {配置项} {当前值} {作用};
  319. #echo {$format} {cyan} {--------------------} {--------------------} {------------------------------};
  320. #local key {};
  321. #foreach {*config[]} {key} {
  322. #math count {$count + 1};
  323. #local value ${xtt-modConfig[$moduleName][$key]};
  324. #local desc {$config[$key]};
  325. #local prefix {$moduleName};
  326. #replace prefix {/} {_};
  327. #echo {$format} {light cyan} {$key} {$value} {$desc};
  328. };
  329. #echo {%c%h} {cyan} { 共列出 $count 项配置信息 };
  330. };
  331. #alias {make-starter} {
  332. #local moduleName {%1};
  333. #local prefix {%2};
  334. class.open module-loader;
  335. #tab {%1.Start};
  336. #alias {%1.Start} {
  337. #if { "${xtt-modConfig[%1][ENABLE]}" == "false" } {
  338. enable-module {%1} {true};
  339. };
  340. #local config {%%0};
  341. #local metaInfo {${%2[META]}};
  342. #foreach {*metaInfo[config][]} {key} {
  343. class.open {%1};
  344. #format {%2[config][$key]} {%s} {$config[$key]};
  345. class.close {%1};
  346. };
  347. #format xtt-modConfig {%s%s} {${xtt-modConfig}} {{%1}{${%2[config]}}};
  348. class.do {%1} %1.Run;
  349. };
  350. #tab {%1.Stop};
  351. #alias {%1.Stop} {
  352. #local reason {%%1};
  353. #if { "$reason" == "" } {
  354. #format reason {人为操作};
  355. };
  356. infoLog <160>由于<130>$reason<160>,%1 模块停止运行。<070>;
  357. #if { "@existsAlias{%1.Pause}" == "true" } {
  358. %1.Pause;
  359. };
  360. #if { "${xtt-modConfig[%1][ENABLE]}" == "true" } {
  361. disable-module {%1} {true};
  362. };
  363. };
  364. class.close module-loader;
  365. };
  366. #alias {LL} {load-lib};
  367. #alias {load-lib} {load-module lib/%1};
  368. #alias {RLL} {reload-lib};
  369. #alias {reload-lib} {
  370. kill-module {lib/%1};
  371. load-lib {%1} {%2};
  372. };
  373. class.close module-loader;