Variables.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace NTERA.Engine.Runtime.Base
  5. {
  6. public static class Variables
  7. {
  8. public class VariableAttribute : Attribute
  9. {
  10. public string Name { get; }
  11. public ValueType Type { get; }
  12. public VariableAttribute(string name, ValueType type)
  13. {
  14. Name = name;
  15. Type = type;
  16. }
  17. }
  18. public static Dictionary<VariableAttribute, Func<EraRuntime, int[], Value>> StaticVariables { get; } = _getFunctions();
  19. private static Dictionary<VariableAttribute, Func<EraRuntime, int[], Value>> _getFunctions()
  20. {
  21. var output = new Dictionary<VariableAttribute, Func<EraRuntime, int[], Value>>();
  22. foreach (MethodInfo method in typeof(Variables).GetMethods(BindingFlags.Public | BindingFlags.Static))
  23. {
  24. var variable = method.GetCustomAttribute<VariableAttribute>();
  25. if (variable != null)
  26. output[variable] = (Func<EraRuntime, int[], Value>)Delegate.CreateDelegate(typeof(Func<EraRuntime, int[], Value>), method);
  27. }
  28. return output;
  29. }
  30. [Variable("LINECOUNT", ValueType.Real)]
  31. public static Value LineCount(EraRuntime runtime, int[] index)
  32. {
  33. return runtime.Console.LineCount;
  34. }
  35. }
  36. }