module-loader.tin 12 KB

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