| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Xml;
- using IwbZero.Expr;
- using IwbZero.ExprFunctions;
- using IwbZero.ToolCommon.AssemblyHelpers;
- using IwbZero.ToolCommon.StringModel;
- namespace WeEngine.Functions
- {
- public class ExprFunctionParam
- {
- public int Index { get; set; }
- public string Name { get; set; }
- public string ExprType { get; set; }
- }
- public class ExprFunction : IwbFunction
- {
- protected new IFunction Function;
- public ExprFunction()
- {
- Params= new List<ExprFunctionParam>();
- }
- public bool IsPublic { get; set; }
- public List<ExprFunctionParam> Params { get; set; }
- //public ExprFunction(IwbFunction f):this()
- //{
- // Id = f.Id;
- // FunctionId = f.FunctionId;
- // FunctionName = f.FunctionName;
- // FunctionClass = f.FunctionClass;
- // FunctionDesc = f.FunctionDesc;
- //}
- /// <summary>
- /// 根据Id加载函数
- /// </summary>
- /// <param name="functionId"></param>
- /// <param name="fileFullClassName"></param>
- /// <returns></returns>
- public static ExprFunction LoadById(string functionId, string fileFullClassName = null)
- {
- var xmlNode = GetFunXmlNode(fileFullClassName);
- var fun = LoadByXml(xmlNode, functionId);
- return fun;
- }
- /// <summary>
- /// 加载函数
- /// </summary>
- /// <param name="xmlNode"></param>
- /// <param name="functionId"></param>
- /// <returns></returns>
- public static ExprFunction LoadByXml(XmlNode xmlNode, string functionId)
- {
- if (!xmlNode.HasChildNodes)
- {
- return null;
- }
- foreach (XmlNode child in xmlNode.ChildNodes)
- {
- if (child.Attributes?["id"] != null && child.Attributes["id"].Value.UAndT() == functionId)
- {
- ExprFunction function = new ExprFunction()
- {
- FunctionId = child.Attributes["id"].Value,
- Id = functionId,
- };
- if (child.HasChildNodes)
- {
- foreach (XmlNode node in child.ChildNodes)
- {
- switch (node.Name.Trim())
- {
- case "Name":
- function.FunctionName = node.FirstChild?.Value;
- break;
- case "ClassName":
- function.FunctionClass = node.FirstChild?.Value;
- break;
- case "Description":
- function.FunctionDesc = node.FirstChild?.Value;
- break;
- }
- }
- }
- return function;
- }
- }
- return null;
- }
- /// <summary>
- /// 加载函数
- /// </summary>
- /// <param name="xmlNode"></param>
- /// <returns></returns>
- public new static List<ExprFunction> LoadByXml(XmlNode xmlNode)
- {
- if (!xmlNode.HasChildNodes)
- {
- return null;
- }
- var funList = new List<ExprFunction>();
- foreach (XmlNode child in xmlNode.ChildNodes)
- {
- if (child.Attributes != null)
- {
- ExprFunction function = new ExprFunction()
- {
- FunctionId = child.Attributes["id"].Value,
- };
- if (function.FunctionId != null)
- function.Id = function.FunctionId;
- if (child.HasChildNodes)
- {
- foreach (XmlNode node in child.ChildNodes)
- {
- switch (node.Name.Trim())
- {
- case "Name":
- function.FunctionName = node.FirstChild?.Value;
- break;
- case "ClassName":
- function.FunctionClass = node.FirstChild?.Value;
- break;
- case "Description":
- function.FunctionDesc = node.FirstChild?.Value;
- break;
- case "IsPublic":
- function.IsPublic = node.FirstChild?.Value.ValB()??false;
- break;
- case "Params":
- if (node.HasChildNodes)
- {
- foreach (XmlNode childNode in node.ChildNodes)
- {
-
- function.Params.Add(CreateParams(childNode));
- }
- }
- break;
- }
- }
- }
- funList.Add(function);
- }
- }
- return funList;
- }
- private static ExprFunctionParam CreateParams(XmlNode xmlNode)
- {
- var funParam = new ExprFunctionParam();
- foreach (XmlNode node in xmlNode.ChildNodes)
- {
- switch (node.Name.Trim())
- {
- case "Index":
- funParam.Index = node.FirstChild?.Value.ValI() ?? 0;
- break;
- case "Name":
- funParam.Name = node.FirstChild?.Value;
- break;
- case "ExprType":
- funParam.ExprType = node.FirstChild?.Value;
- break;
- }
- }
- return funParam;
- }
- /// <summary>
- /// 加载函数XML
- /// </summary>
- /// <param name="fileFullClassName"></param>
- /// <returns></returns>
- public static XmlNode GetFunXmlNode(string fileFullClassName=null)
- {
- fileFullClassName = fileFullClassName ?? @"WeEngine.Functions.function.xml";
- XmlDocument xDoc = new XmlDocument();
- Assembly asm = Assembly.GetExecutingAssembly();
- //Assembly asm2 = AssemblyHelper.LoadAssembly("WeEngine.Runtime.dll");
- //读取嵌入式资源
- Stream sm = asm.GetManifestResourceStream(fileFullClassName);
- if (sm != null)
- {
- xDoc.Load(sm);
- }
- if (!xDoc.HasChildNodes)
- {
- return null;
- }
- XmlNode xmlNode = xDoc.LastChild;
- return xmlNode;
- }
- /// <summary>
- /// 执行函数
- /// </summary>
- /// <param name="poRoot"></param>
- /// <returns></returns>
- public override string InvokeFunction(ExprObject poRoot)
- {
- string result = "";
- if (Function == null)
- {
- LoadFunction();
- }
- if (Function != null)
- {
- result = Function.Invoke(poRoot);
- }
- return result;
- }
- /// <summary>
- /// 加载函数
- /// </summary>
- protected override void LoadFunction()
- {
- if (FunctionClass != "")
- {
- object obj = AssemblyHelper.CreateInstance(FunctionClass);
- if (obj != null && obj is IFunction function)
- {
- Function = function;
- }
- }
- if (Function == null)
- {
- throw new Exception($"Unable to load '{FunctionClass}' for function {FunctionName}.");
- }
- }
- }
- }
|