ExprFunction.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Xml;
  6. using IwbZero.Expr;
  7. using IwbZero.ExprFunctions;
  8. using IwbZero.ToolCommon.AssemblyHelpers;
  9. using IwbZero.ToolCommon.StringModel;
  10. namespace WeEngine.Functions
  11. {
  12. public class ExprFunctionParam
  13. {
  14. public int Index { get; set; }
  15. public string Name { get; set; }
  16. public string ExprType { get; set; }
  17. }
  18. public class ExprFunction : IwbFunction
  19. {
  20. protected new IFunction Function;
  21. public ExprFunction()
  22. {
  23. Params= new List<ExprFunctionParam>();
  24. }
  25. public bool IsPublic { get; set; }
  26. public List<ExprFunctionParam> Params { get; set; }
  27. //public ExprFunction(IwbFunction f):this()
  28. //{
  29. // Id = f.Id;
  30. // FunctionId = f.FunctionId;
  31. // FunctionName = f.FunctionName;
  32. // FunctionClass = f.FunctionClass;
  33. // FunctionDesc = f.FunctionDesc;
  34. //}
  35. /// <summary>
  36. /// 根据Id加载函数
  37. /// </summary>
  38. /// <param name="functionId"></param>
  39. /// <param name="fileFullClassName"></param>
  40. /// <returns></returns>
  41. public static ExprFunction LoadById(string functionId, string fileFullClassName = null)
  42. {
  43. var xmlNode = GetFunXmlNode(fileFullClassName);
  44. var fun = LoadByXml(xmlNode, functionId);
  45. return fun;
  46. }
  47. /// <summary>
  48. /// 加载函数
  49. /// </summary>
  50. /// <param name="xmlNode"></param>
  51. /// <param name="functionId"></param>
  52. /// <returns></returns>
  53. public static ExprFunction LoadByXml(XmlNode xmlNode, string functionId)
  54. {
  55. if (!xmlNode.HasChildNodes)
  56. {
  57. return null;
  58. }
  59. foreach (XmlNode child in xmlNode.ChildNodes)
  60. {
  61. if (child.Attributes?["id"] != null && child.Attributes["id"].Value.UAndT() == functionId)
  62. {
  63. ExprFunction function = new ExprFunction()
  64. {
  65. FunctionId = child.Attributes["id"].Value,
  66. Id = functionId,
  67. };
  68. if (child.HasChildNodes)
  69. {
  70. foreach (XmlNode node in child.ChildNodes)
  71. {
  72. switch (node.Name.Trim())
  73. {
  74. case "Name":
  75. function.FunctionName = node.FirstChild?.Value;
  76. break;
  77. case "ClassName":
  78. function.FunctionClass = node.FirstChild?.Value;
  79. break;
  80. case "Description":
  81. function.FunctionDesc = node.FirstChild?.Value;
  82. break;
  83. }
  84. }
  85. }
  86. return function;
  87. }
  88. }
  89. return null;
  90. }
  91. /// <summary>
  92. /// 加载函数
  93. /// </summary>
  94. /// <param name="xmlNode"></param>
  95. /// <returns></returns>
  96. public new static List<ExprFunction> LoadByXml(XmlNode xmlNode)
  97. {
  98. if (!xmlNode.HasChildNodes)
  99. {
  100. return null;
  101. }
  102. var funList = new List<ExprFunction>();
  103. foreach (XmlNode child in xmlNode.ChildNodes)
  104. {
  105. if (child.Attributes != null)
  106. {
  107. ExprFunction function = new ExprFunction()
  108. {
  109. FunctionId = child.Attributes["id"].Value,
  110. };
  111. if (function.FunctionId != null)
  112. function.Id = function.FunctionId;
  113. if (child.HasChildNodes)
  114. {
  115. foreach (XmlNode node in child.ChildNodes)
  116. {
  117. switch (node.Name.Trim())
  118. {
  119. case "Name":
  120. function.FunctionName = node.FirstChild?.Value;
  121. break;
  122. case "ClassName":
  123. function.FunctionClass = node.FirstChild?.Value;
  124. break;
  125. case "Description":
  126. function.FunctionDesc = node.FirstChild?.Value;
  127. break;
  128. case "IsPublic":
  129. function.IsPublic = node.FirstChild?.Value.ValB()??false;
  130. break;
  131. case "Params":
  132. if (node.HasChildNodes)
  133. {
  134. foreach (XmlNode childNode in node.ChildNodes)
  135. {
  136. function.Params.Add(CreateParams(childNode));
  137. }
  138. }
  139. break;
  140. }
  141. }
  142. }
  143. funList.Add(function);
  144. }
  145. }
  146. return funList;
  147. }
  148. private static ExprFunctionParam CreateParams(XmlNode xmlNode)
  149. {
  150. var funParam = new ExprFunctionParam();
  151. foreach (XmlNode node in xmlNode.ChildNodes)
  152. {
  153. switch (node.Name.Trim())
  154. {
  155. case "Index":
  156. funParam.Index = node.FirstChild?.Value.ValI() ?? 0;
  157. break;
  158. case "Name":
  159. funParam.Name = node.FirstChild?.Value;
  160. break;
  161. case "ExprType":
  162. funParam.ExprType = node.FirstChild?.Value;
  163. break;
  164. }
  165. }
  166. return funParam;
  167. }
  168. /// <summary>
  169. /// 加载函数XML
  170. /// </summary>
  171. /// <param name="fileFullClassName"></param>
  172. /// <returns></returns>
  173. public static XmlNode GetFunXmlNode(string fileFullClassName=null)
  174. {
  175. fileFullClassName = fileFullClassName ?? @"WeEngine.Functions.function.xml";
  176. XmlDocument xDoc = new XmlDocument();
  177. Assembly asm = Assembly.GetExecutingAssembly();
  178. //Assembly asm2 = AssemblyHelper.LoadAssembly("WeEngine.Runtime.dll");
  179. //读取嵌入式资源
  180. Stream sm = asm.GetManifestResourceStream(fileFullClassName);
  181. if (sm != null)
  182. {
  183. xDoc.Load(sm);
  184. }
  185. if (!xDoc.HasChildNodes)
  186. {
  187. return null;
  188. }
  189. XmlNode xmlNode = xDoc.LastChild;
  190. return xmlNode;
  191. }
  192. /// <summary>
  193. /// 执行函数
  194. /// </summary>
  195. /// <param name="poRoot"></param>
  196. /// <returns></returns>
  197. public override string InvokeFunction(ExprObject poRoot)
  198. {
  199. string result = "";
  200. if (Function == null)
  201. {
  202. LoadFunction();
  203. }
  204. if (Function != null)
  205. {
  206. result = Function.Invoke(poRoot);
  207. }
  208. return result;
  209. }
  210. /// <summary>
  211. /// 加载函数
  212. /// </summary>
  213. protected override void LoadFunction()
  214. {
  215. if (FunctionClass != "")
  216. {
  217. object obj = AssemblyHelper.CreateInstance(FunctionClass);
  218. if (obj != null && obj is IFunction function)
  219. {
  220. Function = function;
  221. }
  222. }
  223. if (Function == null)
  224. {
  225. throw new Exception($"Unable to load '{FunctionClass}' for function {FunctionName}.");
  226. }
  227. }
  228. }
  229. }