2 using System.Collections.Generic;
5 using System.Reflection;
6 using System.Diagnostics;
12 #region Public Properties
14 public bool IsRunning { get { return mIsRunning; } }
19 #region Public Methods
21 public Script(string code, Game game)
25 string[] functions = Parse.List(code);
26 if (functions != null)
28 foreach (string function in functions)
30 string[] parts = Parse.Function(function);
33 string[] args = Parse.List(parts[1]);
36 Function func = new Function(parts[0], args);
39 else throw new Exception("Arguments could not be parsed: " + parts[1]);
41 else throw new Exception("Function could not be parsed: " + function);
44 else throw new Exception("Script could not be parsed: " + code);
47 public bool Run(Player player)
57 for (; mRunningIndex < mFunctions.Count; mRunningIndex++)
59 result = Call(mRunningIndex, player);
63 if (mRunningIndex >= mFunctions.Count - 1) mIsRunning = false;
70 #region Private Methods
72 bool Call(int index, Player player)
74 Debug.Assert(0 <= index && index < mFunctions.Count);
77 object[] args = new object[2];
79 args[1] = mFunctions[index].Arguments;
80 return (bool)typeof(Impl).InvokeMember(mFunctions[index].Name, BindingFlags.InvokeMethod, null, null, args);
82 #pragma warning disable 0168
83 catch (System.MissingMethodException ex)
84 #pragma warning restore 0168
86 throw new Exception("Function could not be found: " + mFunctions[index].Name);
97 public static bool True(Player player, string[] args)
102 public static bool False(Player player, string[] args)
107 public static bool Has(Player player, string[] args)
112 public static bool Print(Player player, string[] args)
114 foreach (string arg in args)
116 string line = Parse.String(arg);
117 if (line != null) Console.WriteLine(line);
125 public string Name { get { return mName; } }
126 public string[] Arguments { get { return mArgs; } }
128 public Function(string name, string[] args)
141 #region Private Variables
144 List<Function> mFunctions = new List<Function>();
147 Impl mImpl = new Impl();