2 using System.Collections.Generic;
5 using System.Reflection;
6 using System.Diagnostics;
11 /// The Script class handles the parsing and execution of lists
12 /// of functions. Scripts are closely related to triggers.
16 #region Public Properties
19 /// Determine if the script is in the process of being run.
21 public bool IsRunning { get { return mIsRunning; } }
26 #region Public Methods
29 /// Construct a script object with code and a game reference.
31 /// <param name="code">The script code.</param>
32 /// <param name="game">A game reference.</param>
33 public Script(string code, Game game)
35 mImpl = new Impl(game);
37 string[] functions = Parse.List(code);
38 if (functions != null)
40 foreach (string function in functions)
42 string[] parts = Parse.Function(function);
45 string[] args = Parse.List(parts[1]);
48 Function func = new Function(parts[0], args);
51 else throw new Exception("Arguments could not be parsed: " + parts[1]);
53 else throw new Exception("Function could not be parsed: " + function);
56 else throw new Exception("Script could not be parsed: " + code);
60 /// Start execution of the script. If there is no need to break
61 /// execution before the script ends, it will finish before this method
62 /// call ends. Otherwise, execution will be delayed and will finish sometime
63 /// in the future. This will execute each function in sequence as long
64 /// as each function evaluates to true. If a function does not evaluate to true,
65 /// this method will return and execution will be delayed. In either case,
66 /// the evaluation of the last function is returned by this method.
68 /// <param name="player">The player associated with this script.</param>
69 /// <returns>Evaluation of the last function call.</returns>
70 public bool Run(Player player)
80 for (; mRunningIndex < mFunctions.Count; mRunningIndex++)
82 result = Call(mRunningIndex, player);
86 if (mRunningIndex >= mFunctions.Count - 1) mIsRunning = false;
93 #region Private Methods
96 /// Call a function in the last at a certain index.
98 /// <param name="index">The function index.</param>
99 /// <param name="player">The associated player object.</param>
100 /// <returns>The evaluation of the function.</returns>
101 bool Call(int index, Player player)
103 Debug.Assert(0 <= index && index < mFunctions.Count);
106 object[] args = new object[2];
108 args[1] = mFunctions[index].Arguments;
109 return (bool)typeof(Impl).InvokeMember(mFunctions[index].Name, BindingFlags.InvokeMethod, null, null, args);
111 #pragma warning disable 0168
112 catch (System.MissingMethodException ex)
113 #pragma warning restore 0168
115 throw new Exception("Function could not be found: " + mFunctions[index].Name);
122 #region Private Types
126 public static bool True(Player player, string[] args)
131 public static bool False(Player player, string[] args)
136 public static bool Has(Player player, string[] args)
141 public static bool Print(Player player, string[] args)
143 foreach (string arg in args)
145 string line = Parse.String(arg);
146 if (line != null) Console.WriteLine(line);
152 public Impl(Game game)
162 public string Name { get { return mName; } }
163 public string[] Arguments { get { return mArgs; } }
165 public Function(string name, string[] args)
178 #region Private Variables
181 List<Function> mFunctions = new List<Function>();