EngineComponent.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System.Collections.Generic;
  2. using IwbZero.IwbBase;
  3. using IwbZero.ToolCommon.StringModel;
  4. namespace WeEngine.ComponentInfo
  5. {
  6. public class EngineComponent:IIwbId
  7. {
  8. public EngineComponent()
  9. {
  10. Data=new List<ComponentScript>();
  11. }
  12. /// <summary>
  13. /// 编号
  14. /// </summary>
  15. public string Id { get; set; }
  16. /// <summary>
  17. /// 编号
  18. /// </summary>
  19. public string No
  20. {
  21. get => Id;
  22. set => Id = value;
  23. }
  24. /// <summary>
  25. /// 名称
  26. /// </summary>
  27. public string Name { get; set; }
  28. /// <summary>
  29. /// 参数映射
  30. /// </summary>
  31. public string Parameters { get; set; }
  32. /// <summary>
  33. /// 事务描述
  34. /// </summary>
  35. public string Description { get; set; }
  36. /// <summary>
  37. /// 事务类型
  38. /// </summary>
  39. public string ComponentType { get; set; }
  40. /// <summary>
  41. /// 事务逻辑
  42. /// </summary>
  43. public string ComponentScript
  44. {
  45. get
  46. {
  47. string str = "";
  48. foreach (var componentScript in Data)
  49. {
  50. str += componentScript.ToXmlString()+"\r\n";
  51. }
  52. if (str.IsNotEmpty())
  53. {
  54. str = $"<ComponentScript>{str}</ComponentScript>";
  55. }
  56. return str;
  57. }
  58. }
  59. /// <summary>
  60. /// 类全名
  61. /// </summary>
  62. public string ComponentClass { get; set; }
  63. /// <summary>
  64. /// 程序集
  65. /// </summary>
  66. public string ComponentLib { get; set; }
  67. /// <summary>
  68. /// 注册Key(IOC注入)
  69. /// </summary>
  70. public string RegisterKey { get; set; }
  71. public List<ComponentScript> Data { get; set; }
  72. public EngineComponent ModelFrom(IwbXmlNode xmlNode)
  73. {
  74. if (xmlNode == null)
  75. {
  76. return null;
  77. }
  78. Data = new List<ComponentScript>();
  79. Id = xmlNode.GetChildValue("Id");
  80. No = xmlNode.GetChildValue("No");
  81. Name = xmlNode.GetChildValue("Name");
  82. Parameters = xmlNode.GetChildValue("Parameters");
  83. Description = xmlNode.GetChildValue("Description");
  84. RegisterKey = xmlNode.GetChildValue("RegisterKey");
  85. ComponentClass = xmlNode.GetChildValue("ComponentClass");
  86. ComponentLib = xmlNode.GetChildValue("ComponentLib");
  87. ComponentType = xmlNode.GetChildValue("ComponentType");
  88. var scriptStr = xmlNode.GetChildValue("ComponentScript")?.DecodeBase64();
  89. Data = new ComponentScript().ModelFrom(scriptStr);
  90. return this;
  91. }
  92. public string ConvertComponents2Xml(List<EngineComponent> engineComponents)
  93. {
  94. string str = "";
  95. foreach (var component in engineComponents)
  96. {
  97. str += component.ToXmlString() + "\r\n";
  98. }
  99. if (str.IsNotEmpty())
  100. {
  101. str = $"<ComponentInfos>{str}</ComponentInfos>";
  102. }
  103. return str;
  104. }
  105. public string ToXmlString()
  106. {
  107. string str = "";
  108. str += $"<ComponentInfo id=\"{Id}\">\r\n";
  109. str += $"<Id>{Id}</Id>\r\n";
  110. str += $"<Name>{Name}</Name>\r\n";
  111. str += $"<Parameters>{Parameters}</Parameters>\r\n";
  112. str += $"<Description>{Description}</Description>\r\n";
  113. str += $"<RegisterKey>{RegisterKey}</RegisterKey>\r\n";
  114. str += $"<ComponentScript>{ComponentScript.EncodeBase64()}</ComponentScript>\r\n";
  115. str += $"<ComponentClass>{ComponentClass}</ComponentClass>\r\n";
  116. str += $"<ComponentLib>{ComponentLib}</ComponentLib>\r\n";
  117. str += $"<ComponentType>{ComponentType}</ComponentType>\r\n";
  118. str += "</ComponentInfo>";
  119. return str;
  120. }
  121. public List<EngineComponent> BuildByXml(string xml)
  122. {
  123. var node = IwbXmlNode.ParseGenericXml(xml);
  124. return BuildByXml(node);
  125. }
  126. public List<EngineComponent> BuildByXml(IwbXmlNode xmlNode)
  127. {
  128. var models = new List<EngineComponent>();
  129. var nodes = xmlNode?.Nodes;
  130. if (nodes?.Count > 0)
  131. {
  132. foreach (IwbXmlNode node in nodes)
  133. {
  134. var model = new EngineComponent().ModelFrom(node);
  135. if (model != null) models.Add(model);
  136. }
  137. }
  138. return models;
  139. }
  140. }
  141. public class ComponentScript
  142. {
  143. public string Id { get; set; }
  144. public string No { get; set; }
  145. public string Name { get; set; }
  146. public string Path { get; set; }
  147. public string ParentPath { get; set; }
  148. public string CompType { get; set; }
  149. public ComponentVariable Variable { get; set; }
  150. public string Condition { get; set; }
  151. public List<ComponentScript> Yes { get; set; }
  152. public List<ComponentScript> Not { get; set; }
  153. public string WaitNodes { get; set; }
  154. public string Message { get; set; }
  155. public string ToXmlString()
  156. {
  157. string str = "", str1 = "";
  158. str += $"<{CompType} id=\"{Id}\">";
  159. if (Variable != null)
  160. {
  161. str1 += Variable.ToXmlString();
  162. str += str1.IsEmpty() ? "" : $"<Variable>{str1}</Variable>\r\n";
  163. }
  164. str += CompType.IsEmpty() ? "" : $"<CompType>{CompType}</CompType>\r\n";
  165. str += Name.IsEmpty() ? "" : $"<Name>{Name}</Name>\r\n";
  166. str += Path.IsEmpty() ? "" : $"<Name>{Path}</Name>\r\n";
  167. str += ParentPath.IsEmpty() ? "" : $"<Name>{ParentPath}</Name>\r\n";
  168. str += Condition.IsEmpty() ? "" : $"<Condition>{Condition.FormatCode2Xml()}</Condition>\r\n";
  169. str += WaitNodes.IsEmpty() ? "" : $"<WaitNodes>{WaitNodes}</WaitNodes>\r\n";
  170. str += Message.IsEmpty() ? "" : $"<Message>{Message}</Message>\r\n";
  171. str1 = "";
  172. if (Yes != null)
  173. {
  174. foreach (var cd in Yes)
  175. {
  176. str1 += cd.ToXmlString();
  177. }
  178. }
  179. str += str1.IsEmpty() ? "" : $"<Yes>{str1}</Yes>\r\n";
  180. str1 = "";
  181. if (Not != null)
  182. {
  183. foreach (var cd in Not)
  184. {
  185. str1 += cd.ToXmlString();
  186. }
  187. }
  188. str += str1.IsEmpty() ? "" : $"<No>{str1}</No>\r\n";
  189. str += $"</{CompType}>";
  190. return str;
  191. }
  192. public List<ComponentScript> ModelFrom(string str)
  193. {
  194. var scripts= new List<ComponentScript>();
  195. if (str.IsEmpty())
  196. {
  197. return scripts;
  198. }
  199. var xmlStr = str.StartsWith("<ComponentScript>") ? str : $"<ComponentScript>{str}</ComponentScript>";
  200. var xmlNode = IwbXmlNode.ParseGenericXml(xmlStr);
  201. var scriptNodes = xmlNode.Nodes;
  202. if (scriptNodes?.Count > 0)
  203. {
  204. foreach (IwbXmlNode scriptNode in scriptNodes)
  205. {
  206. var data = new ComponentScript().ModelFrom(scriptNode);
  207. scripts.Add(data);
  208. }
  209. }
  210. return scripts;
  211. }
  212. public ComponentScript ModelFrom(IwbXmlNode xmlNode)
  213. {
  214. if (xmlNode == null)
  215. return null;
  216. Id = xmlNode.GetChildValue("Id");
  217. No = xmlNode.GetChildValue("No");
  218. Name = xmlNode.GetChildValue("Name");
  219. CompType = xmlNode.GetChildValue("CompType");
  220. Path = xmlNode.GetChildValue("Path");
  221. ParentPath = xmlNode.GetChildValue("ParentPath");
  222. Condition = xmlNode.GetChildValue("Condition");
  223. WaitNodes = xmlNode.GetChildValue("WaitNodes");
  224. Message = xmlNode.GetChildValue("Message");
  225. var variableNode = xmlNode.GetChildNode("Variable");
  226. Variable = new ComponentVariable().ModelFrom(variableNode);
  227. Yes = new List<ComponentScript>();
  228. Not = new List<ComponentScript>();
  229. var yesNodes = xmlNode.GetChildNode("Yes")?.Nodes;
  230. if (yesNodes?.Count > 0)
  231. {
  232. foreach (IwbXmlNode y in yesNodes)
  233. {
  234. var yes = new ComponentScript().ModelFrom(y);
  235. if (yes != null) Yes.Add(yes);
  236. }
  237. }
  238. var noNodes = xmlNode.GetChildNode("Not")?.Nodes;
  239. if (noNodes?.Count > 0)
  240. {
  241. foreach (IwbXmlNode n in noNodes)
  242. {
  243. var not = new ComponentScript().ModelFrom(n);
  244. if (not != null) Not.Add(not);
  245. }
  246. }
  247. return this;
  248. }
  249. }
  250. public class ComponentVariable
  251. {
  252. public ComponentVariable(string str)
  253. {
  254. var arr = str.StrToArray(":");
  255. Name = "";
  256. Type = "STRING";
  257. Value = "";
  258. if (arr.Length > 0)
  259. {
  260. Name = arr[0].Substring(2);
  261. }
  262. if (arr.Length > 1)
  263. {
  264. TargetPath = arr[1];
  265. }
  266. if (arr.Length > 2)
  267. {
  268. Value = arr[2];
  269. }
  270. }
  271. public ComponentVariable()
  272. {
  273. }
  274. public string Name { get; set; }
  275. public string Type { get; set; }
  276. public string Value { get; set; }
  277. public string TargetPath { get; set; }
  278. public string TargetName { get; set; }
  279. public string ToXmlString()
  280. {
  281. string str = "";
  282. str += Name.IsEmpty() ? "" : $"<Name>{Name}</Name>\r\n";
  283. str += Type.IsEmpty() ? "" : $"<Type>{Type}</Type>\r\n";
  284. str += Value.IsEmpty() ? "" : $"<Value>{Value}</Value>\r\n";
  285. str += TargetPath.IsEmpty() ? "" : $"<TargetPath>{TargetPath}</TargetPath>\r\n";
  286. str += TargetName.IsEmpty() ? "" : $"<TargetName>{TargetName}</TargetName>\r\n";
  287. return str;
  288. }
  289. public ComponentVariable ModelFrom(IwbXmlNode xmlNode)
  290. {
  291. if (xmlNode == null)
  292. {
  293. return null;
  294. }
  295. Name = xmlNode.GetChildValue("Name");
  296. Type = xmlNode.GetChildValue("Type");
  297. Value = xmlNode.GetChildValue("Value");
  298. TargetPath = xmlNode.GetChildValue("TargetPath");
  299. TargetName = xmlNode.GetChildValue("TargetName");
  300. return this;
  301. }
  302. }
  303. }